高手帮我看看这个程序那里有错
#include<iostream> using namespace std;
typedef int Time;
typedef struct InputData
{
char command;
int num;
Time t;
}InputData;//保存输入的数据
typedef struct StackNode
{
int num;
Time reach;
Time leave;
struct StackNode *next;
}CarNode;//入库车辆信息节点--栈节点
typedef struct Stack
{
CarNode *stack[5];
int top;
}CarStack;//模拟车库--栈
typedef struct Queue
{
CarNode *head;
CarNode *rear;
}LinkQueue;//模拟便道---队列
void InitStack(CarStack *s,int maxsize);/*初始化栈*/
void InitQueue(LinkQueue *road);/*初始化队列*/
void CreatStack(CarNode *Car,CarStack *garage);//车进库
void CreatQueue(CarNode *Car,LinkQueue *road);//车进便道
void PopQueue(CarNode *Car,LinkQueue *road);//车进便道
void PopStack(CarStack *garage,CarStack *Temp,LinkQueue *road,InputData *inputdata);//车出库
void GetCarNode(CarStack *garage,LinkQueue *road,InputData *inputdata,int maxsize);//获取车辆信息
void InfoStack(CarStack *garage);//车库车辆信息
void InfoQueue(LinkQueue *road);//便道车辆信息
void Print(CarNode *Car);//打印出库车信息
void Input(InputData *inputdata);//输入命令
bool SearchInGarage(CarStack garage,int n,int maxsize);//a表示要查找的车牌号,如果在停车场里面,就返回true
bool SearchInRoad(LinkQueue road,int a);//a表示要查找的车牌号,如果在通道里面,就返回true
void main()
{
InputData inputdata;
Input(&inputdata);
}
void InitStack(CarStack *s,int maxsize)/*初始化栈*/
{
int i;
s->top=0;
for(i=0;i<=maxsize-1;i++)
{
s->stack[s->top+i] = NULL;
s->stack[s->top+i] = (CarNode *)malloc(sizeof(CarNode));
}
}
void InitQueue(LinkQueue *road)/*初始化队列*/
{
road->head=(CarNode *)malloc(sizeof(CarNode));
// road->rear=(CarNode *)malloc(sizeof(CarNode));
if (!road->head)
{
cout<<"内存分配失败!"<<endl;
exit(0);
}
if(road->head!=NULL)
{
road->head->next=NULL;
road->rear=road->head;
}
}
void GetCarNode(CarStack *garage,LinkQueue *road,InputData *inputdata,int maxsize)//获取车辆信息
{
// if (SearchInGarage(*garage,inputdata->num,maxsize)||SearchInRoad(*road,inputdata->num))
// {
// cout<<"车库已经有此号码牌的车辆,请重新输入!"<<endl;
// exit(0);
// }
CarNode *Car=(CarNode *)malloc(sizeof(CarNode));
if (!Car)
{
cout<<"内存分配失败!"<<endl;
exit(0);
}
Car->num = inputdata->num;
Car->reach = inputdata->t;
if(garage->top<maxsize&&garage->top>=0)
{
CreatStack(Car,garage);//车进库
cout<<"车辆进入车库!"<<endl;
}
else
{
CreatQueue(Car,road);//车进便道
cout<<"车库已满,车辆停入便道"<<endl;
}
}
void CreatStack(CarNode *Car,CarStack *garage)//车进库
{
garage->stack[garage->top]=Car;
garage->top++;
}
void CreatQueue(CarNode *Car,LinkQueue *road)//车进便道
{
CarNode *QueueCar;
QueueCar=Car;
QueueCar->next=NULL;
road->rear->next=QueueCar;
road->rear=QueueCar;
}
void PopStack(CarStack *garage,CarStack *Temp,LinkQueue *road,InputData *inputdata)//车出库
{
CarNode *Car=(CarNode *)malloc(sizeof(CarNode));
if (!Car)
{
cout<<"内存分配失败!"<<endl;
exit(0);
}
Car->num = inputdata->num;
Car->leave = inputdata->t;
int i = garage->top;
while (i)
{
if (garage->stack[i-1]->num != Car->num)
{
garage->top--;//进临时车站
Temp->stack[Temp->top]=garage->stack[garage->top];
Temp->top++;
i--;//没有找到,继续循环查找
if (i==0)
{
cout<<"没有该车牌的车!"<<endl;
}
}
else//获得车辆信息,并回归临时车栈的车辆
{
Car->reach = garage->stack[i-1]->reach;
Print(Car);//打印车辆收费单
garage->top--;
while(Temp->top>0)//回归临时车站的车辆
{
Temp->top--;
garage->stack[garage->top]=Temp->stack[Temp->top];
garage->top++;
}
if(road->head!=road->rear)//从便道出来一辆车到停车场
{
garage->stack[garage->top]=road->head->next;
cout<<endl;
cout<<"车场有空位,便道第一辆车可以进入!"<<endl;
garage->top++;
road->head=road->head->next;
}
i = 0;//已经找到,退出循环
}
}
}
void InfoStack(CarStack *garage)//车库车辆信息
{
if(garage->top==0)
cout<<"车库里没有车!"<<endl;
else
{
cout<<" _________________________ "<<endl;
cout<<"| |"<<endl;
cout<<"| 车 库 |"<<endl;
cout<<"|位置 车辆号码 到达时间|"<<endl;
for(int i=0;i<garage->top;i++)
{
cout<<"| "<<i+1<<" "<<garage->stack[i]->num<<" "<<garage->stack[i]->reach<<" |"<<endl;
}
cout<<"|_________________________|"<<endl;
}
}
void InfoQueue(LinkQueue *road)//便道车辆信息
{
CarNode *p;
p=road->head->next;
int i=0;
if(road->head==road->rear)
cout<<"便道里没有车!"<<endl;
else
{
cout<<" __________________ "<<endl;
cout<<"| |"<<endl;
cout<<"| 便道 |"<<endl;
cout<<"| 位置 车辆号码 |"<<endl;
while(p!=NULL)
{
cout<<"| "<<++i<<" "<<p->num<<" |"<<endl;
p=p->next;
}
cout<<"|__________________|"<<endl;
}
free(p);
}
void Print(CarNode *Car)//打印出站车辆信息
{
int rh,lh;
float t;
rh=Car->reach;
lh=Car->leave;
t=(lh-rh)*60*0.01;
cout<<" ---------------------------------------------------- "<<endl;
cout<<"| 车辆的号码 进库时间 离开时间 车费(元) |"<<endl;
cout<<"| "<<Car->num<<" "<<rh<<" "<<lh<<" "<<t<<" |"<<endl;
cout<<"|____________________________________________________|"<<endl;
}
void Input(InputData *inputdata)
{
int maxsize;
cout<<"请输入停车场容量 Maxsize (最多为5) : ";
do
{
cin>>maxsize;
if (!(maxsize<5&&maxsize>0))
{
cout<<"输入有误,请重新输入停车场容量:";
}
} while (!(maxsize<5&&maxsize>0));
int i=0,j=0,k=1;
char c ;
int n;
CarStack garage,Temp;
LinkQueue road;
CarNode car;
InitStack(&garage,maxsize);
InitStack(&Temp,maxsize);
InitQueue(&road);
while(k)
{
i=0;
while(!i)
{
cout<<" ____________________________________________________________"<<endl;
cout<<"| |"<<endl;
cout<<"| 欢迎光临停车场,本停车场每分钟收费0.01元 |"<<endl;
cout<<"| |"<<endl;
cout<<"| 请输入车库命令,格式如下(命令,车牌号,入库时间) |"<<endl;
cout<<"| |"<<endl;
cout<<"| A(a)-入库 D(d)-离开 P(p)-查看停车场车辆 P(p)-查看过道车辆 |"<<endl;
cout<<"|____________________________________________________________|"<<endl;
cin>>(inputdata->command)>>(inputdata->num)>>(inputdata->t);
c = inputdata->command;
n = inputdata->num;
if(c!='A'&&c!='a'&&c!='D'&&c!='d'&&c!='W'&&c!='w'&&c!='P'&&c!='p'&&c!='E'&&c!='e')
{
cout<<"命令不正确,请重新输入!"<<endl;
i = 0;
}
else
i = 1;
// if(SearchInGarage(garage,n,maxsize)&&SearchInRoad(road,n))
// {
// i = 0;
// cout<<"命令不正确,请重新输入!"<<endl;
// }
}
switch(c)
{
case 'A':
case 'a':
GetCarNode(&garage,&road,inputdata,maxsize);//获取车辆信息
break;
case 'D':
case 'd':
PopStack(&garage,&Temp,&road,inputdata);//车出库
break;
case 'P':
case 'p'://查询停车场的信息
InfoStack(&garage);//车库车辆信息
break;
case 'W':
case 'w'://查询侯车场的信息
InfoQueue(&road);//便道车辆信息
break;
case 'E':
case 'e':
k=0;
break;
default:break;
}
}
cout<<endl;
cin.clear();
}
bool SearchInGarage(CarStack garage,int n,int maxsize)//a表示要查找的车牌号,如果在停车场里面,就返回true
{
bool tag=false;
for (int i=0;i<maxsize||(i!=garage.top);i++)
{
if (n == garage.stack[i]->num )
{
tag=true;
break;
}
}
return tag;
}
bool SearchInRoad(LinkQueue road,int n)//a表示要查找的车牌号,如果在通道里面,就返回true
{
bool tag=false;
StackNode *p;
p = (StackNode *)malloc(sizeof(StackNode));
if(road.head != road.rear)//如果队列非空
{
p = road.head;
while(p != road.rear)
{
++p;
if(p->num == n)
tag=true;
}//退出此while循环时p指向最后一个元素
}
free(p);
return tag;
}