/*初始化停车场信息,初始状态为第一层已经停有4辆车,
* 其车位号依次为1—4 , 停车时间依次为20, 15, 10 , 5
*/
void Init(struct Garage gar[][6])
{
int i, j
/*给所有的车位的层号,车位号初始化,停车时间初始化为0,停车位全都初始化为空*/
for (i=0i<2i++)
{
for (j=0j<6j++)
{
gar[i][j].lay = i+1
gar[i][j].garagenum = j+1
gar[i][j].time = 0
gar[i][j].isempty = 1
}
}
/*第一层的1-4号车位停车*/
for (i=0i<4i++)
{
gar[0][i].isempty = 0
}
strcpy(gar[0][0].carnum, "GF8888")/*我自己初始化的车牌号,你可以自己改一下*/
gar[0][0].time = 20
strcpy(gar[0][1].carnum, "GF6666")
gar[0][1].time = 15
strcpy(gar[0][2].carnum, "GF9999")
gar[0][2].time = 10
strcpy(gar[0][3].carnum, "GF5858")
gar[0][3].time = 5
}
/*新停入的汽车后,将在此之前的所有车的停车时间加5*/
void AddTime(struct Garage gar[][6])
{
int i, j
for (i=0i<2i++)
{
for (j=0j<6j++)
{
if (gar[i][j].isempty == 0)
{
gar[i][j].time += 5
}
}
}
}
/*停车*/
void Park(struct Garage gar[][6])
{
int i
char num[8]
printf("请输入车牌号:")
scanf("%s", num)
/*查找空车位*/
for (i=0i<6i++)
{
if (gar[0][i].isempty == 1)
{
printf("第一层第%d号车位空着,请在此处停车\n", i+1)
strcpy(gar[0][i].carnum, num)
printf("车牌号:%s 层号:1 车位号: %d \n", num, i+1)
AddTime(gar)/*在此之前停车的所有汽车时间加5*/
gar[0][i].isempty = 0/*表示该车为已经停车*/
gar[0][i].time = 5/*将时间设为5*/
return
}
}
printf("第一层已经没有空车位\n")
for (i=0i<6i++)
{
if (gar[1][i].isempty = 1)
{
printf("第二层第%d号车位空着,请在此处停车\n", i+1)
strcpy(gar[1][i].carnum, num)
printf("车牌号:%s 层号:2 车位号: %d \n", num, i+1)
AddTime(gar)/*在此之前停车的所有汽车时间加5*/
gar[1][i].isempty = 0/*表示该车为已经停车*/
gar[1][i].time = 5/*将时间设为5*/
return
}
}
printf("对不起,1 2层都没有空车位,您现在不能在此停车\n")
}
/*查看所有车辆信息*/
void Information(struct Garage gar[][6])
{
int i, j
printf(" 车牌号 层号 车位号 停车时间\n")
for (i=0i<2i++)
{
for(j=0j<6j++)
{
if (gar[i][j].isempty == 0)
printf(" %s%8d%8d%8d\n", gar[i][j].carnum, gar[i][j].lay, gar[i][j].garagenum, gar[i][j].time)
}
}
printf("\n")
}
/*取车*/
double Leave(struct Garage gar[2][6])
{
int i, j
char num[8]
double charge = 0
printf("请输入要取的车牌号:")
scanf("%s", num)
for (i=0i<2i++)
{
for (j=0j<6j++)
{
if (!strcmp(gar[i][j].carnum, num))
{
printf("您在%d层%d车位停车%d分钟\n", gar[i][j].lay, gar[i][j].garagenum, gar[i][j].time)
charge = gar[i][j].time/5*0.2
printf("停车费用为每5分钟0.2元,您需交%.2lf元\n", charge)
gar[i][j].isempty = 1
return charge
}
}
}
printf("没有您输入的车号。\n\n")
return charge
}
/*是否查看总收入*/
void IsPrintTotal(double total)
{
char ch
printf("是否查看停车收费总计?Y/N")
scanf("%c", &ch)
while (ch!='y' &&ch!='Y' &&ch!='n' &&ch!='N')
{
printf("请输入Y或N ")
scanf("%c", &ch)
printf("\n")
}
switch (ch)
{
case 'Y':
case 'y':
printf("停车收费总计为%.2lf元\n", total)
break
case 'N':
case 'n':
break
}
}
main()
{
int choice
double total = 0
struct Garage gar[2][6]
Init(gar)//初始化第一层已经停有的4辆车
while (1)
{
Instruction()
printf("请输入要进行的操作:")
scanf("%d", &choice)
while (choice<0 || choice>3)
{
printf("输入的不合法,请输入0-3选择:")
scanf("%d", &choice)
}
switch (choice)
{
case 1:
Park(gar)
break
case 2:
total += Leave(gar)
IsPrintTotal(total)
break
case 3:
Information(gar)
break
case 0:
exit(0)
}
}
return 0
}
细节上的优化就看Lz怎么想了,我觉得提示做得还不够好,免强能用了。#include
#include
#define N 3 /*停车场大小*/
#define MAX 50 /*过道大小*/
#define sign 10/*车牌大小*/
#define price 10/*每分钟的价钱*/
char part[N][sign]
char Rpart[MAX][sign]
char time[N][20]
int P,R
partadd(char *t)
{
strcpy(&part[P][0],t)
printf("请输入时间:例如十点十分格式为“10.10”\n")
scanf("%s",&time[P][0])
getchar()
P++
}
Rpartadd(char *t)
{
if(R<MAX)
{
strcpy(&Rpart[R][0],t)
R++
}
else
{
printf("过道己满。无法停车。\n")
}
}
newcar()
{
char temp[sign]
printf("请输入车牌号:")
scanf("%s",temp)
getchar()
if(P<N)
{
partadd(temp)
}
else if(R<MAX)
{
Rpartadd(temp)
}
}
int timed(char *t1,char *t2)
{
int i=0,y=0,x=0,j,n=1
while(1)
{
if(t1[i]=='.')
{
for(j=i-1j>=0j--)
{
y=y+(t1[j]-'0')*(60*n)
n=n*10
}
while(1)
{
if(t1[j]==NULL)
{
for(n=1j>ij--)
{
y=y+(t1[j]-'0')*n
n=n*10
}
break
}
j++
}
i=0
while(1)
{
if(t2[i]=='.')
{
for(j=i-1j>=0j--)
{
x=x+(t2[j]-'0')*(60*n)
n=n*10
}
while(1)
{
if(t2[j]==NULL)
{
for(n=1j>ij--)
{
x=x+(t2[j]-'0')*n
n=n*10
}
break
}
j++
}
y=(x-y)*price
return y
}
i++
}
}
i++
}
}
partcarout(int i)
{
int j,money
char t[20]
printf("请输入现在的时间:例如十点十分格式为“10.10”\n")
scanf("%s",t)
getchar()
money=timed(t,&time[i][0])
printf("收费%d\n",money)
for(j=ij<Pj++)
{
strcpy(&part[j][0],&part[j+1][0])
P--
}
if(R!=0)
{
strcpy(&part[N-1][0],&Rpart[0][0])
P++
strcpy(&time[P][0],t)
Rpartcarout(0)
}
}
Rpartcarout(int i)
{
int j
for(j=ij<Rj++)
{
strcpy(&Rpart[j][0],&Rpart[j+1][0])
R--
}
}
carout()
{
char t[sign]
int i,get=0
printf("请入要离开的车牌号:")
scanf("%s",t)
getchar()
for(i=0i<Pi++)
{
if(strcmp(t,&part[i][0])==0)
{
get=1
partcarout(i)
break
}
}
for(i=0i<R&&get==0i++)
{
if(strcmp(t,&Rpart[i][0])==0)
{
get=1
Rpartcarout(i)
break
}
}
if(get==0)
{
printf("查无此车。\n")
}
}
jopart()
{
int i
for(i=0i<Pi++)
{
printf("%d.%s\n",i,&part[i][0])
}
}
joRpart()
{
int i
for(i=0i<Ri++)
{
printf("%d.%s\n",i,&Rpart[i][0])
}
}
main()
{
int c
while(1)
{
printf("请选择要做的事:\n")
printf("1.加入新车。\n")
printf("2.有车离开。\n")
printf("3.显示在停车场内的车。\n")
printf("4.显示在过道上的车。\n")
printf("5.退出。\n")
c=getchar()
getchar()
switch (c)
{
case '1':newcar()
break
case '2':carout()
break
case '3':jopart()
break
case '4':joRpart()
break
case '5':exit(1)
break
}
}
}
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#include <time.h>
#define max 3
#define price 1
int b=1
typedef struct
{
int day
int hour
int min
}TIME //时间结点
typedef struct
{
char num[10] //车牌号
TIME time //进入停车场的时间
int n //进入停车场的位置
}information
//栈结构体定义
typedef struct node
{
information data
struct node *next
}stacknode stacknode *top1,*top2
//队列结构体定义
typedef struct
{
information data
stacknode *front,*rear
}LQueueLQueue *Q
//函数声明部分/////////////////////////////////////////////////////////
stacknode *Init() //栈的初始化
stacknode *into(stacknode *top1,LQueue *Q) //初始化车辆进入
int expenses(stacknode *p,int x,int y) //停车费用计算函数
stacknode *leave(stacknode *top1,char str[],LQueue *Q) //车辆驶出出场函数
LQueue *InitLQue() //初始化队列函数
LQueue *wait(LQueue *q,stacknode *s) //车辆进入候车便道函数
int EmptyLQue(LQueue *q) //判断候车便道有无等待车辆函数
stacknode *out(LQueue *q)//候车区车辆出队
stacknode *LQinto(stacknode *p,stacknode *top1) //从候车便道进入停车场函数
void show(stacknode *top1) //显示停车场所有信息函数
void T_shou(LQueue *Q) //显示候车区信息
/*函数部分*/
//主函数
void main()
{
char str[10]
Q=InitLQue()
top1=Init()
top2=Init()
Q=InitLQue()
int i
printf("\t\t\t*************************************\n")
printf("\t\t\t\t 停车场管理系统\n")
printf("\t\t\t||1. 车辆进入停车场||\n")
printf("\t\t\t||2. 车辆离开停车场||\n")
printf("\t\t\t||3. 显示停车场内所有车辆信息 ||\n")
printf("\t\t\t||4. 显示候车区内所有车辆信息 ||\n")
printf("\t\t\t||5. 退出 ||\n")
printf("\t\t\t*************************************\n")
while(i!=5)
{
printf("\t请输入选项1-5:")
scanf("%d",&i)
switch(i)
{
case 1:
top1=into(top1,Q)
break
case 2:
printf("请输入离开车辆的车牌号:")
scanf("%s",str)
top1=leave(top1,str,Q)
break
case 3:show(top1)break
case 4:T_shou(Q)break
case 5:exit(1)
default:printf("输入错误,请重新输入1—5:")
break
}
}
}
/*子函数*/
//初始化
stacknode *Init()
{
stacknode *top
top=(stacknode *)malloc(sizeof(stacknode))
top=NULL
return top
}
//初始化车辆进入
stacknode *into(stacknode *top1,LQueue *Q)
{
stacknode *p,*q
time_t rawtime//调用系统时间函数
struct tm *timeinfo //时间结点
time(&rawtime)
timeinfo=localtime(&rawtime)
p=(stacknode *)malloc(sizeof(stacknode))
if(p==NULL)
{
printf("内存分配失败")
return top1
}
printf("请输入进入停车场车辆的车牌号:")
scanf("%s",p->data.num)
q=top1
while(q!=NULL)
{
if(strcmp(p->data.num,q->data.num)==0)
{
printf("车牌号输入有误,该车已进入!")
return top1
}
q=q->next
}
p->data.time.day=timeinfo->tm_mday
p->data.time.hour=timeinfo->tm_hour
p->data.time.min=timeinfo->tm_min
p->data.n=b
if(b>max)
{
printf("停车场已满,请在便道等候!\n")
wait(Q,p)
return top1
}
if(top1==NULL)
{
p->next=NULL
top1=p
}
else
{
p->next=top1
top1=p
}
b++
printf("车辆进入停车场成功,时间已经自动载入!\n")
printf("车牌为%s的汽车驶入时间为:%d号%d点%d分\n",top1->data.num,top1->data.time.day,top1->data.time.hour,top1->data.time.min)
return top1
}
//停车费用计算函数
int expenses(stacknode *p,int x1,int x2,int x3)
{
int w
if(x3!=0)
w=(x1*24+x2+1-(p->data.time.day*24+p->data.time.hour))*price
else
w=(x1*24+x2-(p->data.time.day*24+p->data.time.hour))*price
return w
}
//车辆驶出出场函数
stacknode *leave(stacknode *top1,char str[],LQueue *Q)
{
int i,day,hour,min
time_t rawtime
struct tm *timeinfo
time(&rawtime)
timeinfo=localtime(&rawtime)
day=timeinfo->tm_mday
hour=timeinfo->tm_hour
min=timeinfo->tm_min
stacknode *p,*q
if(top1==NULL)
{
printf("停车场没有车辆!\n")
return top1
}
q=(stacknode *)malloc(sizeof(stacknode))
if(p==NULL)
{
printf("内存分配失败")
return top1
}
q=top1
while(q!=NULL)
{
if(strcmp(q->data.num,str)==0)
break
q=q->next
}
if(q==NULL)
{
printf("输入有误,该车辆不在停车场!\n")
return top1
}
for(i=top1->data.ni>q->data.ni--)
{
p=(stacknode *)malloc(sizeof(stacknode))
if(p==NULL)
{
printf("内存分配失败")
return top1
}
strcpy(p->data.num,top1->data.num)
p->data.time=top1->data.time
p->data.n=top1->data.n-1
top1=top1->next
if(top2==NULL)
{
p->next=NULL
top2=p
}
else
{
p->next=top2
top2=p
}
}
top1=top1->next
while(top2!=NULL)
{
p=(stacknode *)malloc(sizeof(stacknode))if(p==NULL){printf("内存分配失败")return top1}
p->data.n=top2->data.n
strcpy(p->data.num,top2->data.num)
p->data.time=top2->data.time
p->next=top1
top1=p
top2=top2->next
}
if(EmptyLQue(Q))
{
p=out(Q)
p->data.n--
top1=LQinto(p,top1)
}
else
b--
printf("车牌为%s的汽车驶出时间为:%d号%d点%d分\n",q->data.num,day,hour,min)
printf("车辆驶出停车场需要缴纳的费用为:%d元\n",expenses(q,day,hour,min))
return top1
}
//队列函数初始化
LQueue *InitLQue()
{
LQueue *Q
stacknode *p
Q=(LQueue *)malloc(sizeof(LQueue))
p=(stacknode *)malloc(sizeof(stacknode))
p->next=NULL
Q->front=Q->rear=p
return Q
}
//候车区队列入队
LQueue *wait(LQueue *q,stacknode *s)
{
s->next=NULL
q->rear->next=s
q->rear=s
return q
}
//判断候车便道有无车辆等待
int EmptyLQue(LQueue *q)
{
if(q->front==q->rear)
return 0
else
return 1
}
//候车区车辆出队
stacknode *out(LQueue *q)
{
stacknode *p
p=q->front->next
if(q->front->next==q->rear)
{
q->rear=q->front
return p
}
else
q->front->next=p->next
p->next=NULL
return p
}
//候车队列进入停车场
stacknode *LQinto(stacknode *p,stacknode *top1)
{
p->next=top1
top1=p
return top1
}
//显示停车场内所有车辆信息
void show(stacknode *top1)
{
printf(" 停车场内全部车辆信息表\n")
if(top1==NULL)
printf(" 停车场内无车!\n")
else
{
printf("车牌号 进入时间 位置\n")
while(top1!=NULL)
{
printf(" %s%d号%d点%d分 第%d位\n",top1->data.num,top1->data.time.day,top1->data.time.hour,top1->data.time.min,top1->data.n)
top1=top1->next
}
}
}
//显示候车区的汽车信息
void T_shou(LQueue *Q)
{
LQueue *q
q=(LQueue *)malloc(sizeof(LQueue))
q->rear=Q->rear->next
printf(" 候车区信息\n")
if(q->front==q->rear)
printf("候车区没有车辆!\n")
else
{
printf("车牌号 进入时间\n")
while(q!=NULL)
{
printf("%s %d号%d点%d分",q->data.num,q->data.time.day,q->data.time.hour,q->data.time.min)
q->rear=q->rear->next
}
}
}
/*时间函数
int timef()
{
int x,y
time_t rawtime
struct tm *timeinfo
time(&rawtime)
timeinfo=localtime(&rawtime)
x=timeinfo->tm_mday,y=timeinfo->tm_hour
}
time_t rawtime
struct tm *timeinfo
time(&rawtime)
timeinfo=locoltime(&rawtime)
timeinfo->tm_ymday,*/