求公交线路查询系统的c语言程序

需要有查询删除等功能... 需要有查询 删除等功能 展开
 我来答
百度网友53a90be85
2010-05-20 · TA获得超过238个赞
知道答主
回答量:101
采纳率:0%
帮助的人:0
展开全部
这是我写的程序 希望能给你参考
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define NULL 0
int n=0;
int aa[20]={0};
struct road
{long num;
char stage[20][20];
struct road *next;
};
struct road *create()
{struct road *head,*p1,*p2;
int m,i;
char yes_no;
FILE *fp,*fp1;
fp=fopen("d:\\nizaoran.txt","w");
if(fp==NULL)
{printf("file cant be open\n");
exit(1);
}
fp1=fopen("d:\\record.txt","w");
if(fp1==NULL)
{printf("file can't be open\n");
exit(1);
}
head=NULL;
do
{p1=(struct road*)malloc(LEN);
printf("please input %d num:",n+1);
scanf("%ld",&p1->num);
fprintf(fp,"%ld",p1->num);
printf("how many stages?");
scanf("%d",&aa[n]);
fprintf(fp1,"%d\n",aa[n]);
for(i=0;i<aa[n];i++)
{getchar();
printf("please input the %d station:",i+1);
gets(p1->stage[i]);
fprintf(fp," %s",&p1->stage[i]);
}
fputc('\n',fp);
p1->next=NULL;
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
printf("do you want to continue (Y/N)?");
scanf("%c",&yes_no);
}while(yes_no=='y'||yes_no=='Y');
fclose(fp);
free (p1);
return head;
}
void add()
{
struct road temp;
int i=0;
FILE *fp;
fp=fopen("d:\\nizaoran.txt","a");
printf("input the route you want to add:");
scanf("%ld",&temp.num);
fprintf(fp,"%ld",temp.num);
printf("please input how many stages:");
scanf("%d",&aa[n]);
while(i<aa[n])
{getchar();
printf("please input the %d stage\n",i+1);
gets(temp.stage[i]);
fprintf(fp," %s",temp.stage[i]);
i++;
}
fprintf('\n',fp);
n++;
fclose(fp);
}

void display()
{FILE *fp;
char ch;
if((fp=fopen("d:\\nizaoran.txt","r"))==NULL)
{printf("cant open file\n");
exit(1);
}
while(!feof(fp))
{ch=fgetc(fp);
printf("%c",ch);
}
fclose(fp);
}

void modify()
{ FILE *fp;
struct road temp[20];
char a[6],b[20],ch;
int i=0,j=0,k=0,m=0,t=0;
long num;
if((fp=fopen("d:\\nizaoran.txt","r"))==NULL)
{printf("cant open file\n");
exit(1);
}
for(i=0;i<n;i++)
{fscanf(fp,"%ld",&temp[i].num);
printf("%ld ",temp[i].num);
for(j=0;j<aa[i];j++)
{fscanf(fp,"%s",temp[i].stage[j]);
putchar(' ');
puts(temp[i].stage[j]);
}
fgetc(fp);
}
fclose(fp);
printf("which do you want to modify(num/stage)?");
getchar();
gets(a);
if(strcmp("num",a)==0)
{printf("which num:");
scanf("%ld",&num);
if((fp=fopen("d:\\nizaoran.txt","w"))==NULL)
{printf("cant open file\n");
exit(1);
}
for(j=0;j<n;j++)
if(temp[j].num==num)
{printf("please input the right num:");
scanf("%ld",&temp[j].num);
break;
}
if(j==n) printf("no find\n");
}

else if(strcmp("stage",a)==0)
{if((fp=fopen("d:\\nizaoran.txt","w"))==NULL)
{printf("cant open file\n");
exit(1);
}
printf("the num and the stage:");
scanf("%ld%s",&num,b);
for(j=0;j<n;j++)
{if(temp[j].num==num)
{ for(i=0;i<aa[j];i++)
if(strcmp(temp[j].stage[i],b)==0)
{printf("please input the right stage\n");
getchar();
gets(temp[j].stage[i]);
break;
}
if(i==aa[j])
printf("no fould");
break;
}
}
if(j==n)
printf("no found\n");
}
for(i=0;i<n;i++)
{fprintf(fp,"%ld",temp[i].num);
for(j=0;j<aa[i];j++)
fprintf(fp," %s",temp[i].stage[j]);
fputc('\n',fp);
}
fclose(fp);
}
void delete()
{FILE *fp;
long num;
struct road temp[20];
int i=0,j=0;
if((fp=fopen("d:\\nizaoran.txt","r"))==NULL)
{printf("cant open file\n");
exit(1);
}
for(i=0;i<n;i++)
{fscanf(fp,"%ld",&temp[i].num);
printf("%ld",temp[i].num);
for(j=0;j<aa[i];j++)
{fscanf(fp,"%s",temp[i].stage[j]);
putchar(' ');
puts(temp[i].stage[j]);
}
fgetc(fp);
}
fclose(fp);
printf("input the num you want to delete\n");
scanf("%ld",&num);
for(i=0;i<n;i++)
{
if(temp[i].num==num)
break;
}
if(i==n)
{printf("no found\n");
return;
}
else
{
temp[i].num=0;
}
fp=fopen("d:\\nizaoran.txt","w");
for(i=0;i<n;i++)
{
if(temp[i].num!=0)
{
fprintf(fp,"%ld",temp[i].num);
printf("%ld",temp[i].num);
for(j=0;j<aa[i];j++)
{fprintf(fp," %s",temp[i].stage[j]);
putchar(' ');
puts(temp[i].stage[j]);
}
fputc('\n',fp);
putchar('\n');
}
}
fclose(fp);
n--;
printf("\n%d\n",n);
}

void search_num()
{
FILE *fp;
struct road temp[20];
int i,j,i1=0,j1=0;
long num;
if((fp=fopen("d:\\nizaoran.txt","r"))==NULL)
{printf("cant open file\n");
exit(1);
}
for(i=0;i<n;i++)
{fscanf(fp,"%ld",&temp[i].num);
printf("%ld ",temp[i].num);
for(j=0;j<aa[i];j++)
{fscanf(fp,"%s",temp[i].stage[j]);
putchar(' ');
puts(temp[i].stage[j]);
}
fgetc(fp);
}
fclose(fp);
printf("please input the number you want to search\n");
scanf("%ld",&num);
for(i1=0;i1<n;i1++)
{if(temp[i1].num==num)
break;
}
if(i1<n)
{ printf("%ld",temp[i1].num);
for(j1=0;j1<aa[i1];j1++)
{putchar(' ');
puts(temp[i1].stage[j1]);
}
putchar('\n');
}
else printf("no found\n");
}
void search_stage()
{
FILE *fp;
struct road temp[20];
int i,j,t,flag=0;
char a[20];
if((fp=fopen("d:\\nizaoran.txt","r"))==NULL)
{printf("cant open file\n");
exit(1);
}
for(i=0;i<n;i++)
{fscanf(fp,"%ld",&temp[i].num);
printf("%ld ",temp[i].num);
for(j=0;j<aa[i];j++)
{fscanf(fp,"%s",temp[i].stage[j]);
putchar(' ');
puts(temp[i].stage[j]);
}
fgetc(fp);
}
fclose(fp);
printf("please input the stage you want to search\n");
getchar();
gets(a);
for(i=0;i<n;i++)
for(j=0;j<aa[i];j++)
if(strcmp(temp[i].stage[j],a)==0)
{printf("%ld",temp[i].num);
for(t=0;t<aa[i];t++)
{putchar(" ");
puts(temp[i].stage[t]);
}
putchar('\n');
flag=1;
}
if(flag==0) printf("no found\n");
}

void search_route()
{
FILE *fp;
struct road temp[20];
int i,j,first=0,second=0,fir=0,sec=0;
char a[20],b[20];
if((fp=fopen("d:\\nizaoran.txt","r"))==NULL)
{printf("cant open file\n");
exit(1);
}
for(i=0;i<n;i++)
{fscanf(fp,"%ld",&temp[i].num);
printf("%ld ",temp[i].num);
for(j=0;j<aa[i];j++)
{fscanf(fp,"%s",temp[i].stage[j]);
putchar(' ');
puts(temp[i].stage[j]);
}
fgetc(fp);
}
fclose(fp);
printf("please input the first stage\n");
getchar();
gets(a);
printf("please input the second stage\n");
getchar();
gets(b);

for(i=0;i<n;i++)
for(j=0;j<aa[i];j++)
{if(strcmp(temp[i].stage[j],a)==0)
{first=i;
fir=j;
}

if(strcmp(temp[i].stage[j],a)==0)
{second=i;
sec=j;
}
}
if(first==second)
{if(fir>sec)
{t=fir;
fir=sec;
sec=t;
}
printf("%ld",temp[first].num);
for(t=fir;t<=sec;t++)
{ putchar(" ");
puts(temp[fisrt].stage[t]);
}
}
else printf("not the same routine\n");
}
void search()
{int i;
printf("which way do you want(1,2,3):\n");
scanf("%d",&i);
switch(i)
{case 1 : search_num();break;
case 2 : search_stage();break;
case 3 : search_route();break;
}
}
void fileout()
{FILE fp1;
int i;
if((fp1=fopen("d:\\recoord.txt","r"))==NULL);
{n=0;
for(i=0;i<20;i++)
aa[i]=0;
}
else
{fscanf(fp1,"%d",&n);
for(i=0;i<20;i++)
fscanf(fp1," %d",&aa[i]);
}
fclose(fp1);
}

void filein()
{FILE fp1;
int i,count=0;
if((fp1=fopen("d:\\recoord.txt","r"))==NULL)
{printf("file cant be opened\n");
exit(1);
}
else {fprintf(fp1,"%d",n);
for(i=0;i<20;i++)
if(aa[i]>0)
{fprintf(fp1," %d",aa[i]);
count++;
}
if(count<20)
for(i=count;i<20;i++)
fprintf(fp1,"%d",0);

}

fclose(fp1);

}

main()
{int choose;
char yes_no;
fileoout();
do
{clrscr();
printf("\t ******************************\n");
printf("\t please input your choose(0~6):\n");
printf("\t ******************************\n");
printf("\t ********1-create route********\n");
printf("\t ********2-display route*******\n");
printf("\t ********3-search route********\n");
printf("\t ********4-modify route********\n");
printf("\t ********5-add route***********\n");
printf("\t ********6-delete route********\n");
printf("\t ********0-exit****************\n");
printf("\t input your choose:");
scanf("%d",&choose);
printf("\n");
switch(choose)
{case 1 : create();break;
case 2 : display();break;
case 3 : search();break;
case 4 : modify();break;
case 5 : add();break;
case 6 : delete();break;
case 0 : exit(0);
default: printf("error");
}
getchar();
printf("\n do you want to continue to choose menu (y/n)?\n");
do
{ yes_no=getchar();
}while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}while(yes_no=='Y'||yes_no=='y');

filiin();

}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式