最后求助……求大神修改
#include<stdio.h>#include<stdlib.h>
#define len sizeof(struct student)
int n=0;//用于计算元素个数
FILE *fp;
struct student
{
int num;
char name;
char sex;
int age;
float score;
float average;
struct student * next;
};
void save(struct student* head)
{
struct student *p;
char name;
printf("输入储存路径及文件名:");
scanf("%s",name);
fp=fopen(name,"w+");
if(fp==NULL)
{
printf("open error");
exit(1);
}
else
{
p=head;
while(p!=NULL)
{
if(fwrite(p,len,1,fp)!=1)
{
printf("不能保存文件");
exit(1);
}
p=p->next;
}
fclose(fp);
}
printf("保存成功!");
}
struct student*load()
{
n=0;
char name;
FILE *fp;
struct student *p1,*p2,*head;
head=NULL;
printf("请输入文件名:");
scanf("%s",name);
fp=fopen(name,"rb+");
p2=p1=(struct student*)malloc(len);
if(fp==NULL)
{
printf("文件不存在");
return 0;
}
else
{
p1=(struct student*)malloc(len);
fread(p1,len,1,fp);
while(!feof(fp))
{
n++;
if(1==n)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student*)malloc(len);
fread(p1,len,1,fp);
}
p2->next=NULL;
fclose(fp);
printf("读取成功!!");
return head;
}
}
struct student *C()//创建链表
{
int i=0;
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(len);
head=NULL;
printf("please enter the num:");
scanf("%d",&p1->num);fflush(stdin);//fflush(stdin)用于清空缓冲
printf("please enter the name:");
scanf("%s",p1->name);fflush(stdin);
printf("please enter the sex(M/F):");
scanf("%c",&p1->sex);fflush(stdin);
printf("please enter the age:");
scanf("%d",&p1->age);fflush(stdin);
for(i=0;i<5;i++)
{
printf("enter the %d score",i+1);
scanf("%f",&(p1->score));fflush(stdin);
}
p1->average=(p1->score+p1->score+p1->score+p1->score+
p1->score)/5.0;
while(1)
{
n++;
if(1==n)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(len);
printf("please enter the num:");
scanf("%d",&p1->num);fflush(stdin);
if(p1->num==0)
{
p2->next=NULL;
return head;
}
printf("please enter the name:");
scanf("%s",p1->name);fflush(stdin);
printf("please enter the sex(M/F):");
scanf("%c",&p1->sex);fflush(stdin);
printf("please enter the age:");
scanf("%d",&p1->age);fflush(stdin);
for(i=0;i<5;i++)
{
printf("enter the %d score",i+1);
scanf("%d",&(p1->score));fflush(stdin);
}
p1->average=(p1->score+p1->score+p1->score+p1->score+
p1->score)/5.0;
}
}
void print(struct student * head)//打印函数
{
int i=0;
struct student *p;
printf("there are %d records!\n\n",n);
p=head;
if(head)
{
do
{
printf(" the num:%d \n the name:%s\n the sex:%c\n the age:%d\n",
p->num,p->name,p->sex,p->age);
printf("the score:");
for(i=0;i<5;i++)
{
printf("%.1f ",p->score);
}
printf("\nthe average:%f\n",p->average);
p=p->next;
}while(p);
}
}
struct student *D(struct student* head,int num,int *p)//D操作 (删除)
{
struct student* p1,*p2;
if(NULL==head)
{
printf("this is a NULL list\n");
goto END;
}
p1=head;
while(p1->num!=num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
{
head=p1->next;
}
else
{
p2->next=p1->next;
}
printf("delete succeed!");
*p=1;
n=n-1;
}
else
{
printf("NUM:%d is not founded",num);
*p=0;
}
END:
return head;
}
struct student *A(struct student *head)//A操作 (增加)
{
struct student *p0,*p1;
int i=0;
p0=head;
while(p0->next)//当p0->next=NULL
{
p0=p0->next;
}
p1=(struct student*)malloc(len);
p0->next=p1;
printf("please enter the num:");
scanf("%d",&p1->num);fflush(stdin);
printf("please enter the name:");
scanf("%s",p1->name);fflush(stdin);
printf("please enter the sex(M/F):");
scanf("%c",&p1->sex);fflush(stdin);
printf("please enter the age:");
scanf("%d",&p1->age);fflush(stdin);
for(i=0;i<5;i++)
{
printf("enter the %d score",i+1);
scanf("%f",&(p1->score));fflush(stdin);
}
p1->average=(p1->score+p1->score+p1->score+p1->score+
p1->score)/5.0;
p1->next=NULL;
n++;
return head;
}
struct student* M(struct student *head,int num,int *p)//M操作 (修改)
{
struct student *p0;
int i=0;
p0=head;
while(p0->num!=num&&p0->next!=NULL)
{
p0=p0->next;
}
if(p0->num==num)
{
printf("please enter the name:");
scanf("%s",p0->name);fflush(stdin);
printf("please enter the sex(M/F):");
scanf("%c",&p0->sex);fflush(stdin);
printf("please enter the age:");
scanf("%d",&p0->age);fflush(stdin);
for(i=0;i<5;i++)
{
printf("enter the %d score",i+1);
scanf("%f",&(p0->score));fflush(stdin);
}
*p=1;
}
else
{
printf("there is no num:%d",num);
}
return head;
}
void Q(struct student *head,int num,int *p)//Q操作 (查找)
{
int i=0;
struct student* p0;
p0=head;
while(p0->num!=num&&p0->next!=NULL)
{
p0=p0->next;
}
if(p0->num==num)
{
printf(" the num:%d \n the name:%s\n the sex:%c\n the age:%d\n",
p0->num,p0->name,p0->sex,p0->age);
printf("the score:");
for(i=0;i<5;i++)
{
printf("%f ",p0->score);
}
*p=1;
}
}
int main(void)
{
struct student* stu;
int *p,num,k=0;//p用于表示是否成功操作
p=&k;
char c;
ENT:printf("Do you want to creat a list?(y/n)");
scanf("%c",&c);fflush(stdin);
if(c=='y')
{
stu=C();
}
else if(c=='n')
{
stu=load();fflush(stdin);
}
else
{
printf("enter errror\nplease enter again");
goto ENT;
}
print(stu);
printf("Please enter what you want to do:(A/D/M/Q/E)\n");
printf("|*A为增加一个学生数据*|\n|*D为删除一个学生数据*|\n");
printf("|*M为改变一个学生数据*|\n|*Q为查询一个学生数据*|\n");
printf("|*E退出学生数据,并显示数据*|\n");
scanf("%c",&c);fflush(stdin);
STR:if(c=='A')
{
stu=A(stu);
printf("please enter the next operate:");
scanf("%c",&c);fflush(stdin);
goto STR;
}
else if(c=='D')
{
printf("Please enter the num you want to delete:");
scanf("%d",&num);fflush(stdin);
stu=D(stu,num,p);
printf("if you want to stop,please enter 1:");
scanf("%d",p);fflush(stdin);
while(!(*p))
{
printf("Please enter the num you want to delete:");
scanf("%d",&num);fflush(stdin);
stu=D(stu,num,p);
printf("if you want to stop,please enter 1:");
scanf("%d",p);fflush(stdin);
}
printf("please enter the next operate:");
scanf("%c",&c);fflush(stdin);
goto STR;
}
else if(c=='M')
{
printf("Please enter the num you want to Modify:");
scanf("%d",&num);fflush(stdin);
stu=M(stu,num,p);
printf("if you want to stop,please enter 1:");
scanf("%d",p);fflush(stdin);
while(!(*p))
{
printf("Please enter the num you want to Modify:");
scanf("%d",&num);fflush(stdin);
stu=M(stu,num,p);
printf("if you want to stop,please enter 1:");
scanf("%d",p);fflush(stdin);
}
printf("please enter the next operate:");
scanf("%c",&c);fflush(stdin);
goto STR;
}
else if(c=='Q')
{
printf("Please enter the num you want to find:");
scanf("%d",&num);fflush(stdin);
Q(stu,num,p);
printf("if you want to stop,please enter 1:");
scanf("%d",p);fflush(stdin);
while(!(*p))
{
printf("Please enter the num you want to find:");
scanf("%d",&num);fflush(stdin);
Q(stu,num,p);
printf("if you want to stop,please enter 1:");
scanf("%d",p);fflush(stdin);
}
printf("please enter the next operate:");
scanf("%c",&c);fflush(stdin);
goto STR;
}
else if(c=='E')
{
print(stu);
}
else
{
printf("there is no such operate\npease enter again");
scanf("%c",&c);fflush(stdin);
goto STR;
}
save(stu);
return 0;
}
为什么在黄色的地方第二次输入东西不对啊.第二组的数据会变成0 score 是 float型,要用 %f 来读取 fflush(stdin);改成
while((ch = getchar())!='\n');
试试看
快下课了我没时间细估计是这个
别望了 最开始char ch; 牡丹花下死做鬼 发表于 2014-5-21 15:53 static/image/common/back.gif
还是不行:cry:cry不过谢谢你了:lol:
struct student *C()//创建链表
{
int i=0;
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(len);
head=NULL;
printf("please enter the num:");
scanf("%d",&p1->num);fflush(stdin);//fflush(stdin)用于清空缓冲
printf("please enter the name:");
scanf("%s",p1->name);fflush(stdin);
printf("please enter the sex(M/F):");
scanf("%c",&p1->sex);fflush(stdin);
printf("please enter the age:");
scanf("%d",&p1->age);fflush(stdin);
for(i=0;i<5;i++)
{
printf("enter the %d score",i+1);
scanf("%f",&(p1->score));fflush(stdin);
}
p1->average=(p1->score+p1->score+p1->score+p1->score+
p1->score)/5.0;
while(1)
{
n++;
if(1==n)
{
head=p1;
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(len);
printf("please enter the num:");
scanf("%d",&p1->num);fflush(stdin);
if(p1->num==0)
{
p2->next=NULL;
return head;
}
printf("please enter the name:");
scanf("%s",p1->name);fflush(stdin);
printf("please enter the sex(M/F):");
scanf("%c",&p1->sex);fflush(stdin);
printf("please enter the age:");
scanf("%d",&p1->age);fflush(stdin);
for(i=0;i<5;i++)
{
printf("enter the %d score",i+1);
scanf("%d",&(p1->score));fflush(stdin);// 为什么这里调试会出现很小的数字,成绩变成0
}
p1->average=(p1->score+p1->score+p1->score+p1->score+
p1->score)/5.0;
}
}:cry被搞的不行不行的了 for(i=0;i<5;i++)
{
printf("enter the %d score",i+1);
scanf("%f",&(p1->score));fflush(stdin);// 而且还少了
} 额,都是在细节部分出错。。。
//====== 1========
void print(struct student * head)//打印函数
{
....
for(i=0;i<5;i++)
{
printf("%.1f ",p->score);/////////////////
}
...
}
//====== 2========
struct student *A(struct student *head)//A操作 (增加)
{
...
for(i=0;i<5;i++)
{
printf("enter the %d score",i+1);
scanf("%f",&(p1->score));fflush(stdin);///////////////
}
...
}
//====== 3========
void Q(struct student *head,int num,int *p)//Q操作 (查找)
{
...
for(i=0;i<5;i++)
{
printf("%f ",p0->score);////////////////
}
printf("\nthe average:%f \n",p0->average);////////////////
...
}
谢谢大家了,我以后会注意细节的O(∩_∩)O
页:
[1]