学生成绩管理系统思考
作业要求:
目录
思路
基本函数
学习理解大佬的代码:
完成作业:
思路
学生成绩管理系统,首先要初始化系统,
用C语言做学生实验管理系统要求实现对某班学生3门课程(包括语文、数学、英语)成绩的管理功能,学生成绩的统计,如每个人的平均成绩、每门课的平均成绩等;,
按个人平均成绩从高到低的顺序进行排序。输出不及格学生名单。
基本函数
1.结构体 struct Student
定义一个结构体struct ,结构体的关键字,students 定义的结构体对象。学号char ID[20];姓名char Name[10]; 各项成绩float Mark,每个人的平均成绩float average1;
每个科目的平均成绩float average2;
2.录入(void inputt())
利用循环将数据从students[0]开始录入各项信息,计数器num+1,直至到3(内部数据中会有 0 1 2 )
3.修改(void modfiy())
利用循环将数组里面的信息与待修改的信息比对,若相同,对其进行重新复制即修改;若无此项,提示重新输入。
5.排序 (void sort()){按照题目要求此时应该有两次排序:每个人的平均成绩float average1; 每个科目的平均成绩float average2;}
排序的平均分,依次比较两个相邻的成绩,如果他们的顺序如果前小于后就把他们交换过来。走访元素的工作是重复地进行直到没有相邻元素需要交换,也就是说该元素已经排序完成。
利用什么方法:冒泡排序法
6.显示 (void display())
利用循环将已录入的信息打印出来。
7.查询 (void Student_SearchByIndex(){此时按照题目要求应该输出个人平均成绩从高到低的顺序以及不及格学生名单}
8.主函数 main()
利用无限次循环while和swithch()实现各函数的调用,系统根据输入的数字来操控相应的函数,同时显示可供选择的选项
学习理解大佬的代码:
学生成绩管理系统(C语言)_张十五的博客-CSDN博客_学生成绩管理系统
#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()函数的简介:
是将一个字符串复制到另一块空间地址中 的函数,‘\0’是停止拷贝的终止条件,
也复制到目标空间。下面是库中的strcpy()函数声明:
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;
}
}
}
voidsort()//根据平均分排序
(此时注意按照题目要求应该排序两个)
{
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;
}
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].Average1);
}
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].Average1);
}
fclose(fp);
}
完成作业:
#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 Average1;//平均成绩
} students[1000];
int num = 0; //计数器
void Copy(struct Student *arr, int i, int j) {
strcpy(arr[i].ID, arr[j].ID); //strcpy()函数的简介:
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].Average1 = arr[j].Average1;
}
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%10s%10s\n", "学号", "姓名", "语文", "数学", "英语",
"平均成绩");
printf("-------------------------------------------------------------\n");
printf("%10s%10s%8.2f%8.2f%8.2f%8.2f\n", students[index].ID, students[index].Name,
students[index].Mark1, students[index].Mark2, students[index].Mark3, students[index].Average1);
}
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();
students[num].Average1 = (students[num].Mark1 + students[num].Mark2 + students[num].Mark3) / 3;
if (Student_SearchByIndex(students[num].ID) == -1) {
num++; //移向下一个位置
} else {
printf("学号重复,输入数据无效 !!!\n");
}
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].Average1);
}
}
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].Average1 = (students[i].Mark1 + students[i].Mark2 + students[i].Mark3) / 3;
}
for (i = 0; i < num; i++) {
for (j = 1; j < num - i; j++) {
if (students[j - 1].Average1 < students[j].Average1) {
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\n", students[a].ID, students[a].Name,
students[a].Mark1, students[a].Mark2, students[a].Mark3, students[a].Average1);
}
}
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", "学号", "姓名", "计算机");
system("pause");
}
void Save() {
FILE *fp = fopen("temp2.txt", "w+");
fprintf(fp, "%d\n", num);
for (int i = 0 ; i < num ; i++) {
fscanf(fp, "%s %s %f %f %f %f \n", students[i].ID, students[i].Name, &students[i].Mark1, &students[i].Mark2,
&students[i].Mark3, &students[i].Average1);
}
fclose(fp);
}
void Load() {
FILE *fp = fopen("temp2.txt", "r");
fscanf(fp, "%d", &num);
for (int i = 0 ; i < num ; i++) {
fprintf(fp, "%s %s %f %f %f %f \n", students[i].ID, students[i].Name, students[i].Mark1, students[i].Mark2,
students[i].Mark3, students[i].Average1);
}
fclose(fp);
}
int main() {
int i;
float Average2;//语文平均成绩
float Average3;//数学平均成绩
float Average4;//英语平均成绩
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\t0. 退出\n\n\n");
printf("请选择(0-5):");
scanf("%d", &i);
switch (i) {
case 1:
inputt();
break;
case 2:
display();
break;
case 3:
search();
break;
case 4:
sort();
break;
case 5:
SearchLow();
break;
case 0:
exit(0);
default:
;
}
Save();
}
return 0;
}