要求
一、代码
/*************************************************/
/* C语言期末报告第八题:学生成绩管理系系统 */
/* @BJXY modify By XYJ 2022/5/18 */
/*************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<stdlib.h>
#include<string.h>
struct Student{ //每个学生对应一个结构体
char ID[20];//学号
char Name[10];//姓名
float Mark1;//语文成绩
float Mark2;//数学成绩
float Mark3;//英语成绩
float Mark4;//计算机成绩
float All; //总分
float Average;//平均成绩
}students[1000];
int num=0; //计数器
void Copy(struct Student *arr,int i,int j)
{
strcpy(arr[i].ID,arr[j].ID);
strcpy(arr[i].Name,arr[j].Name);
arr[i].Mark1 = arr[j].Mark1;
arr[i].Mark2 = arr[j].Mark2;
arr[i].Mark3 = arr[j].Mark3;
arr[i].Mark4 = arr[j].Mark4;
arr[i].All = arr[j].All;
arr[i].Average = arr[j].Average;
}
int Student_SearchByName(char name[])//通过姓名来检索学生
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(students[i].Name,name)==0) //通过strcmp函数来对比学生姓名,找到返回在数组的位置
{
return i;
}
}
return -1; //未找到返回 -1
}
int Student_SearchByIndex(char id[])//通过学号来检索学生信息
{
int i;
for (i=0;i<num;i++)
{
if (strcmp(students[i].ID,id)==0) //通过strcmp函数来对比学生id,找到返回位置
{
return i;
}
}
return -1; //未找到返回 -1
}
void Student_DisplaySingle(int index)//输出表头
{
printf("学号 姓名 语文 数学 英语 计算机 总成绩 平均成绩\n");
printf("-------------------------------------------------------------\n");
printf("%10s%10s%8.2f%8.2f%8.2f%8.2f%10.2f%10.2f\n",students[index].ID,students[index].Name,
students[index].Mark1,students[index].Mark2,students[index].Mark3,students[index].Mark4,students[index].All,students[index].Average);
}
void input()//利用循环录入学生信息
{
while(1)
{
printf("请输入学号:");
scanf("%s",&students[num].ID);
getchar();
printf("请输入姓名:");
scanf("%s",&students[num].Name);
getchar();
printf("请输入语文成绩:");
scanf("%f",&students[num].Mark1);
getchar();
printf("请输入数学成绩:");
scanf("%f",&students[num].Mark2);
getchar();
printf("请输入英语成绩:");
scanf("%f",&students[num].Mark3);
getchar();
printf("请输入计算机成绩:");
scanf("%f",&students[num].Mark4); //依次输入各项数据
getchar();
students[num].All=students[num].Mark1+students[num].Mark2+students[num].Mark3+students[num].Mark4; //输完数据后自动计算总成绩与平均成绩
students[num].Average=(students[num].Mark1+students[num].Mark2+students[num].Mark3+students[num].Mark4)/4;
if(Student_SearchByIndex(students[num].ID) == -1)
{
num++; //移向下一个位置
}
else
{
printf("学号重复,输入数据无效 !!!\n");
}
printf("是否继续?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
void modify()//修改成绩
{ int x;
int pw=0;
while(1)
{
printf("请输入管理员密码:");
scanf("%d",&pw);
getchar();
if(pw==123456){
x=1;
break;
}
else
{
printf("密码错误,是否继续?(y/n)");
if (getchar()=='n')
{ x=0;
break;
}
}
}
while(x)
{
char id[20];
int index;
printf("请输入要修改的学生的学号:");
scanf("%s",&id);
getchar();
index=Student_SearchByIndex(id); //调用搜查id函数,根据其返回值确定位置
if (index==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要修改的学生信息为:\n");
Student_DisplaySingle(index);
printf("-- 请输入新值--\n");
printf("请输入学号:");
scanf("%s",&students[index].ID);
getchar();
printf("请输入姓名:");
scanf("%s",&students[index].Name);
getchar();
printf("请输入语文成绩:");
scanf("%f",&students[index].Mark1);
getchar();
printf("请输入数学成绩:");
scanf("%f",&students[index].Mark2);
getchar();
printf("请输入英语成绩:");
scanf("%f",&students[index].Mark3);
getchar();
printf("请输入计算机成绩:");
scanf("%f",&students[index].Mark4); //重新录入一套新的数据替代
getchar();
students[index].All=students[index].Mark1+students[index].Mark2+students[index].Mark3+students[index].Mark4;
students[index].Average=(students[index].Mark1+students[index].Mark2+students[index].Mark3+students[index].Mark4)/4;
}
printf("是否继续?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
void display()//打印已录入的学生信息
{
int a;
printf("%10s%10s%8s%8s%8s%8s%10s%10s\n","学号","姓名","语文","数学","英语","计算机","总成绩","平均成绩");
printf("-------------------------------------------------------------\n");
for (a=0;a<num;a++)
{
printf("%10s%10s%8.2f%8.2f%8.2f%8.2f%10.2f%10.2f\n",students[a].ID,students[a].Name,
students[a].Mark1,students[a].Mark2,students[a].Mark3,students[a].Mark4,students[a].All,students[a].Average);
}
}
void search()//查询学生信息
{
while(1)
{
char name[20];
int index;
printf("请输入要查询的学生的姓名:");
scanf("%s",&name);
getchar();
index=Student_SearchByName(name); //调用搜查name函数,根据其返回值确定位置
if (index==-1)
{
printf("学生不存在!\n");
}
else
{
printf("你要查询的学生信息为:\n");
Student_DisplaySingle(index);
}
printf("是否继续?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
void sort()//根据平均分排序
{
int i,j;
for (i=0;i<num;i++)
{
students[i].Average=(students[i].Mark1+students[i].Mark2+students[i].Mark3+students[i].Mark4)/4;
}
for (i=0;i<num;i++)
{
for (j=1;j<num-i;j++)
{
if (students[j-1].Average<students[j].Average)
{
Copy(students,num,j-1);
Copy(students,j-1,j);
Copy(students,j,num);
}
}
}
int a;
printf("学号 姓名 语文 数学 英语 计算机 总成绩 平均成绩\n");
printf("-------------------------------------------------------------\n");
for (a=0;a<num;a++)
{
printf("%10s%10s%8.2f%8.2f%8.2f%8.2f%10.2f%10.2f\n",students[a].ID,students[a].Name,
students[a].Mark1,students[a].Mark2,students[a].Mark3,students[a].Mark4,students[a].All,students[a].Average);
}
}
void SearchHigh()//搜索成绩最高者输出
{
int a;
int max ;
max=students[0].Mark1;
for(a=1;a<num;a++)
{
if(students[a].Mark1>max)
max=students[a].Mark1;
}
for(a=0;a<num;a++)
{
if(max==students[a].Mark1)
printf(" 语文最高分为: %10s 学号: %10s 成绩:%8.2f\n",students[a].Name,students[a].ID,students[a].Mark1);
printf("\n");
}
max=students[0].Mark2;
for(a=1;a<num;a++)
{
if(students[a].Mark2>max)
max=students[a].Mark2;
}
for(a=0;a<num;a++)
{
if(max==students[a].Mark2)
printf(" 数学最高分为: %10s 学号:%10s 成绩:%8.2f\n",students[a].Name,students[a].ID,students[a].Mark2);
printf("\n");
}
max=students[0].Mark3;
for(a=1;a<num;a++)
{
if(students[a].Mark3>max)
max=students[a].Mark3;
}
for(a=0;a<num;a++)
{
if(max==students[a].Mark3)
printf(" 英语最高分为:%10s 学号:%10s 成绩:%8.2f\n",students[a].Name,students[a].ID,students[a].Mark3);
printf("\n");
}
max=students[0].Mark4;
for(a=1;a<num;a++)
{
if(students[a].Mark4>max)
max=students[a].Mark4;
}
for(a=0;a<num;a++)
{
if(max==students[a].Mark4)
printf(" 计算机最高分为: %10s 学号:%10s 成绩:%8.2f\n",students[a].Name,students[a].ID,students[a].Mark4);
} printf("\n");
}
void Save()
{
FILE*fp = fopen("D:\\temp.txt","w+");
fprintf(fp,"%d\n",num);
for(int i = 0 ; i< num ;i++)
{
fprintf(fp,"%s %s %f %f %f %f %f %f\n",students[i].ID,students[i].Name,students[i].Mark1,students[i].Mark2,students[i].Mark3,students[i].Mark4,students[i].All,students[i].Average);
}
fclose(fp);
}
void Eachaverage()
{
float A,B,C,D;
float a=0,b=0,c=0,d=0;
for(int i=0;i<num;i++)
{
a+=students[i].Mark1;
b+=students[i].Mark2;
c+=students[i].Mark3;
d+=students[i].Mark4;
}
A=a/(num);
B=b/(num);
C=c/(num);
D=d/(num);
printf("语文平均成绩:%.2f\n",A);
printf("数学平均成绩:%.2f\n",B);
printf("英语平均成绩:%.2f\n",C);
printf("计算机平均成绩:%.2f\n",D);
}
void Beyondaverage()
{ int t=0,z=0,n=0,m=0;
float A,B,C,D;
float a=0,b=0,c=0,d=0;
for(int i=0;i<num;i++)
{
a+=students[i].Mark1;
b+=students[i].Mark2;
c+=students[i].Mark3;
d+=students[i].Mark4;
}
A=a/(num); //语文平均分
B=b/(num); //数学平均分
C=c/(num); //英语平均分
D=d/(num); //计算机平均分
for(int x=0;x<num;x++)
{
if(students[x].Mark1>A)
++t;
if(students[x].Mark2>B)
++z;
if(students[x].Mark3>C)
++n;
if(students[x].Mark4>D)
++m;
}
printf("超过语文平均成绩的人数:%d\n",t);
printf("超过数学平均成绩的人数:%d\n",z);
printf("超过英语平均成绩的人数:%d\n",n);
printf("超过计算机平均成绩的人数:%d\n",m);
system("pause");
}
void TestRS()
{
printf("\t\t\t\t\t-------- 成绩统计-------\n\n\n\n"); //菜单
printf("\t\t\t\t\t1. 每门课最高成绩\n\n");
printf("\t\t\t\t\t2. 每门课平均成绩\n\n");
printf("\t\t\t\t\t3. 每门课超过平均成绩人数\n\n");
int i=0;
while(1)
{
printf("\n选择功能:");
scanf("%d",&i);
getchar();
printf("\n");
if(i==1)
SearchHigh();
else if(i==2)
Eachaverage();
else if(i==3)
Beyondaverage();
printf("是否需要继续查询?(y/n)");
if (getchar()=='n')
{
break;
}
}
}
void Load()
{
FILE*fp = fopen("D:\\temp.txt","r");
fscanf(fp,"%d",&num);
for(int i = 0 ; i< num ;i++)
{
fscanf(fp,"%s %s %f %f %f %f %f %f\n",students[i].ID,students[i].Name,&students[i].Mark1,&students[i].Mark2,&students[i].Mark3,&students[i].Mark4,&students[i].All,&students[i].Average);
}
fclose(fp);
}
/*主程序*/
int main(){
int i;
while(1){
Load();
printf("\t\t\t\t\t-------- 学生成绩管理系统-------\n\n\n\n"); //菜单
printf("\t\t\t\t\t1. 成绩录入\n\n");
printf("\t\t\t\t\t2. 成绩显示\n\n");
printf("\t\t\t\t\t3. 成绩保存\n\n");
printf("\t\t\t\t\t4. 成绩排序\n\n");
printf("\t\t\t\t\t5. 成绩修改\n\n");
printf("\t\t\t\t\t6. 成绩统计\n\n");
printf("\t\t\t\t\t0. 退出\n\n\n");
printf("请选择(0-6):");
scanf("%d",&i);
switch(i){
case 1:input();break;
case 2:display();break;
case 3:Save();break;
case 4:sort();break;
case 5:modify();break;
case 6:TestRS();break;
case 0:exit(0);
}
Save();
}
return 0;
}
总结
代码思路整体较为简单,涉及内容有结构体,文件,冒泡算法等…