利用C语言实现停车场管理系统

发布时间:2021-08-11 18:49:14 作者:chen
来源:亿速云 阅读:119

本篇内容主要讲解“利用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 100using 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 100using 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 %d\n",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~%d\n",m,n);  scanf("%d",&judge);  while(judge<m||judge>n)//确保输入的是1~n  {    printf("输入不合法,请输入%d~%d\n",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 应缴费用:%lf\n",i,pkl->veh[i].num,hours,hours*5);  }  printf("便道车辆:\n");  if(path->next==NULL)    printf("无\n");  while(path->next!=NULL)  {    path=path->next;    printf("车牌号:%s\n",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 %d\n",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 %d\n",p->veh.num,p->veh.time_in,p->veh.pos);    pre=p;    p=pre->next;  }  free(pre);  free(pkl);  fclose(fp);}

到此,相信大家对“利用C语言实现停车场管理系统”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. C语言如何实现游戏VIP停车场管理系统
  2. C语言源码实现停车场管理系统

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c语言

上一篇:C语言如何实现猜数字小游戏

下一篇:利用C语言写扫雷小游戏

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》