www.pudn.com > pause_fly.rar > pause.c
#include"stdio.h"
#include"stdlib.h"
#include"conio.h"
#define PERHOUR 10
#define N 100
#define OK 1
#define OVERFLOW -2
#define ERROR 0
#define CARNUM 2
typedef int ElemType;
typedef int Status;
struct{
int number;
int time;
int flag;
}car[N];
typedef struct{
ElemType *base;
ElemType *top;
int stacksize;
}Stack;/* 构造两个栈,一个为模拟停车场的栈,一个为辅助栈 */
typedef struct QNode{
ElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;/* 构造一个队列模拟车场外的便道 */
Stack ZS;/* 定义栈ZS */
Stack VS;/* 定义栈VS */
LinkQueue Q;/* 定义队列Q */
QueuePtr p;
int a,b,c,i=0,r;
ElemType e;
int InitStack(Stack *S);
int InitQueue(LinkQueue *Q);
int GetTop(Stack S,ElemType *e);
int Push(Stack *S,ElemType e);
int Pop(Stack *S,ElemType *e);
int EnQueue(LinkQueue *Q,ElemType e);
int DeQueue(LinkQueue *Q,ElemType *e);
void Exchange(ElemType e);
int In(ElemType e);
int In1(ElemType e);
int InitStack(Stack *S){
S->base=(ElemType*)malloc(CARNUM*sizeof(ElemType));
if(!S->base) exit(OVERFLOW);
S->top=S->base;
S->stacksize=CARNUM;
return OK;
}//构造栈
int InitQueue(LinkQueue *Q){
Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q->front) exit(OVERFLOW);
Q->front->next=NULL;
return OK;
}/* 构造一个模拟场外便道的队列 */
int GetTop(Stack S,ElemType *e){
if(S.top==S.base) {
*e=0;
return ERROR;}
*e=*(S.top-1);
return(*e);
} /*GetTop*/
int Push(Stack *S,ElemType e){
if((S->top-S->base)>=CARNUM)
EnQueue(&Q,e); /* 栈满,后来的车去便道等候 */
else
*S->top++=e;
return OK;
}/* 一辆车入停车场 */
int Pop(Stack *S,ElemType *e){
if(S->top==S->base) return ERROR;
*e=*--S->top;
return OK;
}/* 一辆车出停车场 */
int EnQueue(LinkQueue *Q,ElemType e){
p=(QueuePtr)malloc(sizeof(QNode));
if(!p) exit(OVERFLOW);
p->data=e;
p->next=NULL;
Q->rear->next=p;
Q->rear=p;
printf("车号%d进入了便道\n",b);
return OK;
}/* 当停车场满后入的车进便道 */
int DeQueue(LinkQueue *Q,ElemType *e){
int j;
if(Q->front==Q->rear) return ERROR;
p=Q->front->next;
*e=p->data;
Q->front->next=p->next;
if(Q->rear==p) Q->rear=Q->front;
free(p);
j=*e;
printf("车号%d推出了便道进入车场\n",j);
return(OK);
}/* 当停车场有一辆车出栈时便道的一辆车进停车场 */
void Exchange(ElemType e){
int money;
while(In(e)){
Pop(&ZS,&e);/* ZS的栈顶车出栈 */
Push(&VS,e);/* 从ZS出栈的车进VS */
}/* while */
Pop(&ZS,&e);/* 第b辆车开出停车场 */
for(r=0;r