chechang.cpp
资源名称:chechang.rar [点击查看]
上传用户:wahhaolin
上传日期:2008-04-18
资源大小:3k
文件大小:9k
源码类别:
数据结构
开发平台:
Visual C++
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<iostream.h>
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- typedef int status;
- typedef struct{
- char type;
- char num[5];
- int time;
- }ElemType,*LElemType;
- typedef struct{
- LElemType base;
- LElemType top;
- int stacksize;
- } sqstack; //存储车场停车信息的栈
- typedef struct QNode{
- ElemType data;
- struct QNode *next;
- }QNode,*Queueptr;
- typedef struct{
- Queueptr front;
- Queueptr rear;
- } LinkQueue; //存储便道停车信息的队列
- typedef struct LNode{
- char ch[5];
- struct LNode *next;
- }LNode,*ListType;
- typedef struct{
- ListType head,tail;
- int size;
- } tagList; //存储场内车牌号的链表
- //全局变量
- sqstack garage,giveway;
- LinkQueue waitway;
- LElemType elem;
- tagList L;
- int n,i=1,j=1;
- status InitStack(sqstack &s,int n);
- status Push(sqstack &s,ElemType e);
- status Pop(sqstack &s,ElemType &e);
- status InitQueue(LinkQueue &Q);
- status Gethead(LinkQueue s,ElemType &e);
- status EnQueue(LinkQueue &Q,ElemType e);
- status DeQueue(LinkQueue &Q,ElemType &e);
- status DestroyQueue(LinkQueue &Q);
- status InitList(tagList &L);
- status MakeNode(ListType t,char &c);
- status Insert(tagList &L,ListType t);
- status judgenum(char num[]);
- void printmessage();
- void DeleteL(char num[]);
- void count(LElemType q,char wh);
- void Readmessage(char &c);
- void Interpret(char c);
- status InitStack(sqstack &s,int n)
- { s.base=(LElemType)malloc(n*sizeof(ElemType));
- if(!s.base) exit(OVERFLOW);
- s.top=s.base;
- s.stacksize=n;
- return OK;
- }
- status Push(sqstack &s,ElemType e)
- { *s.top=e;
- s.top++;
- return OK;
- }
- status Pop(sqstack &s,ElemType &e)
- {if(s.top==s.base) return ERROR;
- e=*--s.top;
- return OK;
- }
- status InitQueue(LinkQueue &Q)
- { Q.front=Q.rear=(Queueptr)malloc(sizeof(QNode));
- if(!Q.front) exit(OVERFLOW);
- Q.front->next=NULL;
- return OK;
- }
- status DestroyQueue(LinkQueue &Q)
- { while(Q.front){
- Q.rear=Q.front->next;
- free(Q.front);
- Q.front=Q.rear;
- }
- return OK;
- }
- /*status Gethead( LinkQueue s,ElemType &e)
- { if(s.front==s.rear) return ERROR;
- e=s.front->data;
- return OK;
- }*/
- status EnQueue(LinkQueue &Q,ElemType e)
- { Queueptr p;
- Q.rear->data=e;
- p=(Queueptr)malloc(sizeof(QNode));
- if(!p) exit(OVERFLOW);
- p->next=NULL;
- Q.rear->next=p;
- Q.rear=p;
- return OK;
- }
- status DeQueue(LinkQueue &Q,ElemType &e)
- { if(Q.front==Q.rear) return ERROR;
- Queueptr p=Q.front;
- e=p->data;
- Q.front=p->next;
- if(Q.rear==p->next) Q.rear=Q.front;
- free(p);
- return OK;
- }
- status InitList(tagList &L)
- { L.head=new(LNode);
- L.tail=L.head;
- L.size=0;
- for(int i=0;i<5;i++)
- L.head->ch[i]='0';
- return OK;
- }
- status MakeNode(ListType t,char c[])
- { for(int i=0;i<5;i++)
- t->ch[i]=c[i];
- t->next=NULL;
- return OK;
- }
- status Insert(tagList &L,ListType t)
- { L.tail->next=t;
- L.tail=t;
- L.tail->next=NULL;
- return OK;
- }
- status judgenum(char num[])
- { int i=0;
- ListType q=NULL;
- if(L.size>0) q=L.head->next;
- while(q)
- { if(strcmp(num,q->ch)) i=1;
- q=q->next;
- }
- return i;
- }
- void DeleteL(char num[])
- { ListType q=L.head->next,R=L.head;
- while(q)
- { if(!strcmp(q->ch,num))
- { R->next=q->next;
- if(q==L.tail) L.tail=R;
- delete(q);
- q=R->next;
- L.size--;
- }
- else {R=R->next;q=q->next;}
- }
- }
- void count(ElemType q,char wh)
- { cout<<endl;
- cout<<"车 型: ";
- if(q.type=='p')
- {cout<<"客车 ";
- cout<<"收费标准: ";
- if(wh=='g') cout<<"0.15";
- else cout<<"0.075";
- cout<<" 元/分钟 "<<endl;
- }
- else if(q.type=='t')
- { cout<<"卡车 ";
- cout<<"收费标准: ";
- if(wh=='g') cout<<"0.3";
- else cout<<"0.15";
- cout<<" 元/分钟 "<<endl;
- }
- else { cout<<"小汽车 ";
- cout<<"收费标准: ";
- if(wh=='g') cout<<"0.1";
- else cout<<"0.05";
- cout<<" 元/分钟 "<<endl;
- }
- cout<<"停车时间: "<<elem->time-q.time<<" 分钟 ";
- cout<<" 收费金额: ";
- if(wh=='g')
- { if(q.type=='p')
- cout<<0.15*(elem->time-q.time)<<" 元";
- else if(q.type=='t')
- cout<<0.30*(elem->time-q.time)<<" 元";
- else cout<<0.1*(elem->time-q.time)<<" 元";
- }
- else { if(q.type=='p')
- cout<<0.075*(elem->time-q.time)<<" 元";
- else if(q.type=='t')
- cout<<0.15*(elem->time-q.time)<<" 元";
- else cout<<0.05*(elem->time-q.time)<<" 元";
- }
- cout<<endl;
- cout<<"-----------------------------------------------------------------------------"<<endl;
- }
- int main()
- {
- char c='E';
- elem=new(ElemType);
- elem->time=0;
- printmessage();
- InitList(L);
- cout<<"请输入车场总车位数:";
- cin>>n;
- InitStack(garage,n);
- InitQueue(waitway);
- do{
- Readmessage(c);
- Interpret(c);
- }while(c!='E');
- return 0;
- }
- void printmessage()
- {
- printf("n");
- printf("*******************************停车场管理操作程序*******************************n");
- printf("设计者:刘永杰 专业:工程力学02级 学号:0243053018 日期:27/10/2004n"); ;
- printf("--------------------------------------------------------------------------------n");
- printf("程序功能:n");
- printf("请用户按提示信息输入!n");
- printf("--------------------------------------------------------------------------------");
- }
- void Readmessage(char &c)
- {
- char h;
- char num[5];
- int time=0;
- again1:;
- cout<<"n请输入车辆的情况,'A'代表进站,'D'代表出站,'E'代表结束:";
- cin>>c;
- if(c!='A'&&c!='D'&&c!='E')
- { cout<<"输入不正确,请按要求重新输入!"<<endl;goto again1;}
- if(c=='D') {cout<<endl;goto again3;}
- if(c=='E') goto end;
- cout<<endl;
- again2:;
- cout<<"请输入该车的类型,'p'代表客车,'t'代表卡车,'c'代表小汽车:";
- cin>>h;
- if(h!='p'&&h!='t'&&h!='c')
- { cout<<"输入不正确,请按要求重新输入!"<<endl;goto again2;}
- elem->type=h;
- cout<<endl;
- again3:;
- cout<<"请输入该车的车牌:";
- cin>>num;
- if(c!='D')
- {if(judgenum(num)) {cout<<"该车牌已存在!请重新输入!"<<endl;goto again3;}
- else { ListType s;
- s=new(LNode);
- MakeNode(s,num);
- Insert(L,s);
- }
- }
- strcpy(elem->num,num);
- cout<<endl;
- again4:;
- if(c=='A') cout<<"请输入该车进车场时间:";
- else cout<<"请输入该车出车场的时间:";
- cin>>time;
- if(time<=elem->time)
- { cout<<"时间必须递增!请重新输入!"<<endl;
- goto again4;
- }
- elem->time=time;
- cout<<endl;
- end:;
- }
- void Interpret(char c)
- { if(c=='A')
- {if(garage.top-garage.base<garage.stacksize)
- { Push(garage,*elem);
- cout<<"请停在车场第 "<< i<<" 号车位!"<<endl;
- i++;
- }
- else{ EnQueue(waitway,*elem);
- cout<<"请停在便道!"<<endl;
- j++;
- }
- }
- else if(c=='D')
- { int k=1;
- LElemType s,g;
- ElemType e;
- Queueptr q;
- char wh='g';
- s=garage.base;
- while(strcmp(s->num,elem->num)&&k<i)
- { s++;k++;}
- if(k>=i)
- { q=waitway.front;
- k=1;
- while(strcmp(s->num,elem->num)&&k<j)
- { q++;k++;}
- if(k<j) wh='w';
- else wh='n';
- }
- //计算停车费
- if(wh=='g')
- { cout<<"-----------------------------------该车在车场内--------------------------------"<<endl;
- count(*s,wh);
- }
- else if(wh=='w')
- { cout<<"-----------------------------------该车在便道内---------------------------------"<<endl;
- count(q->data,wh);
- }
- else { cout<<"该车没在停车场内!"<<endl;goto end;}
- //让路情况
- if(wh=='g') //从车场开走
- { i--;
- DeleteL(s->num);
- if(++s!=garage.top)
- {
- giveway.base=s;
- giveway.top=garage.top;
- giveway.stacksize=giveway.top-giveway.base;
- cout<<"为该车让路,须从入口退出车场的车辆顺序:"<<endl;
- g=giveway.top;
- while(g!=giveway.base)
- { g--;
- cout<<" "<<g->num;
- }
- cout<<endl;
- }
- else cout<<"不必让路,该车可直接从车场开走!"<<endl;
- s--;
- g=giveway.base;
- while(g!=giveway.top)
- {*s=*g;s++;g++;}
- garage.top--;
- if(waitway.front!=waitway.rear) //便道上的车辆数目不为零
- { if(DeQueue(waitway,e))
- Push(garage,e);
- cout<<"######请便道内车牌为 "<<e.num<<" 的车进车场,停在第"<<i<<"号车位!######"<<endl;
- j--;
- i++;
- }
- }
- else if(wh=='w') //从便道上开走
- { j--;
- DeleteL(s->num);
- if(q!=waitway.front)
- {
- LinkQueue Q;
- InitQueue(Q);
- Queueptr p=waitway.front;
- do{ EnQueue(Q,p->data); //copy它前面的车辆情况
- p=p->next;
- }while(p->next!=q);
- p=waitway.front;
- Queueptr M=q->next;
- do{ p->data=M->data;
- p=p->next;
- M=M->next;
- }while(M);
- cout<<"便道上为该车让路的车辆顺序:";
- Queueptr N=Q.front;
- do{ cout<<" "<<N->data.num;
- p->data=N->data;
- p=p->next;
- N=N->next;
- }while(N!=Q.rear);
- DestroyQueue(Q);
- delete(waitway.rear);
- waitway.rear=p;
- }
- }
- else cout<<"不必让路,该车可直接从便道开走!"<<endl;
- }
- end:;
- }