c语言学生成绩统计系统,
发布网友
发布时间:2022-06-02 18:23
我来回答
共1个回答
热心网友
时间:2024-03-29 07:29
#include<stdio.h>
#include<string.h>
#define NUM1 3
#define NUM2 3
#define NUM3 3
struct student
{
char name[10];
int score;
}c1[NUM1],c2[NUM2],c3[NUM3];//定义三个结构体数组
int i,j,k;
FILE *fp1_order,*fp2_order,*fp3_order;//定义文件指针
//=========================================================
void main(void)
{
void data_in(void);
void score_order(void);
void save_in(void);
void save_order(void);
void open_order(void);
printf("******************************学生成绩文件管理******************************\n");
printf(" programmer:喻翔\n\n");
printf("一、输入三个班的学生姓名和成绩:\n\n");
data_in();
printf("\n\n\n");
printf("############################################################################\n\n");
printf("二、保存输入的姓名和成绩:\n\n");
save_in();
printf("\n\n\n\n");
printf("############################################################################\n\n");
printf("三、排名并保存姓名和成绩:\n\n");
score_order();
save_order();
printf("\n\n\n\n");
printf("############################################################################\n\n");
printf("四、验证排名后保存的文件:\n\n");
open_order();
printf("\n\n\n");
}
//============================================
void data_in(void)
{
printf("请输入一班%d个学生的姓名和成绩:\n",NUM1);
for(i=0;i<NUM1;i++)
{
printf("第%d个:",i+1);
scanf("%s %d",c1[i].name,&c1[i].score);
}
printf("\n\n\n请输入二班%d个学生的姓名和成绩:\n",NUM2);
for(j=0;j<NUM2;j++)
{
printf("第%d个:",j+1);
scanf("%s %d",c2[j].name,&c2[j].score);
}
printf("\n\n\n请输入三班%d个学生的姓名和成绩:\n",NUM3);
for(k=0;k<NUM3;k++)
{
printf("第%d个:",k+1);
scanf("%s %d",c3[k].name,&c3[k].score);
}
}
//=============================================================
void score_order(void)
{
int h,t;
char s[10];
for(h=0;h<NUM1-1;h++)
for(i=0;i<NUM1-1-h;i++)
if(c1[i].score<c1[i+1].score)
{
t=c1[i].score;
c1[i].score=c1[i+1].score;
c1[i+1].score=t;
strcpy(s,c1[i].name);//一定要使用strcpy()函数
strcpy(c1[i].name,c1[i+1].name);
strcpy(c1[i+1].name,s);
}
for(h=0;h<NUM2-1;h++)
for(j=0;j<NUM2-1-h;j++)
if(c2[j].score<c2[j+1].score)
{
t=c2[j].score;
c2[j].score=c2[j+1].score;
c2[j+1].score=t;
strcpy(s,c2[j].name);
strcpy(c2[j].name,c2[j+1].name);
strcpy(c2[j+1].name,s);
}
for(h=0;h<NUM3-1;h++)
for(k=0;k<NUM3-1-h;k++)
if(c3[k].score<c3[k+1].score)
{
t=c3[k].score;
c3[k].score=c3[k+1].score;
c3[k+1].score=t;
strcpy(s,c3[k].name);
strcpy(c3[k].name,c3[k+1].name);
strcpy(c3[k+1].name,s);
}
}
//=====================================================================
void save_in(void)
{
FILE *fp1_in,*fp2_in,*fp3_in;
if((fp1_in=fopen("data1_in.dat","wb"))==NULL)
{
printf("The file can not open!\n");
return;
}
for(i=0;i<NUM1;i++)
if(fwrite(&c1[i],sizeof(struct student),1,fp1_in)!=1)
{
printf("file write error!\n");
return;
}
fclose(fp1_in);
if((fp2_in=fopen("data2_in.dat","wb"))==NULL)
{
printf("The file can not open!\n");
return;
}
for(j=0;j<NUM2;j++)
if(fwrite(&c2[j],sizeof(struct student),1,fp2_in)!=1)
{
printf("file write error!\n");
return;
}
fclose(fp2_in);
if((fp3_in=fopen("data3_in.dat","wb"))==NULL)
{
printf("The file can not open!\n");
return;
}
for(k=0;k<NUM3;k++)
if(fwrite(&c3[k],sizeof(struct student),1,fp3_in)!=1)
{
printf("file write error!\n");
return;
}
fclose(fp3_in);
}
//==============================================================
void save_order(void)
{
if((fp1_order=fopen("data1_order.dat","wb"))==NULL)
{
printf("The file can not open!\n");
return;
}
for(i=0;i<NUM1;i++)
if(fwrite(&c1[i],sizeof(struct student),1,fp1_order)!=1)
{
printf("file write error!\n");
return;
}
fclose(fp1_order);
if((fp2_order=fopen("data2_order.dat","wb"))==NULL)
{
printf("The file can not open!\n");
return;
}
for(j=0;j<NUM2;j++)
if(fwrite(&c2[j],sizeof(struct student),1,fp2_order)!=1)
{
printf("file write error!\n");
return;
}
fclose(fp2_order);
if((fp3_order=fopen("data3_order.dat","wb"))==NULL)
{
printf("The file can not open!\n");
return;
}
for(k=0;k<NUM3;k++)
if(fwrite(&c3[k],sizeof(struct student),1,fp3_order)!=1)
{
printf("file write error!\n");
return;
}
fclose(fp3_order);
}
//=============================================================
void open_order(void)
{
fp1_order=fopen("data1_order.dat","rb");
printf("一班的成绩排名如下:\n");
printf("名次 姓名 C语言成绩\n");
printf("----------------------------------------\n");
for(i=0;i<NUM1;i++)
{
printf("第%d名 ",i+1);
fread(&c1[i],sizeof(struct student),1,fp1_order);
printf("%s %d\n",c1[i].name,c1[i].score);
}
fp2_order=fopen("data2_order.dat","rb");
printf("\n\n\n二班的成绩排名如下:\n");
printf("名次 姓名 C语言成绩\n");
printf("----------------------------------------\n");
for(j=0;j<NUM2;j++)
{
printf("第%d名 ",j+1);
fread(&c2[j],sizeof(struct student),1,fp2_order);
printf("%s %d\n",c2[j].name,c2[j].score);
}
fp3_order=fopen("data3_order.dat","rb");
printf("\n\n\n三班的成绩排名如下:\n");
printf("名次 姓名 C语言成绩\n");
printf("----------------------------------------\n");
for(k=0;k<NUM3;k++)
{
printf("第%d名 ",k+1);
fread(&c3[k],sizeof(struct student),1,fp3_order);
printf("%s %d\n",c3[k].name,c3[k].score);
}
}