严蔚敏习题集的停车场问题还有个小问题不能解决,希望各位大侠援手!
在出站的时候,不知道怎么比较车牌号。我用数字的形式的测试,没问题;用字符形式的一测试就出错。就那两行,我用注释注释出来了,希望各位帮忙看下。之前一个高手帮我看了下,改正了绝大部分错误,但是这个问题是新出的,不知道怎么改了。。。谢谢了!
//停车场问题——用栈模拟停车场&用队列模拟便道
//其中栈用顺序结构实现&队列用单向链表实现
//不考虑从队列出车的情况
#include <iostream.h>
#include "stdlib.h "
#include "string.h "
#define n 2 //车站的容量
#define Price 1 //元/分
typedef struct {//时间信息
int hour,min;
}time;
typedef struct {//车辆信息
char num;
time reach,leave;
}car;
typedef struct node{//便道节点
car data;
struct node *next;
}qnode;
typedef struct {//定义车站
car *base,*top;
int length;
}sqstack;
typedef struct {//定义便道
qnode *front,*rear;
}linkqueue;
//--------------------------------------------------
void initstack(sqstack *S){//生成车站
S-> base=(car *)malloc((n+1)*sizeof(car));
if(!(S-> base)){//分配不成功
cout < < "车站初始化失败,程序现在退出! " < <endl;
exit (-2);
}//if
else{//分配成功
S-> top=S-> base;
S-> length=0;
}//else
}//initstack
void initqueue(linkqueue *Q){//生成便道
Q-> front=(qnode *)malloc(sizeof(qnode));
if(!(Q-> front)){//分配不成功
cout < < "便道初始化失败,程序现在退出! " < <endl;
exit (-2);
}//if
else{//分配成功
Q-> front-> next=NULL;
Q-> rear=Q-> front;
}//else
}//initqueue
void getprice(car *t){//计算价格
cout < < "\n该车在车站内停留的时间为 " < <(((t-> leave.hour)-(t-> reach.hour))*60+((t-> leave.min)-(t-> reach.min))) < < "分, ";
cout < < "应该付 " < <((((t-> leave.hour)-(t-> reach.hour))*60+((t-> leave.min)-(t-> reach.min)))*Price) < < "元钱! " < <endl;
}//getprice
int arrival(sqstack *S,linkqueue *Q){//到达函数
car p;
cout < < "\n来车牌号: ";
cin> > (p.num);
if((S-> length) <n){//车站未满进站
cout < < "\n该车被分配到 " < <(++S-> length);
cout < < "\n进站时间为: ";
cin> > (p.reach.hour)> > (p.reach.min);
*(++S-> top)=p;
}//if
else{//站满进便道
cout < < "\n站满,该车需进便道! " < <endl;
(Q-> rear-> next)=(qnode *)malloc(sizeof(qnode));
if(!(Q-> rear-> next)){//分配不成功
cout < < "\n内存分配失败,错误位置在便道! ";
exit (-2);
}//if
else{//分配成功
Q-> rear-> next-> next=NULL;
Q-> rear-> next-> data.num=(p.num);
Q-> rear=Q-> rear-> next;
}//else
}//else
return 1;
}//arrival
int leave(sqstack *S,sqstack *T,linkqueue *Q){//离开函数
car p,x;
if(!(S-> length))//车站为空
cout < < "\n车站为空,没有要找的车! " < <endl;
else{//车站不为空
cout < < "\n要开走的车的牌号: ";
cin> > (x.num);
while(S-> length){//站内找车
p=*(S-> top--);
S-> length--;
if(!(strcmp(p.num),(x.num))) break;//这句是错误的,想用字符做车牌号。但是这样一改就通不过去了
//if((p.num)-(x.num)==0) break; //这句是正确的,但是车号只能用数字
*(++T-> top)=p;
T-> length++;
}//while
if(!(strcmp(p.num),(x.num))){//找到该车 这句是错误的,想用字符做车牌号。但这样就通不过了
//if((p.num)-(x.num)==0){//找到该车 这句是正确的,如果车号是用数字的话
cout < < "\n该车出站的时间: ";
cin> > (p.leave.hour)> > (p.leave.min);
getprice(&p);
while(T-> length){//临时站车入站
p=*(T-> top--);
T-> length--;
*(++S-> top)=p;
S-> length++;
}//while
if(Q-> front-> next){//队列不空则进站
p=(Q-> front-> next-> data);
*(++S-> top)=p;
Q-> front-> next=Q-> front-> next-> next;
if(!(Q-> front-> next)) Q-> rear=Q-> front;
}//if
}//if
else{//未在车站内找到车,车可能在队列或者根本没到
cout < < "\n没有找到该车! " < <endl;
while(T-> length){//临时站车入站
p=*(T-> top--);
T-> length--;
*(++S-> top)=p;
S-> length++;
}//while
}//else
}//else
return 1;
}//leave
//-------------------------------------------------
void main(){
char x;
sqstack S,T;
linkqueue Q;
initstack(&S);
initstack(&T);
initqueue(&Q);
cout < < "A=来车,D=车走,E=退出 " < <endl;
while(1){
while(1){
cin> > x;
if(x== 'a '||x== 'A '||x== 'd '||x== 'D '||x== 'e '||x== 'E ') break;
}//while
if(x== 'a '||x== 'A ') arrival(&S,&Q);
if(x== 'd '||x== 'D ') leave(&S,&T,&Q);
if(x== 'e '||x== 'E ') exit (0);
}//while
cout < < "\nLenic欢迎您的使用!\n\n\n ";
}//main
[解决办法]
if(!(strcmp((p.num),(x.num)))
p.num 是char型的,不能用strcmp(比较字符串)来比较。
换成if(p.num!=(x.num))即可。