数据结构实验公交车系统(完整代码私信)
1.查询公交车信息
2.查询站点信息
3.查询两个站点之间的路线(最多一次换乘)
4.添加、删除、修改公交车,站点,路线
创建4个文本文档(即txt)
routes:编号+站点(按顺序输入站点编号)+距离
stations:按顺序自定义站名
buses:按顺序写明每个bus经过的站点
bus_name:例如no.1 up,no.1 down
routes:记录路线数,公交车线路存在往返,所以一条边记为两条路线
station:建议写字母或英文
buses:写出所有车的路线(包含往返)
bus_name:每辆车都要写往返,即上行和下行,对应buses中的顺序
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100
int STATION_NUM;//站点总数
char *STATION[MAX];//存储站点名称
int ROUTE_NUM;//路段总数
int ROUTES[MAX][4];//存储路段信息
int BUS_NUM;//公交线路总数
char *BUS_NAME[MAX];//存储公交线路名称
int BUSES[MAX][3];//存储公交线路信息
void Read1()//从文件中读取路段信息保存到ROUTES数组
{
FILE *fp;
fp= fopen("routes.txt","r+");
if(fp==NULL){
printf("打开文件失败!\n");
return;
}
fscanf(fp,"%d",&ROUTE_NUM);
int i;
for(i=0;i<ROUTE_NUM;i++)
fscanf(fp,"%d%d%d%d",&ROUTES[i][0],&ROUTES[i][1],&ROUTES[i][2],&ROUTES[i][3]);
fclose(fp);
}
void Read2()//从文件中读取站点名称保存到STATION数组
{
FILE *fp;
fp= fopen("stations.txt","r+");
if(fp==NULL){
printf("打开文件失败!\n");
return;
}
char str[30];
int i=0,len;
while(fgets(str,30,fp)!=NULL){
len=strlen(str);
if(str[len-1]=='\n')
str[len-1]='\0';
char *pstr=(char*)malloc((len+1)*sizeof(char));
strcpy(pstr,str);
STATION[i]=pstr;//直接等于str为什么不行
i++;
}
STATION_NUM=i;
fclose(fp);
}
void Read3()//从文件中读取公交信息保存到BUSES数组
{
FILE *fp;
fp=fopen("buses.txt","r+");
if(fp==NULL){
printf("打开文件失败!\n");
return;
}
fscanf(fp,"%d",&BUS_NUM);
int i;
for(i=0;i<BUS_NUM;i++){
fscanf(fp,"%d%d%d",&BUSES[i][0],&BUSES[i][1],&BUSES[i][2]);
}
fclose(fp);
}
void Read4()//从文件中读取公交名称保存到BUS_NAME数组
{
FILE *fp;
fp= fopen("bus_name.txt","r+");
if(fp==NULL){
printf("打开文件失败!\n");
return;
}
char str[30];
int i=0,len;
while(fgets(str,30,fp)!=NULL){
len=strlen(str);
if(str[len-1]=='\n')
str[len-1]='\0';
char *pstr=(char*)malloc((len+1)*sizeof(char));
strcpy(pstr,str);
BUS_NAME[i]=pstr;
i++;
}
fclose(fp);
//for(i=0;i<BUS_NUM;i++)
// printf("%s\n",BUS_NAME[i]);
}
void ReadFile()//读取文件内容保存在数组
{
Read1();
Read2();
Read3();
Read4();
}
完整代码移步:https://ttaozhi.com/t/t/newshop.html?id=AxAHVv8XJ8&share=L3MGNPop