目录
主菜单
10个选项分别为:
1.输入成绩
2.输出成绩
3.按总成绩排序
4.按学号查询成绩
5.修改信息
6.删除学生
7.计算各科平均分
8.文件读入
other.退出
printf("------------------------\n");
printf("------学生成绩管理------\n");
printf("------1.输入成绩 ------\n");
printf("------2.输出成绩 ------\n");
printf("------3.按总成绩排序----\n");
printf("------4.按学号查询成绩--\n");
printf("------5.修改信息 ------\n");
printf("------6.删除学生 ------\n");
printf("------7.计算各科平均分--\n");
printf("------8.文件读入 ------\n");
printf("------9.保存至文件------\n");
printf("------other.退出 ------\n");
printf("------------------------\n");
printf("输入选项:");
scanf("%d",&option);
1.输入成绩
分别输入姓名学号成绩信息,其中用到了getchar();来消除缓存
void input(struct Stu Class[],int j)
{
printf("输入第%d位同学姓名:",j+1);
scanf("%s",Class[j].Name);
getchar();
printf("输入第%d位同学学号:",j+1);
scanf("%d",&Class[j].Number);
printf("输入第%d位同学语文成绩:",j+1);
scanf("%d",&Class[j].Language_score);
printf("输入第%d位同学数学成绩:",j+1);
scanf("%d",&Class[j].Math_score);
printf("输入第%d位同学英语成绩:",j+1);
scanf("%d",&Class[j].English_score);
printf("输入第%d位同学体育成绩:",j+1);
scanf("%d",&Class[j].PE_score);
printf("输入第%d位同学C语言成绩:",j+1);
scanf("%d",&Class[j].C_score);
Class[j].Sum_score=Class[j].Language_score+Class[j].Math_score+Class[j].English_score+Class[j].PE_score+Class[j].C_score;
}
2.输出成绩
void output(struct Stu Class[],int n)
{
printf("---------------------------------------------\n");
printf("姓名 学号 语文 数学 英语 体育 C语言 总分");
printf("\n");
int j;
for(j=0; j<n; j++)
{
printf("%5s",Class[j].Name);
printf("%5d",Class[j].Number);
printf("%6d",Class[j].Language_score);
printf("%6d",Class[j].Math_score);
printf("%6d",Class[j].English_score);
printf("%6d",Class[j].PE_score);
printf("%6d",Class[j].C_score);
printf("%7d",Class[j].Sum_score);
printf("\n");
}
printf("---------------------------------------------\n");
}
3.按总成绩排序
排序算法是冒泡排序,排序时交换结构体
void Sort(struct Stu Class[],int n)
{
int j,k;
struct Stu t;
for(j=0; j<n-1; j++)
{
for(k=0; k<n-j-1; k++)
{
if(Class[k].Sum_score<Class[k+1].Sum_score)
{
t=Class[k];
Class[k]=Class[k+1];
Class[k+1]=t;
}
}
}
}
4.按学号查询成绩
分为查找成功和查找失败
5.修改信息
可以对指定学号的各项信息进行修改
7.计算各科平均分
8.文件读入
采用了fprintf函数
9.保存至文件
采用了fscanf函数
other.退出
总代码
#include<stdio.h>
#include<windows.h>
#define Max 25
#define obj_num 5
struct Stu
{
char Name[25]; //姓名
int Number; //学号
int Language_score;//语文成绩
int Math_score; //数学成绩
int English_score; //英语成绩
int PE_score; //体育成绩
int C_score; //C语言成绩
int Sum_score; //总成绩
};
void input(struct Stu Class[],int j)
{
printf("输入第%d位同学姓名:",j+1);
scanf("%s",Class[j].Name);
getchar();
printf("输入第%d位同学学号:",j+1);
scanf("%d",&Class[j].Number);
printf("输入第%d位同学语文成绩:",j+1);
scanf("%d",&Class[j].Language_score);
printf("输入第%d位同学数学成绩:",j+1);
scanf("%d",&Class[j].Math_score);
printf("输入第%d位同学英语成绩:",j+1);
scanf("%d",&Class[j].English_score);
printf("输入第%d位同学体育成绩:",j+1);
scanf("%d",&Class[j].PE_score);
printf("输入第%d位同学C语言成绩:",j+1);
scanf("%d",&Class[j].C_score);
Class[j].Sum_score=Class[j].Language_score+Class[j].Math_score+Class[j].English_score+Class[j].PE_score+Class[j].C_score;
}
void output(struct Stu Class[],int n)
{
printf("---------------------------------------------\n");
printf("姓名 学号 语文 数学 英语 体育 C语言 总分");
printf("\n");
int j;
for(j=0; j<n; j++)
{
printf("%5s",Class[j].Name);
printf("%5d",Class[j].Number);
printf("%6d",Class[j].Language_score);
printf("%6d",Class[j].Math_score);
printf("%6d",Class[j].English_score);
printf("%6d",Class[j].PE_score);
printf("%6d",Class[j].C_score);
printf("%7d",Class[j].Sum_score);
printf("\n");
}
printf("---------------------------------------------\n");
}
void outputOne(struct Stu Class[],int j)
{
printf("姓名 学号 语文 数学 英语 体育 C语言 总分");
printf("\n");
printf("%5s",Class[j].Name);
printf("%5d",Class[j].Number);
printf("%6d",Class[j].Language_score);
printf("%6d",Class[j].Math_score);
printf("%6d",Class[j].English_score);
printf("%6d",Class[j].PE_score);
printf("%6d",Class[j].C_score);
printf("%7d",Class[j].Sum_score);
printf("\n");
}
void Sort(struct Stu Class[],int n)
{
int j,k;
struct Stu t;
for(j=0; j<n-1; j++)
{
for(k=0; k<n-j-1; k++)
{
if(Class[k].Sum_score<Class[k+1].Sum_score)
{
t=Class[k];
Class[k]=Class[k+1];
Class[k+1]=t;
}
}
}
}
int search(int number,int n,struct Stu Class[])
{
int i,p=0;
for(i=0; i<n; i++)
{
if(Class[i].Number==number)
{
p=i;
return p;
}
}
return -1;
}
void modify(int number,int n,struct Stu Class[])
{
int index=search(number,n,Class);
if(index==-1)
{
printf("找不到该学号\n");
}
else
{
printf("学号%d的学生是第%d位\n",number,index+1);
input(Class,index);
printf("修改成功\n");
output(Class,n);
}
}
void del(int number,int n,struct Stu Class[])
{
int index=search(number,n,Class);
if(index==-1)
{
printf("找不到该学号\n");
}
else
{
printf("学号%d的学生是第%d位\n",number,index);
int i;
for(i=index; i<n-1; i++)
{
Class[i]= Class[i+1];
}
printf("删除成功\n");
output(Class,n-1);
}
}
double calave(int n,struct Stu Class[],double ave[])
{
int Language_score_sum=0;
int Math_score_sum=0;
int English_score_sum=0;
int PE_score_sum=0;
int C_score_sum=0;
for(int i=0; i<n; i++)
{
Language_score_sum+=Class[i].Language_score;
Math_score_sum+=Class[i].Math_score;
English_score_sum+=Class[i].English_score;
PE_score_sum+=Class[i].PE_score;
C_score_sum+=Class[i].C_score;
}
ave[0]=(double)Language_score_sum/n;
ave[1]=(double)Math_score_sum/n;
ave[2]=(double)English_score_sum/n;
ave[3]=(double)PE_score_sum/n;
ave[4]=(double)C_score_sum/n;
}
void save(int n,struct Stu Class[])
{
FILE *fp;
fp = fopen("score_data.txt","w");
if(fp == NULL)
{
printf("error!");
return;
}
else
{
for(int j=0; j<n; j++)
{
fprintf(fp,"%s %d %d %d %d %d %d %d\n",Class[j].Name,Class[j].Number,Class[j].Language_score,Class[j].Math_score,Class[j].English_score,Class[j].PE_score,Class[j].C_score,Class[j].Sum_score);
}
printf("文件写入成功\n");
}
fclose(fp);
}
int read(int n,struct Stu Class[])
{
FILE *fp;
fp = fopen("score_data.txt","r");
int count=0;
if(fp == NULL)
{
printf("error!");
return 0;
}
else
{
for(int j=0; j<n; j++)
{
int flag=fscanf(fp,"%s %d %d %d %d %d %d %d\n",Class[j].Name,&Class[j].Number,&Class[j].Language_score,&Class[j].Math_score,&Class[j].English_score,&Class[j].PE_score,&Class[j].C_score,&Class[j].Sum_score);
if(flag!=-1) count++;
}
printf("文件读取成功\n");
}
fclose(fp);
return count;
}
void menu(struct Stu Class[])
{
int n=0,count;
int option=1;
while(option<=9&&option>=1)
{
printf("------------------------\n");
printf("------学生成绩管理------\n");
printf("------1.输入成绩 ------\n");
printf("------2.输出成绩 ------\n");
printf("------3.按总成绩排序----\n");
printf("------4.按学号查询成绩--\n");
printf("------5.修改信息 ------\n");
printf("------6.删除学生 ------\n");
printf("------7.计算各科平均分--\n");
printf("------8.文件读入 ------\n");
printf("------9.保存至文件------\n");
printf("------other.退出 ------\n");
printf("------------------------\n");
printf("输入选项:");
scanf("%d",&option);
switch(option)
{
case 1:
{
system("cls");
printf("输入学生的个数(不大于%d):",Max-n);
scanf("%d",&count);
while(count--)
{
input(Class,n++);
}
output(Class,n);
break;
}
case 2:
{
system("cls");
output(Class,n);
break;
}
case 3:
{
system("cls");
Sort(Class,n);
output(Class,n);
break;
}
case 4:
{
system("cls");
int number;
printf("输入要查找的学号:");
scanf("%d",&number);
int index=search(number,n,Class);
if(index==-1)
{
printf("查找失败\n");
}
else
{
printf("查找成功\n");
outputOne(Class,index);
}
break;
}
case 5:
{
system("cls");
output(Class,n);
int number;
printf("输入要修改成绩的学生对应的学号:");
scanf("%d",&number);
modify(number,n,Class);
output(Class,n);
break;
}
case 6:
{
system("cls");
output(Class,n);
int number;
printf("输入要删除的学号:");
scanf("%d",&number);
del(number,n,Class);
n--;
system("cls");
output(Class,n);
break;
}
case 7:
{
system("cls");
output(Class,n);
double ave[obj_num];
calave(n,Class,ave);
char str[]="平均分";
printf("\n");
printf("%5s",str);
printf(" ");
printf("%5.2lf",ave[0]);
printf("%6.2lf",ave[1]);
printf("%6.2lf",ave[2]);
printf("%6.2lf",ave[3]);
printf("%6.2lf",ave[4]);
printf("\n");
break;
}
case 8:
{
system("cls");
n-=n;
n+=read(Max,Class);
break;
}
case 9:
{
system("cls");
save(n,Class);
break;
}
default :
{
break;
}
}
}
}
int main()
{
struct Stu Class [Max];
menu(Class);
return 0;
}