这个程序怎么改才对
#include<stdio.h>#include<malloc.h>
#include <stdlib.h>
struct op
{
char name[20];
int age;
char sex;
};
typedef struct op opp;
opp a,*b=&a;
struct lk
{
char name[20];
int age;
char sex;
struct lk*next;
};
typedef struct lk lkk;
lkk* del(lkk*head)
{
char a[20];
lkk*d,*dd;
if(head==0)
{
printf(" no data for find\n");
getch();
exit(0);
}
d=dd=head;
printf("input the name to find\n");
do
{
fflush(stdin);
scanf("%s",a);
if(strlen(a)>19)
printf(" you put too long\n");
}while(strlen(a)>19);
while(dd!=0)
{
if(strcmp(dd->name,a)==0)
{
printf("%s\t%d\t%c\n",dd->name,dd->age,dd->sex);
getch();
printf("the date is del!\n");
break;
}
d=dd;
dd=dd->next;
if(dd==0)
{
printf("no find\n");
getch();
exit(0);}
}
if(dd==head)
{
head=head->next;
free(dd);
}
else
{
d->next=dd->next;
free(dd);}
return(head);
}
lkk*vget(FILE*fp)
{
lkk*head,*p,*q;
p=(lkk*)malloc(sizeof(lkk));
if(p==0)
{
printf("error bnbn\n");
getch();
exit(0);
}
fread(p,sizeof(opp),1,fp);
head=p;
p->next=0;
fseek(fp,sizeof(opp),0);
while(!feof(fp))
{
q=(lkk*)malloc(sizeof(lkk));
if(q==0)
{
printf("error nnnn\n");
getch();
exit(0);
}
fread(q,sizeof(opp),1,fp);
p->next=q;
q->next=0;
p=q;
}
return(head);
}还是这里有问题???
lkk*creat(int n)
{
lkk*head,*p,*q;
int i;
p=(lkk*)malloc(sizeof(lkk));
if(p==0)
{
printf("error\n");
getch();
exit(0);
}
head=p;
printf("input the 1 stdunt date\n");
do
{
printf("name:");
scanf("%s",p->name);
i=strlen(p->name);
if(i>19)
printf("too long\n");
}while(i>19);
do
{
fflush(stdin);
printf("age:");
scanf("%d",&p->age);
if(p->age<0)
printf("error\n");
}while(p->age<0);
do
{
printf("sex: b for boy g for gile\n");
while(getchar()!='\n')
;
scanf("%c",&p->sex);
if(p->sex!='b'&&p->sex!='g')
printf("input error\n");
}while(p->sex!='b'&&p->sex!='g');
p->next=0;
for(i=1;i<n;i++)
{
if(n==1)
break;
q=(lkk*)malloc(sizeof(lkk));
if(q==0)
{
printf("error error\n");
getch();
exit(0);
}
p->next=q;
printf("input the %d stdunt date\n",i+1);
do
{
printf("name:");
scanf("%s",q->name);
i=strlen(q->name);
if(i>19)
printf("too long\n");
}while(i>19);
do
{
printf("age:");
scanf("%d",&q->age);
if(q->age<0)
printf("error\n");
}while(q->age<0);
do
{
getchar();
printf("sex: b for boy g for gile\n");
scanf("%c",&q->sex);
if(q->sex!='b'&&q->sex!='g')
printf("input error\n");
}while(q->sex!='b'&&q->sex!='g');
q->next=0;
p=q;
}
return(head);
}
void print(lkk*head)
{
if(head==0)
{
printf("no data\n");
getch();
exit(0);
}
while(head!=0)
{
printf("%s\t%d\t%c\n",head->name,head->age,head->sex);
head=head->next;
}
}
main()
{
FILE*fp;
lkk*head=0,*p,*d;
int i,j,k;
char c,cc,ccc,a[20];
lk:do
{
printf("1.---------- input -----------\n");
printf("2.---------- find -----------\n");
printf("3.---------- del -----------\n");
printf("4.---------- save -----------\n");
printf("5.---------- exit -----------\n");
printf("7.---------- open -----------\n");
printf("\n\n\n");
printf("6.---------input 1-5 to contuie--------\n");
fflush(stdin);
c=getchar();
getchar();
switch(c)
{
case '1':printf("input the stdunt numbers\n");scanf("%d",&i);head=creat(i);
getchar();p=head;break;
case '2': printf("1.|------find age ----------|\n");
printf("2.|------find sex ----------|\n");
printf("3.|------find age and sex---|\n");
printf("4.|------ find name -------|\n");
fflush(stdin);
cc=getchar();
getchar();d=head;
if(head==0)
{
printf("head==0\n");
getch();
exit(0);
}
switch(cc)
{
case '1':printf("the age:");scanf("%d",&j);
head=p;
d=head;
while(d!=0)
{
if(d->age==j)
{
printf("name:%s\tage:%d\tsex:%c\n",d->name,d->age,d->sex);
}
d=d->next;
}
break;
case '2':printf("the sex:");fflush(stdin);scanf("%c",&cc);
head=p;
d=head;
while(d!=0)
{
if(d->sex==cc)
{
printf("name:%s\tage:%d\tsex:%c\n",d->name,d->age,d->sex);
}
d=d->next;
}
break;
case '4':printf("input the name\n");fflush(stdin);
do
{
scanf("%s",a);
if(strlen(a)>19)
{
printf(" out\n");
}
fflush(stdin);
}while(strlen(a)>19);
while(head!=0)
{
if(strcmp(head->name,a)==0)
printf("%s\t%d\t%c\n",head->name,head->age,head->sex);
head=head->next;
if(head==0)
{
printf(" no find\n");
getch();
break;}
}
break;
default:printf("error error\n");getch();
}break;
case '5':exit(0);
case '4':
printf("input the file\n");
scanf("%s",a);
fp=fopen(a,"wb");
if(fp==0)
{
printf("can not open\n");
getch();
exit(0);
}
if(head==0)
{
printf("no date to find\n");
getch();
goto lk;
}
while(head!=0)
{
strcpy(b->name,head->name);
b->age=head->age;
b->sex=head->sex;
fwrite(b,sizeof(opp),1,fp);
head=head->next;难道说这里错了???
}
printf("date is save!\n");
fclose(fp);
break;
case '7':printf("input the file to open\n");
scanf("%s",a);
fp=fopen(a,"rb+");
if(fp==0)
{
printf("cannot open files\n");
getch();
goto lk;
}
p=head=vget(fp);调用VGET打开一个文件
printf("now type the file\n");
getch();
print(head);显示出的后面有乱码
break;
case '3':
head=del(head);
break;
default:printf("error rrrr\n");
}
}while(1);
}