全國鐵路運(yùn)輸網(wǎng)最佳經(jīng)由問題_第1頁
全國鐵路運(yùn)輸網(wǎng)最佳經(jīng)由問題_第2頁
全國鐵路運(yùn)輸網(wǎng)最佳經(jīng)由問題_第3頁
全國鐵路運(yùn)輸網(wǎng)最佳經(jīng)由問題_第4頁
全國鐵路運(yùn)輸網(wǎng)最佳經(jīng)由問題_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上#include<fstream>#include<iostream>#include<string>#include<stdio.h>#include<iomanip>#define SIZE_view 50#define SIZE_line 100#define SIZE_way 300#define MAXNODE 30 /定義最多的頂點(diǎn)數(shù)#define MAXCOST 1000/自己寫的頭文件/#include<addview.h>/ / using namespace std;struc

2、t view_info /*城市信息結(jié)構(gòu)*/ int id; char name20; int code; char shortname20; char LName100;/ 經(jīng)過此車站的鐵路線名稱 viewsSIZE_view; struct line_info /鐵路線信息結(jié)構(gòu)int Lid;char LName20;int start_id; /始發(fā)站idint end_id; /終點(diǎn)站idint dist; /鐵路線長度int sign;/通行標(biāo)志linesSIZE_line;struct way_info /鐵路度的信息結(jié)構(gòu)int station1;int station2;int

3、dist;waysSIZE_way;struct path_info /用于最短路徑的查詢 int count; int pathSIZE_view; ; int view_count,line_count,way_count;/用來存儲文件中有多少條記錄 void readviews() int i; ifstream infile("views.txt",ios:in); /打開文件 infile >>view_count ; /把文件中的記錄賦值給view_count if(!infile) /打開文件失敗 cerr<<"open e

4、rror!"<<endl; exit(1); /infile>>view_count;/ 先讀入文件個數(shù) for(i=0;i<view_count;i+) infile>>viewsi.id>>>>viewsi.code>>viewsi.shortname>>viewsi.LName; /view_count=i;/給出原文件中車站的個數(shù) infile.close(); cout<<" "<<"id"<&

5、lt;" "<<"name"<<" "<<"code"<<" "<<"shortname"<<" "<<"LName"<<endl; for(i=0;i<view_count;i+) cout<<" "<<viewsi.id<<" "<<viewsi

6、.name<<" "<<viewsi.code <<" "<<viewsi.shortname<<" "<<viewsi.LName<<endl; void readways() /讀文件ways.txt int i; ifstream infile("ways.txt",ios:in); /打開文件 infile>>way_count; /把文件中的記錄賦值給way_count if(!infile) /打開文件失敗

7、cerr<<"open error!"<<endl; exit(1); for(i=0;i<way_count;i+) infile>>waysi.station1>>waysi.station2>>waysi.dist; infile.close(); /測試用,輸出路段的信息 cout<<" "<<"station1"<<" "<<"station2"<<"

8、 "<<"dist"<<endl; for(i=0;i<way_count;i+) cout<<" "<<waysi.station1<<" "<<waysi.station2<<" "<<waysi.dist<<endl ;void readlines() /讀文件lines.txt int i; ifstream infile("lines.txt",ios:in);

9、/打開文件 infile>>line_count; /把文件中的記錄賦值給line_count if(!infile) /打開文件失敗 cerr<<"open error!"<<endl; exit(1); for(i=0;i<line_count;i+) infile>>linesi.Lid>>linesi.LName>>linesi.start_id>>linesi.end_id>>linesi.dist>>linesi.sign; infile.close

10、(); /* cout<<" "<<"Lid"<<" "<<"LName"<<" "<<"start_id"<<" "<<"end_id"<<" "<<"dist"<<" "<<"sign"<<endl;

11、 for(i=0;i<view_count;i+) cout<<" "<<linesi.Lid<<" "<<linesi.LName<<" "<<linesi.start_id <<" "<<linesi.end_id<<" "<<linesi.dist<<" "<<linesi.sign<<endl; */ /這里

12、是輸出文件中的信息 void search () /查詢車站信息(所在的鐵路線) cout<<"please enter the station name:" char sta_name20; cin>>sta_name; /輸入要查詢的名字 cout<<endl; ifstream infile("views.txt ",ios:in); /讀文件 infile >>view_count ; /讀出文件記錄的個數(shù) if(!infile) /打開文件失敗 cerr<<"open err

13、or!"<<endl; exit(1); int i,mark; for(i=0;i<view_count;i+) infile>>viewsi.id>>>>viewsi.code>>viewsi.shortname>>viewsi.LName; infile.close(); for(i=0;i< view_count;i+) if(strcmp(sta_name,views )=0) /找到車站 cout<<"the station'

14、s informations is:n"<<endl; cout<<" "<<"id"<<" "<<"name"<<" "<<"code"<<" "<<"shortname"<<" "<<"LName"<<endl; cout<<&q

15、uot; "<<viewsi.id<<" "<< <<" "<< views i.code <<" "<< views i.shortname<<" "<< views i.LName <<endl; break; mark=i; if(mark=( view_count -1) /若沒有找到,輸出提示 cout<<"sorry,the sta

16、tion is not in here"<<endl;void addview() cout<<"please enter the new view's informations:"<<endl; /輸入新車站信息 cout<<"id:" cin>>viewsview_count.id; cout<<"name:" cin>>viewsview_; cout<<"code:" cin

17、>>viewsview_count.code; cout<<"shortname:" cin>>viewsview_count.shortname; cout<<"LName:" cin>>viewsview_count.LName; ofstream outfile("views.txt",ios:app); /打開views文件,并且寫入數(shù)據(jù) outfile<<view_count<<endl; if(!outfile) /文件打開失敗 cerr&

18、lt;<"open error!"<<endl; exit(1); outfile<<viewsview_count.id<<" "<<viewsview_<<" "<<viewsview_count.code <<" " <<viewsview_count.shortname<<" "<<viewsview_count.LName<<en

19、dl; /在文件末尾添加 view_count=view_count+1; outfile.close(); /關(guān)閉文件 cout<<"successfully! the new station is added"<<endl; cout<<"now station number is:"<<view_count<<endl; void addway() cout<<"please enter the new way's informations:"<

20、;<endl; /輸入新車站信息 cout<<"station1:" cin>>waysway_count.station1; / station1的id cout<<"station2:" cin>>waysway_count.station2; /station2的id cout<<"dist:" cin>>waysway_count.dist; /路段的長度 ofstream outfile("ways.txt",ios:app);

21、 /打開way.txt文件 outfile<<way_count<<endl; if(!outfile) /文件打開失敗 cerr<<"open error!"<<endl; exit(1); outfile<<waysway_count.station1<<" "<<waysway_count.station2<<" "<<waysway_count.dist; /在文件末尾添加 outfile.close(); /關(guān)閉文件

22、cout<<"successfully! the new station is added"<<endl; way_count=way_count+1; cout<<"now station number is:"<<view_count<<endl; void addline() cout<<"please enter the new line's informations:"<<endl; /輸入新鐵路線信息 cout<<&qu

23、ot;Lid:" cin>>linesline_count.Lid; cout<<"LName:" cin>>linesline_count.LName; cout<<"start_id:" cin>>linesline_count.start_id; cout<<"end_id:" cin>>linesline_count.end_id; cout<<"dist:" cin>>linesline_

24、count.dist; cout<<"sign:" cin>>linesline_count.sign; ofstream outfile("lines.txt",ios:app); /打開文件 outfile <<line_count << endl; if(!outfile) /文件打開失敗 cerr<<"open error!"<<endl; exit(1); outfile<<linesline_count.Lid<<"

25、"<<linesline_count.LName<<" "<<linesline_count.start_id <<" " <<linesline_count.end_id<<" "<<linesline_count.dist<<linesline_count.sign<<endl; /在文件末尾添加 outfile.close(); /關(guān)閉文件 cout<<"successfully! the

26、new line is added"<<endl; line_count=line_count+1; cout<<"now line number is:"<<line_count<<endl; /-void floyed() /弗洛伊德(Floyed)算法 int i, j, k, m, start_num, end_num; /i,j用來表示起始點(diǎn)和終點(diǎn)int dist_listSIZE_viewSIZE_view; /定義了一個數(shù)組 view_count=view_count+1;struct path_inf

27、o path_listSIZE_viewSIZE_view; /定義了一個path_info結(jié)構(gòu)的變量,包含著count和path用來存儲經(jīng)過的路徑for(i=1;i<=view_count;i+) /這里開始讀文件,先讓所有的線都為最大值for(j=1;j<=view_count;j+)dist_listij=MAXCOST;for(int t=0;t<=way_count;t+) i=wayst.station1;j=wayst.station2;dist_listij=wayst.dist;/把文件中的數(shù)據(jù)賦值給dist_listij=wayst.dist;形式for

28、(i =0; i< view_count; i+) for (j= 0; j<view_count; j+) if (i = j) dist_listij = 0; continue; dist_listij = -1; path_listij.count = 0; for (k = 0; k< way_count; k+) / if (waysk.station1 = i && waysk.station2 = j) /把起始點(diǎn)和終點(diǎn)分別給予i,j dist_listij = waysk.dist; path_listij.count = 2; path_l

29、istij.path0 = i; path_listij.path1 = j; break; for (k = 0; k<= view_count-1; k+) for (i = 0; i < view_count; i+) for (j = 0; j< view_count; j+) if (i = k | j = k | i = j) continue; if (dist_listik = -1 | dist_listkj = -1) continue; if ( (dist_listij = -1) | (dist_listij != -1) &&(di

30、st_listik + dist_listkj < dist_listij) dist_listij = dist_listik + dist_listkj;/shortestij=shortestik+shortestkj;path_listij.count = path_listik.count + path_listkj.count - 1; /pathij=k;for (m = 0; m < path_listik.count; m+) path_listij.pathm = path_listik.pathm; for (m = 0; m < path_listkj

31、.count; m+) path_listij.pathm+path_listik.count = path_listkj.pathm+1; cout<<" Floyed table:n"cout<<" All views in the school:n"for (i = 0; i < view_count-1; i+) cout<<" "<<i+1<<":"<<<<endl;cout<<&q

32、uot; Please input the start number: "cin>>start_num;cout<<" Please input the end number: "cin>>end_num;cout<<endl<<endl;cout<<"From"<<viewsstart_<<"to"<<viewsend_;if (dist_liststart_numend_nu

33、m = -1) cout<<"no way."<<endl;else cout<<"distance is "<<dist_liststart_numend_num<<", and path is :"/dist_list用來表示兩點(diǎn)間的長度k = path_liststart_numend_num.path0-1; /path_list用來保存路徑pathm在這里表明是通過的那個車站的cout<<;for (m = 1; m < pa

34、th_liststart_numend_num.count; m+) k = path_liststart_numend_num.pathm-1; /這里應(yīng)該也是int型,k是經(jīng)過的路徑的id cout<<"->"<<; cout<<endl<<endl; void main()readviews(); readlines() ;readways(); while(1)int menu;cout<<endl<<endl<<endl<<endl;cout<<" 全國鐵路運(yùn)輸網(wǎng)最佳經(jīng)由系統(tǒng) "<<endl;cout<<"*"<<endl;cout<<" 1:增加車站信息 "<<endl;cout<<" 2:增加鐵路線信息 "<<endl; cout<<" 3:查詢車站信息 "<<endl;cout<<

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論