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

刚开始在Codeblocks下用C语言写的,但是用指针传递参数的时候总是出问题。后来就用C++,但是调用了C的输入输出和文件操作的头文件,所以代码都是C的
main.cpp
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#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
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
#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 %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);
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq920444182/article/details/73694889








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。