代码如下:
#include
#include
#define SIZE 3
struct Student
{
int num;
char name[20];
char sex[2];
int score[5];
float aver;
struct Student *next;
}stu[SIZE],*head,*p,*q;
void save()
{
FILE *fp;
int i,j;
fp = fopen("stu.c","w+");
head = p = (struct Student *)malloc(sizeof(struct Student));
head ->next = &stu[0];
for(i = 0;i < SIZE;i++)
{
scanf("%d ",&stu[i].num);
fprintf(fp,"%d\n",stu[i].num);
gets(stu[i].name);
fprintf(fp,"%s\n",stu[i].name);
gets(stu[i].sex);
fprintf(fp,"%s\n",stu[i].sex);
for(j = 0;j < 5;j++)
scanf("%d",&stu[i].score[j]);
fprintf(fp,"%d %d %d %d %d\n",stu[i].score[0],stu[i].score[1],stu[i].score[2],
stu[i].score[3],stu[i].score[4]);
stu[i].aver = (stu[i].score[0] + stu[i].score[1] + stu[i].score[2] + stu[i].score[3]
+ stu[i].score[4])/5.0;
fprintf(fp,"%f\n",stu[i].aver);
}
fclose(fp);
}
void sort()
{
FILE *fp_in,*fp_sort;
int i,j;
float stu_aver[SIZE];
struct Student temp;
fp_in = fopen("stu.c","r+");
for(i = 0;i < SIZE;i++)
{
fscanf(fp_in,"%d%f",&stu[i].num,&stu[i].aver);
//printf("fuck %f\n",stu[i].aver);
//stu_aver[i] = stu[i].aver;
}
//for(i = 0;i < SIZE;i++)
//printf("%f\n",stu_aver[i]);
for(i = 0;i < SIZE - 1;i++)
{
for(j = 0;j < SIZE - 1 - i;j++)
{
if(stu[j].aver > stu[j + 1].aver)
{
temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
fp_sort = fopen("stu-sort","w+");
for(i = 0;i < SIZE;i++)
{
fprintf(fp_sort,"%d\n",stu[i].num);
fprintf(fp_sort,"%s\n",stu[i].name);
fprintf(fp_sort,"%s\n",stu[i].sex);
fprintf(fp_sort,"%d %d %d %d %d\n",stu[i].score[0],stu[i].score[1],stu[i].score[2],
stu[i].score[3],stu[i].score[4]);
fprintf(fp_sort,"%lf\n",stu[i].aver);
}
fclose(fp_sort);
fclose(fp_in);
}
void add()
{
FILE *fp_new,*fp_in;
int i;
q = (struct Student *)malloc(sizeof(struct Student));
scanf("%d ",&(q ->num));
gets(q ->name);
gets(q ->sex);
for(i = 0;i < 5;i++)
scanf("%d",&(q ->score[i]));
q ->aver = (q ->score[0] +q ->score[1] + q ->score[2] +q ->score[3]
+ q ->score[4])/5.0;
fp_in = fopen("stu-sort","r+");
for(i = 0;i < SIZE;i++)
{
fscanf(fp_in,"%d%f",&stu[i].num,&stu[i].aver);
//printf("fuck %f\n",stu[i].aver);
//stu_aver[i] = stu[i].aver;
}
//for(i = 0;i < SIZE;i++)
//printf("%f\n",stu[i].aver);
//printf("%f\n",q ->aver);
head ->next = &stu[0];
for(i = 0;i < SIZE;i++)
{
if(i <= SIZE - 2)
stu[i].next = &stu[i + 1];
else
stu[i].next = NULL;
}
p = head;
if(q ->aver <= stu[0].aver)
{
p ->next = q;
q ->next = &stu[0];
}
else if(q ->aver >= stu[SIZE - 1].aver)
{
stu[SIZE - 1].next = q;
q ->next = NULL;
}
else
{
for(i = 0;i < SIZE - 1;i++)
{
if((q ->aver > stu[i].aver) && (q ->aver < stu[i + 1].aver))
{
stu[i].next = q;
q ->next = &stu[i + 1];
}
}
}
fclose(fp_in);
fp_new = fopen("new","w+");
p = head ->next;
for(;;)
{
fprintf(fp_new,"%d\n",p ->num);
fprintf(fp_new,"%s\n",p ->name);
fprintf(fp_new,"%s\n",p ->sex);
fprintf(fp_new,"%d %d %d %d %d\n",p ->score[0],p ->score[1],p ->score[2],
p ->score[3],p ->score[4]);
fprintf(fp_new,"%lf\n",p ->aver);
p = p ->next;
if(p == NULL)
break;
}
fclose(fp_new);
}
void search()
{
int i;
p = head ->next;
for(;;)
{
for(i = 0;i < 5;i++)
{
if(p ->score[i] < 60)
{
printf("%d\n",p ->num);
printf("%s\n",p ->name);
printf("%s\n",p ->sex);
printf("%d %d %d %d %d\n",p ->score[0],p ->score[1],p ->score[2],
p ->score[3],p ->score[4]);
printf("%lf\n",p ->aver);
break;
}
}
if(p ->aver >= 90)
{
printf("%d\n",p ->num);
printf("%s\n",p ->name);
printf("%s\n",p ->sex);
printf("%d %d %d %d %d\n",p ->score[0],p ->score[1],p ->score[2],
p ->score[3],p ->score[4]);
printf("%lf\n",p ->aver);
}
p = p ->next;
if(p == NULL)
break;
}
}
int main(void)
{
//FILE *fp;
//int i,j;
save();
sort();
add();
printf("\n");
search();
return 0;
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。