C语言源码实现停车场管理系统分享!

本文实例为大家分享了C语言停车场管理系统的具体代码,供大家参考,具体内容如下

题目要求:

C语言源码实现停车场管理系统

刚开始在Codeblocks下用C语言写的,但是用指针传递参数的时候总是出问题。后来就用C++,但是调用了C的输入输出和文件操作的头文件,所以代码都是C的

main.cpp

  #include <iostream>  #include <cstdio>  #include <cstdlib>  #include <windows.h>  #include <ctime>  #include <cstring>  #include <conio.h>  #define N 100  using namespace std;  typedef struct  {    char num[8];//车牌号    long int time_in;    int pos;//车辆的状态,0表示停在便道中,1表示停在停车场  } vehicle; //定义车辆类型  typedef struct  {    vehicle veh[N];    int top;  } SqStack; //用栈表示停车场  typedef struct LNode  {    vehicle veh;    struct LNode *next;  } LinkList; //用单链表表示便道  void Load(FILE *,SqStack *,LinkList *);  void ShowMenu(int );  int MakeChoice(int ,int );  void Parking(SqStack *,LinkList *);  void Back(SqStack *);  void EnterPkl(SqStack *,LinkList *);  void LeavePath(LinkList *);  void View(SqStack *,LinkList *);  void Write_and_Quit(FILE *,SqStack *,LinkList *);  int main()  {    SqStack *pkl;    LinkList *path;    FILE *fp;    pkl=(SqStack *)malloc(sizeof(SqStack));    path=(LinkList *)malloc(sizeof(LinkList));    fp=fopen("Parking_lot.txt","r+");    if(fp==NULL)    {      printf("数据加载失败!按任意键退出程序");      getch();      return 0;    }    Load(fp,pkl,path);    while(1)    {      system("cls");      ShowMenu(pkl->top);      switch(MakeChoice(1,6))      {      case 1:        system("cls");        Parking(pkl,path);        break;      case 2:        system("cls");        Back(pkl);        break;      case 3:        system("cls");        EnterPkl(pkl,path);        break;      case 4:        system("cls");        LeavePath(path);        break;      case 5:        system("cls");        View(pkl,path);        break;      default:        system("cls");        Write_and_Quit(fp,pkl,path);        return 0;      }    }    return 0;  }

function.cpp

  #include <iostream>  #include <cstdio>  #include <cstdlib>  #include <windows.h>  #include <ctime>  #include <cstring>  #include <conio.h>  #define N 100  using namespace std;  typedef struct  {    char num[8];//车牌号    long int time_in;    int pos;//车辆的状态,0表示停在便道中,1表示停在停车场  } vehicle; //定义车辆类型  typedef struct  {    vehicle veh[N];    int top;  } SqStack; //用栈表示停车场  typedef struct LNode  {    vehicle veh;    struct LNode *next;  } LinkList; //用单链表表示便道  void Load(FILE * fp,SqStack * pkl,LinkList * path)  {    pkl->top=-1;    path->next=NULL;    LinkList *p;    char num[8];    long int time_in;    int pos;    while(fscanf(fp,"%s %ld %dn",num,&time_in,&pos)!=EOF)    {      if(pos==0)//该车辆在便道中      {        //尾插法建立单链表        p=(LinkList *)malloc(sizeof(LinkList));        strcpy(p->veh.num,num);        p->veh.time_in=time_in;        p->veh.pos=pos;        path->next=p;        path=p;      }      else//该车辆在停车场中      {        ++pkl->top;        strcpy(pkl->veh[pkl->top].num,num);        pkl->veh[pkl->top].time_in=time_in;        pkl->veh[pkl->top].pos=pos;      }    }    path->next=NULL;  }  void ShowMenu(int n)  {    printf("********一个简单的停车场管理系统********n");    if(n+1==N)      printf("***************停车场已满***************n");    else      printf("**********当前停车场共有%03d辆车**********n",n+1);    printf("********说明:停车场每小时收费5元********n");    printf("****************1.停车******************n");    printf("****************2.取车******************n");    printf("*********3.便道车辆进入停车场***********n");    printf("**************4.离开便道****************n");    printf("**************5.查看车辆****************n");    printf("****************6.退出******************n");  }  int MakeChoice(int m,int n)  {    int judge;    printf("请输入%d~%dn",m,n);    scanf("%d",&judge);    while(judge<m||judge>n)//确保输入的是1~n    {      printf("输入不合法,请输入%d~%dn",m,n);      fflush(stdin);//如果不加这句,输入一些字母会导致函数无限循环      scanf("%d",&judge);    }    return judge;  }  void Parking(SqStack *pkl,LinkList *path)  {    LinkList *r;    printf("请输入车牌号:");    if(pkl->top<N-1)    {      fflush(stdin);      scanf("%8s",pkl->veh[++pkl->top].num);      time(&(pkl->veh[pkl->top].time_in));      pkl->veh[pkl->top].pos=1;      printf("您的车辆已停至%2d号车位n",pkl->top);    }    else    {      fflush(stdin);      r=(LinkList *)malloc(sizeof(LinkList));      scanf("%8s",r->veh.num);      printf("停车场已满,您要暂时停放在便道中吗?n");      printf("1.确定 2.取消n");      if(MakeChoice(1,2)==1)      {        while(path->next!=NULL)          path=path->next;        r->veh.time_in=0;        r->veh.pos=0;        path->next=r;        r->next=NULL;        printf("您的车辆已停放到便道中n");      }      else        free(r);    }    printf("按任意键返回主菜单");    getch();    return;  }  void Back(SqStack *pkl)  {    int n,i=0;    long int time_out;    double hours;    vehicle t_pkl[N];    printf("请输入您的车辆所在的车位(目前还有个小问题,前面的车走了之后当前车位会-1):");    n=MakeChoice(0,pkl->top);    printf("%2d上的车辆车牌号为%s,您确定要取走该车辆吗?n",n,pkl->veh[n].num);    printf("1.确定 2.取消n");    if(MakeChoice(1,2)==1)    {      time(&time_out);      hours=(time_out-pkl->veh[n].time_in)/3600.0;      printf("本次停车共计%lf小时,收费%lf元,请按任意键确认支付n",hours,hours*5);      getch();      for(i=0; pkl->top>=n; --pkl->top,++i) //把第n辆到第pkl->top辆车移到t_pkl        t_pkl[i]=pkl->veh[pkl->top];      //此时pkl->top指向第n-1辆车      for(i-=2; i>=0; --i) //把第n+1辆到第pkl->top辆车移回pkl        pkl->veh[++pkl->top]=t_pkl[i];      printf("支付成功!n");      printf("取车成功,按任意键返回主菜单");      getch();      return;    }    else    {      printf("按任意键返回主菜单");      getch();      return;    }  }  void EnterPkl(SqStack *pkl,LinkList *path)  {    if(pkl->top==N-1)      printf("停车场已满!");    else    {      printf("您确定将便道中第一辆车(车牌号:%8s)停入停车场吗?n",path->next->veh.num);      printf("1.确定 2.取消n");      if(MakeChoice(1,2)==1)      {        pkl->veh[++pkl->top]=path->next->veh;        time(&pkl->veh[pkl->top].time_in);        path->next=path->next->next;        printf("已停入停车场n");      }    }    printf("按任意键返回主菜单");    getch();    return;  }  void LeavePath(LinkList *path)  {    int i=0,n;    LinkList *q;    printf("请输入要离开便道的车辆的位序:");    scanf("%d",&n);    while(i<n&&path!=NULL)    {      ++i;      q=path;//保存当前节点的前一个节点,如果找到的位置在链表最后,需要将前一个节点的指针域置为NULL      path=path->next;    }    if(path!=NULL)    {      printf("您确定便道中第%03d辆车(车牌号:%8s)离开便道吗?n",n,path->veh.num);      printf("1.确定 2.取消n");      if(MakeChoice(1,2)==1)      {        if(path->next!=NULL)//确定离开并且不是便道中最后一辆车        {          q=path->next;          path->next=q->next;          free(q);          printf("第%03d辆车已离开便道n",n);        }        else//确定离开并且是便道中最后一辆车        {          printf("第%03d辆车已离开便道n",n);          q->next=NULL;          free(path);        }      }    }    else      printf("没有找到第%03d辆车n",n);    printf("按任意键返回主菜单");    getch();    return;  }  void View(SqStack *pkl,LinkList *path)  {    int i;    long int time_out;    double hours;    time(&time_out);    printf("停车场共有%03d辆车:n",pkl->top+1);    for(i=0; i<=pkl->top; ++i)    {      hours=(time_out-pkl->veh[i].time_in)/3600.0;      printf("车位:%2d 车牌号:%8s 停车时长:%lf 应缴费用:%lfn",i,pkl->veh[i].num,hours,hours*5);    }    printf("便道车辆:n");    if(path->next==NULL)      printf("无n");    while(path->next!=NULL)    {      path=path->next;      printf("车牌号:%sn",path->veh.num);    }    printf("按任意键返回主菜单");    getch();    return;  }  void Write_and_Quit(FILE *fp,SqStack *pkl,LinkList *path)  {    rewind(fp);    LinkList *pre=path,*p=path->next;    for(; pkl->top>-1; --pkl->top)      fprintf(fp,"%s %ld %dn",pkl->veh[pkl->top].num,pkl->veh[pkl->top].time_in,pkl->veh[pkl->top].pos);    while(p!=NULL)    {      free(pre);      fprintf(fp,"%s %ld %dn",p->veh.num,p->veh.time_in,p->veh.pos);      pre=p;      p=pre->next;    }    free(pre);    free(pkl);    fclose(fp);  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

—-想了解C语言源码实现停车场管理系统分享!全部内容且更多的C语言教程关注<计算机技术网(www.ctvol.com)!!>

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/483560.html

(0)
上一篇 2020年11月10日
下一篇 2020年11月10日

精彩推荐