烟台汽车行程显示系统 c++ 实现

老师布置的作业,呵呵,做了做,呵呵。

 

题目要求:

实验2:栈和队列的基本操作:烟台公共汽车行程显示系统

考察烟台某路汽车。调查以下事实:

1.      所有站点名称。

2.      每日发车班次

3.      每班车到达各站点的时间 (假设汽车在两个站点之间总是用同样的时间)

请编写程序实现烟台该路汽车行程显示系统,为乘客选择乘车路线及返程路线。

例子:某乘客乘坐10:20分的10路汽车由烟台大学到文化中心,要求输出如下内容:

烟台大学   清泉寨     工商学院…                  …文化中心

10:20     10:22     10:24 …                  …10:40

文化中心  …                                     …烟台大学

2:00                                              2:20

 

提示

1.    为该路汽车站点建立线性表(顺序链式存储均可),每个站点包含站点名以及到达下一站的时间

2.    建立汽车始发点发车时刻表

3.    当乘客选择一个班车时,建立一个循环队列入队乘客所经过的各个站点,建立一个链式堆栈入栈各个站点。

4.    计算乘客行程的始发站和返程始发的站点时间

5.    将循环队列各元素出队,并为每个站点配备到达时间

6.    将堆栈个各元素出栈,并为每个站点配备时间

7.    如果配备时间有难度,只输出行程路线和返程路线即可。即只输出:

烟台大学   清泉寨     工商学院…                  …文化中心

文化中心  …                                     …烟台大学

 

要求:

1.      各函数之间要用空行分隔。

2.      每个函数要有注释,说明该函数的功能。用注释说明新变量的功能,程序段的思想和功能。

3.      采用内缩制,有明显的程序层次。

4.      采用C语言函数和变量命名规则

代码如下:

#include <iostream> #include <string> using namespace std; //建立链表 struct ListData { string station; string time; ListData * next; }; ListData* LinkedlistInit() //初始化一个站点 { ListData * L = new ListData; if (L == NULL) { cout <<"没有足够的内存空间!"<<endl; } L->next = NULL; return L; } int LinkedListLength(ListData * L) //求总站数 { ListData * p = new ListData; p = L->next; int j = 0; while(p) { j++; p = p->next; } return j; } ListData * LinkedListInsert(ListData * L,ListData * p,ListData e) //插入新的站点 { ListData * pre = new ListData; pre = L; while (pre && pre->next!=p) { pre = pre->next; } if (!pre) { cout <<"不存在*p结点!"<<endl; exit(0); } ListData * s= new ListData; s->station = e.station; s->time = e.time; s->next = pre->next; pre->next = s; return L; } ListData * LinkedListDel(ListData * L,string station) //删除一个站点 { if (L->next == NULL) { cout <<"空表!"<<endl; } ListData * pre = new ListData; pre = L; while (pre->next && pre->next->station != station) { pre = pre->next; } if (pre->next==NULL) { cout <<"不存在该站名!"<<endl; exit(0); } ListData * s= new ListData; s = pre->next; pre->next = s->next; delete(s); return L; } ListData * ListgetprebyName(ListData * L , string name) //根据站名得到前一个站 { ListData * p = new ListData; ListData * q = new ListData; q = L; p = L->next; while(p->station != name) { p = p->next; q = q->next; } if (p != NULL && q != L) { cout <<"前一个站为:"<<q->station<<endl; return q; } else { cout <<"没有该站!"<<endl; return NULL; } } void Listtraversal(ListData * L) //遍历List { ListData * p = new ListData; p = L; while(p != NULL) { cout <<"站名 :"<<p->station<<"\t\t"; cout <<"到达时间 :"<<p->time<<endl; p = p->next; } } string ListgetTime(ListData * L , string station) //根据站名得到时间 { ListData * p = new ListData; p = L->next; while(p->station != station) { p = p->next; } if (p != NULL) { return p->time; } else { cout <<"没有该站!"<<endl; exit(0); } } //建立链栈 struct LinkedData { public: string station; string time; LinkedData * next; }; //链栈的初始化 LinkedData* SeqStackInit() { LinkedData *top = new LinkedData; top = NULL; return top; } //链栈的判空 bool SeqStackEmpty(LinkedData* s) { if (NULL == s) { return true; } else return false; } //链栈的入栈 LinkedData * SeqStackPush(LinkedData* s, string sta, string ti) { LinkedData* s1 = new LinkedData; s1->station = sta; s1->time = ti; s1->next = s; s = s1; //cout<<"入栈元素:"<<s->station<<endl; return s; } //链栈的出栈 LinkedData * SeqStackPop(LinkedData* s) { if (NULL != s) { string sta; string ti; LinkedData * p = new LinkedData; p = s; sta = s->station; ti = s->time; cout <<"站点:"<<s->station<<"\t\t"; cout <<"时间:"<<s->time<<endl; s = s->next; delete(p); return s; } } //链栈中取出栈顶元素 string SeqStackGetTop(LinkedData* s) { if (NULL != s) return s->station; } //链栈的长度 int SeqStackLength(LinkedData* s) { int length = 0; LinkedData* s1 = s; //cout <<s->station<<endl; while(NULL != s1) { length++; s1 = s1->next; } return length; } //建立循环队列 #define MAXSIZE 100 struct SeQueueData { string station[MAXSIZE]; string time[MAXSIZE]; int front ; int rear ; }; //置空队 SeQueueData * SeQueueInit() { SeQueueData * Q = new SeQueueData; Q->front = Q->rear = 0; return Q; } //入队 SeQueueData * SeQueueIn(SeQueueData * Q,string sta,string ti) { if((Q->rear+1)%MAXSIZE==Q->front) { cout <<"队满!"<<endl; exit(0); } Q->rear = (Q->rear+1)%MAXSIZE; Q->station[Q->rear] = sta; Q->time[Q->rear] = ti; //cout<<sta<<endl; //cout <<ti<<endl; return Q; } //出队 SeQueueData * SeQueueOut(SeQueueData * Q) { if (Q->front==Q->rear) { cout <<"队空!"<<endl; exit(0); } Q->front = (Q->front+1)%MAXSIZE; string sta; string ti; sta = Q->station[Q->front]; ti = Q->time[Q->front]; return Q; } //判断队空 bool SeQueueEmpty(SeQueueData * Q) { if (Q->front==Q->rear) { return true; } else { return false; } } //求队中的元素个数 int SeQueueLength(SeQueueData * Q) { return (Q->rear - Q->front+MAXSIZE)%MAXSIZE; } SeQueueData * setSeQueueData(ListData * listedData,string startstation,string stopstation) { //cout<<"链表元素入队..."<<endl; SeQueueData * sequeueData = new SeQueueData; sequeueData = SeQueueInit(); ListData * p = new ListData; p = listedData; while(p->station!=startstation && p!=NULL) { p = p->next; } while(p->station!=stopstation)//链表元素入队 { SeQueueIn(sequeueData,p->station,p->time); cout <<sequeueData->station[sequeueData->front]<<"\t\t"; cout <<sequeueData->time[sequeueData->front]<<endl; p = p->next; } SeQueueIn(sequeueData,p->station,p->time); //cout<<"链表元素入队完成。"<<endl; //cout<<"队的长度:"<<SeQueueLength(sequeueData)<<endl; return sequeueData; } LinkedData * setLinkedData(ListData * listedData,string startstation,string stopstation) { //cout<<"链表元素入栈..."<<endl; ListData * p2 = new ListData; p2 = listedData; LinkedData * linkedData = SeqStackInit(); while(p2->station!=startstation) { cout <<p2->station<<endl; p2 = p2->next; } while(p2->station!=stopstation)//链表元素入栈 { linkedData = SeqStackPush(linkedData,p2->station,p2->time); p2 = p2->next; } linkedData = SeqStackPush(linkedData,p2->station,p2->time); //cout<<"链表元素入栈完成。"<<endl; //cout <<"栈的长度:"<<SeqStackLength(linkedData)<<endl; return linkedData; } void getgo(SeQueueData * sequeueData) { //cout <<"出队..."<<endl; cout<<"去程:"<<endl; sequeueData->front = (sequeueData->front+1)%MAXSIZE; cout <<"站点:"<<sequeueData->station[sequeueData->front]<<"\t\t"; cout <<"时间"<<sequeueData->time[sequeueData->front]<<endl; while(sequeueData->front!=sequeueData->rear) { sequeueData = SeQueueOut(sequeueData); cout <<"站点:"<<sequeueData->station[sequeueData->front]<<"\t\t"; cout <<"时间"<<sequeueData->time[sequeueData->front]<<endl; } //cout<<"队的长度:"<<SeQueueLength(sequeueData)<<endl; } void getback(LinkedData * linkedData) { //cout<<"出栈..."<<endl; cout<<"回程:"<<endl; int length = SeqStackLength(linkedData); while(length > 0) { linkedData = SeqStackPop(linkedData); length--; } //cout <<"栈的长度:"<<SeqStackLength(linkedData)<<endl; } int main() { //1. 为该路汽车站点建立线性表 cout <<"为该路汽车站点建立线性表..."<<endl; ListData ld1; ld1.station = "烟台大学"; ld1.time = "8:00"; ListData ld2; ld2.station = "烟台大学西门"; ld2.time = "9:00"; ListData ld3; ld3.station = "烟台清泉学校"; ld3.time = "10:00"; ListData ld4; ld4.station = "体育公园"; ld4.time = "11:00"; ListData ld5; ld5.station = "滨州医学院"; ld5.time = "12:00"; ListData ld6; ld6.station = "烟台世贸中心"; ld6.time = "13:00"; ListData * listedData = new ListData; listedData = LinkedlistInit(); LinkedListInsert(listedData,listedData->next,ld6); LinkedListInsert(listedData,listedData->next,ld5); LinkedListInsert(listedData,listedData->next,ld4); LinkedListInsert(listedData,listedData->next,ld3); LinkedListInsert(listedData,listedData->next,ld2); LinkedListInsert(listedData,listedData->next,ld1); cout <<"为该路汽车站点建立线性表完成。"<<endl; //Listtraversal(listedData->next); cout<<"链表元素入队..."<<endl; SeQueueData * sequeueData = new SeQueueData; sequeueData = SeQueueInit(); ListData * p = new ListData; p = listedData; while(p!=NULL)//链表元素入队 { SeQueueIn(sequeueData,p->station,p->time); p = p->next; } cout<<"链表元素入队完成。"<<endl; sequeueData->front = (sequeueData->front+1)%MAXSIZE; //cout<<"队的长度:"<<SeQueueLength(sequeueData)<<endl; cout<<"链表元素入栈..."<<endl; ListData * p2 = new ListData; p2 = listedData; LinkedData * linkedData = SeqStackInit(); while(LinkedListLength(p2) > 0)//链表元素入栈 { p2 = p2->next; linkedData = SeqStackPush(linkedData,p2->station,p2->time); } cout<<"链表元素入栈完成。"<<endl; //cout <<"栈的长度:"<<SeqStackLength(linkedData)<<endl; //出队 //cout <<"出队..."<<endl; cout<<"去程:"<<endl; sequeueData->front = (sequeueData->front+1)%MAXSIZE; cout <<"站点:"<<sequeueData->station[sequeueData->front]<<"\t\t"; cout <<"时间"<<sequeueData->time[sequeueData->front]<<endl; while(sequeueData->front!=sequeueData->rear) { sequeueData = SeQueueOut(sequeueData); cout <<"站点:"<<sequeueData->station[sequeueData->front]<<"\t\t"; cout <<"时间"<<sequeueData->time[sequeueData->front]<<endl; } //cout<<"队的长度:"<<SeQueueLength(sequeueData)<<endl; //出栈 //cout<<"出栈..."<<endl; cout<<"回程:"<<endl; int length = SeqStackLength(linkedData); while(length > 0) { linkedData = SeqStackPop(linkedData); length--; } //cout <<"栈的长度:"<<SeqStackLength(linkedData)<<endl; //cout <<"按 任意键 进行路线查询||输入0退出系统"<<endl; while(1) { cout <<"请输入起始站名:"<<endl; string startstation; string stopstation; cin >>startstation>>stopstation; linkedData = setLinkedData(listedData,startstation,stopstation); sequeueData = setSeQueueData(listedData,startstation,stopstation); getgo(sequeueData); getback(linkedData); } return 0; }


别忘了,仅供参考。自己的作业自己写,呵呵。

转载于:https://www.cnblogs.com/lcqBlogs/archive/2011/09/24/2392389.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值