|
楼主 |
发表于 2021-3-29 21:34:32
|
显示全部楼层
全部代码
#include <stdio.h>
struct Date
{
int year,month,day;
};
struct student
{
char name[20];
float mathScore;
};
void input(student score[],int n);//录入学生信息
void sort(student score[],int n);//按数学成绩排序
void aver(student score[],int n);//学生的数学成绩平均分
void createFile(FILE *fp);//创建存储数据的文件
void save(student score[],int n,FILE *fp)
const int n=10;//输入10个数
int main()
{
FILE *fp;
student str[n];
input(str,n);
sort(str,n);
output(str,n);
createFile(fp);
save(str,n,fp);
}
void input(student score[],int n)
{
int i;
struct student score[10];
for(i=0;i<10;i++)//从键盘输入10个学生的姓名和分数
scanf("%s%f",&score[i].name,&score[i].mathScore);
}
void sort(student score[],int n)
{
int j;
struct student t;
for(i=0;i<9;i++)
{
for(j=9;j>i;j--)
{
if(score[i].mathScore>score[j].mathScore)
{
t=score[j];
score[j]=score[i];
score[i]=t;
}
}
}
}
void aver(student score[],int n)
{
float aver,sum=0;
for(i=0;i<10;i++)
{
sum=sum+score[i].mathScore;
}
aver=sum/10;
}
void createFile(FILE *fp)
{
fp=fopen("student score","w");
if(!fp)
{
printf("file open faile!");
exit(0);
}
fprintf(fp,"%s %s","姓名","成绩");
fclose(fp);
printf("File created successfully!\n");
}
void save(student score[],int n;FILE *fp)
{
fp=fopen("student score","a");
if(!fp)
{
printf("file open faile!");
exit(0);
}
for(i=0;i<10;i++)
{fprintf("%s\t%.2f\n",score[i].name,score[i].mathScore);}
fprintf("the average score=%.2f\n",aver);
fclose(fp);
printf("data save successfully!\n");
}
|
|