⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jt.cpp

📁 交通咨询的事例程序
💻 CPP
📖 第 1 页 / 共 4 页
字号:
				a[k].at[1]=q->info.stata[t].arrivetime[1];
				k++;				
			}
			q = q->nextarc;
		}
	}
	if((fp=fopen("train.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); 
}

//显示城市编辑项目选择界面
void cityedit(ALGraph *G)
{
	char i;
	do {
		 cout << endl
		 << "请选择城市编辑项目:"
		 << endl;
		 cout << "1=增加城市" << endl
		 << "2=删除城市" << endl;
	     cout << "选择?";
		 cin  >> i;
	}while ( i!='1' && i!='2' );
	
	if ( i=='1' ) EnterVertex(G);
	if ( i=='2' ) DeleteVertex(G);
}

//增加城市
void EnterVertex(ALGraph *G)
{
	char v[10],c;
	int i;
	cout << endl
		 << "请输入新增城市的名称:";
	cin  >> v;
	i=LocateVertex(G,v);
	if(i>=0&&i<G->vexnum)
	{
		cout << endl
			 << "错误!此城市已存在"
			 << endl;
		return;
	}
	else
	{
		cout << endl
			 << "确认?(Y/N)";
		
		cin  >> c;
		if ( c=='Y'||c=='y' )
		{
			i=G->vexnum;
			strcpy(G->vertices[i].cityname,v);
			G->vertices[i].planefirstarc=NULL;
			G->vertices[i].trainfirstarc=NULL;
			G->vexnum=i+1;
			save(G);
		}
		else return;
	}
}

//删除城市
void DeleteVertex(ALGraph *G)
{
	int i,j,k,n;
	char v[10],c;
	ArcNode *p,*q,*m;
	cout << endl
		 << "请输入删除的城市:"
		 << endl;
	cin >> v;
	cout << endl
		 << "确认?(Y/N)";

	cin  >> c;
	if ( c=='Y'||c=='y' )
	{
		n=0;
		while(n<G->vexnum&&strcmp(G->vertices[n].cityname,v)!=0)
			n++;
		if ( n==G->vexnum )
			cout << endl
				 << "错误!无法找到此城市!"
				 << endl;
		else
		{
			i = LocateVertex(G,v);
			p = G->vertices[i].planefirstarc;
			while ( p!=NULL )
			{
				q = p;
				p = p->nextarc;
				free(q);
			}
			p = G->vertices[i].trainfirstarc;
			while(p!=NULL)
			{
				q = p;
				p = p->nextarc;
				free(q);
			}
			for ( j=i;j<G->vexnum-1;j++ )
			{
				strcpy(G->vertices[j].cityname,G->vertices[j+1].cityname);
				G->vertices[j].planefirstarc=G->vertices[j+1].planefirstarc;
				G->vertices[j].trainfirstarc=G->vertices[j+1].trainfirstarc;
			}
			G->vertices[j].planefirstarc=NULL;
			G->vertices[j].trainfirstarc=NULL;
			for ( k=0;k<G->vexnum-1;k++ )
			{
				p=G->vertices[k].planefirstarc;
				while ( p!=NULL )
				{
					if ( p->adjvex>i )
					{
						p->adjvex=p->adjvex-1;
						q = p;
						p = p->nextarc;
					}
					else if ( p->adjvex==i )
					{
						if ( p==G->vertices[k].planefirstarc )
						{
							m = p;
							G->vertices[k].planefirstarc=p->nextarc;
							p = p->nextarc;
							free(m);
						}
						else
						{
						    q->nextarc=p->nextarc;
							m = p;
							p = p->nextarc;
							free(m);
						}
					}
					else
					{
						q = p;
						p = p->nextarc;
					}
				}
			}
			for ( k=0; k<G->vexnum-1; k++ )
			{
				p = G->vertices[k].trainfirstarc;
				while ( p!=NULL )
				{
					if(p->adjvex>i)
					{
						p->adjvex = p->adjvex-1;
						q = p;
						p = p->nextarc;
					}
					else if ( p->adjvex==i )
					{
						if ( p==G->vertices[k].trainfirstarc )
						{
							m = p;
							G->vertices[k].trainfirstarc=p->nextarc;
							p=p->nextarc;
							free(m);
						}
						else
						{
							q->nextarc=p->nextarc;
							m=p;
							p=p->nextarc;
							free(m);
						}
					}
					else
					{
						q=p;
						p=p->nextarc;
					}
				}
			}
		}
		G->vexnum--;
		save(G);
	}
	else return;
}

//飞机航班编辑项目选择界面
void flightedit(ALGraph *G)
{
	char i;
	do{
		cout << endl 
			 << "请选择飞机航班编辑项目:"
			 << endl;
		cout << "1=新增航班" << endl
			 << "2=删除航班" << endl;
		cout << "选择?";
		cin  >> i;
	}while ( i!='1' && i!='2' );
	
	if ( i=='1' ) EnterplaneArc(G);
	if ( i=='2' ) DeleteplaneArc(G);
}

//列车车次编辑项目选择界面
void trainedit(ALGraph *G)
{
	char i;
	do {
		cout << endl
			 << "请选择列车车次编辑项目:"
			 << endl;
		cout << "1=新增车次" << endl
			 << "2=删除车次" << endl;
		cout << "选择?";
		cin  >> i;
	}while ( i!='1' && i!='2' );
	
	if ( i=='1' ) EntertrainArc(G);
	if ( i=='2' ) DeletetrainArc(G);
}

//增加飞机航班
void EnterplaneArc(ALGraph *G)
{
	int i,j,bt[2],at[2];
	int code;
	float money;
	int m,t;
	char vt[10],vh[10],c;
	ArcNode *p,*q;
	cout << endl
		 << "请输入新增飞机航班的信息:"
		 << 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( "\n到达时间:" );
	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] );				
	}

	cout << endl
		 << "确认?(Y/N)";
	cin  >> c;
	if (c=='Y'||c=='y' )
	{
		i=LocateVertex(G,vt);
		j=LocateVertex(G,vh);
		if ( i==-1 )
		{
			cout << endl
				 << "错误!无法找到起始城市"
				 << endl;
			return;
		}
		if ( j==-1 )
		{
			cout << endl
				 << "错误!无法找到到达城市"
				 << endl;
			return;
		}
		q=G->vertices[i].planefirstarc;
		m=0;
		while(q!=NULL)
		{
			if(q->adjvex==j)
			{
				t=q->info.last+1;
				q->info.stata[t].number=code;
				q->info.stata[t].expenditure=money;
				q->info.stata[t].begintime[0]=bt[0];
				q->info.stata[t].begintime[1]=bt[1];
				q->info.stata[t].arrivetime[0]=at[0];
				q->info.stata[t].arrivetime[1]=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=code;
			p->info.stata[0].expenditure=money;
			p->info.stata[0].begintime[0]=bt[0];
			p->info.stata[0].begintime[1]=bt[1];
			p->info.stata[0].arrivetime[0]=at[0];
			p->info.stata[0].arrivetime[1]=at[1];
			p->info.last=0;
			p->nextarc=G->vertices[i].planefirstarc;
			G->vertices[i].planefirstarc=p;
			G->planearcnum++;
		}
		save(G);
	}
	else return;
}

//增加列车车次
void EntertrainArc(ALGraph *G)
{
	int i,j,bt[2],at[2];
	int code;
	float money;
	int m,t;
	char vt[10],vh[10],c;
	ArcNode *p,*q;
	cout << endl
		 << "请输入新增列车车次的信息:"
		 << endl;
	cout << "列车车次编号:";
	cin  >> code;
	
	cout << "起始城市:";
	cin  >> vt;
	cout << "目的城市:";
	cin  >> vh;
	cout << "车次费用:";
	cin  >> money;
	
	printf( "\n发车时间:" );
	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] );		
	}
	
	cout << endl
		 <<"确认?(Y/N)";
	cin  >> c;
	if ( c=='Y' || c=='y' )
	{
		i = LocateVertex(G,vt);
		j = LocateVertex(G,vh);
		if ( i==-1 )
		{ 
			cout << endl
				 << "错误!无法找到起始城市"
				 << endl;
			return;
		}
		if ( j==-1 )
		{
			cout << endl
				 << "错误!无法找到到达城市"
				 << endl;
			return;
		}
		q=G->vertices[i].trainfirstarc;
		m=0;
		while(q!=NULL)
		{
			if(q->adjvex==j)
			{
				t=q->info.last+1;
				q->info.stata[t].number=code;
				q->info.stata[t].expenditure=money;
				q->info.stata[t].begintime[0]=bt[0];
				q->info.stata[t].begintime[1]=bt[1];
				q->info.stata[t].arrivetime[0]=at[0];
				q->info.stata[t].arrivetime[1]=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=code;
			p->info.stata[0].expenditure=money;
			p->info.stata[0].begintime[0]=bt[0];
			p->info.stata[0].begintime[1]=bt[1];
			p->info.stata[0].arrivetime[0]=at[0];
			p->info.stata[0].arrivetime[1]=at[1];
			p->info.last=0;
			p->nextarc=G->vertices[i].trainfirstarc;
			G->vertices[i].trainfirstarc=p;
			G->trainarcnum++;
		}
		save(G);
	}
	else return;
}

//删除飞机航班        
void DeleteplaneArc(ALGraph *G)
{
	int i,j;
	int code;
	char vt[10],vh[10],c;
	int n;
	int k;
	ArcNode *p,*q;
	
	cout << endl
		 << "请输入删除飞机航班的信息:"
		 << endl;
	cout << "飞机航班的编号:";
	cin  >> code;
	
	cout << "起始城市:";
	cin  >> vt;
	cout << "目的城市:";
	cin  >> vh;
	cout << endl
		 << "确认?(Y/N)";
	cin  >> c;
	if(c=='Y'||c=='y')
	{
		i=LocateVertex(G,vt);
		j=LocateVertex(G,vh);
		if ( i==-1 )
		{
			cout << endl
				 << "错误!无法找到起始城市"
				 << endl;
			return;
		}
		if ( j==-1 )
		{
			cout << endl
				 << "错误!无法找到目的城市"
				 << endl;
			return;
		}
		p=G->vertices[i].planefirstarc;
		q=p;
		while(p!=NULL)
		{
			if(p->adjvex==j)
			{
				n=-1;
				for ( k=0;k<=p->info.last;k++ )
				{
					if(p->info.stata[k].number==code)
					{
						n = k;
						break;
					}
				}
				if ( n!=-1 )
				{
					if ( p->info.last==0 )
					{ 
						if ( q==p )
							G->vertices[i].planefirstarc=p->nextarc;
						else
							q->nextarc=p->nextarc;
						free(p);
					}
					else
					{
						for(k=n;k<p->info.last;k++)
						{
							p->info.stata[k].number=p->info.stata[k+1].number;
							p->info.stata[k].expenditure=p->info.stata[k+1].expenditure;
							p->info.stata[k].begintime[0]=p->info.stata[k+1].begintime[0];
							p->info.stata[k].begintime[1]=p->info.stata[k+1].begintime[1];
							p->info.stata[k].arrivetime[0]=p->info.stata[k+1].arrivetime[0];
							p->info.stata[k].arrivetime[1]=p->info.stata[k+1].arrivetime[1];
						}
						p->info.last=p->info.last-1;
					}
				}
				else cout << endl
						  << "在此两城市之间无法找到No."
						  << code
						  << "飞机航班"
						  << endl;
				save(G);
				return;
			}
			q = p;
			p = p->nextarc;
		}
		if ( p==NULL) cout << endl
						   << "在此两城市之间无飞机航班存在"
						   << endl;
	}
	else return;
}

//删除列车车次
void DeletetrainArc(ALGraph *G)
{
	int i,j;
	int code;
	char vt[10],vh[10],c;
	int n;
	int k;
	ArcNode *p,*q;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -