logo资料库

c++数据结构实验报告(图专题).doc

第1页 / 共16页
第2页 / 共16页
第3页 / 共16页
第4页 / 共16页
第5页 / 共16页
第6页 / 共16页
第7页 / 共16页
第8页 / 共16页
资料共16页,剩余部分请下载后查看
实验名称: 公交线路查询(图)
本科实验报告 实验名称: 公交线路查询(图) 数据结构与算法设计 A(C++ 课程名称: 描述) 任课教师: 高飞 实验教师: 高飞 学生姓名: 唐高鹏 实验时间: 2018 年 5 月 23 日 实验地点: 良乡 1-309 实验类型: □ 原理验证 □ 综合设计 □ 自主创新 学号/班级: 1120170429/05951701 组 号: 6 学 院: 信息与电子学院 同组搭档: 冯珂 赵鹏 专 业: 电子信息(实验班) 成 绩:
(1)问题描述 当一个用户从甲地到乙地时,由于不同需求,就有不同的交通方式及不同 的交通路线。有人希望以最快速度到达,有人希望以最短距离到达,有人希望 用最少的费用等。交通方式有公交车和地铁。编写一北京公交线路查询系统, 通过输入起始站、终点站,为用户提供三种或以上决策的交通咨询。 (2)设计要求 a. 提供对交通线路进行编辑功能。要求可添加或删除线路。 b. 提供两种交通工具,公交车和地铁,设定路程所需要的时间、距离及费用 等参数。 c. 提供多种决策:最短距离、最快到达、最少费用、最少换乘次数等。 d. 中途不考虑等候、拥堵等消耗时间。 e. 该系统以人机对话方式进行。用户输入起始站、终点站及需求原则,系统 输出乘车方案:乘什么车、乘几路车、距离、时间、费用换乘方法等相关信息。 (3)数据结构及算法分析 a. 可以以邻接表作交通图的存储结构,表示边的结构内除包含有邻接点的信 息外,还应包括交通工具、路程时间和费用等多种属性。 b. 使用图的基本算法:插入、删除、排序、深度优先级搜索和广度优先搜索 等算法。 (4)设计与实现 主要代码: 主函数: int main() { string passwd; string c; Route R; int i; R.Creat(); while(1) { system("cls"); cout << "公交线路查询系统\n"; cout << "请选择要进行的操作\n"; cout << "1、对线路进行修改\n"; cout << "2、查询线路信息\n"; cin >> c; if(c == "1") {
system("cls"); Manager(R); } else if(c == "2") { system("cls"); User(R); } else { } } return 0; } cout << "您没有选择任何一个选项!\n"; system("pause"); 修改路线 void Manager(Route &R) {string c1; while(1) {system("cls"); cout << "请选择要进行的操作\n"; cout << "1.显示全部线路\n"; cout << "2.建立新线路\n"; cout << "3.删除线路\n"; cout << "4.查询一条线路\n"; cout << "5.查询站点\n"; cout << "6.保存目前线路\n"; cout << "7.返回上级菜单\n"; cin >> c1; system("cls"); if(c1 == "1") {R.Showall(); system("pause"); system("cls");} else if(c1 == "2") {R.Add(); system("pause");} else if(c1 == "3")
{ R.Dele(); system("pause");} else if(c1 == "4") {R.CheckRoute();} else if(c1 == "5") {R.CheckStation();} else if(c1 == "6") {R.Save(); cout << "已保存!!!\n"; system("pause");} else if(c1 == "7") {break;} else {cout << "您没有选择任何一个选项!\n"; system("pause");}} system("cls");} 建立新线路 void Route::Add() {RouteData *nr = new RouteData; cout << "您将要建立一条新的公交线路。 请按照指示输入新路线!\n"; cout << "请输入路线的名称:";cin >> nr->rn; point1: cout << "请输入该线路的类型(0 为地铁,1 为公交):"; cin >> nr->type; if(nr->type == 0) {cout << "请输入该路线所需费用\n"; cin >> nr->fee;} else if(nr->type == 1) {cout << "请输入该条线路的票价(元):"; else{cout << "输入有误,请重新输入!\n"; cin >> nr->fee;} goto point1;} StationData * w = nr->link; cout << "请依次输入所需信息(输入#结束)\n"; cout << "请输入站点名: "; string s;cin >> s; while(s != "#") {StationData * pp = new StationData;
pp->sn = s; cout << "请输入与下一站的时间间隔(min):"; cin >> pp->time; cout << "请输入与下一站的距离(km):"; cin >> pp->distance; if(nr->link == NULL) {nr->link = pp;w = nr->link;} else {w->next = pp;w = pp; } cout << "请输入站点名: "; cin >> s;} ShowOneRoute(nr); point2: cout << "您确定保存么?保存 Y 或 y,不保存 N 或 n。\n 选项:"; char ch;cin >> ch; if(ch == 'Y' || ch == 'y') {RouteData * k = head; if(head == NULL) {head = nr;k = nr;} else{while(k->next != NULL) k = k->next; k->next = nr;} num++;} else if(ch == 'N' || ch == 'n'); else goto point2;} 查询线路 void Route::CheckRoute() {string rr;point2: cout << "请输入您需要查询的线路名,然后将会显示该线路的相关信息。\n"; cout << "线路名:"; cin >> rr; cout << endl; RouteData * p = head; int tf = 0; while(p != NULL) {if(p->rn == rr) {tf = 1; ShowOneRoute(p); break;} p = p->next;} if(tf == 0) {cout << "没有找到该线路!请重新输入……\n"; system("pause"); goto point2;} cout << endl;
system("pause");} 保存线路 void Route::Save() { fstream f(".\\data.txt",ios::out); if(!f) return; RouteData * r = head; while(r != NULL) { StationData * s = r->link; f << r->rn << " " << r->type << " " << r->fee << "\n"; while(s != NULL) { f << s->sn << " " << s->time << " " << s->distance << " "; s = s->next; } f << "#\n"; r = r->next; } f << "end"; f.close(); } 删除线路 void Route::Dele() {Showall(); string s; RouteData * n; point3: cout << "请选择您希望删除的线路的名字(不包括前缀地铁或公交):"; cin >> s; n = Find(s); if(n == NULL) {point4: cout << "没有找到您所想删除的路线!\n1.重新输入\n2.离开\n 选项:"; int i; cin >> i; if(i == 1) goto point3; else if(i == 2);
else goto point4; } else {ShowOneRoute(n); point5: cout << "确定删除该路线么?删除 Y 或 y,不删除 N 或 n\n 选项:"; char ch; cin >> ch; if(ch == 'Y' || ch == 'y') {RouteData * p = head; while(p->next != n) p = p->next; p->next = n->next; Save();} else if(ch == 'N' || ch == 'n'); else goto point5; }} 查询路线 void User(Route &R) {string c1,c2; while(1) {system("cls"); cout << "请选择要进行的操作\n"; cout << "1.查询一条线路\n";cout << "2.查询站点信息\n"; cout << "3.搜索信息\n"; cout << "4.返回上级菜单\n"; cin >> c1;system("cls"); if(c1 == "1"){R.CheckRoute();} else if(c1 == "2") {R.CheckStation();} else if(c1 == "3") {cout << "\n\n"; Graph G;G.CreatGraph(R); string start,end;point4: cout << "请输入起始站点名称:"; cin >> start; cout << "请输入目的地站点名称:"; cin >> end; if(start == end) {cout << "起始站与目的地不能相同!!!\n"; system("pause");goto point4;} cout << "请选择路线查询方式 :\n"; cout << "1.最短距离\n";cout << "2.最短时间\n"; cout << "3.最少换乘\n";cout << "4.最少花费\n";
{G.DijkstraDistance(R,start,end); system("pause");system("cls");} else if(c2 == "2") {G.DijkstraTime(R,start,end); system("pause");system("cls");} else if(c2 == "3") {cout << "请选择最少换乘模式\n"; cout << "1.最短距离\n"; cout << " 2.最短时间\n"; int whatever;cin >> whatever; G.MinChange(R,start,end,whatever); system("pause");system("cls");} else if(c2 == "4") {cout << " cout << " cout << " cout << " int whatever; cin >> whatever; 请选择最少花费模式 1.最短距离 2.最短时间 选项:"; \n"; \n"; \n"; cin >> c2; if(c2 == "1") G.MinMoney(R,start,end,whatever); system("pause");system("cls");}} else if(c1 == "4") break; else{cout << "您没有选择任何一个选项!\n"; system("pause"); } } system("cls");} 最短距离 void Graph::DijkstraDistance(Route &R,string start,string end) {int v0,v1; v0 = FindNumber(start); v1 = FindNumber(end); int v =v0; lowcost[v0] = 0; Queue queue; int i,num = 0; for(i=0;i
分享到:
收藏