📄 tingche.txt
字号:
#include "stdio.h"
#define N 2 /*停车场容量*/
#define M 5 /*停车单价*/
#define True 1
#define False 0
typedef struct{
int num; /*车牌号*/
int arrtime; /*到达/离开时间*/
}ELEMTP; /*顺序栈的数据元素类型*/
typedef struct{
ELEMTP elem[N];
int top;
}SqStack; /*顺序栈类型*/
typedef struct node{
int num; /*车牌号/便道上的车辆数量*/
struct node *next;
}QNode; /*链队列的数据元素类型*/
typedef struct{
QNode *front, *rear;
}LQueue; /*链队列类型*/
void InitStack_Sq (SqStack *s); /*初始化栈*/
int Push_Sq(SqStack *s,ELEMTP x); /*入栈*/
ELEMTP Pop_Sq(SqStack *s); /*出栈*/
void InitQueue_L(LQueue *q); /*初始化队列*/
void EnQueue_L (LQueue *q,int num1); /*入队列*/
int DelQueue_L(LQueue *q); /*出队列*/
/*以上函数定义在本书附带的光盘中*/
void Arrive (SqStack *s1, LQueue *q,ELEMTP x){
/*车辆x进入停车场*/
int f;
f=Push_Sq(s1,x);
if (f==False){ /*停车场栈s1已满入便道q */
EnQueue_L(q,x.num);
printf("第%d号车停在便道第%d车位上\n",x.num,q->front->num);
}
else printf("第%d号车停在停车场第%d车位上\n",x.num,s1->top);
}/* Arrive */
void Delive (SqStack *s1,SqStack *s2, LQueue *q,ELEMTP x){
/*车辆x离开停车场*/
int n,f=False;
ELEMTP y; QNode *p;
while ((s1->top>0) && (f!=True)){ /*在栈s1中寻找车辆x */
y=Pop_Sq(s1);
if (y.num!=x.num) n=Push_Sq(s2,y);
else f=True;
}
if (y.num==x.num){ /*寻找到车辆x*/
printf("第%d号车应收费%d元\n",y.num,(x.arrtime-y.arrtime)*M);
while (s2->top>0){ /*将栈s2中的车辆倒回到栈s1中*/
y=Pop_Sq(s2);
f=Push_Sq(s1,y);
}
n=DelQueue_L(q);
if (n!=NULL){ /*便道q上的第一辆车入栈s1*/
y.num=n;
y.arrtime=x.arrtime;
f=Push_Sq(s1,y);
printf("第%d号车停在停车场第%d号车位上\n",y.num,s1->top);
}
}
else{ /*栈s1中未找到车辆x*/
while (s2->top>0){ /*将栈s2中的车辆倒回到栈s1中*/
y=Pop_Sq(s2);
f=Push_Sq(s1,y);
}
p=q->front; /*在便道q上找到车辆x*/
f=False;
while (f==False && p->next!=NULL)
if (p->next->num!=x.num)
p=p->next;
else{
p->next=p->next->next;
q->front->num--;
if (p->next==NULL)
q->rear=q->front;
printf("第%d号车离开便道\n",x.num);
f=True;
}
if (f==False)
printf("输入数据错误,停车场和便道上均无%d号车\n",x.num);
}
}/* Delive */
void Display(SqStack *s1, LQueue *q){
/*显示停车场的状况*/
int k; QNode *p;
printf("停车场状况:\n");
if(s1->top!=0){
printf("车道 车号\n");
for(k=0;k<s1->top;k++)
printf("%d %d\n",k+1,s1->elem[k].num);
}
else printf("停车场没有车辆\n");
printf("便道状况:\n");
if(q->front->num){
printf("车道 车号\n");
for(k=1,p=q->front->next;p;p=p->next)
printf("%d %d\n",k++,p->num);
}
else printf("便道没有车辆\n");
}/* Display */
void main()
{ char ch1;
SqStack *s1,*s2;
LQueue *q;
ELEMTP x;
int flag;
s1=(SqStack *) malloc (sizeof(SqStack));
s2=(SqStack *) malloc (sizeof(SqStack));
q=(LQueue *) malloc (sizeof (LQueue));
InitStack_Sq(s1);
InitStack_Sq(s2);
InitQueue_L (q);
flag=True;
while(flag){
printf("请输入您的选择\n");
printf("S---------显示停车场状况\n");
printf("A------车辆到达\n");
printf("D------车辆离开\n");
printf("E------程序结束\n");
ch1=getchar();
switch (ch1){
case 'S': Display(s1,q);break;
case 'A': printf("输入数据:车牌号,到达时间:");
scanf("%d,%d",&x.num,&x.arrtime);
Arrive(s1,q,x);break;
case 'D': printf("输入数据:车牌号,离开时间:");
scanf("%d,%d",&x.num,&x.arrtime);
Delive(s1,s2,q,x);break;
case 'E': flag=False;
printf("程序正常结束\n"); break;
default: printf("输入数据错误,重新输入\n");
}
/* ch2=getchar(); */
}
}/*main*/
void InitStack_Sq (SqStack *s)
{s->top=0;
}
int Push_Sq(SqStack *s,ELEMTP x)
{ if (s->top==N)
return (False);
else
{s->elem[s->top]=x;s->top++;
return(True);
}
}
ELEMTP Pop_Sq(SqStack *s)
{ ELEMTP x;
if (s->top==0)
{ x.num=NULL;
x.arrtime=NULL;
return(x);
}
else
{ s->top--;
return (s->elem[s->top]);
}
}
void InitQueue_L(LQueue *q)
{ q->front=(QNode *)malloc(sizeof(QNode));
q->rear=q->front;
q->front->next=NULL;
q->front->num=0;
}
void EnQueue_L (LQueue *q,int num1)
{ QNode *p;
p=(QNode *)malloc(sizeof(QNode));
p->num=num1;
p->next=NULL;
q->rear->next=p;
q->rear=p;
q->front->num++;
}
int DelQueue_L(LQueue *q)
{ QNode *p;
int n;
if (q->front==q->rear)
return (NULL);
else
{ p=q->front->next;
q->front->next=p->next;
if (p->next==NULL)
q->rear=q->front;
n=p->num;
free(p);
q->front->num--;
return(n);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -