|
50鱼币
#include<stdio.h>
#include<stdlib.h>
#define len sizeof(struct student)
int n=0;//用于计算元素个数
FILE *fp;
struct student
{
int num;
char name[20];
char sex;
int age;
float score[5];
float average;
struct student * next;
};
void save(struct student* head)
{
struct student *p;
char name[30];
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[20];
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[i]));fflush(stdin);
}
p1->average=(p1->score[0]+p1->score[1]+p1->score[2]+p1->score[3]+
p1->score[4])/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[i]));fflush(stdin);
}
p1->average=(p1->score[0]+p1->score[1]+p1->score[2]+p1->score[3]+
p1->score[4])/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[i]);
}
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[i]));fflush(stdin);
}
p1->average=(p1->score[0]+p1->score[1]+p1->score[2]+p1->score[3]+
p1->score[4])/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[i]));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[i]);
}
*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 来读取
|