全国交通咨询模拟系统源代码

合集下载

2数据结构——全国交通咨询模拟系统实验报告

2数据结构——全国交通咨询模拟系统实验报告

2数据结构——全国交通咨询模拟系统实验报告全国交通咨询模拟一、设计目的掌握线性表、栈、图结构和对文件的操作,学习屏幕编辑和菜单技术,掌握用最短路径及其搜索算法编制较综合性的程序,能用图的邻接存储结构求解最优路线问题,解决有关实际问题。

得到软件设计技能的训练。

二、问题描述交通咨询模拟。

根据旅客的不同需要,要考虑到旅客希望在旅途中的时间尽可能短、希望旅费尽可能省等的要求。

三、基本要求1、对城市信息(城市名、城市间的里程)进行编辑:具备添加、修改、删除功能;2、对城市间的交通工具:火车。

对列车时刻表进行编辑:里程、和列车班次的添加、修改、删除;3、提供两种最优决策:最快到达或最省钱到达。

全程只考虑一种交通工具,可以不考虑回程;4、咨询以用户和计算机对话方式进行,要注意人机交互的屏幕界面。

由用户选择最优决策原则和交通工具,输入起始站、终点站、出发时间,输出信息:最快需要多长时间才能到达及旅费,或者最少需要多少旅费才能到达及时间,并详细说明依次于何时何地乘坐哪一趟列车何时到达何地。

四、具体实现1、思路(1) 数据存储。

城市信息(城市名、代码)、交通信息(城市间的里程、各航班和列车时刻)存储于磁盘文件。

在实验中本想用文本储存数据,但操作不熟悉,而是改用图的邻接矩阵储存原始信息,而后用数组进行添加删改(2) 数据的逻辑结构。

根据设计任务的描述,其城市之间的旅游交通问题是典型的图结构,可看作为无向图,图的顶点是城市,边是城市之间所耗费的时间(要包括中转站的时间)或旅费。

(3) 数据的存储结构。

采用邻接表和邻接矩阵都可作为数据的存储结构,这里建议采用邻接矩阵作为数据的存储结构。

(4) 用不同的功能模块对城市信息和交通信息进行编辑。

添加、修改、删除功能可用菜单方式或命令提示方式。

只要能方便的对城市信息和交通信息进行管理即可,但要注意人机界面,具体实现由学生自行设计,也可参考有关程序(届时在网上提供)。

这些工作有不小的工作量。

交通咨询程设计含源代码可以运行.doc

交通咨询程设计含源代码可以运行.doc

目录第一章需求分析 (1)1.1题目分析 (2)1.2实现方法 (2)1.2.1图的实现和声明 (2)1.2.2程序功能的实现 (3)第二章系统功能模块设计 (4)第三章流程图 (5)第四章运行调试 (6)第五章实验总结 (9)第六章致谢 (9)附录一.............................................. 错误!未定义书签。

附录二.............................................. 错误!未定义书签。

I交通咨询模拟第一章需求分析设计、实现一个全国大城市间的交通咨询程序,为旅客提供三种最优决策方案:一是建立交通网络图的存储结构,二实现两个城市间的最短路经问题。

程序所具有的功能特色本程序主要目的是为了给用户提供路径咨询。

实现了帮助用户了解全国各大城市间往来的最短路径问题,第二,可以提供用户查询各大城市的相关信息。

本程序最大的特点是支持用户自己添加城市信息及城市,或添加城市的路径,既就有可扩展性。

该程序所做的工作的是模拟全国交通咨询,为旅客提供三种最优决策的交通咨询。

此程序规定:(1)在程序中输入城市名称时,需输入10个字母以内的字母串;输入列车或飞机编号时需输入一个整型数据;输入列车或飞机的费用时需输入一个实型数据;输入列车或飞机开始时间和到达时间时均需输入两个整型数据(以hh:mm的形式);在选择功能时,应输入与所选功能对应的一个整型数据。

(2)程序的输出信息主要是:最快需要多少时间才能到达,或最少需要多少旅费才能到达,或最少需要多少次中转到达,并详细说明依次于何时乘坐哪一趟列车或哪一次班机到何地。

(3)程序的功能包括:提供对城市信息的编辑,提供列车时刻表和飞机航班表的编辑,提供三种最优决策:最快到达、最省钱到达、最少中转次数到达。

1.1题目分析1.通过对题目的分析知,是要让我们能够通过利用所学的数据结构的基本知识和技能来解决程序设计问,因此在搞程序设计之前先好好的把书复习一遍,弄清楚各个知识之间的联系。

课程设计全国交通咨询模拟系统

课程设计全国交通咨询模拟系统

19级数据结构课程设计题目及要求一、课程设计目的《数据结构》是计算机专业一门重要的专业技术基础课程,是一门关键性核心课程。

《数据结构》课程的目标是使学生学会分析研究计算机加工对象的特性,选择合适的数据结构和存储表示,以及编制相应的实现算法,培养和提高学生程序设计的能力。

《数据结构》课程也是一门实践性较强的课程,设置实践环节是十分重要的。

本课程设计的目标就是要达到理论与实际应用相结合,提高学生组织数据及编写大型程序的能力,并培养基本的、良好的程序设计技能以及合作能力。

题目八:全国交通咨询模拟系统【问题描述】出于不同目的的旅客对交通工具有不同的要求。

例如,因公出差的旅客希望在旅途中的时间尽可能短,出门旅游的游客则期望旅费尽可能省,而老年旅客则要求中转次数最少。

编制一个全国城市间的交通咨询程序,为旅客提供两种或三种最优决策的交通咨询。

【基本要求】(1)提供对城市信息进行编辑(如:添加或删除)的功能。

(2)城市之间有两种交通工具:火车和飞机。

提供对列车时刻表和飞机航班进行编辑(增设或删除)的功能。

(3)提供两种最优决策:最快到达或最省钱到达。

全程只考虑一种交通工具。

(4)旅途中耗费的总时间应该包括中转站的等候时间。

(5)咨询以用户和计算机的对话方式进行。

由用户输入起始站、终点站、最优决策原则和交通工具,输出信息:最快需要多长时间才能到达或者最少需要多少旅费才能到达,并详细说明依次于何时乘坐哪一趟列车或哪一次班机到何地。

四、设计工作要求1、课程设计完成基本的功能要求本次课程设计可以从以下的题目中任选其一,每个题目基本实现的要求是:(1)有菜单功能(2)有读写数据存盘功能(3)有数据图形显示或动画显示。

2、课程设计完成成品要求(1)程序设计书(Word格式)●包括程序设计目标●问题描述●需求分析●概要设计●详细设计●源程序清单(要求格式整齐400行以上,要有注释说明)●软件说明书(给出软件如何使用,使用时的注意事项)●测试报告(每个函数的功能测试,输入条件,输出结果)●课程设计总结。

全国交通咨询模拟系统源代码

全国交通咨询模拟系统源代码

12050741班(郭晓湛,李佳豪,廖川,杨鹤)课题:全国交通系统咨询模拟源程序代码:#include <windows.h>#include <stdio.h>#include <string.h>#include <vector>#include<iostream>#include <malloc.h>#define INF 65535 //定义一个最大数定为无穷值#define MAX 23using namespace std;static int c_number=10;static int k=0;static int v=0,z=0,r=0,t=0;typedef struct search{int c_transer;int c_cost;int c_time;int f_cost;int f_time;}search;search m[20],x[20],n[20];typedef int costAdj[MAX+1][MAX+1];//图邻接矩阵从1开始记数int Path[MAX+1][MAX+1];//图邻接矩阵从1开始记数typedef struct unDiGraph{int numVerts; //结点costAdj cost; //邻接矩阵}unDiGraph,*UNG; //图的定义typedef struct c_edit{char a[10];}c_edit;c_edit add[10];costAdj B,L;//定位函数,输出城市列表,增添城市。

int pr(int i,int j){int h=0;if (j==0){h=i;}else if (j==1){cin>>add[i].a;}switch(h)//运用switch语句。

{case(0):cout<<"";break;case(1) : cout<<"成都 ";break; case(2) : cout<<"西安 ";break; case(3) : cout<<"郑州 ";break; case(4) : cout<<"武汉 ";break; case(5) : cout<<"株洲 ";break;case(6) : cout<<"贵阳 ";break;case(7) : cout<<"柳州 ";break;case(8) : cout<<"广州 ";break;case(9) : cout<<"南宁 ";break;case(10) : cout<<"徐州 ";break;default:cout<<add[i-10].a;}return 1;}//输出城市列表及相应代码void pri(){int i;cout<<" 城市及其代码"<<endl<<endl<<endl;cout<<"****************************"<<endl; for (i=1;i<=c_number;i++){cout<<i<<".";pr(i,0);}cout<<endl<<"***********************"<<endl<<endl<<endl< <endl<<endl<<endl;}//构造带权(费用)图返回首地址G:unDiGraph *CreateCostG(int o)//火车的花费的存贮和编辑功能{unDiGraph *G;int i,j;int a=0,b=0,f,h=1;if(!(G=(unDiGraph *)malloc(sizeof(unDiGraph)))) //为G 分配存储空间。

交通咨询系统c语言

交通咨询系统c语言

2020年6月交通咨询系统目录一、需求分析..............................................错误!未定义书签。

1、程序的功能及设计要求................................错误!未定义书签。

2、输入输出的要求......................................错误!未定义书签。

二、环境说明..................................................错误!未定义书签。

三、详细设计..................................................错误!未定义书签。

1、模块设计...............................................错误!未定义书签。

2、画出各函数的调用关系图、主要函数的流程图。

.............错误!未定义书签。

2、详细代码...............................................错误!未定义书签。

四、调试分析..................................................错误!未定义书签。

1、测试数据:.............................................错误!未定义书签。

2、借鉴的资料.............................................错误!未定义书签。

五、课程总结..................................................错误!未定义书签。

六、附录......................................................错误!未定义书签。

一、需求分析1、程序的功能及设计要求在交通网络非常发达、交通工具和交通方式不断更新的今天,人们在出差、旅游或做其他出行时,不仅关心节省交通费用,而且对里程和所需时间等问题也感兴趣。

全国交通系统查询源程序

全国交通系统查询源程序
if(!final[w])
if(D[w]<min)
{
v=w;
min=D[w];
}
final[v]=1; //更新当前最短路径及距离。
void gouzaotrain(mgraph &g) ; //构造飞机图
void addplane(mgraph &g); //增加飞机信息
int plane_money(mgraph G, int p[][max_vex],int v0,int m,int D[max_vex]) ;//求飞机费用,迪杰斯特拉算法,邻接表存储结构(费用作为权值)。
p[w][j]=w;
}
}
return D[m];
}
void inputDGTrain_TXT(mgraph g) //写入文本文档中
{
int v=0,w;
int i, min,j;
int final[100];
for ( v=0; v<G.vexnum; v++)//初始化p[][]、final[]和D[]
{
final[v]=0;
D[v]=G.arcs[v0][v].adj;
//更新当前最短路径及距离。
for(w=0;w<G.vexnum;++w)
if(!final[w]&&(min+G.arcs[v][w].adj<D[w])&&G.arcs[v][w].adj!=INFINITY+1)
{
D[w]=min+G.arcs[v][w].adj;
{
int i,j;

全国交通咨询系统C++代码

全国交通咨询系统C++代码

//全国交通咨询系统-代码部分//作者:JUNE 2011-6-11#include <iostream>#include <stdio.h>#include <windows.h>#include <string.h>#define Dij_MAXN 33#define MAX_VERTEX_NUM 31#define MAX_STRING_NUM 10#define MAX_TRAFFIC_NUM 10using namespace std;typedef short int CityType; //定义城市节点typedef struct TrafficNode /////////////////////////////////////////////////{char name[MAX_STRING_NUM]; //班次//int StartTime,StopTime; //起止时间int Time;int EndCity; //火车到达城市的编号int Cost; //票价} TrafficNodeDat;typedef struct VNode //////////////////////////////////////////////////////{CityType city; //城市编号int TrainNum,FlightNum; //标记下面Train数组和Flight数组里元素个数TrafficNodeDat Train[MAX_TRAFFIC_NUM]; //数组成员为结构体,记录了到达城市、起止时间、票价和班次TrafficNodeDat Flight[MAX_TRAFFIC_NUM];} VNodeDat;typedef struct PNode ///////////////////////////////////////////////{int City;int TraNo;} PNodeDat;char CityName[MAX_VERTEX_NUM][MAX_STRING_NUM]; //城市名,采用第一下标为该城市在本程序中的编号int CityNum; //城市数目VNodeDat AdjList[MAX_VERTEX_NUM]; //下标所在城市的线路int MinTime,StartTime;int curPath;int SeekCity(char *name)////////////////////////////////////////////{//查找城市int i;for(i=0;i<CityNum;i++)if(strcmp(name,CityName[i])==0)return i;return -1;}int SaveFile()///////////////////////////////////////////////////////////////{//将火车飞机交通信息写入文件FILE *fp;int i,j,total;if((fp=fopen("city.txt","wb"))==NULL){cout<<"\n\t无法打开文件!\n"; return -1;}fprintf(fp,"%d\n",CityNum);//在city文件中输入城市总数for(i=0;i<CityNum;i++)fprintf(fp,"%s\n",CityName[i]);//再输入城市名fclose(fp);total=0;if((fp=fopen("train.txt","wb"))==NULL){cout<<"\n\t无法打开文件!\n"; return -1;}for(i=0;i<CityNum;i++)total+=AdjList[i].TrainNum;fprintf(fp,"%d\n",total);//在train文件中输入火车总数for(i=0;i<CityNum;i++)for(j=0;j<AdjList[i].TrainNum;j++){fprintf(fp,"%s %s %s ",AdjList[i].Train[j].name,CityName[i],CityName[AdjList[i].Train[j].EndCity]);//输入火车车次,始发站和终点站fprintf(fp,"%d %d\n",AdjList[i].Train[j].Time,AdjList[i].Train[j].Cost);//输入发车时间和到站时间和费用}fclose(fp);total=0;if((fp=fopen("flight.txt","wb"))==NULL){cout<<"\n\t无法打开文件!\n";return -1;}for(i=0;i<CityNum;i++)total+=AdjList[i].FlightNum;fprintf(fp,"%d\n",total);//在flight文件中输入飞机总数for(i=0;i<CityNum;i++)for(j=0;j<AdjList[i].FlightNum;j++){fprintf(fp,"%s %s %s ",AdjList[i].Flight[j].name,CityName[i],CityName[AdjList[i].Flight[j].EndCity]);//输入飞机航班号,始发站和终点站fprintf(fp,"%d %d\n",AdjList[i].Flight[j].Time,AdjList[i].Flight[j].Cost);//输入起飞时间,到达时间和费用}fclose(fp);return 1;}int InsertCity(char *Name)///////////////////////////////////////////////////////{//添加城市strcpy(CityName[CityNum],Name);AdjList[CityNum].city=CityNum;//设置城市编号AdjList[CityNum].FlightNum=0;AdjList[CityNum].TrainNum=0;//新增城市火车数和飞机数初始为零CityNum++;//城市总数加1return 1;}int DelCity(char *Name)/////////////////////////////////////////////////////////{//删除城市int city,i,j;city=SeekCity(Name);for(i=city;i<CityNum-1;i++)strcpy(CityName[i],CityName[i+1]);AdjList[i].FlightNum=AdjList[i+1].FlightNum;AdjList[i].TrainNum=AdjList[i+1].TrainNum;for(j=0;j<AdjList[i].FlightNum;j++){AdjList[i].Flight[j].Cost=AdjList[i+1].Flight[j].Cost;AdjList[i].Flight[j].EndCity=AdjList[i+1].Flight[j].EndCity;strcpy(AdjList[i].Flight[j].name,AdjList[i+1].Flight[j].name);AdjList[i].Flight[j].Time=AdjList[i+1].Flight[j].Time;}}CityNum--;return 1;}int InsertTrain(char *train,char *StartCity,char *EndCity,int Time,int cost)//////////////////////////////////////////////{//添加火车路线int i,j;i=SeekCity(StartCity);j=SeekCity(EndCity);AdjList[i].Train[AdjList[i].TrainNum].Cost=cost;AdjList[i].Train[AdjList[i].TrainNum].EndCity= j;AdjList[i].Train[AdjList[i].TrainNum].Time=Time;strcpy(AdjList[i].Train[AdjList[i].TrainNum].name,train);AdjList[i].TrainNum++;return 1;}int InsertFlight(char *flight,char *StartCity,char *EndCity,int Time,int cost){//添加飞机路线int i,j;i=SeekCity(StartCity);j=SeekCity(EndCity);AdjList[i].Flight[AdjList[i].FlightNum].Cost=cost;AdjList[i].Flight[AdjList[i].FlightNum].EndCit y=j;AdjList[i].Flight[AdjList[i].FlightNum].Time=Time;strcpy(AdjList[i].Flight[AdjList[i].FlightNum].name,flight);AdjList[i].FlightNum++;return 1;}int DelPath(char *name)/////////////////////////////////////////////////////////////////////////{//删除路线int i,j,flag=0;for(i=0;i<CityNum;i++){for(j=0;j<AdjList[i].FlightNum;j++)if(strcmp(AdjList[i].Flight[j].name,name)==0){flag=1;break;}//找到要删除的路线,找到跳出for循环if(flag){for(;j<AdjList[i].FlightNum-1;j++){AdjList[i].Flight[j].Cost=AdjList[i].Flight[j+1].Cost;AdjList[i].Flight[j].EndCity=AdjList[i].Flight[j+1].EndCity;strcpy(AdjList[i].Flight[j].name,AdjList[i].Flight[j+1].name);AdjList[i].Flight[j].Time=AdjList[i].Flight[j+1].Time;}AdjList[i].FlightNum--;//将要删除路线后面的路线全部向前移,并将总数减1 break;}for(j=0;j<AdjList[i].TrainNum;j++)if(strcmp(AdjList[i].Train[j].name,name)==0){flag=1;break;}if(flag){for(;j<AdjList[i].TrainNum-1;j++){AdjList[i].Train[j].Cost=AdjList[i].Train[j+1].Cost;AdjList[i].Train[j].EndCity=AdjList[i].Train[j+1].EndCity;strcpy(AdjList[i].Train[j].name,AdjList[i].Train[j+1].name);AdjList[i].Train[j].Time=AdjList[i].Train[j+1].Time;}AdjList[i].TrainNum--;break;}}return 1;}void Dijkstra_Output(int s[30][30],int PreCity[30],int p_end,int TravelType)/////////////////////////// {//输出最短路径int track[30];int i=0,j,k,min,tmp,end,cost=0;j=p_end;track[i++]=j;//下标为0中存储了终点站的城市编号,i变为1while(PreCity[j]>=0){cost+=s[PreCity[j]][j];//循环后cost为总费用j=PreCity[j];track[i++]=j;}cout<<"\n\t您要查找的路线为:";cout<<"\n\t*─────────────────────────────*\n"; cout<<"\t车号起始站终点站时间花费\n";if(!TravelType)//类型为火车{for(i--;i>0;i--)//初始时i--后i为路线中的城市数目{end=track[i-1];min=32767;for(k=0;k<AdjList[track[i]].TrainNum;k++)if(AdjList[track[i]].Train[k].EndCity==end&&min>AdjList[track[i]].Train[k].Cost){min=AdjList[track[i]].Train[k].Cost;//min为路径中的最小花费tmp=k;//k为一条路径中花费最小的火车编号}printf("\n\t%s",AdjList[track[i]].Train[tmp].name);//输出火车班次printf("\t%10s",CityName[track[i]]);//输出起始城市名printf("\t%10s",CityName[track[i-1]]);//输出终点城市名printf("\t%d",AdjList[track[i]].Train[tmp].Time);//输出时间printf("\t%d\n",AdjList[track[i]].Train[tmp].Cost);//输出花费}}else//类型为飞机{for(i--;i>0;i--){end=track[i-1];min=32767;for(k=0;k<AdjList[track[i]].FlightNum;k++)if(AdjList[track[i]].Flight[k].EndCity==end&&min>AdjList[track[i]].Flight[k].Cost){min=AdjList[track[i]].Flight[k].Cost;tmp=k;}printf("\n\t%s",AdjList[track[i]].Flight[tmp].name);printf("\t%10s",CityName[track[i]]);printf("\t%10s",CityName[track[i-1]]);printf("\t%d",AdjList[track[i]].Flight[tmp].Time);printf("\t%d\n",AdjList[track[i]].Flight[tmp].Cost);}}cout<<"\n\t最终得出最少为: ";cout<<cost<<"\n";cout<<"\t*─────────────────────────────*\n"; }void Dijkstra(int s[30][30],int p_start,int p_end,int TravelType){//迪杰斯特拉算法计算最短路径int PreCity[30];//数组的值为计算出的路线中下标表示的城市中的前一个城市编号int i,j,min,pre,pos;for(i=0;i<CityNum;i++) PreCity[i]=-1;PreCity[p_start]=-2;while(PreCity[p_end]==-1){min=-1;for(i=0;i<CityNum;i++)if(PreCity[i]!=-1)//i为起始站{for(j=0;j<CityNum;j++)if(PreCity[j]==-1&&s[i][j]>0&&(min<0||s[i][j]<min)){pre=i;pos=j;//j为起始站中花费最小的到达站的城市编号min=s[i][j];}}PreCity[pos]=pre;}Dijkstra_Output(s,PreCity,p_end,TravelType);}int CalcMinCost(int StartCity,int EndCity,int TravelType){//查询最小耗费路线int s[30][30];int i,j,min,end,flag1,flag2;flag1=0;flag2=0;for(i=0;i<CityNum;i++)for(j=0;j<CityNum;j++)s[i][j]=-1;//设空路径if(TravelType==0)//判断初始城市和终点城市是否在火车交通路线中for(i=0;i<CityNum;i++)for(j=0;j<AdjList[i].TrainNum;j++){if(AdjList[i].Train[j].EndCity==StartCity) flag1=1;if(AdjList[i].Train[j].EndCity==EndCity) flag2=1;}else if(TravelType==1)//判断初始和终点城市是否在飞机交通路线中for(i=0;i<CityNum;i++)for(j=0;j<AdjList[i].FlightNum;j++){if(AdjList[i].Flight[j].EndCity==StartCity) flag1=1;if(AdjList[i].Flight[j].EndCity==EndCity) flag2=1;}if(flag1!=1&&flag2!=1){printf("\n\t抱歉!没有您要查找的路线!");return 0;}if(TravelType==0){for(i=0;i<CityNum;i++){min=32767;j=0;while(j<AdjList[i].TrainNum){min=32767;end=AdjList[i].Train[j].EndCity;while(end==AdjList[i].Train[j].EndCity&&j<AdjList[i].TrainNum) {if(AdjList[i].Train[j].Cost<min) min=AdjList[i].Train[j].Cost;j++;}//min为i城市中的最小花费,end为其路线终点站s[i][end]=min;}}}else{for(i=0;i<CityNum;i++){min=32767;j=0;while(j<AdjList[i].FlightNum){min=32767;end=AdjList[i].Flight[j].EndCity;while(end==AdjList[i].Flight[j].EndCity&&j<AdjList[i].FlightNum) {if(AdjList[i].Flight[j].Cost<min) min=AdjList[i].Flight[j].Cost;j++;}s[i][end]=min;}}}Dijkstra(s,StartCity,EndCity,TravelType);return 1;}int CalcMinRange(int StartCity,int EndCity,int TravelType){//查询最短路程int s[30][30];int i,j,min,end,flag1,flag2;flag1=0;flag2=0;for(i=0;i<CityNum;i++)for(j=0;j<CityNum;j++)s[i][j]=-1;//设空路径if(TravelType==0)//判断初始城市和终点城市是否在火车交通路线中for(i=0;i<CityNum;i++)for(j=0;j<AdjList[i].TrainNum;j++){if(AdjList[i].Train[j].EndCity==StartCity) flag1=1;if(AdjList[i].Train[j].EndCity==EndCity) flag2=1;}else if(TravelType==1)//判断初始和终点城市是否在飞机交通路线中for(i=0;i<CityNum;i++)for(j=0;j<AdjList[i].FlightNum;j++){if(AdjList[i].Flight[j].EndCity==StartCity) flag1=1;if(AdjList[i].Flight[j].EndCity==EndCity) flag2=1;}if(flag1!=1&&flag2!=1){printf("\n\t抱歉!没有您要查找的路线!");return 0;}if(TravelType==0){for(i=0;i<CityNum;i++){min=32767;j=0;while(j<AdjList[i].TrainNum){min=32767;end=AdjList[i].Train[j].EndCity;while(end==AdjList[i].Train[j].EndCity&&j<AdjList[i].TrainNum) {if(AdjList[i].Train[j].Time<min) min=AdjList[i].Train[j].Time;j++;}//min为i城市中的最短路程,end为其路线终点站s[i][end]=min;}}}else{for(i=0;i<CityNum;i++){min=32767;j=0;while(j<AdjList[i].FlightNum){min=32767;end=AdjList[i].Flight[j].EndCity;while(end==AdjList[i].Flight[j].EndCity&&j<AdjList[i].FlightNum) {if(AdjList[i].Flight[j].Time<min) min=AdjList[i].Flight[j].Time;j++;}s[i][end]=min;}}}Dijkstra(s,StartCity,EndCity,TravelType);return 1;}int InitSystem(){/////////////////////////////////////////////////////////////////////////////从文件中读信息FILE *fp;int i,j,num,cost,time;char stmp1[10];char stmp2[10];char stmp3[10];fp=fopen("city.txt","r");if(!fp){cout<<"\n\t无法打开文件!\n";return -1;}fscanf(fp,"%d",&CityNum);//读入城市总数for(i=0;i<CityNum;i++){fscanf(fp,"%s",&CityName[i]);AdjList[i].city=i;AdjList[i].TrainNum=0;AdjList[i].FlightNum=0;}fclose(fp);fp=fopen("train.txt","r");if(!fp){cout<<"\n\t无法打开文件!\n";return -1;}fscanf(fp,"%d",&num);//读入火车总数for(i=0;i<num;i++){fscanf(fp,"%s",&stmp1);//车次fscanf(fp,"%s",&stmp2);//始发站fscanf(fp,"%s",&stmp3);//终点站j=SeekCity(stmp2);//查找始发站城市编号AdjList[j].Train[AdjList[j].TrainNum].EndCity=SeekCity(stmp3); strcpy(AdjList[j].Train[AdjList[j].TrainNum].name,stmp1);fscanf(fp,"%d",&time);AdjList[j].Train[AdjList[j].TrainNum].Time=time;fscanf(fp,"%d",&cost);AdjList[j].Train[AdjList[j].TrainNum].Cost=cost;AdjList[j].TrainNum++;fclose(fp);fp=fopen("flight.txt","r");if(!fp){cout<<"\n\t无法打开文件!\n";return -1;}fscanf(fp,"%d",&num);for(i=0;i<num;i++){fscanf(fp,"%s",&stmp1);//航班号fscanf(fp,"%s",&stmp2);//始发站fscanf(fp,"%s",&stmp3);//终点站j=SeekCity(stmp2);AdjList[j].Flight[AdjList[j].FlightNum].EndCity=SeekCity(stmp3);strcpy(AdjList[j].Flight[AdjList[j].FlightNum].name,stmp1);fscanf(fp,"%d",&time);AdjList[j].Flight[AdjList[j].FlightNum].Time=time;fscanf(fp,"%d",&cost);AdjList[j].Flight[AdjList[j].FlightNum].Cost=cost;AdjList[j].FlightNum++;}fclose(fp);return 1;}void Administrators(){////////////////////////////////////////////////////////////////////////////////////////管理员的界面char name[10];char s_city[10];char e_city[10];int Command,cost,flag;int traveltype;int time;char user[10],password[10];cout<<"\n\t请输入用户名:";cin>>user;cout<<"\t请输入密码:";cin>>password;if((strcmp("june",user)!=0)||(strcmp("123",password)!=0))//主界面选择验证,用户名为:june,密码为:123{cout<<"\n\t输入信息错误!\n";system("cls");//清屏return;}do{cout<<"\t\t\t\tloading";Sleep(600);cout<<"\ .";Sleep(400);cout<<" .";Sleep(200);cout<<" ."<<endl;Sleep(100);cout<<endl;cout<<"\t\t\t\t 成功!";Sleep(1000);system("cls");//清屏cout<<"\n\t******这里是管理员的界面,欢迎进入******\n"; cout<<"\t∷━━━━━━━━━━━━━━━━━━━∷\n"; cout<<"\t 1.添加城市\n";cout<<"\t 2.删除城市\n";cout<<"\t 3.添加交通路线\n";cout<<"\t 4.删除交通路线\n";cout<<"\t 0.返回上一级菜单\n";cout<<"\t∷**************************************∷\n"; cout<<"\n\t请选择:";scanf("%d",&Command);switch(Command){case 0:system("cls");//清屏return;case 1://添加城市InitSystem();cout<<"\n\t输入城市名:";scanf("%s",&name);InsertCity(name);SaveFile();cout<<"\t系统信息输入成功!\n";break;case 2://删除城市InitSystem();cout<<"\n\t输入城市名:";scanf("%s",&name);DelCity(name);SaveFile();cout<<"\t系统信息输入成功!\n";break;case 3://添加交通路线InitSystem();cout<<"\t请输入起始站城市名:";scanf("%s",&s_city);cout<<"\t请输入终点站城市名:";scanf("%s",&e_city);cout<<"\t请选择交通工具(火车0,飞机1):";scanf("%d",&traveltype);cout<<"\t请输入火车/飞机班次:";scanf("%s",&name);cout<<"\t请输入所耗费的时间:";scanf("%d",&time);printf("\t请输入票价:");scanf("%d",&cost);if(traveltype)InsertFlight(name,s_city,e_city,time,cost);elseInsertTrain(name,s_city,e_city,time,cost);SaveFile();cout<<"\t系统信息输入成功!\n";break;case 4://删除交通路线InitSystem();cout<<"\t请输入班次:";scanf("%s",&name);DelPath(name);SaveFile();cout<<"\t系统信息输入成功!\n";break;default:cout<<"\t选择序号错误!请重新选择!\n";flag=1;}}while(flag);}void Line(int city,int traveltype)//////////////////////////////////////////////////{//////////////////////////////////////////////////////////////////////////////////查找城市中的所有路线int i;printf("\n\t您要查找的路线为:");printf("\n\t*─────────────────────────────*\n"); printf("\t车号起始站终点站时间花费\n");if(!traveltype)//类型为火车{for(i=0;i<AdjList[city].TrainNum;i++){printf("\n\t%s",AdjList[city].Train[i].name);//输出火车班次printf("\t%10s",CityName[city]);//输出起始城市名printf("\t%10s",CityName[AdjList[city].Train[i].EndCity]);//输出终点城市名printf("\t%2d",AdjList[city].Train[i].Time);//输出起止时间printf("\t%d\n",AdjList[city].Train[i].Cost);//输出花费}}else{for(i=0;i<AdjList[city].FlightNum;i++){printf("\n\t%s",AdjList[city].Flight[i].name);//输出火车班次printf("\t%10s",CityName[city]);//输出起始城市名printf("\t%10s",CityName[AdjList[city].Flight[i].EndCity]);//输出终点城市名printf("\t%2d",AdjList[city].Flight[i].Time );//输出起止时间printf("\t%d\n",AdjList[city].Flight[i].Cost);//输出花费}}printf("\t*─────────────────────────────*\n"); }void User(){//////////////////////////////////////////////////////////////////////////////////////用户界面system("cls");//清屏char name[10];int startcity,endcity;int Command,flag,traveltype;do{cout<<"\n\t*******这里是用户的界面,欢迎进入*******\n";cout<<"\t*━━━━━━━━━━━━━━━━━━━*\n";cout<<"\t 1.查询最小耗费路线\n";cout<<"\t 2.查询最短时间路线\n";cout<<"\t 3.查询城市所有路线\n";cout<<"\t 0.返回上一级菜单\n";cout<<"\t****************************************\n"; cout<<"\n\t请选择:";//用户功能选择scanf("%d",&Command);switch(Command){case 0:Sleep(100);system("cls");//清屏return;case 1://最小耗费InitSystem();cout<<"\n\t请输入起始城市:";scanf("%s",&name);startcity=SeekCity(name);if(startcity<0){cout<<"\t输入城市名错误!\n";break;}cout<<"\t请输入终点城市:";scanf("%s",&name);endcity=SeekCity(name);if(endcity<0){cout<<"\t输入城市名错误!\n";break;}cout<<"\t请选择交通工具(火车0,飞机1):";scanf("%d",&traveltype);if(traveltype!=0&&traveltype!=1){cout<<"\t输入错误!\n";break;}CalcMinCost(startcity,endcity,traveltype);cout<<"\n";system("pause");system("cls");//清屏break;case 2://最短时间InitSystem();cout<<"\n\t请输入起始城市:";scanf("%s",&name);startcity=SeekCity(name);if(startcity<0){cout<<"\t输入城市名错误!\n";break;}cout<<"\t请输入终点城市:";scanf("%s",&name);endcity=SeekCity(name);if(endcity<0){cout<<"\t输入城市名错误!\n";break;}cout<<"\t请选择交通工具(火车0,飞机1):"; scanf("%d",&traveltype);if(traveltype!=0&&traveltype!=1){printf("\t输入错误!\n");break;}CalcMinRange(startcity,endcity,traveltype); cout<<"\n";system("pause");system("cls");//清屏break;case 3://查找城市所有路线InitSystem();cout<<"\n\t请输入要查找路线的城市:"; scanf("%s",&name);startcity=SeekCity(name);if(startcity<0){cout<<"\t输入城市名错误!\n";break;}cout<<"\t请选择交通工具(火车0,飞机1):"; scanf("%d",&traveltype);if(traveltype!=0&&traveltype!=1){cout<<"\t输入错误!\n";break;}Line(startcity,traveltype);cout<<"\n";system("pause");system("cls");//清屏break;default:cout<<"\t选择序号错误!请重新选择!\n";flag=1;}}while(flag);}int main(){///////////////////////////////////////////////////////////////////////////////////////////////主界面int Command;int x=0;system("Color 3f");while(1){cout<<"\t ※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n"; cout<<"\t ※※\n"; cout<<"\t ※※\n"; cout<<"\t ※欢迎进入全国交通咨询系统※\n"; cout<<"\t ※※\n"; cout<<"\t ※※\n"; cout<<"\t ※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n"; cout<<"\n\t\t\t< 请选择登录身份>\n";cout<<"\n\t\t\t\t1: 管理员\n\n\t\t\t\t2: 旅客\n\n\t\t\t\t0: 退出\n\t您的选择是:"; cin>>Command;switch(Command){case 0:return 0;case 1:Administrators();break;case 2:User();break;default:cout<<"\t选择序号错误!请重新选择!\n"; }}}///以下是本系统用到的数据:/*Train.tet11K1 kunming huaihua 12 150K4 kunming huaihua 14 140K5 kunming huaihua 15 100K7 kunming shanghai 30 240K1 changsha shanghai 10 120K2 changsha shanghai 9 140K3 changsha shanghai 11 100K1 huaihua changsha 6 70K2 huaihua changsha 7 65K4 huaihua changsha 5 60K6 huaihua changsha 8 55city.txt4kunmingchangshahuaihuashanghaiflight.txt11F1 kunming huaihua 12 150F4 kunming huaihua 14 140F5 kunming huaihua 15 100F7 kunming shanghai 30 240F1 changsha shanghai 10 120F2 changsha shanghai 9 140F3 changsha shanghai 11 100F1 huaihua changsha 6 70F2 huaihua changsha 7 65F4 huaihua changsha 5 60F6 huaihua changsha 8 55如果对本代码有疑问之处,请下载本人文档里的《数据结构课程设计-全国交通咨询系统》以及《全国交通咨询系统测试说明》进行进一步了解。

2数据结构——全国交通咨询模拟系统实验报告

2数据结构——全国交通咨询模拟系统实验报告

2数据结构——全国交通咨询模拟系统实验报告
实验目的:
实验环境:
本次实验的开发环境为Java编程语言,使用了面向对象的编程思想,采用了数据结构中的图和链表等数据结构。

实验内容:
1.设计交通工具类:
首先,根据系统需求,设计了交通工具类,该类用于表示交通工具的
基本信息,包括交通工具的名称、班次、票价等。

在该类中,使用了链表
数据结构来存储交通工具的班次信息。

2.设计城市类:
接下来,设计了城市类,用于表示各个城市的交通信息。

在该类中,
使用了图数据结构来表示城市之间的联通关系,每个城市作为图的一个节点,城市间的交通工具作为图的边。

实验结果:
经过测试,实验系统能够准确地输出起点城市到终点城市的最短路径
和最佳交通方案,用户可以根据输出信息来决定如何安排旅行计划。

实验总结:。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

12050741班(郭晓湛,李佳豪,廖川,杨鹤)课题:全国交通系统咨询模拟源程序代码:#include <windows.h>#include <stdio.h>#include <string.h>#include <vector>#include<iostream>#include <malloc.h>#define INF 65535 //定义一个最大数定为无穷值#define MAX 23using namespace std;static int c_number=10;static int k=0;static int v=0,z=0,r=0,t=0;typedef struct search{int c_transer;int c_cost;int c_time;int f_cost;int f_time;}search;search m[20],x[20],n[20];typedef int costAdj[MAX+1][MAX+1];//图邻接矩阵从1开始记数int Path[MAX+1][MAX+1];//图邻接矩阵从1开始记数typedef struct unDiGraph{int numVerts; //结点costAdj cost; //邻接矩阵}unDiGraph,*UNG; //图的定义typedef struct c_edit{char a[10];}c_edit;c_edit add[10];costAdj B,L;//定位函数,输出城市列表,增添城市。

int pr(int i,int j){int h=0;if (j==0){h=i;}else if (j==1){cin>>add[i].a;}switch(h)//运用switch语句。

{case(0):cout<<"";break;case(1) : cout<<"成都 ";break; case(2) : cout<<"西安 ";break; case(3) : cout<<"郑州 ";break; case(4) : cout<<"武汉 ";break; case(5) : cout<<"株洲 ";break;case(6) : cout<<"贵阳 ";break;case(7) : cout<<"柳州 ";break;case(8) : cout<<"广州 ";break;case(9) : cout<<"南宁 ";break;case(10) : cout<<"徐州 ";break;default:cout<<add[i-10].a;}return 1;}//输出城市列表及相应代码void pri(){int i;cout<<" 城市及其代码"<<endl<<endl<<endl;cout<<"****************************"<<endl; for (i=1;i<=c_number;i++){cout<<i<<".";pr(i,0);}cout<<endl<<"***********************"<<endl<<endl<<endl< <endl<<endl<<endl;}//构造带权(费用)图返回首地址G:unDiGraph *CreateCostG(int o)//火车的花费的存贮和编辑功能{unDiGraph *G;int i,j;int a=0,b=0,f,h=1;if(!(G=(unDiGraph *)malloc(sizeof(unDiGraph)))) //为G 分配存储空间。

{return(NULL);}for(i=1;i<c_number+1;i++){for(j=1;j<c_number+1;j++){G->cost[i][j]=INF; //初始化使G->cost[i][j]为无穷。

}}G->numVerts=c_number;G->cost[1][2]=G->cost[2][1]=112;G->cost[1][6]=G->cost[6][1]=190;G->cost[2][3]=G->cost[3][2]=136;G->cost[2][5]=G->cost[5][2]=325;G->cost[3][4]=G->cost[4][3]=142;G->cost[3][5]=G->cost[5][3]=230;G->cost[3][6]=G->cost[6][3]=213;G->cost[4][7]=G->cost[7][4]=247;G->cost[4][8]=G->cost[8][4]=250;G->cost[5][7]=G->cost[7][5]=161;G->cost[5][9]=G->cost[9][5]=208;G->cost[5][10]=G->cost[10][5]=261;G->cost[6][8]=G->cost[8][6]=317;G->cost[7][8]=G->cost[8][7]=208;G->cost[8][10]=G->cost[10][8]=367;G->cost[9][10]=G->cost[10][9]=444;if (o){while(h==1){v=v+1;pri();cout<<"火车花费编辑"<<endl;cout<<"请输入开始城市的代码"<<endl;cin>>a;cout<<"请输入结尾城市的代码"<<endl;cin>>b;cout<<"请输入你的两地花费"<<endl;cin>>m[v].c_cost;n[v].c_cost=a;x[v].c_cost=b;cout<<"请选择:"<<endl;cout<<"**************************************************** *****"<<endl;cout<<"1:继续更改城市费用"<<endl;cout<<"0:返回上一级菜单"<<endl;cout<<"**************************************************** *****"<<endl;cin>>h;switch(h){case 1:h=1;break;case 0:h=0;break;default:{cout<<"选择出错"<<endl;}}}}f=v+1;while (v--){G->cost[n[v].c_cost][x[v].c_cost]=m[v].c_cost; }v=f;return(G);}//构造带权(时间)图返回首地址G:unDiGraph *CreateTimeG(int o)//火车的时间的存贮和编辑功能{unDiGraph *G;int i,j;int a=0,b=0,f,h=1;if(!(G=(unDiGraph *)malloc(sizeof(unDiGraph)))) //为G 分配存储空间。

{return(NULL);}for(i=1;i<c_number+1;i++){for(j=1;j<c_number+1;j++){G->cost[i][j]=INF;//初始化使G->cost[i][j]为无穷。

}}G->numVerts=c_number;G->cost[1][2]=G->cost[2][1]=15;G->cost[1][6]=G->cost[6][1]=16;G->cost[2][3]=G->cost[3][2]=6;G->cost[2][5]=G->cost[5][2]=20;G->cost[3][4]=G->cost[4][3]=6;G->cost[3][5]=G->cost[5][3]=9;G->cost[3][6]=G->cost[6][3]=24;G->cost[4][7]=G->cost[7][4]=13;G->cost[4][8]=G->cost[8][4]=14;G->cost[5][7]=G->cost[7][5]=8;G->cost[5][9]=G->cost[9][5]=12;G->cost[5][10]=G->cost[10][5]=17;G->cost[6][8]=G->cost[8][6]=23;G->cost[7][8]=G->cost[8][7]=13;G->cost[8][10]=G->cost[10][8]=20;G->cost[9][10]=G->cost[10][9]=33;if (o){while(h==1){z=z+1;pri();cout<<"火车时间编辑"<<endl;cout<<"请输入开始城市的代码"<<endl; cin>>a;cout<<"请输入结尾城市的代码"<<endl; cin>>b;cout<<"请输入你的两地时间"<<endl; cin>>m[z].c_time;n[z].c_time=a;x[z].c_time=b;cout<<"请选择:"<<endl;cout<<"*********************************"<<endl; cout<<"1:继续更改城市时间"<<endl;cout<<"0:返回上一级菜单"<<endl;cout<<"********************************"<<endl; cin>>h;switch(h){case 1: system("cls");h=1;break;case 0: system("cls");h=0;break;default:{cout<<"选择出错"<<endl;}}}}f=z+1;while (z--){G->cost[n[z].c_time][x[z].c_time]=m[z].c_time;}z=f;return(G);}unDiGraph *CreatetranserG(int o)//火车中转次数的存贮和编辑功能{unDiGraph *G;int i,j;int a=0,b=0,f,h=1;if(!(G=(unDiGraph *)malloc(sizeof(unDiGraph)))) //为G 分配存储空间。

相关文档
最新文档