#include<iostream>
#include<string>
#include<stdio.h>
#include<iomanip>
#define SIZE_view 50
#define SIZE_line 100
#define SIZE_way 300
#define MAXNODE 30//定义最多的顶点数
#define MAXCOST 1000
//自己写的头文件
//#include<addview.h>// //
using namespace std
struct view_info/*城市信息结构*/
{
int id
char name[20]
int code
char shortname[20]
} views[SIZE_view]
struct line_info //铁路线信息结构
{
int Lid
char LName[20]
int start_id //始发站id
int end_id//终点站id
int dist //铁路线长度
int sign//通行标志
}lines[SIZE_line]
struct way_info //铁路度的信息结构
{
int station1
int station2
int dist
}ways[SIZE_way]
struct path_info //用于最短路径的查询
{
int count
int path[SIZE_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 error!"<<endl
exit(1)
}
//infile>>view_count // 先读入文件个数
for(i=0i<view_counti++)
{
infile>>views[i].id>>views[i].name>>views[i].code>>views[i].shortname>>views[i].LName
}
//view_count=i//给出原文件中车站的个数
infile.close()
cout<<" "<<"id"<<""<<"name"<<""<<"code"<<""<<"shortname"<<""<<"LName"<<endl
for(i=0i<view_counti++)
cout<<" "<<views[i].id<<" "<<views[i].name<<" "<<views[i].code
<<" "<<views[i].shortname<<" "<<views[i].LName<<endl
}
void readways() //读文件ways.txt
{
int i
ifstream infile("ways.txt",ios::in) //打开文件
infile>>way_count////把文件中的记录赋值给way_count
if(!infile) //打开文件失败
{
cerr<<"open error!"<<endl
exit(1)
}
for(i=0i<way_counti++)
infile>>ways[i].station1>>ways[i].station2>>ways[i].dist
infile.close()
//测试用,输出路段的信息
cout<<" "<<"station1"<<" "<<"station2"<<" "<<"dist"<<endl
for(i=0i<way_counti++)
cout<<" "<<ways[i].station1<<" "<<ways[i].station2<<" "<<ways[i].dist<<endl
}
void readlines() //读文件lines.txt
{
int i
ifstream infile("lines.txt",ios::in) //打开文件
infile>>line_count //把文件中的记录赋值给line_count
if(!infile) //打开文件失败
{
cerr<<"open error!"<<endl
exit(1)
}
for(i=0i<line_counti++)
infile>>lines[i].Lid>>lines[i].LName>>lines[i].start_id>>lines[i].end_id>>lines[i].dist>>lines[i].sign
infile.close()
/*
cout<<" "<<"Lid"<<""<<"LName"<<""<<"start_id"<<""<<"end_id"<<" "<<"dist"<<""<<"sign"<<endl
for(i=0i<view_counti++)
cout<<" "<<lines[i].Lid<<" "<<lines[i].LName<<" "<<lines[i].start_id
<<" "<<lines[i].end_id<<" "<<lines[i].dist<<" "<<lines[i].sign<<endl
*/ //这里是输出文件中的信息
}
void search () //查询车站信息(所在的铁路线)
{
cout<<"please enter the station name:"
char sta_name[20]
cin>>sta_name //输入要查询的名字
cout<<endl
ifstream infile("views.txt ",ios::in) //读文件
infile >>view_count //读出文件记录的个数
if(!infile) //打开文件失败
{
cerr<<"open error!"<<endl
exit(1)
}
int i,mark
for(i=0i<view_counti++)
infile>>views[i].id>>views[i].name>>views[i].code>>views[i].shortname>>views[i].LName
infile.close()
for(i=0i<view_counti++)
{
if(strcmp(sta_name,views [i].name)==0)//找到车站
{
cout<<"the station's informations is:\n"<<endl
cout<<""<<"id"<<""<<"name"<<""<<"code"<<""<<"shortname"<<""<<"LName"<<endl
cout<<""<<views[i].id<<""<<views[i].name<<""<<views [i].code
<<""<<views [i].shortname<<" "<<views [i].LName <<endl
break
}
mark=i
}
if(mark==( view_count -1)) //若没有找到,输出提示
{
cout<<"sorry,the station is not in here"<<endl
}
}
void addview()
{
cout<<"please enter the new view's informations:"<<endl //输入新车站信息
cout<<"id:"
cin>>views[view_count].id
cout<<"name:"
cin>>views[view_count].name
cout<<"code:"
cin>>views[view_count].code
cout<<"shortname:"
cin>>views[view_count].shortname
cout<<"LName:"
cin>>views[view_count].LName
ofstream outfile("views.txt",ios::app) //打开views文件,并且写入数据
outfile<<view_count<<endl
if(!outfile)//文件打开失败
{
cerr<<"open error!"<<endl
exit(1)
}
outfile<<views[view_count].id<<" "<<views[view_count].name<<" "<<views[view_count].code
<<" " <<views[view_count].shortname<<" "<<views[view_count].LName<<endl
//在文件末尾添加
view_count=view_count+1
outfile.close() //关闭文件
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:"<<endl //输入新车站信息
cout<<"station1:"
cin>>ways[way_count].station1// station1的id
cout<<"station2:"
cin>>ways[way_count].station2 //station2的id
cout<<"dist:"
cin>>ways[way_count].dist //路段的长度
ofstream outfile("ways.txt",ios::app) //打开way.txt文件
outfile<<way_count<<endl
if(!outfile)//文件打开失败
{
cerr<<"open error!"<<endl
exit(1)
}
outfile<<ways[way_count].station1<<" "<<ways[way_count].station2<<" "<<ways[way_count].dist
//在文件末尾添加
outfile.close() //关闭文件
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<<"Lid:"
cin>>lines[line_count].Lid
cout<<"LName:"
cin>>lines[line_count].LName
cout<<"start_id:"
cin>>lines[line_count].start_id
cout<<"end_id:"
cin>>lines[line_count].end_id
cout<<"dist:"
cin>>lines[line_count].dist
cout<<"sign:"
cin>>lines[line_count].sign
ofstream outfile("lines.txt",ios::app) //打开文件
outfile <<line_count <<endl
if(!outfile)//文件打开失败
{
cerr<<"open error!"<<endl
exit(1)
}
outfile<<lines[line_count].Lid<<" "<<lines[line_count].LName<<" "<<lines[line_count].start_id
<<" " <<lines[line_count].end_id<<" "<<lines[line_count].dist<<lines[line_count].sign<<endl
//在文件末尾添加
outfile.close() //关闭文件
cout<<"successfully! the 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用来表示起始点和终点
int dist_list[SIZE_view][SIZE_view]//定义了一个数组
view_count=view_count+1
struct path_info path_list[SIZE_view][SIZE_view] //定义了一个path_info结构的变量,包含着count和path[]用来存储经过的路径
for(i=1i<=view_counti++) //这里开始读文件,先让所有的线都为最大值
for(j=1j<=view_countj++)
dist_list[i][j]=MAXCOST
for(int t=0t<=way_countt++)
{
i=ways[t].station1
j=ways[t].station2
dist_list[i][j]=ways[t].dist//把文件中的数据赋值给dist_list[i][j]=ways[t].dist形式
}
for (i =0i<view_counti++)
{
for (j= 0j<view_countj++)
{
if (i == j)
{
dist_list[i][j] = 0
continue
}
dist_list[i][j] = -1
path_list[i][j].count = 0
for (k = 0k<way_countk++) //
{
if (ways[k].station1 == i &&ways[k].station2 == j) //把起始点和终点分别给予i,j
{
dist_list[i][j] = ways[k].dist
path_list[i][j].count = 2
path_list[i][j].path[0] = i
path_list[i][j].path[1] = j
break
}
}
}
}
for (k = 0k<= view_count-1k++)
{
for (i = 0i <view_counti++)
for (j = 0j<view_countj++)
{
if (i == k || j == k || i == j)
continue
if (dist_list[i][k] == -1 || dist_list[k][j] == -1)
continue
if ( (dist_list[i][j] == -1) ||
((dist_list[i][j] != -1) &&(dist_list[i][k] + dist_list[k][j] <dist_list[i][j])))
{
dist_list[i][j] = dist_list[i][k] + dist_list[k][j]
// shortest[i][j]=shortest[i][k]+shortest[k][j]
path_list[i][j].count = path_list[i][k].count + path_list[k][j].count - 1
// path[i][j]=k
for (m = 0m <path_list[i][k].countm++)
path_list[i][j].path[m] = path_list[i][k].path[m]
for (m = 0m <path_list[k][j].countm++)
path_list[i][j].path[m+path_list[i][k].count] = path_list[k][j].path[m+1]
}
}
}
cout<<" Floyed table:\n"
cout<<" All views in the school:\n"
for (i = 0i <view_count-1i++)
cout<<""<<i+1<<":"<<views[i].name<<endl
cout<<" Please input the start number: "
cin>>start_num
cout<<" Please input the end number: "
cin>>end_num
cout<<endl<<endl
cout<<"From"<<views[start_num-1].name<<"to"<<views[end_num-1].name
if (dist_list[start_num][end_num] == -1)
cout<<"no way."<<endl
else
{
cout<<"distance is "<<dist_list[start_num][end_num]<<", and path is :"//dist_list[][]用来表示两点间的长度
k = path_list[start_num][end_num].path[0]-1 //path_list[][]用来保存路径path[m]在这里表明是通过的那个车站的
cout<<views[k].name
for (m = 1m <path_list[start_num][end_num].countm++)
{
k = path_list[start_num][end_num].path[m]-1//这里应该也是int型,k是经过的路径的id
cout<<"->"<<views[k].name
}
}
cout<<endl<<endl
}
void main()
{
readviews()
readlines()
readways()
while(1)
{
int menu
cout<<endl<<endl<<endl<<endl
cout<<" 全国铁路运输网最佳经由系统 "<<endl
cout<<"**********************************************************"<<endl
cout<<" 1:增加车站信息 "<<endl
cout<<" 2:增加铁路线信息 "<<endl
cout<<" 3:查询车站信息 "<<endl
cout<<" 4:查询最短路径 "<<endl
cout<<" 5:退出界面 "<<endl
cout<<"**********************************************************"<<endl
cout<<"输入要进行的 *** 作的代码(1--5):"<<endl
cin>>menu
while(menu<1||menu>5)
{
cout<<"error!please enter again:"
cin>>menu
}
switch(menu)
{
case 1:
while(1)
{
addview()
while (1)
{
addway()
cout<<"do you want to continue (y/n) "<<endl
char con
cin>>con
if(con=='y')
addway()
else
break
}
cout<<"do you want to continue (y/n) "<<endl
char con
cin>>con
if(con=='y')
addview()
else
break
}
break
case 2:
while(1)
{
addline()
cout<<"do you want to continue (y/n) "<<endl
char con
cin>>con
if(con=='y')
addline()
else
break
}
break
case 3:
while(1)
{
search ()
cout<<"do you want to continue (y/n) "<<endl
char con
cin>>con
if(con=='y')
search ()
else
break
}
break
case 4:
while(1)
{
floyed()
cout<<"do you want to continue (y/n) "<<endl
char con
cin>>con
if(con=='y')
floyed()
else
break
}
break
case 5:
{
cout<<"谢谢使用,再见!"<<endl
exit(1)
}
}
}
}
包含的表主要有列车(列车编号,车种,始发站,始发站,终到站,发时,到时,里程)
车站(车站id,车站名)
经过(列车编号,车站名,站次,里程,到时,发时)
车票(车票编号,车次,发站,到站,发站次,到站次,发时,到时,座位类型,座位号,车票日期,票价)
主要是这四个表,查询插入 *** 作有些复杂不过都能完成
详细看列车票务系统数据库课程设计说明书,文库里有
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)