C程式模擬停車場管理問題
阿新 • • 發佈:2019-01-04
//2010.05.21 FJC //隊首可開走 #include<stdio.h> #include<malloc.h> #define maxsize 2 typedef int datatype; //佇列開始 typedef struct node { datatype data; struct node *next; }linklist; typedef struct {linklist *front,*rear; }linkqueue; linkqueue *creat(q) linkqueue *q; {q->front=(linklist*)malloc(sizeof(linklist)); q->front->next=NULL; q->rear=q->front; return q; } int isnull(q) linkqueue *q; {if(q->front==q->rear) return 1; else return 0; } linkqueue *enqueue(q,x) linkqueue *q; datatype x; { q->rear->next=(linklist*)malloc(sizeof(linklist)); q->rear=q->rear->next; q->rear->data=x; q->rear->next=NULL; return q; } datatype dequeue(q) linkqueue *q; { linklist *s; if(isnull(q)) {printf("queue is empty!\n"); return NULL; } else {s=q->front; q->front=q->front->next; free(s); return(q->front->data); } } datatype headqueue(q) linkqueue *q; { linklist *s; if(isnull(q)) {return NULL; } else {return(q->front->next->data);//注意頭結點 } } int dequeue2(q,x) linkqueue *q; datatype x; { linklist*s; s=q->front; while(s->data!=x) { } } //佇列結束 typedef struct { datatype data; datatype time; }stackdata; typedef struct { stackdata data[maxsize]; int top; }stack; stack* creatstack() {stack *s; s=(stack*)malloc(sizeof(stack)); s->top=-1; return s; } int isempty(stack *s) {if(s->top==-1) return 1; else return 0; } int isfull(stack *s) {if(s->top==maxsize-1) return 1; else return 0; } void push(stack *s,datatype x,datatype y) {if(isfull(s)) printf("overflow!\n"); else { s->top++; s->data[s->top].data=x; s->data[s->top].time=y; } } void push2(stack *s,stackdata data) { s->top++; s->data[s->top]=data; } stackdata pop(stack *s) {if(isempty(s)); else {s->top--; return(s->data[s->top+1]); } } datatype top(stack *s) {if(!isempty(s)) {return (s->data[s->top].data);} return NULL; } int execute(que,s1,s2,sign,x,y) linklist *que; stack *s1,*s2; char sign; datatype x; datatype y; { datatype t,t1; stackdata data; datatype dat; if(sign=='A') {if(!isfull(s1)) {push(s1,x,y); printf("%d車於%d時進入停車場\n",x,y); } else {enqueue(que,x); printf("%d車於%d時在便道上停車\n",x,y);} } if(sign=='B') { if(headqueue(que)==x) { dequeue(que); printf("便道上的車%d於%d時開走\n",x,y); return 1; } if(isempty(s1)) {printf("停車場內無車輛!\n");} else { while(x!=top(s1)&&s1->top!=-1) {data=pop(s1); push2(s2,data); } if(s1->top==-1) {printf("無此車輛!\n"); return 0; } t1=pop(s1).time; t=y-t1; if(t>=0) {printf("%d車於%d時開出停車場,共用時%d\n",x,y,t); while(!isempty(s2)) {data=pop(s2); push2(s1,data); } if(!isnull(que)) { dat=dequeue(que); push(s1,dat,y); printf("%d車於%d時進入停車場\n",dat,y); } } else {printf("輸入有誤!\n"); push(s1,x,t1); return 0; } } } return 1; } void main() { char ch; datatype data,time; linkqueue *que=(linkqueue*)malloc(sizeof(linkqueue)); stack *s1,*s2; que=creat(que); s1=creatstack(); s2=creatstack(); printf(" ******停車場管理模擬系統******\n"); printf("請輸入車輛資訊:\n"); printf("汽車到達或離去資訊:\n"); ch=getchar(); while(ch!='E') {printf("輸入車號及到達時間(整數,空格分開):\n"); scanf("%d%d",&data,&time); execute(que,s1,s2,ch,data,time); ch=getchar();//接收空格 printf("汽車到達或離去資訊:\n"); ch=getchar(); } printf("程式結束\n"); }