#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=0i<扰宏坦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=0i<ni++)
{fscanf(fp,"%ld",&temp[i].num)
printf("%ld ",temp[i].num)
for(j=0j<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=0j<nj++)
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=0j<nj++)
{if(temp[j].num==num)
{ for(i=0i<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=0i<ni++)
{fprintf(fp,"%ld",temp[i].num)
for(j=0j<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=0i<ni++)
{fscanf(fp,"%ld",&temp[i].num)
printf("%ld",temp[i].num)
for(j=0j<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=0i<ni++)
{
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=0i<ni++)
{
if(temp[i].num!=0)
{
fprintf(fp,"%ld",temp[i].num)
printf("%ld",temp[i].num)
for(j=0j<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=0i<ni++)
{fscanf(fp,"%ld",&temp[i].num)
printf("%ld ",temp[i].num)
for(j=0j<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=0i1<ni1++)
{if(temp[i1].num==num)
break
}
if(i1<n)
{ printf("%ld",temp[i1].num)
for(j1=0j1<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=0i<ni++)
{fscanf(fp,"%ld",&temp[i].num)
printf("%ld ",temp[i].num)
for(j=0j<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=0i<ni++)
for(j=0j<aa[i]j++)
if(strcmp(temp[i].stage[j],a)==0)
{printf("%ld",temp[i].num)
for(t=0t<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=0i<ni++)
{fscanf(fp,"%ld",&temp[i].num)
printf("%ld ",temp[i].num)
for(j=0j<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=0i<ni++)
for(j=0j<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=firt<=sect++)
{ 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=0i<20i++)
aa[i]=0
}
else
{fscanf(fp1,"%d",&n)
for(i=0i<20i++)
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=0i<20i++)
if(aa[i]>0)
{fprintf(fp1," %d",aa[i])
count++
}
if(count<20)
for(i=counti<20i++)
fprintf(fp1,"%d",0)
}
fclose(fp1)
}
main()
{int choose
char yes_no
fileoout()
do
{clrscr()
printf("\t******************************\n")
printf("\tplease 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("\tinput 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()
}
#include<stdio.h>double ROUTE=0.0
double CONVERT(int mile)
{
double km
km=1.609344*mile
return km
}
//定义BUS函数
double BUS(int way)
{
double kmWay
kmWay=CONVERT(way)
printf("裂雀携by Bus:%f km\n",kmWay)
return kmWay
}
//定义肆伏WALK函数
double WALK(int way)
{
double kmWay
kmWay=CONVERT(way)
printf("by Walk:%f km\n",kmWay)
return kmWay
}
//主函数
void main()
{
int chose,way
printf("Please choose the way to travel!\n==============================\n1:by bus\n2:by walk\n0:quit\n==============================\n")
while(1)
{
if(scanf("%d",&chose)==1&&chose>=1&&chose<岁芹=2)
{
printf("Please input miles : ")
scanf("%d",&way)
switch(chose)
{
case 1:
printf("The current route KM is: %f km\n",ROUTE+BUS(way))
break
case 2:
printf("The current route KM is: %f km\n",ROUTE+WALK(way))
break
}
}
else if(chose!=0)
printf("Sorry!You input errors!\n")
else
exit(0)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)