|
|
|
关键字:C语言数据结构
|
来源:8844软件服务
pqspqs创建于
2006-12-28 11:25 阅读
41次
网友评论
条 |
各位大虾,小弟编了一个小程序,是用TC编的。主要的要求是:设有一个车站,用栈的结构表示;车站的外面有边道,用队列的结构表示。当车辆进栈的时候,如果栈中有空位,则进栈;如果没有空位,则在边道上等候,直到有栈中的车辆出栈才可以进栈。且出栈的车辆要按在栈中停留的时间交纳费用。 我在其中设了一个辅助栈,用来临时容纳为要给离去的车辆让路而退出来的车辆。输入的数据有三个数据项:车辆的进栈或出栈,车牌号,车子进出栈的时刻;栈中则为车牌号,进栈的时刻;队列中则为车牌号,因为车辆在边道上的等待时间并不计入费用中。 我的结果可以出来,但是我不明白的是:我每输入一条命令后,无论结果如何都会出现 The order is wrong!和Input the order!: 这是我无法理解的! 以下便是我的源程序: #include<stdio.h> #include<stdlib.h> struct { char status; int num; int time; }a; /*命令的结构*/ typedef struct{ int num; int time; }Element; struct { Element *base; Element *top; int stacksize; }S,VS; /*S为栈,VS为辅助栈*/ void main(){ typedef struct{ int num; struct QNode *next; }QNode,*QueuePtr; QueuePtr l; struct { QueuePtr front; QueuePtr rear; }Q; /*队列*/ int n,x,m=0,order,money,b=0; printf(\nInput the size of the garage and the cost per hour:); scanf(%d%d,&n,&x); S.base=(Element*)malloc(n*sizeof(Element)); S.top=S.base; S.stacksize=n; VS.base=(Element *)malloc((n-1)*sizeof(Element)); VS.top=VS.base; VS.stacksize=n-1; Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode)); Q.front->next=NULL; /*各结构的初始化*/ while (b!=1){ printf(\nInput the order!:); scanf(%c,%d,%d,&(a.status),&(a.num),&(a.time)); switch(a.status) { case E:b=1;break; case A: if (S.top-S.base<S.stacksize){ (*(S.top)).num=a.num; (*(S.top)).time=a.time; S.top++; order=S.top-S.base; printf(The %d car is in the %d of garage!\n,a.num,order); } else { Q.rear=(QueuePtr)malloc(sizeof(QNode)); Q.rear->next=NULL; Q.front->next=Q.rear; Q.rear->num=a.num; m++; printf(The %d car is in the %d of Queue!\n,a.num,m); } break; case D: while ((*(--S.top)).num!=a.num){ (*(VS.top)).num=(*(S.top)).num; (*(VS.top)).time=(*(S.top)).time; VS.top++; } money=(a.time-(*(S.top)).time)*x; printf(The %d car is out of %d of garage and the cost is %d!\n,a.num,S.top-S.base+1,money); while (VS.top!=VS.base){ (*(S.top)).num=(*(--VS.top)).num; (*(S.top)).time=(*(VS.top)).time; S.top++; } if (m!=0){ l=Q.front->next; (*(S.top)).num=l->num; (*(S.top)).time=a.time; S.top++; printf(The %d car is in the %d of garage!\n,l->num,S.stacksize); l=Q.front->next; Q.front->next=Q.front->next->next; free(l); m--; } break; default: printf(The order is wrong!\n);break; } } printf(\nThe program has finished!\n); } 敬请各位大虾不吝赐教!多谢!!
|
|
|
『打印』
『收藏』
『导出Word』
|