?? forgive.cpp
字號:
#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");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -