📄 jt.cpp
字号:
#define MAX_VERTEX_NUM 18
#define NULL 0
#define MAX_ARC_SIZE 100
#define MAX_ROUTE_NUM 5
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include <iostream.h>
#include "jt.h"
char city[MAX_VERTEX_NUM][10];
int TTime[2];
int time[2];
int time1[2];
int time2[2];
int c[MAX_VERTEX_NUM];
int d[MAX_VERTEX_NUM];
//显示程序功能选择界面
void main(void )
{
ALGraph G;
char i;
cout << "请选择程序功能:" << endl;
cout << "1=管理员管理" << endl
<< "2=用户咨询" << endl
<< "3=显示交通系统" << endl
<< "4=退出" << endl;
cout << "选择?";
cin >> i;
while(i!='4')
{
switch(i)
{
case '1': Administer(&G); break;
case '2': UserDemand(G); break;
case '3': PrintGraph(&G); break;
}
cout << endl
<< "请选择程序功能:"
<< endl;
cout << "1=管理员管理" << endl
<< "2=用户咨询" << endl
<< "3=显示交通系统" << endl
<< "4=退出" << endl;
cout << "选择?";
cin >> i;
}
}
//管理员管理项目选择界面
void Administer(ALGraph *G)
{
char i;
cout << endl
<< "请选择管理项目:"
<< endl;
cout << "1=初始化交通系统" << endl
<< "2=城市编辑" << endl
<< "3=飞机航班编辑" << endl
<< "4=列车车次编辑" << endl
<< "5=返回上一级菜单" << endl;
cout << "选择?";
cin >> i;
while ( i!='5' )
{
switch(i)
{
case '1': initgraph(G); break;
case '2': cityedit(G); break;
case '3': flightedit(G); break;
case '4': trainedit(G); break;
}
cout << endl
<< "请选择管理项目:"
<< endl;
cout << "1=初始化交通系统" << endl
<< "2=城市编辑" << endl
<< "3=飞机航班编辑" << endl
<< "4=列车车次编辑" << endl
<< "5=返回上一级菜单" << endl;
cout << "选择?";
cin >> i;
}
}
//化交通系统方式选择界面
void initgraph(ALGraph *G)
{
char i;
do{
cout << endl
<< "请选择初始化方式:"
<< endl;
cout << "1=键盘" << endl
<< "2=文档" << endl;
cout << "选择?";
cin >> i;
}while ( i!='1' && i!='2' );
switch(i)
{
case '1': createcityfile();
createplanefile();
createtrainfile();
CreateGraph(G);
break;
case '2': CreateGraph(G);
break;
}
}
//创建城市名称文档
void createcityfile()
{
int i=0;
int j;
char flag='y';
FILE *fp;
cout << endl
<< "请输入城市名称的信息:"
<< endl;
while(flag=='y'||flag=='Y')
{
cout << "城市名称:";
cin >> city[i];
i++;
cout << "继续输入? (Y/N)";
cin >> flag;
}
cout << endl;
if ( (fp=fopen("city.txt","wb"))==NULL )
{
cout << "无法打开文件!" << endl;
return;
}
for( j=0; j<i; j++ )
fprintf( fp,"%10s",city[j] );
fclose( fp );
}
//创建飞机航班文档
void createplanefile()
{
int code,bt[2],at[2];
float money;
int i;
int count;
char vt[10],vh[10],flag;
FILE *fp;
flag='y';
count=0;
while ( flag=='Y' || flag=='y' )
{
cout << "请输入飞机航班的信息:" << endl;
cout << "飞机航班编号:";
cin >> code;
cout << "起始城市:";
cin >> vt;
cout << "目的城市:";
cin >> vh;
cout << "航班费用:";
cin >> money;
printf( "起飞时间:" );
scanf( "%d:%d", &bt[0], &bt[1] );
while(bt[0]<0||bt[0]>=24||bt[1]<0||bt[1]>=60)
{
printf( "\n时间输入有误,请重新输入!\n" );
scanf( "%d:%d", &bt[0], &bt[1] );
}
printf( "到达时间:" );
scanf( "%d:%d", &at[0], &at[1] );
while ( at[0]<0 || at[0]>=24 || at[1]<0 || at[1]>=60 )
{
printf( "\n时间输入有误,请重新输入!\n" );
scanf( "%d:%d", &at[0], &at[1] );
}
a[count].co=code;
strcpy(a[count].vt,vt);
strcpy(a[count].vh,vh);
a[count].bt[0]=bt[0];
a[count].bt[1]=bt[1];
a[count].at[0]=at[0];
a[count].at[1]=at[1];
a[count].mo=money;
count++;
cout << "继续输入? (Y/N)";
cin >> flag;
cout << endl;
}
if ((fp=fopen("plane.txt","wb"))==NULL)
cout << endl
<< "无法打开文件!"
<< endl;
fprintf( fp, "%d", count );
for ( i=0; i<count; i++ )
if ( fwrite(&a[i],sizeof(struct arc),1,fp)!=1 )
cout << endl
<< "文件写入错误!"
<< endl;
fclose(fp);
}
//创建列车车次文档
void createtrainfile()
{
int code,bt[2],at[2];
float money;
int i;
int count;
char vt[10],vh[10],flag;
FILE *fp;
flag='y';
count=0;
while(flag=='y'||flag=='Y')
{
cout << "请输入列车车次的信息:" << endl;
cout <<"列车车次编号:";
cin >> code;
cout << "起始城市:";
cin >> vt;
cout << "目的城市:";
cin >> vh;
cout << "车次费用:";
cin >> money;
printf( "发车时间:" );
scanf( "%d:%d", &bt[0], &bt[1] );
while(bt[0]<0||bt[0]>=24||bt[1]<0||bt[1]>=60)
{
printf( "\n时间输入有误,请重新输入!\n" );
scanf( "%d:%d", &bt[0], &bt[1] );
}
printf( "到达时间:" );
scanf( "%d:%d", &at[0], &at[1] );
while(at[0]<0||at[0]>=24||at[1]<0||at[1]>=60)
{
printf( "\n时间输入有误,请重新输入\n" );
scanf( "%d:%d", &at[0], &at[1] );
}
a[count].co=code;
strcpy(a[count].vt,vt);
strcpy(a[count].vh,vh);
a[count].bt[0]=bt[0];
a[count].bt[1]=bt[1];
a[count].at[0]=at[0];
a[count].at[1]=at[1];
a[count].mo=money;
count++;
cout << "继续输入?(Y/N)";
cin >> flag;
cout << endl;
}
if ((fp=fopen("train.txt","wb"))==NULL)
cout << endl
<< "无法打开文件!"
<< endl;
fprintf( fp,"%d",count);
for(i=0;i<count;i++)
if ( fwrite(&a[i],sizeof(struct arc),1,fp)!=1 )
cout << endl
<< "文件写入错误!"
<< endl;
fclose(fp);
}
//城市名在交通系统中定位操作
int LocateVertex(ALGraph *G,char *v)
{
int j,k;
j=-1;
for(k=0;k<G->vexnum;k++)
if(strcmp(G->vertices[k].cityname,v)==0)
{
j=k;
break;
}
return(j);
}
//用city,plan,train三个文档创建城市交通系统
void CreateGraph(ALGraph *G)
{
int i,j,k;
int arc_num;
int count1,count2;
int m,t;
ArcNode *p,*q;
FILE *fp;
i=0;
if((fp=fopen("city.txt","rb"))==NULL)
{
cout << endl
<< "无法打开文件!"
<< endl;
return;
}
while(!feof(fp))
{
fscanf(fp,"%10s",city[i]);
i++;
}
fclose(fp);
j=0;
while(j<i)
{
strcpy(G->vertices[j].cityname,city[j]);
G->vertices[j].planefirstarc=NULL;
G->vertices[j].trainfirstarc=NULL;
j++;
}
G->vexnum=i;
if ( (fp=fopen("plane.txt","rb"))==NULL )
cout << endl
<< "无法打开文件!"
<< endl;
k=0;
fscanf(fp,"%d",&count1);
while(k<count1)
{
if ( fread(&a[k],sizeof(struct arc),1,fp)!=1 )
cout << endl
<< "文件读入错误!"
<< endl;
k++;
}
fclose(fp);
k=0;
arc_num=0;
while(k<count1)
{
i=LocateVertex(G,a[k].vt);
j=LocateVertex(G,a[k].vh);
q=G->vertices[i].planefirstarc;
m=0;
while(q!=NULL)
{
if(q->adjvex==j)
{
t=q->info.last+1;
q->info.stata[t].number=a[k].co;
q->info.stata[t].expenditure=a[k].mo;
q->info.stata[t].begintime[0]=a[k].bt[0];
q->info.stata[t].begintime[1]=a[k].bt[1];
q->info.stata[t].arrivetime[0]=a[k].at[0];
q->info.stata[t].arrivetime[1]=a[k].at[1];
q->info.last=t;
m=1;
break;
}
q=q->nextarc;
}
if(m==0)
{
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
p->info.stata[0].number=a[k].co;
p->info.stata[0].expenditure=a[k].mo;
p->info.stata[0].begintime[0]=a[k].bt[0];
p->info.stata[0].begintime[1]=a[k].bt[1];
p->info.stata[0].arrivetime[0]=a[k].at[0];
p->info.stata[0].arrivetime[1]=a[k].at[1];
p->info.last=0;
p->nextarc=G->vertices[i].planefirstarc;
G->vertices[i].planefirstarc=p;
arc_num++;
}
k++;
}
G->planearcnum=arc_num;
if((fp=fopen("train.txt","rb"))==NULL)
{
cout << endl
<< "无法打开文件!"
<< endl;
return;
}
k=0;
fscanf(fp,"%d",&count2);
while(k<count2)
{
if ( fread(&a[k],sizeof(struct arc),1,fp)!=1 )
cout << endl
<< "文件读入错误!"
<< endl;
k++;
}
fclose(fp);
k=0;
arc_num=0;
while(k<count2)
{
i=LocateVertex(G,a[k].vt);
j=LocateVertex(G,a[k].vh);
q=G->vertices[i].trainfirstarc;
m=0;
while(q!=NULL)
{
if(q->adjvex==j)
{
t=q->info.last+1;
q->info.stata[t].number=a[k].co;
q->info.stata[t].expenditure=a[k].mo;
q->info.stata[t].begintime[0]=a[k].bt[0];
q->info.stata[t].begintime[1]=a[k].bt[1];
q->info.stata[t].arrivetime[0]=a[k].at[0];
q->info.stata[t].arrivetime[1]=a[k].at[1];
q->info.last=t;
m=1;
break;
}
q=q->nextarc;
}
if(m==0)
{
p=(ArcNode*)malloc(sizeof(ArcNode));
p->adjvex=j;
p->info.stata[0].number=a[k].co;
p->info.stata[0].expenditure=a[k].mo;
p->info.stata[0].begintime[0]=a[k].bt[0];
p->info.stata[0].begintime[1]=a[k].bt[1];
p->info.stata[0].arrivetime[0]=a[k].at[0];
p->info.stata[0].arrivetime[1]=a[k].at[1];
p->info.last=0;
p->nextarc=G->vertices[i].trainfirstarc;
G->vertices[i].trainfirstarc=p;
arc_num++;
}
k++;
}
G->trainarcnum=arc_num;
}
//保存城市交通系统到相应的文档
void save(ALGraph *G)
{
int i,j,k,t;
ArcNode *q;
FILE *fp;
j = 0;
while ( j<G->vexnum )
{
strcpy(city[j],G->vertices[j].cityname);
j++;
}
i = 0;
if ( (fp=fopen("city.txt","wb"))==NULL )
cout << endl
<< "错误!无法打开文件!"
<< endl;
while ( i<G->vexnum )
{
fprintf(fp,"%10s",city[i]);
i++;
}
fclose( fp );
k = 0;
for( i=0; i<G->vexnum; i++ )
{
q = G->vertices[i].planefirstarc;
while( q!=NULL )
{
for(t=0;t<=q->info.last;t++)
{
strcpy(a[k].vt,G->vertices[i].cityname);
strcpy(a[k].vh,G->vertices[q->adjvex].cityname);
a[k].co=q->info.stata[t].number;
a[k].mo=q->info.stata[t].expenditure;
a[k].bt[0]=q->info.stata[t].begintime[0];
a[k].bt[1]=q->info.stata[t].begintime[1];
a[k].at[0]=q->info.stata[t].arrivetime[0];
a[k].at[1]=q->info.stata[t].arrivetime[1];
k++;
}
q = q->nextarc;
}
}
if ( (fp=fopen("plane.txt","wb"))==NULL )
{
cout << endl
<< "无法打开文件!"
<< endl;
return;
}
i = 0;
fprintf(fp,"%d",k);
while ( i<k )
{
if ( fwrite(&a[i],sizeof(struct arc),1,fp)!=1 )
cout << endl
<< "文件写入错误!"
<< endl;
i++;
}
fclose(fp);
k = 0;
for ( i=0; i<G->vexnum; i++ )
{
q = G->vertices[i].trainfirstarc;
while ( q!=NULL )
{
for( t=0; t<=q->info.last; t++ )
{
strcpy(a[k].vt,G->vertices[i].cityname);
strcpy(a[k].vh,G->vertices[q->adjvex].cityname);
a[k].co=q->info.stata[t].number;
a[k].mo=q->info.stata[t].expenditure;
a[k].bt[0]=q->info.stata[t].begintime[0];
a[k].bt[1]=q->info.stata[t].begintime[1];
a[k].at[0]=q->info.stata[t].arrivetime[0];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -