数据结构课程设计交通资讯系统.docx
《数据结构课程设计交通资讯系统.docx》由会员分享,可在线阅读,更多相关《数据结构课程设计交通资讯系统.docx(31页珍藏版)》请在冰豆网上搜索。
![数据结构课程设计交通资讯系统.docx](https://file1.bdocx.com/fileroot1/2023-1/24/4cd15e0c-73e7-497e-b040-e8762aef6710/4cd15e0c-73e7-497e-b040-e8762aef67101.gif)
数据结构课程设计交通资讯系统
交通咨询系统
一、实验目的:
1、充分了解并掌握最短路径问题及其应用。
学习迪杰斯特拉算法和弗洛伊德算法。
2、根据有向图的存储结构解决实际问题。
3、了解程序模块化的过程。
4、给用户提供路径咨询。
实现了帮助用户了解全国各大城市间往来的最短路径问题。
5、可以提供用户查询各大城市的相关信息。
二、实验要求:
设计一个交通咨询系统,能让旅客在任一个城市顶点到另一个城市顶点之间的最短路径(里程)或最低花费或最少时间等问题。
(城市名称用数字代替)。
本程序可以在TC2.0和VC6.0中运行。
三、总体设计:
本程序页面清晰,功能明确,主要分为两个部分,即交通查询部分和管理员管理部分。
交通查询部分又分为城市信息、城市路径、最短路径查询3个模块,而其中最短路径查询系统又分为2个城市之间的最短路径和1个城市到其它各城市之间路径的查询。
而管理员管理的部分则包括交通查询里面的功能以及添加城市、添加城市间的路径、删除城市以及删除两个城市之间的路径等部分。
四、详细设计:
详细设计全国交通信息咨询系统的存储结构:
structcity_info/*城市信息结构*/
{charname[20];charinfo[100];}cities[SIZE_city];
structway_info/*路径信息结构*/
{intstart_id;intend_id;intdist;}ways[SIZE_way];
structpath_info/*路径查询结构*/
{intcount;intpath[SIZE_city];};
typedefstructGraph
{charvexs[SIZE_city];/*顶点*/intarcs[SIZE_city][SIZE_city];/*邻接矩阵*/intvexnum;/*顶点数*/intarcnum;/*弧数*/}Graph;
划分本程序采用自顶向下的编程模式,分为7个主模块,即考虑到7种算法的实现:
1、显示全国各地城市相关信息的算法;
voidShowCity();初始条件:
voidReadCitiesFile();voidReadWaysFile();读取数据成功,操作结果:
根据用户查询城市的名字,输出城市的特色;
2、添加城市及城市相关信息的算法;
voidAddView();初始条件:
构造了结构体数组views[VIEW_SIZE];操作结果:
添加一个城市顶点到views数组中,并将数据输出到VIEWS文件中保存。
3、删除一个城市的算法;
voidDelView(); 初始条件:
存在views[VIEW_SIZE];操作结果:
从views数组中删除一个城市顶点,并重新将数组中数据输出到VIEWS文件中保存。
4、是查询城市之间的路径的算法;
voidShowWay();初始条件:
构造了结构体数组ways[WAY_SIZE];操作结果:
添加一个城市顶点到ways数组中,并将数据输出到WAYS文件中保存。
5、添加城市之间的路径的算法;
voidAddWay();初始条件:
构造了结构体数组ways[VIEW_SIZE];操作结果:
添加一条城市间的路径到ways数组中,并将数据输出到WAYS文件中保存。
6、删除城市间的路径的算法;
voidDelWay();初始条件:
存在ways[VIEW_SIZE];操作结果:
从ways数组中删除一个城市顶点,并重新将数组中数据输出到WAYS文件中保存。
7、查询两个城市间最短路径的算法
voidSearchWay();初始条件:
存在城市信息及路径的存储结构操作结果:
根据用户输入,输出两个城市间的最短路径
}ATDGraph
五、程序代码:
#include"stdio.h"
#include"string.h"
#defineSIZE_city20
#defineSIZE_way300
#defineINFINITY32768
#defineTRUE1
#defineFALSE0
structcity_info/*城市信息结构*/
{charname[20];
charinfo[100];
}cities[SIZE_city];
structway_info/*路径信息结构*/
{intstart_id;
intend_id;
intdist;
}ways[SIZE_way];
structpath_info/*路径查询结构*/
{intcount;
intpath[SIZE_city];
};
typedefstructGraph
{
charvexs[SIZE_city];/*顶点*/
intarcs[SIZE_city][SIZE_city];/*邻接矩阵*/
intvexnum;/*顶点数*/
intarcnum;/*弧数*/
}Graph;
intcity_count,way_count;
intcommenu2,commenu3;
/*函数的声明*/
voidcounseling();
voidLogin();
voidmodify();
voidReadCitiesFile();
voidReadWaysFile();
voidShowCity();
voidAddCity();
voidDelCity();
voidShowWay();
voidAddWay();
voidDelWay();
voidSearchWay();
voidmain()
{
system("cls");
printf("\n\n");
printf("********************************************************************************\n");
printf("****\n");
printf("**Thetrafficcounselingsystem**\n");
printf("**madebyzhengyanran**\n");
printf("********************************************************************************\n");
printf("\n");
ye();
printf("Quiting...\n");
printf("\n\n");
return;
}
ye()
{
intcommenu1;
commenu1=1;
while(commenu1!
=0)
{
printf("\n");
printf("Welcome!
Youcanchooseasfollow:
\n\n");
printf("1--------Thetrafficcounseling.\n\n");/*显示景点信息*/
printf("2--------TheAdministratorLoginin.\n\n");/*增加景点方便系统后台管理*/
printf("0--------Quit.\n\n");
printf("Whatwillyoudo:
");
scanf("%d",&commenu1);
switch(commenu1)
{
case1:
{
system("cls");
counseling();
}
break;
case2:
Login();
break;
}
}
}
voidcounseling()
{
ReadCitiesFile();
ReadWaysFile();
commenu2=1;
while(commenu2!
=0)
{
printf("\n\n");
printf("Welcome!
Youcando:
\n\n");
printf("1--------Showtheinfoofcities.\n\n");/*显示景点信息*/
printf("2--------Showtheinfoofways.\n\n");/*显示路径信息*/
printf("3--------Searchthebestway.\n\n");/*寻找最佳路径*/
printf("0--------Quit\n\n");/*退出系统查询系统*/
printf("Whatwillyoudo:
");
scanf("%d",&commenu2);
switch(commenu2)
{
case1:
ShowCity();
break;
case2:
ShowWay();
break;
case3:
SearchWay();
break;
}
}
}
voidLogin()
{
charhit[6];
charprehit[6]="yanran";
inti;
printf("\nPleaseinputthehitnumber(6bit):
");
scanf("%s",&hit);
while(strcmp(&hit[6],&prehit[6]))
{
system("cls");
modify();
}
}
voidmodify()
{
ReadCitiesFile();
ReadWaysFile();
commenu3=1;
printf("\n\n");
printf("Welcome!
Youcando:
\n\n");
printf("1--------Showtheinfoofcities.\n\n");/*显示景点信息*/
printf("2--------Addacity.\n\n");/*增加景点方便系统后台管理*/
printf("3--------Deleteacity.\n\n");/*删除景点方便系统后台管理*/
printf("5--------Addaway.\n\n");/*增加路径方便系统后台管理*/
printf("4--------Showtheinfoofways.\n\n");/*显示路径信息*/
printf("6--------Deleteaway.\n\n");/*删除路径方便系统后台管理*/
printf("7--------Searchthebestway.\n\n");/*寻找最佳路径*/
printf("0--------Quit\n\n");/*退出系统查询系统*/
printf("Whatwillyoudo:
");
scanf("%d",&commenu3);
while(commenu3!
=0)
{
switch(commenu3)
{
case1:
ShowCity();
break;
case2:
AddCity();
break;
case3:
DelCity();
break;
case4:
ShowWay();
break;
case5:
AddWay();
break;
case6:
DelWay();
break;
case7:
SearchWay();
break;
}
}
returnye();
}
voidReadCitiesFile()
{
inti;
FILE*fp;
city_count=0;
if((fp=fopen("CITIES","rb"))==NULL)/*打开CITIES文件,进行读写操作*/
{return;}
for(i=0;i{
if(fread(&cities[i],sizeof(structcity_info),1,fp)==1)
city_count=city_count+1;
else
break;
}
fclose(fp);
}
voidReadWaysFile()
{
inti;FILE*fp;way_count=0;
if((fp=fopen("WAYS","rb"))==NULL)/*读取WAYS文件*/
{return;}
for(i=0;i{
if(fread(&ways[i],sizeof(structway_info),1,fp)==1)
way_count=way_count+1;
else
break;
}
fclose(fp);
}
voidAddCity()
{
FILE*fp;
structcity_infonew_city;
printf("Addingacity...\n");
if((fp=fopen("CITIES","ab"))==NULL)
{
printf("OpenfileCITIESfailed.\n");
exit(0);
}
printf("\nThenameofnewcity:
");
scanf("%s",new_city.name);
printf("\nTheinfoofnewcity:
");
scanf("%s",new_city.info);
if(fwrite(&new_city,sizeof(structcity_info),1,fp)==1)/*将num个城市写入文件中*/
{
cities[city_count]=new_city;
city_count=city_count+1;
printf("Addacitysuccessfully.\n");
}
else
printf("Writeerror.\n");
fclose(fp);
printf("\n\n");
returnmodify();
}
/*删除城市的同时,遍历路径表,将大于所删序号的编号减小1*/
voidDelCity()
{
inti,select_id;FILE*fp;
printf("Deletingacity...\n");select_id=1;
while(select_id!
=0)
{
if(city_count==0)
{
printf("\nThereisnotanycity.Youcan'tdeleteanycity.\n\n\n");
returnmodify();;
}
printf("\nAllcitiesintheschool:
\n");
for(i=0;iprintf("\n%d:
%s\n",i+1,cities[i].name);
printf("\nPress1-%dtodeleteacity,orpress0tocancel:
",city_count);
scanf("%d",&select_id);
if(select_id>0&&select_id<=city_count)
{
city_count=city_count-1;
for(i=select_id-1;icities[i]=cities[i+1];fp=fopen("CITIES","wb+");
for(i=0;ifwrite(&cities[i],sizeof(structcity_info),1,fp);
fclose(fp);
}
}
printf("\n\n");
return;
}
voidShowCity()
{
inti,select_id;
printf("\nShowingtheinfoofcities...\n");
if(city_count==0)
{
printf("\nThereisnotanycity.\n\n\n");
returnmodify();
}
printf("\nAllcitiesintheschool:
\n");
for(i=0;iprintf("\n%d:
%s\n",i+1,cities[i].name);
select_id=1;
while(select_id!
=0)
{
printf("\nPress1-%dtoshowtheinfoofcity,orpress0tocancel:
",city_count);
scanf("%d",&select_id);
if(select_id>0&&select_id<=city_count)
printf("\n%s:
%s\n",cities[select_id-1].name,cities[select_id-1].info);
}
printf("\n\n");
returncounseling();
}
voidAddWay()
{
inti;inttmp_num;FILE*fp;
structway_infonew_way;
printf("\nAddingaway...\n");
if(city_count==0)
{
printf("\nThereisnotanyview.Youcan'taddaway.\n\n\n");
returnmodify();;
}
printf("\nAllcitiesintheschool:
\n");
for(i=0;iprintf("\n%d:
%s\n",i+1,cities[i].name);
if(city_count==1)
{
printf("\nThereisonlyonecity.Youcan'taddaway.\n\n\n");
return;
}
if((fp=fopen("WAYS","ab"))==NULL)
{
printf("\nOpenfileWAYSfailed.\n");
exit(0);
}
tmp_num=0;
while
(1)
{
printf("\nIDof1stview:
");
scanf("%d",&tmp_num);
if(tmp_num==0)
{
printf("\n\n");
return;
}
if(tmp_num>0&&tmp_num<=city_count)
{
new_way.start_id=tmp_num-1;
break;
}
else
printf("\nWrongID!
Pleaseinput1-%d,or0tocancel.\n",city_count);
}
while
(1)
{
printf("\nIDof2ndcity:
");
scanf("%d",&tmp_num);
if(tmp_num==0)
{
printf("\n\n");
return;
}
if(tmp_num>0&&tmp_num<=city_count)
{
new_way.end_id=tmp_num-1;
break;
}
else
printf("\nWrongID!
Pleaseinput1-%d,or0tocancel.\n",city_count);
}
while
(1)
{
printf("\nDistance:
");
scanf("%d",&tmp_num);
if(tmp_num==0)
{
printf("\n\n");
return;
}
if(tmp_num>0)
{
new_way.dist=tmp_num;
break;
}
else
printf("\nError!
Pleaseinputpositivewholenumber,or0tocancel.\n");
}
if(fwrite(&new_way,sizeof(structway_info),1,fp)==1)
{
ways[way_count]=new_way;
way_count=way_count+1;
printf("\nAddaviewsuccessfully.");
}
else
printf("\nWriteerror.\n");
fclose(fp);
printf("\n\n");
returnmodify();
}
voidDelWay()
{
inti,select_id;FILE*fp;
printf("\nDeletingaway...\n");
select_id=1;
while(select_id!
=0)
{
if(way_count==0)
{
pri