zzp719238
  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 
  4 #define MAX 10 // 从校园中取10个点
  5 #define INTMAX 66666
  6 //Floyd算法数据
  7 int L[MAX+1][MAX+1]; // 存最短路径长度
  8 int D[MAX+1][MAX+1]; // 存最短路径点
  9 
 10 // 地点结构体
 11 typedef struct
 12 {
 13     int siteNum; // 地点编号
 14     string siteName; // 地点名称
 15     string sitePresentation; // 地点介绍
 16 }Site;
 17 
 18 // 校园平面图
 19 class SGraph
 20 {
 21 private:
 22     Site site[MAX+1]; // 校园
 23     int graph[MAX+1][MAX+1]; // 图的邻接矩阵
 24     int siteSum; // 地点数
 25     int pathSum;
 26 
 27 public:
 28     // 默认为科大校园信息
 29     SGraph(){
 30         siteSum=10;
 31         pathSum=45;
 32         site[1].siteName="东门";
 33         site[2].siteName="春晖学堂";
 34         site[3].siteName="图书馆";
 35         site[4].siteName="文馨书院";
 36         site[5].siteName="秋实楼";
 37         site[6].siteName="逸夫楼";
 38         site[7].siteName="体育馆";
 39         site[8].siteName="颐园";
 40         site[9].siteName="3号楼";
 41         site[10].siteName="6号楼";
 42 
 43         site[1].siteNum=1;
 44         site[2].siteNum=2;
 45         site[3].siteNum=3;
 46         site[4].siteNum=4;
 47         site[5].siteNum=5;
 48         site[6].siteNum=6;
 49         site[7].siteNum=7;
 50         site[8].siteNum=8;
 51         site[9].siteNum=9;
 52         site[10].siteNum=10;
 53 
 54         site[1].sitePresentation="内科大正门";
 55         site[2].sitePresentation="内科大主教";
 56         site[3].sitePresentation="内科大最喜欢的地方";
 57         site[4].sitePresentation="内科大二教";
 58         site[5].sitePresentation="内科大实验楼,含高配机房";
 59         site[6].sitePresentation="内科大最气派的教学楼";
 60         site[7].sitePresentation="内科大体测地点";
 61         site[8].sitePresentation="内科大的食堂之一";
 62         site[9].sitePresentation="内科大女寝之一,常见许多男子楼下等候女友";
 63         site[10].sitePresentation="内科大混寝之一";
 64         for(int i=0;i<MAX;i++)
 65         {
 66             graph[i][0]=INTMAX;
 67             graph[0][i]=INTMAX;
 68         }
 69         graph[1][2]=79;
 70         graph[2][1]=79;
 71         graph[1][3]=567;
 72         graph[3][1]=567;
 73         graph[1][4]=667;
 74         graph[4][1]=667;
 75         graph[1][5]=901;
 76         graph[5][1]=901;
 77         graph[1][6]=1100;
 78         graph[6][1]=1100;
 79         graph[1][7]=1000;
 80         graph[7][1]=1000;
 81         graph[1][8]=928;
 82         graph[8][1]=928;
 83         graph[1][9]=872;
 84         graph[9][1]=872;
 85         graph[1][10]=364;
 86         graph[10][1]=364;
 87         graph[1][1]=INTMAX;
 88         graph[2][3]=505;
 89         graph[3][2]=505;
 90         graph[2][4]=591;
 91         graph[4][2]=591;
 92         graph[2][5]=791;
 93         graph[5][2]=791;
 94         graph[2][6]=1000;
 95         graph[6][2]=1000;
 96         graph[2][7]=950;
 97         graph[7][2]=950;
 98         graph[2][8]=850;
 99         graph[8][1]=850;
100         graph[2][9]=410;
101         graph[9][2]=410;
102         graph[2][10]=386;
103         graph[10][2]=386;
104         graph[2][2]=INTMAX;
105         graph[3][4]=384;
106         graph[4][3]=384;
107         graph[3][5]=435;
108         graph[5][3]=435;
109         graph[3][6]=829;
110         graph[6][3]=829;
111         graph[3][7]=905;
112         graph[7][3]=905;
113         graph[3][8]=756;
114         graph[8][3]=756;
115         graph[3][9]=450;
116         graph[9][3]=450;
117         graph[3][10]=667;
118         graph[10][3]=667;
119         graph[3][3]=INTMAX;
120         graph[4][5]=450;
121         graph[5][4]=450;
122         graph[4][6]=552;
123         graph[6][4]=552;
124         graph[4][7]=829;
125         graph[7][4]=829;
126         graph[4][8]=905;
127         graph[8][4]=905;
128         graph[4][9]=756;
129         graph[9][4]=756;
130         graph[4][10]=450;
131         graph[10][4]=450;
132         graph[4][4]=INTMAX;
133         graph[5][6]=444;
134         graph[6][5]=444;
135         graph[5][7]=561;
136         graph[7][5]=561;
137         graph[5][8]=511;
138         graph[8][5]=511;
139         graph[5][9]=752;
140         graph[9][5]=752;
141         graph[5][10]=1000;
142         graph[10][5]=1000;
143         graph[5][5]=INTMAX;
144         graph[6][7]=137;
145         graph[7][6]=137;
146         graph[6][8]=586;
147         graph[8][6]=586;
148         graph[6][9]=926;
149         graph[9][6]=926;
150         graph[6][10]=1200;
151         graph[10][6]=1200;
152         graph[6][6]=INTMAX;
153         graph[7][8]=521;
154         graph[8][7]=521;
155         graph[7][9]=861;
156         graph[9][7]=861;
157         graph[7][10]=1100;
158         graph[10][7]=1100;
159         graph[7][7]=INTMAX;
160         graph[8][9]=427;
161         graph[9][8]=427;
162         graph[8][10]=884;
163         graph[10][8]=884;
164         graph[8][8]=INTMAX;
165         graph[9][10]=458;
166         graph[10][9]=458;
167         graph[9][9]=INTMAX;
168         graph[10][10]=INTMAX;
169 
170 
171     }
172     ~SGraph(){cout<<"\t\t\t\t\t平面图已释放"<<endl;}
173 
174     // 获取地点编号
175     int getsiteNum(string siteName)
176     {
177         for(int i=1;i<=MAX;i++)
178         {
179             if(siteName==site[i].siteName)
180             {
181                 return i;
182             }
183         }
184         cout<<"\t\t\t\t\t未找到该地点!"<<endl;
185         exit(0);
186     }
187 
188     // 构建平面图
189     void createSGraph()
190     {
191         // 存入地点信息
192         for(int i=1;i<=MAX;i++)
193         {
194             site[i].siteNum=i;
195             cout<<"\t\t\t\t\t请输入地点名称:";
196             cin>>site[i].siteName;
197             cout<<"\t\t\t\t\t请输入地点介绍:";
198             cin>>site[i].sitePresentation;
199         }
200 
201         // 存入平面图信息
202         siteSum=MAX;
203         cout<<"\t\t\t\t\t请输入边数:";
204         cin>>pathSum;
205 
206         // 初始化边为无穷大
207         for(int i=1;i<=MAX;i++)
208         {
209             for(int j=1;j<=MAX;j++)
210             {
211                 graph[i][j]=INTMAX;
212             }
213         }
214         // 存邻接矩阵
215         for(int k=1;k<=pathSum;k++)
216         {
217             int siteNum1,siteNum2; // 起始地点编号、终止地点编号
218             string siteName1,siteName2; // 起始地点名称、终止地点名称
219             int length;
220             cout<<"\t\t\t\t\t请输入起点:";
221             cin>>siteName1;
222             siteNum1=getsiteNum(siteName1);
223             cout<<"\t\t\t\t\t请输入终点:";
224             cin>>siteName2;
225             siteNum2=getsiteNum(siteName2);
226             cout<<"\t\t\t\t\t请输入长度:";
227             cin>>length;
228             graph[siteNum1][siteNum2]=length;
229             graph[siteNum2][siteNum1]=length;
230         }
231         cout<<"\t\t\t\t\t[添加信息成功!]"<<endl;
232         cout<<"\t\t\t\t\t";
233         system("pause");
234     }
235 
236     // 在D://学校编号.txt文件中存入数据
237     void saveData()
238     {
239         cout<<"\t\t\t\t\t请输入学校编号:";
240         int ischoolNum;
241         cin>>ischoolNum;
242         // 合成文件路径
243         char cschoolNum[20];
244         sprintf(cschoolNum,"%d",ischoolNum); // 将数据按照格式写入字符串
245         char filePath[300]="D:\\计算机编程\\C_Dev-C++\\大二数据结构作业\\课程设计\\";
246         strcat(filePath,cschoolNum);
247         strcat(filePath,".txt");
248         // 存入文件
249         ofstream out(filePath);
250         if(!out)
251         {
252             cout<<"\t\t\t\t\t文件保存失败!"<<endl;
253             exit(0);
254         }
255 
256         // 存邻接矩阵
257         for(int i=1;i<=MAX;i++)
258         {
259             for(int j=1;j<=MAX;j++)
260             {
261                 out<<graph[i][j]<<\' \';
262             }
263         }
264         out<<endl;
265         // 存边数和点数
266         out<<siteSum<<\' \'<<pathSum<<endl;
267         // 存地点信息
268         for(int i=1;i<=MAX;i++)
269         {
270             out<<site[i].siteNum<<endl;
271             out<<site[i].siteName<<endl;
272             out<<site[i].sitePresentation<<endl;
273         }
274         out.close();
275         cout<<"\t\t\t\t\t[已成功保存数据!]"<<endl;
276         cout<<"\t\t\t\t\t";
277         system("pause");
278     }
279 
280     // 从D://学校编号.txt文件中读取数据
281     void readData()
282     {
283         cout<<"\t\t\t\t\t请输入学校编号:";
284         int ischoolNum;
285         cin>>ischoolNum;
286         // 合成文件路径
287         char cschoolNum[20];
288         sprintf(cschoolNum,"%d",ischoolNum); // 将数据按照格式写入字符串
289         char filePath[300]="D:\\计算机编程\\C_Dev-C++\\大二数据结构作业\\课程设计\\";
290         strcat(filePath,cschoolNum);
291         strcat(filePath,".txt");
292 
293         ifstream in(filePath);
294         if(!in)
295         {
296             cout<<"\t\t\t\t\t!文件读取失败!"<<endl;
297             exit(0);
298         }
299 
300         // 读邻接矩阵
301         for(int i=1;i<=MAX;i++)
302         {
303             for(int j=1;j<=MAX;j++)
304             {
305                 in>>graph[i][j];
306             }
307         }
308         // 读边数和点数
309         in>>siteSum>>pathSum;
310         // 读地点信息
311         for(int i=1;i<=MAX;i++)
312         {
313             in>>site[i].siteNum;
314             in>>site[i].siteName;
315             in>>site[i].sitePresentation;
316         }
317         in.close();
318         cout<<"\t\t\t\t\t[已成功读取数据!]"<<endl;
319         cout<<"\t\t\t\t\t";
320         system("pause");
321     }
322 
323     // 查询地点信息
324     void researchSite()
325     {
326         string siteName;
327         int siteNum;
328         cout<<"\t\t\t\t\t请输入你要查询的地点名称:";
329         cin>>siteName;
330         siteNum=getsiteNum(siteName);
331         cout<<"\t\t\t\t\t地点编号:"<<site[siteNum].siteNum<<endl;
332         cout<<"\t\t\t\t\t地点名称:"<<site[siteNum].siteName<<endl;
333         cout<<"\t\t\t\t\t地点介绍:"<<site[siteNum].sitePresentation<<endl;
334         cout<<"\t\t\t\t\t";
335         system("pause");
336     }
337 
338     // Floyd算法求最短路径
339     void floyd()
340     {
341 
342         // 初始化存最短路径长与最短路径的两个数组
343         for(int i=1;i<=MAX;i++)
344         {
345             for(int j=1;j<=MAX;j++)
346             {
347                 L[i][j]=graph[i][j];
348                 // 如果两个点有路径,就保存
349                 if(i!=j&&graph[i][j]<INTMAX)
350                 {
351                     D[i][j]=i;
352                 }
353                 else
354                 {
355                     D[i][j]=-1;
356                 }
357             }
358         }
359         // 找最短路径,并记录最短路径长
360         for(int k=1;k<=MAX;k++)
361         {
362             for(int i=1;i<=MAX;i++)
363             {
364                 for(int j=1;j<=MAX;j++)
365                 {
366                         if((L[i][k]+L[k][j])<L[i][j])
367                         {
368                             L[i][j]=L[i][k]+L[k][j];
369                             D[i][j]=D[k][j];
370                         }
371                 }
372             }
373         }
374     }
375 
376     // 获取最短路径
377     void getMinlength()
378     {
379         while(1)
380         {
381             string siteName1,siteName2;
382             int siteNum1,siteNum2;
383             cout<<"\t\t\t\t\t请输入要查找的起点名称:";
384             cin>>siteName1;
385             siteNum1=getsiteNum(siteName1);
386             cout<<"\t\t\t\t\t请输入要查找的终点名称:";
387             cin>>siteName2;
388             siteNum2=getsiteNum(siteName2);
389             if(siteNum1==siteNum2||siteNum1>MAX||siteNum1<1||siteNum2>MAX||siteNum2<1)
390             {
391                 cout<<"\t\t\t\t\t[输入路径有误,请重新输入]"<<endl;
392                 continue;
393             }
394             else
395             {
396                 cout<<"\t\t\t\t\t起点:"<<site[siteNum1].siteName<<endl;
397                 cout<<"\t\t\t\t\t终点:"<<site[siteNum2].siteName<<endl;
398                 cout<<"\t\t\t\t\t最短路径:";
399                 int dNum=D[siteNum1][siteNum2];
400                 if(dNum==-1)
401                 {
402                     cout<<""<<endl;
403                 }
404                 else
405                 {
406                     int minSite[MAX+1]; // 记录中间点
407                     int ans=0; // 计数器
408                     minSite[ans]=siteNum2;
409                     ans++;
410                     while(dNum!=siteNum1)
411                     {
412                         minSite[ans]=dNum;
413                         ans++;
414                         dNum=D[siteNum1][dNum];
415                     }
416                     minSite[ans]=siteNum1;
417                     for(int i=ans;i>0;i--)
418                     {
419                         cout<<site[minSite[i]].siteName<<"--->";
420                     }
421                     cout<<site[minSite[0]].siteName<<", 路程为"<<L[siteNum1][siteNum2]<<"m"<<endl;
422                     cout<<"\n\t\t\t\t\t";
423                     system("pause");
424                     break;
425                 }
426             }
427         }
428     }
429 };
430 
431 void display_MainActivity(); // 显示主界面
432 
433 int main()
434 {
435     SGraph sg;
436     while(1)
437     {
438         display_MainActivity();
439         int choice;
440         cin>>choice;
441         if(choice==1)
442         {
443             sg.createSGraph();
444         }
445         else if(choice==2)
446         {
447             sg.floyd();
448             sg.getMinlength();
449         }
450         else if(choice==3)
451         {
452             sg.readData();
453         }
454         else if(choice==4)
455         {
456             sg.saveData();
457         }
458         else if(choice==5)
459         {
460             sg.researchSite();
461         }
462         else if(choice==6)
463         {
464             cout<<"\t\t\t\t\t[感谢您的使用]"<<endl;
465             exit(0);
466         }
467         else
468         {
469             cout<<"\t\t\t\t\t[指令有误,请重新输入]"<<endl;
470             cout<<"\t\t\t\t\t";
471             system("pause");
472         }
473 
474     }
475 
476 }
477 
478 void display_MainActivity()
479 {
480     system("cls"); // 清屏
481     int i; // 下标
482     cout<<"\t\t\t\t\t#  #  #  #  #  #  #  #  #  #  #  #  #\n";
483     cout<<"\t\t\t\t\t#                                   #\n";
484     cout<<"\t\t\t\t\t#            校园导航系统           #\n";
485     for(i=4; i<=5; i++)
486     {
487         cout<<"\t\t\t\t\t#                                   #\n";
488     }
489     cout<<"\t\t\t\t\t#            1.校园平面添加         #\n";
490     cout<<"\t\t\t\t\t#                                   #\n";
491     cout<<"\t\t\t\t\t#            2.最短路径查询         #\n";
492     cout<<"\t\t\t\t\t#                                   #\n";
493     cout<<"\t\t\t\t\t#            3.读取数据             #\n";
494     cout<<"\t\t\t\t\t#                                   #\n";
495     cout<<"\t\t\t\t\t#            4.保存数据             #\n";
496     cout<<"\t\t\t\t\t#                                   #\n";
497     cout<<"\t\t\t\t\t#            5.查询地点信息         #\n";
498     cout<<"\t\t\t\t\t#                                   #\n";
499     cout<<"\t\t\t\t\t#            6.退出系统             #\n";
500     cout<<"\t\t\t\t\t#                                   #\n";
501     cout<<"\t\t\t\t\t#  #  #  #  #  #  #  #  #  #  #  #  #\n";
502     cout<<"\t\t\t\t\t               请输入您的指令:";
503 }

 

分类:

技术点:

相关文章: