大家好,又见面了,我是你们的朋友全栈君。
学生成绩管理系统
思路
学生成绩管理系统,首先要初始化系统,开始一个新的学生成绩系统初始化记录学生姓名,学号,院系,然后输入学生各科成绩,数学,英语,语文成绩。记录完各课成绩以后,可查看学生平均成绩和是否及格,成绩查询其中有学号查询,姓名查询,院系查询,还有全部输出,可以清晰的看到及格人数,按照分数高低排列,最后还可以添加和删除学生成绩,或者更改学生成绩,避免人为录入成绩错误。
基本函数
1.结构体 struct Student
定义一个结构体struct ,结构体的关键字,students 定义的结构体对象。学号char ID[20];姓名char Name[10]; 各项成绩float Mark,总成绩 float All 平均成绩 float Average;
2.录入(void inputt())
利用循环将数据从students[0]开始录入各项信息,计数器num+1。
2.删除(void deletee())
利用循环将数组里面的信息与待删除的信息比对,若相同,将此项删除并将后面的信息向前移动一位,计数器num-1;若无此项,提示重新输入。
3.修改(void modfiy())
利用循环将数组里面的信息与待修改的信息比对,若相同,对其进行重新复制即修改;若无此项,提示重新输入。
4.插入(void insert())
提示输入插入的位置,将该位置后及该位置的信息向后挪一位,将新信息录入该位置。计数器num+1。
5.排序 (void sort())
走排序的平均分,依次比较两个相邻的成绩,如果他们的顺序如果前小于后就把他们交换过来。走访元素的工作是重复地进行直到没有相邻元素需要交换,也就是说该元素已经排序完成。
6.显示 (void display())
利用循环将已录入的信息打印出来。
7.查询 (void Student_SearchByIndex())
提示要查询学生的学号,利用循环比对,若相同,将此项信息打印出来;若无此项,提示重新输入。
8.主函数 main()
利用无限次循环while和swithch()实现各函数的调用,系统根据输入的数字来操控相应的函数,同时显示可供选择的选项。
#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("%10s%10s%8s%8s%8s%10s\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 inputt()//利用循环录入学生信息
{
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()//修改成绩
{
while(1)
{
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 deletee()//删除学生信息
{
int i;
while(1)
{
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("是否真的要删除?(y/n)");
if (getchar()=='y')
{
for (i=index;i<num-1;i++)
{
Copy(students,i,i+1);
//students[i]=students[i+1]; //把后边的对象都向前移动
}
num--;
}
getchar();
}
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 insert()//指定位置插入学生信息
{
int a,b,c;
printf("请输入你要插入的位置");
scanf("%d",&a);
if(a>num) {
printf("输入的位置有误,请重新输入,当前共%d条数据\n",num);
scanf("%d",&a);}
b=num-1;
for(;b>=a-1;b--)
{
//strcpy(students[b+1].ID,students[b].ID);
//strcpy(students[b+1].Name,students[b].Name);
//students[b+1].Mark1=students[b].Mark1;
//students[b+1].Mark2=students[b].Mark2;
//students[b+1].Mark3=students[b].Mark3;
//students[b+1].Mark4=students[b].Mark4;
//students[b+1].All=students[b].All;
//students[b+1].Average=students[b].Average;
Copy(students,b+1,b); //根据其输入的位置,将其及以后的数据向后移动一个位置
}
num++;
printf("请输入学号:");
scanf("%s",&students[a-1].ID);
getchar();
printf("请输入姓名:");
scanf("%s",&students[a-1].Name);
getchar();
printf("请输入语文成绩:");
scanf("%f",&students[a-1].Mark1);
getchar();
printf("请输入数学成绩:");
scanf("%f",&students[a-1].Mark2);
getchar();
printf("请输入英语成绩:");
scanf("%f",&students[a-1].Mark3);
getchar();
printf("请输入计算机成绩:");
scanf("%f",&students[a-1].Mark4); //输入新数据
getchar();
students[a-1].All=students[a-1].Mark1+students[a-1].Mark2+students[a-1].Mark3+students[a-1].Mark4;
students[a-1].Average=(students[a-1].Mark1+students[a-1].Mark2+students[a-1].Mark3+students[a-1].Mark4)/4;
}
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;
//struct students tmp;
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);
//tmp=students[j-1];
//students[j-1]=students[j];
//students[j]=tmp; //冒泡排序
}
}
}
int a;
printf("%10s%10s%8s%8s%8s%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 SearchLow()//搜索不及格的并输出
{
int a;
printf(" 语文不及格的有%10s%10s%8s\n","学号","姓名","语文");
for(a=0;a<num;a++)
{
if(students[a].Mark1<60)
printf("%10s%10s%8.2f\n",students[a].Name,students[a].ID,students[a].Mark1); //从头搜索到尾,若小于60就输出
}
printf(" 数学不及格的有%10s%10s%8s\n","学号","姓名","数学");
for(a=0;a<num;a++)
{
if(students[a].Mark2<60)
printf("%10s%10s%8.2f\n",students[a].Name,students[a].ID,students[a].Mark2);
}
printf(" 英语不及格的有%10s%10s%8s\n","学号","姓名","英语");
for(a=0;a<num;a++)
{
if(students[a].Mark3<60)
printf("%10s%10s%8.2f\n",students[a].Name,students[a].ID,students[a].Mark3);
}
printf(" 计算机不及格的有%10s%10s%8s\n","学号","姓名","计算机");
for(a=0;a<num;a++)
{
if(students[a].Mark4<60)
printf("%10s%10s%8.2f\n",students[a].Name,students[a].ID,students[a].Mark4);
}
system("pause");
}
void SearchHigh()//搜索成绩最高者输出
{
int a;
int max ;
printf(" 语文最高分为%10s%10s%8s\n","学号","姓名","语文");
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(" 数学最高分为%10s%10s%8s\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(" 英语最高分为%10s%10s%8s\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(" 计算机最高分为%10s%10s%8s\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);
}
system("pause");
}
void Save()
{
FILE*fp = fopen("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 Load()
{
FILE*fp = fopen("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\t7. 按平均成绩排序\n\n");
printf("\t\t\t\t\t8. 输出各科目不及格学生\n\n");
printf("\t\t\t\t\t9. 输出各科目最高分\n\n");
printf("\t\t\t\t\t0. 退出\n\n\n");
printf("请选择(0-9):");
scanf("%d",&i);
switch(i){
case 1:inputt();break;
case 2:modify();break;
case 3:deletee();break;
case 4:insert();break;
case 5:display();break;
case 6:search();break;
case 7:sort();break;
case 8:SearchLow();break;
case 9:SearchHigh();break;
case 0:exit(0);
default: ;
}
Save();
}
return 0;
}
代码进行了以下修改
1.关于含有数组成员的结构体拷贝的问题
2.对学号的唯一,进行了限定(inputt函数)
3.增加了从文件读写
现存问题
1.输出格式
2.插入时(insert)没对学号进行唯一性判断
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/151589.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...