[原创]自编停车场问题!
自己编写的停车场问题,一起来讨论一下如何修改会更好吧!!!
代码如下:
#include<stdio.h>
#include<stdlib.h>
#define MaxStackSize 2 /*栈的最大值*/
#define MaxQueueSize 100 /*队列的最大值*/
#define money 2 /*停车场每小时的费用*/
typedef struct /*车的数据结构*/
{int num;
float time;
}CarType;
/*定义栈*/
typedef struct /*定义堆栈*/
{CarType stack[MaxStackSize];
int top;
}SeqStack;
void StackInitate(SeqStack *S) /*初始化堆栈*/
{S->top=0;
}
int stackfull(SeqStack S) /*判断栈是否满*/
{if(S.top>=MaxStackSize)
return 1;
else return 0;
}
int StackNotEmpty(SeqStack S) /*判断是否为非空栈*/
{if(S.top<=0) return 0;
else return 1;
}
int StackPush(SeqStack *S,CarType x) /*入栈*/
{if(S->top>=MaxStackSize)
{printf("堆栈已满无法插入!\n");
return 0;
}
else
{S->stack[S->top]=x;
S->top++;
return 1;
}
}
int StackPop(SeqStack *S,CarType *d) /*出栈*/
{if(S->top<=0)
{printf("堆栈已空无数据元素出栈!\n");
return 0;
}
else
{S->top--;
*d=S->stack[S->top];
return 1;
}
}
int StackTop(SeqStack S,CarType *d) /*取栈顶元素*/
{if(S.top<=0)
{printf("堆栈已空!\n");
return 0;
}
else
{*d=S.stack[S.top-1];
return 1;
}
}
/*定义队列*/
typedef struct
{
CarType queue[MaxQueueSize];
int rear; /*队尾指针*/
int front; /*队头指针*/
int count; /*计数器*/
} SeqCQueue;
void QueueInitiate(SeqCQueue *Q)
/*初始化顺序循环队列Q*/
{Q->rear = 0; /*定义初始队尾指针下标值*/
Q->front = 0; /*定义初始队头指针下标值*/
Q->count = 0; /*定义初始计数器值*/
}
int QueueNotEmpty(SeqCQueue *Q)
{if(Q->count==0 && Q->rear == Q->front)
return 0;
else return 1;
}
int QueueAppend(SeqCQueue *Q, CarType x) /*入队*/
{ if(Q->count > 0 && Q->rear == Q->front)
{ printf("便道已满无法插入! \n");
return 0; }
else
{ Q->queue[Q->rear] = x;
Q->rear = (Q->rear + 1) % MaxQueueSize;
Q->count++;
return 1; }
}
int QueueDelete(SeqCQueue *Q, CarType *d) /*出队*/
{ if(Q->count == 0)
{printf("队列已空无数据元素出队列! \n");
return 0; }
else
{
*d = Q->queue[Q->front];
Q->front = (Q->front + 1) % MaxQueueSize;
Q->count--;
return 1; }
}
void enter(CarType *car,SeqCQueue *Carland,SeqStack *Carparking) /*进入停车场*/
{if(!QueueNotEmpty(Carland))
if(!stackfull(*Carparking))
{printf("停车场未满,可放入停车场\n\n");
StackPush(Carparking,*car);
}
else
{printf("停车场已满,请在便道上等候\n\n");
QueueAppend(Carland,*car);
}
else {printf("停车场已满,请在便道上等候\n\n");
QueueAppend(Carland,*car);
}
}
float out(CarType *car,SeqCQueue *Carland,SeqStack *Carparking) /*离开停车场*/
{void StackInitate(SeqStack *S);
void QueueInitiate(SeqCQueue *Q);
CarType cartemp;
float time;
SeqStack Temp; /*定义临时车道*/
StackInitate(&Temp); /*初始化临时车道*/
if(StackNotEmpty(*Carparking))
do
{if(StackNotEmpty(*Carparking))
{StackPop(Carparking,&cartemp);
StackPush(&Temp,cartemp);
}
else
{printf("找不到此车\n");
break;
}
}while(cartemp.num!=car->num);
else printf("无车可拿\n");
if(cartemp.num==car->num)
{StackPop(&Temp,&cartemp);
printf("此车已经出停车场了\n");
time=car->time-cartemp.time;
}
while(StackNotEmpty(Temp))
{StackPop(&Temp,&cartemp);
StackPush(Carparking,cartemp);
}
QueueDelete(Carland, &cartemp);
if(cartemp.num==car->num)
cartemp.time=car->time; /*可认为这辆车进入的时间是另一辆车出车的时间*/
StackPush(Carparking,cartemp);
return time;
}
void main()
{void StackInitate(SeqStack *S);
void QueueInitiate(SeqCQueue *Q);
void enter(CarType *car,SeqCQueue *Carland,SeqStack *Carparking);
float out(CarType *car,SeqCQueue *Carland,SeqStack *Carparking);
SeqStack Carparking; /*定义停车场*/
SeqCQueue Carland; /*定义便道*/
char a='a',b; /*a用于记录操作,b用来记下回车键*/
CarType car;
float time;
FILE *fp; /*用一个文本来记录所有的数据*/
StackInitate(&Carparking); /*初始化停车场*/
QueueInitiate(&Carland); /*初始化便道*/
if((fp=fopen("datarecorder.txt","w+"))==NULL) /*打开数据记录文本*/
{printf("Cannot open file strike any key exit!");
exit (0);
}
printf("\t\t停车场管理\n");
printf("****************************************\n");
while(a!='E')
{ printf("\t请输入您操作:\n");
printf("\t1.入车 请输入A+回车键\n");
printf("\t2.出车 请输入D+回车键\n");
printf("\t3.结束 请输入E+回车键\n");
scanf("%c",&a);
if(a=='A') /*入车*/
{printf("请输入车牌号和入车时间(以小时为单位)用','隔开\n");
scanf("%d,%f",&car.num,&car.time);
enter(&car,&Carland,&Carparking);
fprintf(fp,"操作%c,车牌号%d,时间%f\n",a,car.num,car.time);
printf("****************************************\n\n");
}
if(a=='D') /*出车*/
{printf("请输入车牌号和出车时间用','隔开\n");
scanf("%d,%f",&car.num,&car.time);
time=out(&car,&Carland,&Carparking);
printf("现在每小时收费两元\n");
printf("您的停车时间为:%f\n应缴纳的费用为:%f\n",time,time*money);
fprintf(fp,"操作%c,车牌号%d,时间%f\n",a,car.num,car.time);
fprintf(fp,"车牌号%d,花时间%f,应缴纳的费用为:%f\n",car.num,time,time*money);
printf("****************************************\n\n");
}
scanf("%c",&b); /*用来存放多出来的回车符*/
}
printf("\t\tTHE END\n****************************************\n");
}
我是在vc里运行的,是可以的,但是在wintc里就不可以,也不知道是什么原因?
如何修改会更好呢?