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

2数据结构——全国交通咨询模拟系统实验报告全国交通咨询模拟一、设计目的掌握线性表、栈、图结构和对文件的操作,学习屏幕编辑和菜单技术,掌握用最短路径及其搜索算法编制较综合性的程序,能用图的邻接存储结构求解最优路线问题,解决有关实际问题。
得到软件设计技能的训练。
二、问题描述交通咨询模拟。
根据旅客的不同需要,要考虑到旅客希望在旅途中的时间尽可能短、希望旅费尽可能省等的要求。
三、基本要求1、对城市信息(城市名、城市间的里程)进行编辑:具备添加、修改、删除功能;2、对城市间的交通工具:火车。
对列车时刻表进行编辑:里程、和列车班次的添加、修改、删除;3、提供两种最优决策:最快到达或最省钱到达。
全程只考虑一种交通工具,可以不考虑回程;4、咨询以用户和计算机对话方式进行,要注意人机交互的屏幕界面。
由用户选择最优决策原则和交通工具,输入起始站、终点站、出发时间,输出信息:最快需要多长时间才能到达及旅费,或者最少需要多少旅费才能到达及时间,并详细说明依次于何时何地乘坐哪一趟列车何时到达何地。
四、具体实现1、思路(1) 数据存储。
城市信息(城市名、代码)、交通信息(城市间的里程、各航班和列车时刻)存储于磁盘文件。
在实验中本想用文本储存数据,但操作不熟悉,而是改用图的邻接矩阵储存原始信息,而后用数组进行添加删改(2) 数据的逻辑结构。
根据设计任务的描述,其城市之间的旅游交通问题是典型的图结构,可看作为无向图,图的顶点是城市,边是城市之间所耗费的时间(要包括中转站的时间)或旅费。
(3) 数据的存储结构。
采用邻接表和邻接矩阵都可作为数据的存储结构,这里建议采用邻接矩阵作为数据的存储结构。
(4) 用不同的功能模块对城市信息和交通信息进行编辑。
添加、修改、删除功能可用菜单方式或命令提示方式。
只要能方便的对城市信息和交通信息进行管理即可,但要注意人机界面,具体实现由学生自行设计,也可参考有关程序(届时在网上提供)。
这些工作有不小的工作量。
交通咨询程设计含源代码可以运行.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语言

2020年6月交通咨询系统目录一、需求分析..............................................错误!未定义书签。
1、程序的功能及设计要求................................错误!未定义书签。
2、输入输出的要求......................................错误!未定义书签。
二、环境说明..................................................错误!未定义书签。
三、详细设计..................................................错误!未定义书签。
1、模块设计...............................................错误!未定义书签。
2、画出各函数的调用关系图、主要函数的流程图。
.............错误!未定义书签。
2、详细代码...............................................错误!未定义书签。
四、调试分析..................................................错误!未定义书签。
1、测试数据:.............................................错误!未定义书签。
2、借鉴的资料.............................................错误!未定义书签。
五、课程总结..................................................错误!未定义书签。
六、附录......................................................错误!未定义书签。
一、需求分析1、程序的功能及设计要求在交通网络非常发达、交通工具和交通方式不断更新的今天,人们在出差、旅游或做其他出行时,不仅关心节省交通费用,而且对里程和所需时间等问题也感兴趣。
全国交通系统查询源程序

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++代码

//全国交通咨询系统-代码部分//作者: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数据结构——全国交通咨询模拟系统实验报告
实验目的:
实验环境:
本次实验的开发环境为Java编程语言,使用了面向对象的编程思想,采用了数据结构中的图和链表等数据结构。
实验内容:
1.设计交通工具类:
首先,根据系统需求,设计了交通工具类,该类用于表示交通工具的
基本信息,包括交通工具的名称、班次、票价等。
在该类中,使用了链表
数据结构来存储交通工具的班次信息。
2.设计城市类:
接下来,设计了城市类,用于表示各个城市的交通信息。
在该类中,
使用了图数据结构来表示城市之间的联通关系,每个城市作为图的一个节点,城市间的交通工具作为图的边。
实验结果:
经过测试,实验系统能够准确地输出起点城市到终点城市的最短路径
和最佳交通方案,用户可以根据输出信息来决定如何安排旅行计划。
实验总结:。
数据结构课程设计交通咨询系统实验参考代码

数据结构课程设计交通咨询系统实验参考代码#include<stdio.h>#include<stdlib.h>#define MVNum 100//最大顶点数#define Maxint 32767enum boolean{FALSE,TRUE};typedef char VertexType;typedef int Adjmatrix;typedef struct{VertexType vexs[MVNum];//顶点数组,类型假定为charAdjmatrix arcs[MVNum][MVNum];//邻接矩阵,类型假定为int 型}MGraph;int D1[MVNum],P1[MVNum];int D[MVNum][MVNum],P[MVNum][MVNum];/*建立有向图的储存结构*/void CreateMGraph(MGraph * G,int n,int e){//采用邻接矩阵表示法构造有向图G,n、e表示图的当前顶点数和边数int i,j,k,w;for(i=1;i<=n;i++)//输入顶点信息G->vexs[i]=(char)i;for(i=1;i<=n;i++)for(j=1;j<=n;j++)G->arcs[i][j]=Maxint;//初始化邻接矩阵printf("输入%d条边人i、j及w:\n",e);for(k=1;k<=e;k++)//读入e条边,建立邻接矩阵{scanf("%d,%d,%d",&i,&j,&w);G->arcs[i][j]=w;}printf("有向图人存储结构建立完毕!\n");}/*迪杰斯特拉算法*/void Dijkstra(MGraph *G,int v1,int n){//利用迪杰斯特拉算法,求出有向图G的v1顶点到其他顶点v 的最短路径P[v]及权D[v]int D2[MVNum],P2[MVNum];int v,i,w,min;enum boolean S[MVNum];for(v=1;v<=n;v++)//初始化S和D{S[v]=FALSE;//置空最短路径终点集D2[v]=G->arcs[v1][v];//置初始的最短路径值if(D2[v]<Maxint)P2[v]=v1;//v1是v的前趋(双亲)elseP2[v]=0;//v无前趋(双亲)}D2[v1]=0;S[v1]=TRUE;//S集初始时只有源点,距离为0for(i=2;i<n;i++)//其余n-1个顶点{min=Maxint;for(w=1;w<=n;w++)if(!S[w] && D2[w]<min){ v=w;min=D2[w]; }//w顶点离v1顶点更近S[v]=TRUE;for(w=1;w<=n;w++)//更新当前最短路径及距离if(!S[w]&&(D2[v]+G->arcs[v][w]<D2[w])) { D2[w]=D2[v]+G->arcs[v][w];P2[w]=v;}}printf("路径长度,路径\n");for(i=1;i<=n;i++){ printf("%5d",D2[i]);printf("%5d",i); v=P2[i];while(v!=0){printf("<-%d",v);v=P2[v];}printf("\n");}}/*费洛伊德算法*/void Floyd(MGraph *G,int n){//利用费洛伊德算法,求出最短路径int i,j,k;for(i=1;i<=n;i++)for(j=1;j<=n;j++){if(G->arcs[i][j]!=Maxint)P[i][j]=j;elseP[i][j]=0;D[i][j]=G->arcs[i][j];}for(k=1;k<=n;k++){for(i=1;i<=n;i++)for(j=1;j<=n;j++){ if(D[i][k]+D[k][j]<D[i][j]){D[i][j]=D[i][k]+D[k][j];P[i][j]=P[i][k];}}}}void main(){MGraph *G;int n,e,v,w,k;int xz=1;G=(MGraph *)malloc(sizeof(MGraph));printf("输入图中顶点个数和边数n,e:");scanf("%d,%d",&n,&e);CreateMGraph(G,n,e);//建立图的存储结构while(xz!=0){printf("***********求城市之间的最短距离***********\n");printf("================================= =========\n");printf("1.求一个城市到所有城市的最短距离\n");printf("2.求任意的两个城市之间的最短距离\n");printf("================================== ========\n");printf("请选择:1或2,选择0 退出:\n");scanf("%d",&xz);if(xz==2){Floyd(G,n);////调用费洛伊德算法printf("输入源点和终点:v,w:\n");scanf("%d,%d",&v,&w);k=P[v][w];if(k==0)printf("顶点 %d 到 %d 无路径!\n",v,w);else{printf("从顶点 %d 到 %d 最短路径是 %d",v,w,v); while(k!=w){printf("->%d",k);k=P[k][w];//k为v的后继顶点}printf("->%d",k);//输出后继顶点printf("路径长度:%d\n",D[v][w]);}}elseif(xz==1){printf("求单源路径,输入源点v;");scanf("%d",&v);Dijkstra(G,v,n);//调用迪杰斯特拉算法}}printf("结束求最短路径,再见!\n");}。
C++数据结构-全国交通咨询模拟报告

数据结构课程设计报告班级:195182学号:20181003991姓名:钟欢日期:2019.12一、课程设计题目与要求1.课程设计题目:全国交通咨询模拟2.问题描述:出于不同目的的旅客对交通工具有不同的要求。
例如,因公出差的旅客希望在旅途中的时间尽可能短,出门旅游的游客则期望旅费尽可能省,而老年旅客则要求中转次数最少。
编制一个全国城市间的交通咨询程序,为旅客提供两种或三种最优决策的交通咨询。
3.基本要求:3.1提供对城市信息进行编辑(如添加或删除)的功能。
3.2城市之间有两种交通工具:火车和飞机。
提供对列车时刻表和飞机航班进行编辑(增加或删除)的功能。
3.3提供两种最优决策:最快到达或最省钱到达。
全程只考虑一种交通工具。
3.4旅途中耗费的总时间应该包括中转站的等候时间。
3.5咨询以用户和计算机的对话方式进行。
二、需求分析1.问题描述搭建一个全国交通咨询模拟系统,可以实现简单的人机互动从而达到咨询的效果。
需要完成的功能有,对城市、列车时刻表、航班时刻表进行编辑(手动/文件添加,删除),能够根据出发城市以及到达城市进行相关线路推荐(花费最少线路、耗时最短线路),其中整个线路的耗时总时长包括中转等待时间。
2.程序的功能:2.1维护功能:2.1.1航班操作2.1.2列车操作2.1.3更改密码2.2咨询功能2.2.1选择出行交通工具2.2.2选择要查询的信息:○1查询花费最小的路径○2查询耗时最短的路径2.2.3进行进一步的输入得到所需要的结果三、设计1.设计思想1.1数据结构设计1.1.1逻辑结构设计采用图结构来表示该全国交通网络,用一个结构体来表示时间,结构体内有标准化的(天,时,分)的相关表示以及操作,重载的运算符“-”。
全国交通网络中的城市用结点表示,两个城市之间的航线或者列车线用两个结点之间的边来表示。
城市结点中包含城市名字、城市编号、第一条航线/列车线以及航线/列车线的数目;边结点中包含到达城市名称,指向下一航线/列车线的指针,以及指向该边所指代航线/列车线信息的指针;航班线/列车线信息结点包含航班号/列车号,出发时间,到达时间,花费时间,花费金额。
数据结构《交通咨询系统》完整代码

数据结构《交通咨询系统》完整代码#include#include#include#include#define zuida 32767#define max 20typedef struct lu{int distance;int cost;int time;}lu,lujin[max][max];typedef struct city{char name[20];}citys[max];typedef struct{citys clist;lujin arcs;int c_n,l_n;}Graph;typedef struct{char adjvex;int mincost;int mindistance;int mintime;}closedge;void zairu(Graph *G);void Administer(Graph G);void show(Graph G);int insertcity(Graph *G);int insertlu(Graph *G);int Located(Graph *G, char *p);void baocun(Graph G);int serchlu(Graph *G);int main(){int y,j;Graph G;zairu(&G);//载⼊交通信息while(1){system("cls");printf("\n\n\n\n\n\n");printf("**********************************欢迎使⽤交通咨询系统**************************\n\n"); printf("\t\t\t※※※※※※※※※※※※※※※※※※※※※\n");printf("\t\t\t※※ 1=管理员管理※\n");printf("\t\t\t※※ 2=⽤户咨询路径※\n");printf("\t\t\t※※ 3=显⽰交通系统信息※\n");printf("\t\t\t※※ 4=退出※\n");printf("\t\t\t※※※※※※※※※※※※※※※※※※※※※\n\n\n");printf("********************************************************************************\n");printf("\t\t\t请选择?");scanf("%d",&y);getchar();switch(y){case 1: system("cls");Administer(G);break; //管理员case 2: system("cls");j = serchlu(&G);break; //⽤户case 3: show(G);//显⽰所有路径break;case 4: system("cls");printf("\n\n\n\t\t\t\t谢谢使⽤.......\n\n\n\n");exit(0);break;default:printf("请按提⽰重新输⼊\n");}}return 0;}void Administer(Graph G)/* 显⽰管理员管理项⽬选择界⾯ */{int i,j=0,x,y;char password[5],a;printf("请输⼊登陆密码(admin)|错误三次退出|:");while(1){ for(x=0;x<5;x++){scanf("%c",&a);password[x]=a;}if(password[0]!='a'||password[1]!='d'||password[2]!='m'||password[3]!='i'||password[4]!='n') {printf("输⼊错误,请重新输⼊!\n密码:");j++;if(j==3)exit(0);scanf("%c",&a);}else break;}system("cls");while(1){ printf("\n\n\n\n\n\n");printf("********************************************************************************\n\n");printf("\t\t\t※※※※※※※请选择管理项⽬※※※※※※\n");printf("\t\t\t※※ 1=增加城市※\n");printf("\t\t\t※※ 2=增加旅游路径※\n");printf("\t\t\t※※ 3=返回上⼀级菜单※\n");printf("\t\t\t※※※※※※※※※※※※※※※※※※※※\n\n\n");printf("********************************************************************************\n");printf("选择?");scanf("%d",&y);switch(y){case 1:i =insertcity(&G); /*增加城市*/if(i == -1)printf("该城市已经存在\n");elseprintf("增加成功\n");break;case 2: i = insertlu(&G);/*增加路径*/if(i == -1)printf("增加失败、不存在此城市\n");elseprintf("增加成功\n");break;case 3: baocun (G);system("cls");main(); break; /*返回主菜单*/default:printf("请按提⽰重新输⼊\n");}}}int insertlu(Graph *G)//增加城市交通信息{char Fromc[20], Toc[20];int i, j;int d, c, t;printf("输⼊增加路径的出发城市、⽬的城市、距离(公⾥)、花费(元)、时间(⼩时): \n"); scanf("%s %s %d %d %d", Fromc, Toc, &d, &c, &t);getchar();i = Located(G, Fromc);j = Located(G, Toc);if(i == -1 || j == -1)return -1;else{G->arcs[i][j].distance = G->arcs[j][i].distance = d;G->arcs[i][j].cost = G->arcs[j][i].cost = c;G->arcs[i][j].time = G->arcs[j][i].time = t;G->l_n++;}return 1;}int insertcity(Graph *G)//增加城市{char name[20];printf("输⼊要增加的城市: ");scanf("%s", name);getchar();if(G->c_n == 0){strcpy(G->clist[0].name, name);G->c_n++;}else{for(int i = 0; i < G->c_n; i++)if(strcmp(G->clist[i].name, name) == 0)return -1;elsestrcpy(G->clist[G->c_n].name, name);G->c_n++;}return 1;void mindistance(Graph *G, int v0, int v1)//最短距离{int *d;int vd,w,i,j,v;int mind, *pred, *finald;finald = (int *)malloc(G ->c_n * sizeof(int));//判断顶点是否已求出最短路径d = (int *)malloc(G ->c_n * sizeof(int));//储存起始点到各点的最短路径pred = (int *)malloc(G ->c_n * sizeof(int));//最后⽤于输出最短路径for(v=0;v < G->c_n; v++){finald[v] =0 ;d[v] =G ->arcs[v0][v].cost ;if(d[v] < zuida)pred[v] =v0;elsepred[v] =-1;}d[v0] = 0;//到起始点⽆路径finald[v0] =1;//v0放⼊到final数组⾥for(i=1;ic_n ;i++)//从1开始、因为起始点已经在final⾥⾯、剩下n - 1个顶点、循环n - 1次。
全国交通咨询系统源代码c++版

CreateGraph(G);
break;
case 2:
CreateGraph(G);
break;
}
}
void createcityfile()//新建“城市”文件
void EnterVertex(ALGraph *G);
void ExpenditureDispose(int k,infolist (*arcs)[dingdian],ALGraph G,int v0,int v1,float *M,int *final);
void flightedit(ALGraph *G);//编辑航班
int at[2];
float mo;
} a[hu];
char city[dingdian][10];
int TTime[2],time[2],time1[2],time2[2];
int c[dingdian],d[dingdian];
void guanliyuan(ALGraph *G);
{
case 1:
guanliyuan(&G);//管理员
break;
case 2:
UserDemand(G);//用户咨询
break;
case 3:
char cityname[10];
ArcNode *planefirstarc,*trainfirstarc;
} VNode,AdjList[dingdian];
typedef struct
{
AdjList vertices;
int vexnum,planearcnum,trainarcnum;
全国交通咨询模拟 数据结构 源代码

全国交通咨询模拟数据结构源代码在进行全国交通咨询模拟的开发过程中,合理的数据结构设计是至关重要的。
本文将详细介绍一个基于数据结构的全国交通咨询模拟的源代码。
1. 数据结构设计在进行全国交通咨询模拟时,我们需要考虑以下几个关键的数据结构:1.1 城市节点城市节点是全国交通咨询模拟的基本组成单元。
每个城市节点包含以下信息:- 城市名称:用字符串表示城市的名称。
- 城市编号:用整数表示城市的唯一编号。
- 邻接城市:用链表或数组表示与当前城市直接相连的邻接城市。
- 交通方式:用枚举类型表示与邻接城市之间的交通方式,如公路、铁路、航空等。
1.2 邻接链表邻接链表是用于存储城市节点之间连接关系的数据结构。
每个城市节点都有一个邻接链表,用于存储与该城市直接相连的邻接城市。
1.3 城市图城市图是全国交通咨询模拟的核心数据结构,由所有城市节点和它们之间的连接关系组成。
城市图可以用邻接链表的数组来表示,数组的索引对应城市编号,数组的每个元素对应一个城市节点。
2. 源代码实现下面是一个简化版的全国交通咨询模拟的源代码示例:```c++#include <iostream>#include <string>#include <vector>// 定义交通方式的枚举类型enum class Transportation {Highway,Railway,Aviation};// 城市节点的定义struct CityNode {std::string name;int id;std::vector<CityNode*> adjacentCities;std::vector<Transportation> transportationModes;};// 城市图的定义class CityGraph {public:CityGraph(int numCities) {cities.resize(numCities);}void addCity(int id, const std::string& name) {CityNode* city = new CityNode;city->id = id;city->name = name;cities[id] = city;}void addConnection(int city1Id, int city2Id, Transportation mode) {CityNode* city1 = cities[city1Id];CityNode* city2 = cities[city2Id];city1->adjacentCities.push_back(city2);city2->adjacentCities.push_back(city1);city1->transportationModes.push_back(mode);city2->transportationModes.push_back(mode);}void printCityConnections(int cityId) {CityNode* city = cities[cityId];std::cout << "Connections for city " << city->name << " (ID: " << city->id << "):" << std::endl;for (int i = 0; i < city->adjacentCities.size(); i++) {CityNode* adjacentCity = city->adjacentCities[i];Transportation mode = city->transportationModes[i];std::cout << "- " << adjacentCity->name << " (ID: " << adjacentCity->id << "), Transportation: ";switch (mode) {case Transportation::Highway:std::cout << "Highway";break;case Transportation::Railway:std::cout << "Railway";break;case Transportation::Aviation:std::cout << "Aviation";break;}std::cout << std::endl;}}private:std::vector<CityNode*> cities;};int main() {// 创建城市图CityGraph graph(5);// 添加城市节点graph.addCity(0, "City A");graph.addCity(1, "City B");graph.addCity(2, "City C");graph.addCity(3, "City D");graph.addCity(4, "City E");// 添加城市连接关系graph.addConnection(0, 1, Transportation::Highway); graph.addConnection(0, 2, Transportation::Railway); graph.addConnection(1, 3, Transportation::Aviation); graph.addConnection(2, 4, Transportation::Highway); // 打印城市连接关系graph.printCityConnections(0);return 0;}```3. 示例说明上述源代码示例实现了一个简化的全国交通咨询模拟。
全国交通咨询模拟系统源代码

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 分配存储空间。
全国交通咨询模拟 数据结构 源代码

全国交通咨询摹拟数据结构源代码在全国范围内建立一个交通咨询摹拟系统,可以为用户提供交通信息查询和路线规划的功能。
为了实现这个系统,我们需要设计和实现合适的数据结构,并编写相应的源代码。
首先,我们需要考虑的是如何表示交通网络。
一个简单而有效的方法是使用图来表示道路和交通节点。
我们可以使用邻接矩阵或者邻接表来表示图。
在这个摹拟系统中,每一个城市可以看做是一个节点,每条道路可以看做是两个节点之间的边。
邻接矩阵可以用一个二维数组来表示,其中数组的每一个元素表示两个节点之间的距离或者道路的权重。
邻接表则使用链表来表示每一个节点的邻接节点。
接下来,我们需要考虑如何实现交通信息查询功能。
用户可以输入起始城市和目的城市,系统需要返回最短路径或者最快路径。
为了实现这个功能,我们可以使用Dijkstra算法或者A*算法。
Dijkstra算法可以找到两个节点之间的最短路径,而A*算法则可以找到两个节点之间的最快路径。
这些算法需要在图的基础上进行计算,并使用合适的数据结构来存储节点和路径信息。
除了交通信息查询,用户还可以查询特定城市的交通拥堵情况。
为了实现这个功能,我们可以为每一个城市维护一个拥堵指数。
拥堵指数可以表示道路的通行能力或者交通流量。
当用户查询某个城市的交通拥堵情况时,系统可以返回该城市的拥堵指数,并根据指数的高低提供相应的建议。
在实现这个交通咨询摹拟系统时,我们还需要考虑数据的存储和更新。
可以使用数据库来存储城市、道路和拥堵指数等信息。
数据库可以提供高效的数据查询和更新功能,并且可以方便地与系统的源代码进行交互。
以下是一个简单的伪代码示例,展示了如何使用数据结构和算法实现交通咨询摹拟系统:```class City:def __init__(self, name): = nameself.adjacent_cities = []self.congestion_index = 0def add_adjacent_city(self, city, distance):self.adjacent_cities.append((city, distance))def update_congestion_index(self, index):self.congestion_index = indexclass TrafficConsultation:def __init__(self):self.cities = {}def add_city(self, name):city = City(name)self.cities[name] = citydef add_road(self, city1, city2, distance):self.cities[city1].add_adjacent_city(self.cities[city2], distance) self.cities[city2].add_adjacent_city(self.cities[city1], distance) def update_congestion_index(self, city, index):self.cities[city].update_congestion_index(index)def shortest_path(self, start_city, end_city):# 使用Dijkstra算法计算最短路径# 返回最短路径和距离def fastest_path(self, start_city, end_city):# 使用A*算法计算最快路径# 返回最快路径和时间def get_congestion_index(self, city):# 返回城市的拥堵指数# 创建交通咨询摹拟系统实例consultation = TrafficConsultation()# 添加城市consultation.add_city("北京")consultation.add_city("上海")consultation.add_city("广州")# 添加道路consultation.add_road("北京", "上海", 1000) consultation.add_road("北京", "广州", 1500) consultation.add_road("上海", "广州", 1200)# 更新拥堵指数consultation.update_congestion_index("北京", 80) consultation.update_congestion_index("上海", 70)consultation.update_congestion_index("广州", 90)# 查询最短路径和最快路径shortest_path, distance = consultation.shortest_path("北京", "广州")fastest_path, time = consultation.fastest_path("北京", "广州")# 查询拥堵指数congestion_index = consultation.get_congestion_index("北京")```以上是一个简单的交通咨询摹拟系统的设计和实现示例。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 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 分配存储空间。