#include<stdio.h>#include<string.h>#include<stdlib.h>#include <conio.h>#define N 100 struct subway//定义一个结构体 { char num[N]; char line[N];//地铁线路 char sstation[N];//起始站 char ostation[N];//终点站 char wstation[N];//途径站数 char charge[N];//票价 }sub[N];//给结构体起别名sub; int k=1,n,m; char readfile();//文件读取 void save();//将信息保存至文件 void insert();//增加地铁信息 void Delete();//删除地铁信息 void Search();//查询地铁信息 void Print();//浏览地铁信息 void Update();//更新地铁信息 void menu();//主菜单调用其他函数 int main(){ while(k) menu(); system("pause"); return 0;} char readfile(){ FILE *fp;//要读取的文件指针 int i=0;//数组移动 if ((fp=fopen("subway.txt","r"))==NULL)//打开操作不成功 { printf("Open file is error!"); system("pause"); exit(0); }
时间: 2024-01-04 09:03:19 浏览: 124
#include<stdio.h>
//文件打开成功
while(!feof(fp)){//feof函数判断是否到达文件末尾
fscanf(fp,"%s",sub[i].num);//读取地铁编号
fscanf(fp,"%s",sub[i].line);//读取地铁线路
fscanf(fp,"%s",sub[i].sstation);//读取起始站
fscanf(fp,"%s",sub[i].ostation);//读取终点站
fscanf(fp,"%s",sub[i].wstation);//读取途径站数
fscanf(fp,"%s",sub[i].charge);//读取票价
i++;
}
fclose(fp);//关闭文件
return 'r';//返回'r'表示读取成功
}
void save(){
FILE *fp;//要写入的文件指针
int i;
if ((fp=fopen("subway.txt","w"))==NULL)//打开操作不成功
{
printf("Open file is error!");
system("pause");
exit(0);
}
for(i=0;i<n;i++){
fprintf(fp,"%s %s %s %s %s %s\n",sub[i].num,sub[i].line,sub[i].sstation,sub[i].ostation,sub[i].wstation,sub[i].charge);//将信息写入文件中
}
fclose(fp);//关闭文件
printf("Save successfully!\n");
}
void insert(){
printf("Please input the number of subway:");
scanf("%s",sub[n].num);
printf("Please input the line of subway:");
scanf("%s",sub[n].line);
printf("Please input the start station of subway:");
scanf("%s",sub[n].sstation);
printf("Please input the end station of subway:");
scanf("%s",sub[n].ostation);
printf("Please input the passing stations of subway:");
scanf("%s",sub[n].wstation);
printf("Please input the charge of subway:");
scanf("%s",sub[n].charge);
n++;//增加地铁信息数量
printf("Insert successfully!\n");
}
void Delete(){
int i,j;
char num[N];//要删除的地铁编号
printf("Please input the number of subway you want to delete:");
scanf("%s",num);
for(i=0;i<n;i++){
if(strcmp(num,sub[i].num)==0){//查找到要删除的地铁信息
for(j=i;j<n-1;j++){
sub[j]=sub[j+1];//将后面的信息前移
}
n--;//减少地铁信息数量
printf("Delete successfully!\n");
return;
}
}
printf("Can't find the subway information!\n");
}
void Search(){
int i;
char num[N];//要查询的地铁编号
printf("Please input the number of subway you want to search:");
scanf("%s",num);
for(i=0;i<n;i++){
if(strcmp(num,sub[i].num)==0){//查找到要查询的地铁信息
printf("Number:%s\tLine:%s\tStart Station:%s\tEnd Station:%s\tPassing Stations:%s\tCharge:%s\n",sub[i].num,sub[i].line,sub[i].sstation,sub[i].ostation,sub[i].wstation,sub[i].charge);
return;
}
}
printf("Can't find the subway information!\n");
}
void Print(){
int i;
for(i=0;i<n;i++){
printf("Number:%s\tLine:%s\tStart Station:%s\tEnd Station:%s\tPassing Stations:%s\tCharge:%s\n",sub[i].num,sub[i].line,sub[i].sstation,sub[i].ostation,sub[i].wstation,sub[i].charge);
}
}
void Update(){
int i;
char num[N];//要更新的地铁编号
printf("Please input the number of subway you want to update:");
scanf("%s",num);
for(i=0;i<n;i++){
if(strcmp(num,sub[i].num)==0){//查找到要更新的地铁信息
printf("Please input the new line of subway:");
scanf("%s",sub[i].line);
printf("Please input the new start station of subway:");
scanf("%s",sub[i].sstation);
printf("Please input the new end station of subway:");
scanf("%s",sub[i].ostation);
printf("Please input the new passing stations of subway:");
scanf("%s",sub[i].wstation);
printf("Please input the new charge of subway:");
scanf("%s",sub[i].charge);
printf("Update successfully!\n");
return;
}
}
printf("Can't find the subway information!\n");
}
void menu(){
printf("******************************************\n");
printf("* Subway System *\n");
printf("* *\n");
printf("* 1.Read file *\n");
printf("* 2.Save *\n");
printf("* 3.Insert *\n");
printf("* 4.Delete *\n");
printf("* 5.Search *\n");
printf("* 6.Print *\n");
printf("* 7.Update *\n");
printf("* 0.Exit *\n");
printf("* *\n");
printf("******************************************\n");
printf("Please input the number of function you want to execute:");
scanf("%d",&m);
switch(m){
case 1:if(readfile()=='r') printf("Read file successfully!\n");break;
case 2:save();break;
case 3:insert();break;
case 4:Delete();break;
case 5:Search();break;
case 6:Print();break;
case 7:Update();break;
case 0:k=0;break;
default:printf("Input error! Please input again!\n");break;
}
}
阅读全文