📄 paking.c
字号:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define N 2
#define M 5
#define True 1
#define False 0
typedef struct{
int num;
int arrtime;
}ElemType;
typedef struct{
ElemType 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){
s->top=0;
}
int Push_Sq(SqStack *s,ElemType x){
if(s->top==M)
return 0;
s->elem[s->top]=x;
s->top++;
return 1;
}
ElemType Pop_Sq(SqStack *s){
ElemType x,y;
if(s->top==0)
return y;
s->top--;
x=s->elem[s->top];
return x;
}
void InitQueue_L(LQueue *q){
QNode *p;
p=(QNode *)malloc(sizeof(QNode));
p->next=NULL;
q->front=q->rear=p;
}
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;
}
int DelQueue_L(LQueue *q){
int x;
QNode *p;
if(q->front==q->rear)
return 0;
p=q->front->next;
x=p->num;
q->front->next=p->next;
if(q->rear==p)
q->rear=q->front;
return p->num;
}
void Arrive(SqStack *s1,LQueue *q,ElemType x){
int f;
f=Push_Sq(s1,x);
if(f==False){
EnQueue_L(q,x.num);
printf("第%d号车停在便道第%d车位上\n",x.num,s1->top);
}
else
printf("第%d号车停在停车场第%d车位上\n",x.num,s1->top);
}
void Delive(SqStack *s1,SqStack *s2,LQueue *q,ElemType x){
int n=0,f=False;
ElemType y;
QNode *p;
while((s1->top>0)&&(f!=True)){
y=Pop_Sq(s1);
if(y.num!=x.num)
n=Push_Sq(s2,y);
else
f=True;
}
if(y.num==x.num){
printf("第%d号车应收费%d元\n",y.num,(x.arrtime-y.arrtime)*M);
while(s2->top>0){
y=Pop_Sq(s2);
f=Push_Sq(s1,y);
}
n=DelQueue_L(q);
if(n!=NULL){
y.num=n;
y.arrtime=x.arrtime;
f=Push_Sq(s1,y);
printf("第%d号车停在停车场第%d车位上\n",y.num,s1->top);
}
}
else{
while(s2->top>0){
y=Pop_Sq(s2);
f=Push_Sq(s1,y);
}
p=q->front;
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);
}
}
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");
}
void main(){
char ch1,ch2;
SqStack *s1,*s2;
LQueue *q;
ElemType 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");
}
fflush(stdin);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -