Bean沅 Bean沅
关注数: 78 粉丝数: 166 发帖数: 3,500 关注贴吧数: 30
课程设计的程序有点小错误 求大神解决啊 !!明天要交了 #define INFINITY 10000 /*无穷大*/ #define MAX_VERTEX_NUM 40 #define MAX 40 #include<stdlib.h> #include<string.h> #include <iostream> using namespace std; #include <iomanip> typedef struct ArCell { int adj; //路径长度 }ArCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; typedef struct //图中顶点表示主要景点,存放景点的编号、名称、简介等信息, { char name[30]; int num; char introduction[100];//简介 }infotype; typedef struct { infotype vexs[MAX_VERTEX_NUM]; AdjMatrix arcs; int vexnum,arcnum; } MGraph; MGraph b; MGraph InitGraph(void); void Menu(void); void Browser(MGraph *G); void ShortestPath_DIJ(MGraph * G); void Floyd(MGraph *G); void Search(MGraph *G); /***********************主函数*******************************/ void main(void) { system("color 1f"); system("mode con: cols=100 lines=40"); int i; b=InitGraph(); Menu(); cin>>i; while(i!=5) { switch(i) { case 1:system("cls");Browser(&b);Menu();break; case 2:system("cls");ShortestPath_DIJ(&b);Menu();break; case 3:system("cls");Floyd(&b);Menu();break; case 4:system("cls");Search(&b);Menu();break; case 5:exit(1);break; default:break; } cin>>i; } } /*************************************************************/ /*************************定义景点编号,名称及简介************/ MGraph InitGraph(void) { MGraph G; int i,j; G.vexnum=13; //十个景点 G.arcnum=14; //邻接矩阵 for(i=0;i<G.vexnum;i++) G.vexs[i].num=i; //各景点的代码,名称及简介 strcpy(G.vexs[0].name,"学生活动中心"); strcpy(G.vexs[0].introduction," 大学生活动中心,举办各种活动的主要场所 "); strcpy(G.vexs[1].name," 李子湖 "); strcpy(G.vexs[1].introduction," 美丽的湖畔,清新的环境,约会的圣地 "); strcpy(G.vexs[2].name," 逸夫楼 "); strcpy(G.vexs[2].introduction," 学校的特色教学楼,共五层,可容纳多人 "); strcpy(G.vexs[3].name," 游泳馆 "); strcpy(G.vexs[3].introduction," 拥有两个泳池的游泳赛场 "); strcpy(G.vexs[4].name," 老图书馆 "); strcpy(G.vexs[4].introduction," 拥有全校机房,环境舒适 "); strcpy(G.vexs[5].name," 德园 "); strcpy(G.vexs[5].introduction," 男生女生公寓 "); strcpy(G.vexs[6].name," 食堂 "); strcpy(G.vexs[6].introduction," 标准食堂,三层,清洁卫生 "); strcpy(G.vexs[7].name," 教师楼 "); strcpy(G.vexs[7].introduction," 德园13舍,供给老师休息 "); strcpy(G.vexs[8].name," 新食堂 "); strcpy(G.vexs[8].introduction," 标准食堂,两层,清洁卫生 "); strcpy(G.vexs[9].name," 田径场 "); strcpy(G.vexs[9].introduction," 全新塑胶跑道,中间为人工草皮足球场,排球场和篮球场 "); strcpy(G.vexs[10].name,"图书馆 "); strcpy(G.vexs[10].introduction," 藏书几十万册,设施良好,每层楼均有阅览室 "); strcpy(G.vexs[11].name,"教学楼 "); strcpy(G.vexs[11].introduction," 学校上课活动的主要教室场所 "); strcpy(G.vexs[12].name,"力学楼 "); strcpy(G.vexs[12].introduction," 力学实验场所 "); for(i=0;i<G.vexnum;i++) for(j=0;j<G.vexnum;j++) G.arcs[i][j].adj=INFINITY; // 各景点之间的距离,没有的均为无穷大 G.arcs[0][1].adj=20; G.arcs[0][2].adj=20; G.arcs[1][5].adj=20; G.arcs[1][6].adj=30; G.arcs[2][3].adj=10; G.arcs[2][5].adj=30; G.arcs[3][4].adj=10; G.arcs[4][11].adj=10; G.arcs[5][6].adj=30; G.arcs[6][7].adj=10; G.arcs[7][8].adj=20; G.arcs[8][9].adj=10; G.arcs[9][10].adj=20; G.arcs[10][12].adj=100; G.arcs[11][12].adj=20; for(i=0;i<G.vexnum;i++) for(j=0;j<G.vexnum;j++) G.arcs[j][i].adj=G.arcs[i][j].adj; return G; } /*******************************************************************************************/ /********************************主菜单(显示输入提示) ****************************************/ void Menu() { cout<<" 重庆交通大学导游图"<<endl; cout<<"┏━━━━━━━━━━━━━━━━━━━━┓"<<endl; cout<<"┃1.浏览各景点及简介 ┃"<<endl; cout<<"┃2.查看所有游览路线 ┃"<<endl; cout<<"┃3.选择出发点和目的地 ┃"<<endl; cout<<"┃4.查看景点信息 ┃"<<endl; cout<<"┃5.退出系统 ┃"<<endl; cout<<"┗━━━━━━━━━━━━━━━━━━━━┛"<<endl; cout<<"Option-:"; } /************************************显示景点编号、名称、简介****************************************/ void Browser(MGraph *G) { int v; cout<<"┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl; cout<<"┃编号 景点名称 ┃简介 ┃"<<endl; for(v=0;v<G->vexnum;v++) cout<<"┃"<<G->vexs[v].num<<setw(5)<<" "<<G->vexs[v].name<<setw(10)<<"┃"<<G->vexs[v].introduction<<setw(3)<<"┃"<<endl; cout<<"┗━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl; } /********************迪杰斯特拉算法来计算出起点到各个顶点之间的最短路径,v0为起点***********************/ void ShortestPath_DIJ(MGraph * G) { int v,w,i,min,t=0,x,flag=1,v0; int final[20], D[20], p[20][20]; cout<<"┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl; cout<<"┃编号 景点名称 ┃简介 ┃"<<endl; for(v=0;v<G->vexnum;v++) cout<<"┃"<<G->vexs[v].num<<setw(5)<<" "<<G->vexs[v].name<<setw(10)<<"┃"<<G->vexs[v].introduction<<setw(3)<<"┃"<<endl; cout<<"┗━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl; while(flag) { cout<<"请输入一个起始景点编号:";cin>>v0; if(v0<0||v0>G->vexnum) { cout<<"景点编号不存在!请重新输入景点编号:";cin>>v0; } if(v0>=0&&v0<G->vexnum) flag=0; } for(v=0;v<G->vexnum;v++) { final[v]=0; D[v]=G->arcs[v0][v].adj; for(w=0;w<G->vexnum;w++) p[v][w]=0; if(D[v]<INFINITY) { p[v][v0]=1;p[v][v]=1; } } D[v0]=0;final[v0]=1; for(i=1;i<G->vexnum;i++) { min=INFINITY; for(w=0;w<G->vexnum;w++) if(!final[w]) if(D[w]<min){v=w;min=D[w];} final[v]=1; for(w=0;w<G->vexnum;w++) if(!final[w]&&(min+G->arcs[v][w].adj<D[w])) { D[w]=min+G->arcs[v][w].adj; for(x=0;x<G->vexnum;x++) p[w][x]=p[v][x]; p[w][w]=1; } } for(v=0;v<G->vexnum;v++) { if(v0!=v) cout<<G->vexs[v0].name; for(w=0;w<G->vexnum;w++) { if(p[v][w]&&w!=v0) cout<<"-->"<<G->vexs[w].name; t++; } if(t>G->vexnum-1&&v0!=v) cout<<"总路线长"<<D[v]<<endl; } } /*********************************Floyd函数***************************************/ void Floyd(MGraph *G) { int v,u,i,w,k,j,flag=1,p[10][10][10],D[10][10]; cout<<"┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl; cout<<"┃编号 景点名称 ┃简介 ┃"<<endl; for(v=0;v<G->vexnum;v++) cout<<"┃"<<G->vexs[v].num<<setw(5)<<" "<<G->vexs[v].name<<setw(10)<<"┃"<<G->vexs[v].introduction<<setw(3)<<"┃"<<endl; cout<<"┗━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl; for(v=0;v<G->vexnum;v++) for(w=0;w<G->vexnum;w++) { D[v][w]=G->arcs[v][w].adj; for(u=0;u<G->vexnum;u++) p[v][w][u]=0; if(D[v][w]<INFINITY) { p[v][w][v]=1;p[v][w][w]=1; } } for(u=0;u<G->vexnum;u++) for(v=0;v<G->vexnum;v++) for(w=0;w<G->vexnum;w++) if(D[v][u]+D[u][w]<D[v][w]) { D[v][w]=D[v][u]+D[u][w]; for(i=0;i<G->vexnum;i++) p[v][w][i]=p[v][u][i]||p[u][w][i]; } while(flag) { cout<<"请输入出发点和目的地的编号(用空格隔开):";cin>>k>>j; if(k<0||k>G->vexnum||j<0||j>G->vexnum) { cout<<"景点编号不存在!请重新输入出发点和目的地的编号:";cin>>k>>j; } if(k>=0&&k<G->vexnum&&j>=0&&j<G->vexnum) flag=0; } cout<<G->vexs[k].name; for(u=0;u<G->vexnum;u++) if(p[k][j][u]&&k!=u&&j!=u) cout<<"-->"<<G->vexs[u].name; cout<<"-->"<<G->vexs[j].name; cout<<"总路线长"<<D[k][j]<<endl; }//Floyd end /************************************查找景点*******************************************************/ void Search(MGraph *G) { int k,v,flag=1; cout<<"┏━━━━━━━━━━━━━┓"<<endl; cout<<"┃编号 景点名称 ┃"<<endl; for(v=0;v<G->vexnum;v++) cout<<"┃"<<G->vexs[v].num<<setw(5)<<" "<<G->vexs[v].name<<setw(10)<<"┃"<<endl; cout<<"┗━━━━━━━━━━━━━┛"<<endl; while(flag) { cout<<"请输入要查询的景点编号:"; cin>>k; if(k<0||k>G->vexnum) { cout<<"景点编号不存在!请重新输入景点编号:"; cin>>k; } if(k>=0&&k<G->vexnum) flag=0; } cout<<"┏━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓"<<endl; cout<<"┃编号 景点名称 ┃简介 ┃"<<endl; cout<<"┃"<<G->vexs[k].num<<setw(5)<<" "<<G->vexs[k].name<<setw(10)<<"┃"<<G->vexs[k].introduction<<setw(3)<<"┃"<<endl; cout<<"┗━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛"<<endl; }//Search end 程序可以运行 程序运行了以后第三个目的地和出发地这个选项一直有错误 谁能帮我解决了 把改好的程序发给我啊 谢谢了
1 下一页