#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct student
{
char xuehao[20];
char name[20];
int math;
int eng;
int sum;
}STU;
struct singlelist
{
STU data;
struct singlelist * next;
};
struct singlist* createlist()
{
struct singlelist* headNode=(struct singlelist*)malloc(sizeof(struct singlelist));
headNode->next=NULL;
return headNode;
};
struct singlelist* createNode(STU data)
{
struct singlelist* newNode=(struct singlelist*)malloc(sizeof(struct singlelist));
newNode->data=data;
newNode->next=NULL;
return newNode;
};
void insertNode(struct singlelist* headNode,STU data)
{
struct singlelist* newNode=createNode(data);
newNode->next=headNode->next;
headNode->next=newNode;
}
void printflist(struct singlelist* headNode)
{
struct singlelist* pMove=headNode->next;
printf("学号\t姓名\t数学\t英语\t总成绩\n");
while(pMove)
{
printf("%s\t%s\t%d\t%d\t%d\n",pMove->data.xuehao,pMove->data.name,pMove->data.math,pMove->data.eng,pMove->data.sum);
pMove=pMove->next;
}
printf("\n");
}
void sortlist(struct singlelist *head)
{
printf("\t 总成绩排序\n\n");
struct singlelist *pfirst=NULL,*psecond=NULL,*pend=NULL;
pfirst=head->next;
psecond=head;
while(pfirst != pend) //外循环
{
while(pfirst->next != pend)//内循环
{
if(pfirst->data.sum > pfirst->next->data.sum)
{
STU temp;
temp=pfirst->data;
pfirst->data=pfirst->next->data;
pfirst->next->data=temp;
}
pfirst=pfirst->next;
}
pend=pfirst;
pfirst=head->next;
}
}
void insertdata(struct singlelist* headNode)
{
int n;
printf("请输入你要插入的学生信息人数: ");
scanf("%d",&n);
printf("\n");
struct singlelist* list=headNode;
int i;
STU stu[100];
printf("学号\t姓名\t数学\t英语\n");
for(i=0;i<n;i++)
{
scanf("%s%s%d%d",stu[i].xuehao,stu[i].name,&stu[i].math,&stu[i].eng);
stu[i].sum=stu[i].math+stu[i].eng;
insertNode(list,stu[i]);
}
}
void save(struct singlelist *headnode)
{
FILE *fp;
struct singlelist* pMove=headnode->next;
char filename[20];
printf("请输入你要保存到的文件位置: ");
scanf("%s",filename);
if((fp=fopen(filename,"w"))==NULL)
{
printf("cannot open the file\n");
exit(1);
}
while(pMove)
{
fprintf(fp,"%s\t%s\t%d\t%d\t%d\n",pMove->data.xuehao,pMove->data.name,pMove->data.math,pMove->data.eng,pMove->data.sum);
pMove=pMove->next;
}
printf("保存成功!\n");
fclose(fp);
}
void read()
{
FILE *fp;
STU stu[100];
char filename[20];
printf("请输入你要读取的文件位置: ");
scanf("%s",filename);
printf("\n");
if((fp=fopen(filename,"r"))==NULL)
{
printf("cannot open the file\n");
exit(1);
}
int i=1;
while(fscanf(fp,"%s%s%d%d%d",stu[i].xuehao,stu[i].name,&stu[i].math,&stu[i].eng,&stu[i].sum)!=EOF)
{
printf("%s\t%s\t%d\t%d\t%d\n",stu[i].xuehao,stu[i].name,stu[i].math,stu[i].eng,stu[i].sum);
i++;
}
printf("读取成功!\n");
fclose(fp);
}
int main()
{
struct singlelist* list =createlist();
insertdata(list);
printflist(list);
sortlist(list);
printflist(list);
save(list);
read();
}
10000小时计划
148.5h