C语言实现学生管理系统
1、需求设计
2、效果展示
封面:
录入界面:
成绩排名:
删除:
删除后排名:
新增:
标题:
查询:
查询菜单:
注:背景颜色是窗口属性调节的,不是代码生成的!
3、功能设计
3.1 头文件
#include<iostream> //引用C++的输入输出流,便于字符串处理
#include<conio.h> //引用控制台头文件,调用函数getch()等待键盘输入
#include<stdlib.h> //C语言标准库头文件可调用多种基础功能
#include<stdio.h> //C语言的输入输出头文件
using namespace std; //调用C++标准库,功能多样
3.2 学生结构体
struct students
{
string name;//姓名
string num;//学号
int chinese;//语文成绩
int math;//数学成绩
int english;//英语成绩
int sum;//总分
int rank;//排名
}stu[100];
3.3 中间学生结构体
//用于后续“删除”功能的实现,具体解释将在后面提供。
struct student
{
string name;//姓名
string num;//学号
int chinese;//语文成绩
int math;//数学成绩
int english;//英语成绩
int sum;//总分
int rank;//排名
}_stu[100];
3.4 函数声明
void cover();//封面函数声明
void menu_main();//主菜单函数声明
void menu_search();//搜索菜单函数声明
void menu_single();//单科成绩菜单函数声明
3.5 录入学生信息
cout<<"Please enter the number of students:"<<endl;
cin>>n;
cout<<"Please enter student information:"<<endl;
cout<<"INCLUDE:";
for(int i = 1; i <= n; i++)
{
cout<<endl<<"Name : "; cin>>stu[i].name;
cout<<"Numbers : "; cin>>stu[i].num;
cout<<"Chinese score : "; cin>>stu[i].chinese;
cout<<"Math score : "; cin>>stu[i].math;
cout<<"English score : "; cin>>stu[i].english;
stu[i].sum = stu[i].chinese + stu[i].math + stu[i].english;
}
3.6 学生成绩排名
//冒泡排序算法
for(int i = 1; i <= n ; i++)
{
for(int j = i+1 ; j <= n ; j++)
{
if(stu[j].sum>stu[i].sum)
{
//根据总分排序
int temp = stu[j].sum;
stu[j].sum = stu[i].sum;
stu[i].sum = temp;
//更换学号
string temp0 = stu[j].num;
stu[j].num = stu[i].num;
stu[i].num = temp0;
//更换名字
string temp1 = stu[j].name;
stu[j].name = stu[i].name;
stu[i].name = temp1;
//更换语文成绩
int temp2 = stu[j].chinese;
stu[j].chinese = stu[i].chinese;
stu[i].chinese = temp2;
//更换数学成绩
int temp3 = stu[j].math;
stu[j].math = stu[i].math;stu[i].math = temp3;
// 更换英语成绩
int temp4 = stu[j].english;
stu[j].english = stu[i].english;
stu[i].english = temp4;
}
}
}
3.7 删除学生信息
算法:

string del_name;
// struct students _stu[100];
cout<<"Please enter the name of the student whose grade you wish to delete(请输入你要删除成绩的学生的姓名):";
cin>>del_name;
for(int i = 1; i <= n; i++)
{
if(del_name == stu[i].name)//找到了
{
// _stu[i].name = "\\";// \ -是转义字符
// _stu[i].num = "\\" ;
for(int j = 1; j <= i-1; j++)
{
_stu[j].name = stu[j].name;//将i前面的数据转移
_stu[j].num = stu[j].num;
_stu[j].chinese = stu[j].chinese;
_stu[j].math = stu[j].math;
_stu[j].english = stu[j].english;
_stu[j].sum = stu[j].sum;
}
//显然,_stu[i]中和i对应的数据是空的,但是没关系,重新排序就好了 ,此行必在最后面
//name = ' ',num = ' ', c = 0, m = 0, e = 0 ,sum = 0,r = ?
for(int j = i+1; j <= n; j++)
{
_stu[j-1].name = stu[j].name;//将i后面的数据转移
_stu[j-1].num = stu[j].num;
_stu[j-1].chinese = stu[j].chinese;
_stu[j-1].math = stu[j].math;
_stu[j-1].english = stu[j].english;
_stu[j-1].sum = stu[j].sum;
}
n = n - 1;//关键步骤, 别忘了让整体的数组元素减少一个
for(int k = 1; k <= n; k++)
{
stu[k].name = _stu[k].name;//换回数据
stu[k].num = _stu[k].num;
stu[k].chinese = _stu[k].chinese;
stu[k].math = _stu[k].math;
stu[k].english = _stu[k].english;
stu[k].sum = _stu[k].sum;
}
}
3.8 查询学生成绩
string name,_name,num;
int chinese,math,english,sum;
cout<<"Please enter the name of the person whose score you want to change:(你要修改谁的成绩?):";
cin>>name;
cout<<"Please re-enter the correct information (请重新输入正确的信息):"<<endl;
cout<<"Name : "<<' '; cin>>_name;
cout<<"Number : "<<' '; cin>>num;
cout<<"Chinese : "<<' '; cin>>chinese;
cout<<"English : "<<' '; cin>>english;
cout<<"Math : "<<' '; cin>>math;
sum = chinese + english + math;
for(int i = 1; i <= n; i++)
{
if(name == stu[i].name)
{
stu[i].name = _name;
stu[i].num = num;
stu[i].chinese = chinese;
stu[i].math = math;
stu[i].english = english;
stu[i].sum = sum;
}
}
3.9 显示学生成绩
system("cls");//清屏函数
cout<<"Student grades are as follows:"<<endl;
cout<<"学号\t姓名\t语文\t数学\t英语\t总分\t排名"<<endl;
for(int i = 1; i <= n; i++)
{
stu[i].rank = i;
cout<<stu[i].num<<'\t'<<stu[i].name<<'\t'<<stu[i].chinese<<'\t'
<<stu[i].math<<'\t'<<stu[i].english<<'\t'<<stu[i].sum<<'\t'
<<stu[i].rank<<endl;
}
getch();
注:单科成绩单与不及格名单都在查询功能模块内,详细功能请参阅下面的完整代码。
4、源代码
#include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
struct students
{
string name;//姓名
string num;//学号
int chinese;//语文成绩
int math;//数学成绩
int english;//英语成绩
int sum;//总分
int rank;//排名
}stu[100];
//替身结构体
struct student
{
string name;//姓名
string num;//学号
int chinese;//语文成绩
int math;//数学成绩
int english;//英语成绩
int sum;//总分
int rank;//排名
}_stu[100];
void cover();//封面函数声明
void menu_main();
void menu_search();
void menu_single();
int main()
{
cover();
getch();
int n;
int flag;
system("cls");
cout<<"请输入学生人数:"<<endl;
cin>>n;
cout<<"请录入学生信息:"<<endl;
cout<<"包含以下内容:"<<endl;
for(int i = 1; i <= n; i++)
{
cout<<"----------------第 "<<i<<" 名学生的数据:-------------------"<<endl;
cout<<"姓名 : "; cin>>stu[i].name;
cout<<"学号 : "; cin>>stu[i].num;
cout<<"语文成绩 : "; cin>>stu[i].chinese;
cout<<"数学成绩 : "; cin>>stu[i].math;
cout<<"英语成绩 : "; cin>>stu[i].english;
stu[i].sum = stu[i].chinese + stu[i].math + stu[i].english;
}
do{
menu_main();
cin>>flag;
if(flag == 1)//新增一位学生
{
n = n+1;
cout<<"请输入新学生的资料 : "<<endl;
cout<<"包含以下内容:";
cout<<endl<<"姓名 : "; cin>>stu[n].name;
cout<<"学号 : "; cin>>stu[n].num;
cout<<"语文成绩 : "; cin><stu[n].chinese;
cout<<"数学成绩 : "; cin>>stu[n].math;
cout<<"English score : "; cin>>stu[n].english;
stu[n].sum = stu[n].chinese + stu[n].math + stu[n].english;
cout<<"Add to complete (^v^)!"<<endl;
getch();
}
if(flag == 2)//成绩排名
{
//冒泡排序
for(int i = 1; i <= n ; i++)
{
for(int j = i+1 ; j <= n ; j++)
{
if(stu[j].sum>stu[i].sum)
{
//根据总分排序
int temp = stu[j].sum;
stu[j].sum = stu[i].sum;
stu[i].sum = temp;
//更换学号
string temp0 = stu[j].num;
stu[j].num = stu[i].num;
stu[i].num = temp0;
//更换名字
string temp1 = stu[j].name;
stu[j].name = stu[i].name;
stu[i].name = temp1;
//更换语文成绩
int temp2 = stu[j].chinese;
stu[j].chinese = stu[i].chinese;
stu[i].chinese = temp2;
//更换数学成绩
int temp3 = stu[j].math;
stu[j].math = stu[i].math;
stu[i].math = temp3;
//更换英语成绩
int temp4 = stu[j].english;
stu[j].english = stu[i].english;
stu[i].english = temp4;
}
}
}
//排名
for(int i = 1; i <= n; i++)
{
stu[i].rank = i;
}
cout<<"The student ranking is complete !(^v^)!"<<endl;
getch();
}
if(flag == 3)//查询学生成绩
{
int mark;
do{
menu_search();
cin>>mark;
if(mark == 1)
{
string num;
cout<<"Input number:";
cin>>num;
for(int i = 1; i <= n; i++)
{
if(num == stu[i].num)
{
cout<<"Name : "<<stu[i].name<<endl;
cout<<"Chinese score : "<<stu[i].chinese<<endl;
cout<<"Math score : "<<stu[i].math<<endl;
cout<<"English score : "<<stu[i].english<<endl;
cout<<"Sum of scores : "<<stu[i].sum<<endl;
cout<<"Student rank : "<<stu[i].rank<<endl;
getch();
}
}
}
if(mark == 2)
{
string name;
cout<<"Input name : ";
cin>>name;
for(int i = 1; i <= n; i++)
{
if(name == stu[i].name)
{
cout<<"Number : "<<stu[i].num<<endl;
cout<<"Chinese score : "<<stu[i].chinese<<endl;
cout<<"Math score : "<<stu[i].math<<endl;
cout<<"English score : "<<stu[i].english<<endl;
cout<<"Sum of scores : "<<stu[i].sum<<endl;
cout<<"Student rank : "<<stu[i].rank<<endl;
getch();
}
}
}
if(mark == 3)
{
int sum;
cout<<"Input sum of score : ";
cin>>sum;
for(int i = 1; i <= n; i++)
{
if(sum == stu[i].sum)
{
cout<<"Number : "<<stu[i].num<<endl;
cout<<"Name : "<<stu[i].name<<endl;
cout<<"Chinese score : "<<stu[i].chinese<<endl;
cout<<"Math score : "<<stu[i].math<<endl;
cout<<"English score : "<<stu[i].english<<endl;
cout<<"Rank of student "<<stu[i].rank<<endl;
getch();
}
}
}
if(mark == 4)
{
int find;
int search;
do{
menu_single();
cin>>find;
if(find == 1)//查语文
{
for(int i = 1; i <= n; i++)
{
_stu[i].name = stu[i].name;
_stu[i].num = stu[i].num;
_stu[i].chinese = stu[i].chinese;
}
for(int i = 1; i <= n; i++)
{
for(int j = i+1; j <= n; j++)
{
if(_stu[i].chinese < _stu[j].chinese)
{ //更换语文成绩
int temp2 = _stu[j].chinese;
_stu[j].chinese = _stu[i].chinese;
_stu[i].chinese = temp2;
//更换学号
string temp0 = _stu[j].num;
_stu[j].num = _stu[i].num;
_stu[i].num = temp0;
//更换名字
string temp1 = _stu[j].name;
_stu[j].name = _stu[i].name;
_stu[i].name = temp1;
}
}
}
cout<<"Chinese transcripts : "<<endl;
cout<<"Name\tNumber\tChinese : "<<endl;
for(int i = 1; i <= n; i++)
{
cout<<_stu[i].name<<'\t'<<_stu[i].num<<'\t'<<_stu[i].chinese<<endl;
}
cout<<"Failure list of Chinese grades (score < 60 ): "<<endl;
cout<<"Name\tNumber\tChinese : "<<endl;
for(int i = 1; i <= n; i++)
{
if(_stu[i].chinese < 60)
cout<<_stu[i].name<<'\t'<<_stu[i].num<<'\t'<<_stu[i].chinese<<endl;
}
cout<<"For a quick search, press 5 (快捷搜索); Return to the parent directory, press 0"<<endl;
cin>>search;
if(search == 5)
{
do{
system("cls");
int con;
string S_name,S_num;
cout<<"[Quick search]"<<endl<<endl;
cout<<"Press '1' to continue, press '0' to stop."<<endl;
getch();
cin>>con;
if(con == 1)
{
cout<<"Please enter the name and student id"<<endl;
```
cout<<"Name: "; cin>>S_name; cout<<"ID : "; cin>>S_num;
for(int i = 1; i <= n; i++)
{
if(S_name == _stu[i].name && S_num == _stu[i].num)
{
cout<<"Chinese : "<<_stu[i].chinese<<endl;
}
}
getch();
}
if(con == 0)
search = 0;
} while( search != 0);
}
cout<<"After the display is complete, press any key to return to the parent directory (@^V^@);"<<endl;
getch();
}
if(find == 2)//查数学
{
for(int i = 1; i <= n; i++)
{
_stu[i].name = stu[i].name;
_stu[i].num = stu[i].num;
_stu[i].math = stu[i].math;
}
for(int i = 1; i <= n; i++)
{
for(int j = i + 1; j <= n; j++)
{
if(_stu[i].math < _stu[j].math)
{ //更换数学成绩
int temp2 = _stu[j].math;
_stu[j].math = _stu[i].math;
_stu[i].math = temp2;
//更换学号
string temp0 = _stu[j].num;
_stu[j].num = _stu[i].num;
_stu[i].num = temp0;
//更换名字
string temp1 = _stu[j].name;
_stu[j].name = _stu[i].name;
_stu[i].name = temp1;
}
}
}
cout<<"math transcripts : "<<endl;
cout<<"Name\tNumber\tMath : "<<endl;
for(int i = 1; i <= n; i++)
{
cout<<_stu[i].name<<'\t'<<_stu[i].num<<'\t'<<_stu[i].math<<endl;
}
cout<<"Failure list of math grades (score < 60 ): "<<endl;
cout<<"Name\tNumber\tMath : "<<endl;
for(int i = 1; i <= n; i++)
{
if(_stu[i].math < 60)
cout<<_stu[i].name<<'\t'<<_stu[i].num<<'\t'<<_stu[i].math<<endl;
}
cout<<"For a quick search, press 5,(快捷搜索);"<<"Return to the parent directory , press 0 "<<endl;
cin>>search;
if(search == 5)
{
do{
system("cls");
int con;
string S_name, S_num;
cout<<"[Quick search]"<<endl<<endl;
cout<<"Press '1' to continue, press '0' to stop ."<<endl;
getch();
cin>>con;
if(con == 1)
{
cout<<"Please enter the name and student id"<<endl;
cout<<"Name: "; cin>>S_name; cout<<"ID : "; cin>>S_num;
for(int i = 1; i <= n; i++)
{
if(S_name == _stu[i].name && S_num == _stu[i].num)
{
cout<<"Math : "<<_stu[i].math<<endl;
}
}
getch();
}
if(con == 0)
search = 0;
}while( search != 0);
}
cout<<"After the display is complete, press any key to return to the parent directory(@^V^@);"<<endl;
getch();
}
if(find == 3)// 查英语
{
for(int i = 1; i <= n; i++)
{
_stu[i].name = stu[i].name;
_stu[i].num = stu[i].num;
_stu[i].english = stu[i].english;
}
for(int i = 1; i <= n; i++)
{
for(int j = i + 1; j <= n; j++)
{
if(_stu[i].english < _stu[j].english)
{
// 更换英语成绩
int temp2 = _stu[j].english;
_stu[j].english = _stu[i].english;
_stu[i].english = temp2;
// 更换学号
string temp0 = _stu[j].num;
_stu[j].num = _stu[i].num;
_stu[i].num = temp0;
// 更换名字
string temp1 = _stu[j].name;
_stu[j].name = _stu[i].name;
_stu[i].name = temp1;
}
}
}
cout<<"英语成绩单 : "<<endl;
cout<<"姓名\t学号\t英语成绩 : "<<endl;
for(int i = 1; i <= n; i++)
{
cout<<_stu[i].name<<'\t'<<_stu[i].num<<'\t'<<_stu[i].english<<endl;
}
cout<<"英语成绩不及格名单 (分数 < 60 ) : "<<endl;
cout<<"姓名\t学号\t英语成绩 : "<<endl;
for(int i = 1; i <= n; i++)
{
if(_stu[i].english < 60)
cout<<_stu[i].name<<'\t'<<_stu[i].num<<'\t'<<_stu[i].english<<endl;
}
cout<<"为了快速搜索,请按 5,(快捷搜索);"<<"返回上级目录 ,请按 0 "<<endl;
cin>>search;
if(search == 5)
{
do{
system("cls");
int con;
string S_name, S_num;
cout<<"[快速搜索]"<<endl<<endl;
cout<<"按 '1' 继续,按 '0' 停止 ."<<endl;
getch();
cin>>con;
if(con == 1)
{
cout<<"请输入姓名和学号"<<endl;
cout<<"姓名: "; cin>>S_name; cout<<"学号 : "; cin>>S_num;
for(int i = 1; i <= n; i++)
{
if(S_name == _stu[i].name && S_num == _stu[i].num)
{
cout<<"英语成绩 : "<<_stu[i].english<<endl;
}
}
getch();
}
if(con == 0)
search = 0;
}while( search != 0);
}
cout<<"After the display is complete, press any key to return to the parent directory(@^V^@);"<<endl;
getch();
}
}while(find != 0);
}
}while(mark != 0);
cout<<"All content is displayed"<<endl;
getch();
}
if(flag == 4)//修改成绩
{
string name,_name,num;
int chinese,math,english,sum;
cout<<"Please enter the name of the person whose score you wish to change:(你要修改谁的成绩?):";
cin>>name;
cout<<"Please re-enter the correct information (请重新输入正确的信息):"<<endl;
cout<<"Name : "<<' '; cin>>_name;
cout<<"Number : "<<' '; cin>>num;
cout<<"Chinese : "<<' '; cin>>chinese;
cout<<"English : "<<' '; cin>>english;
cout<<"Math : "<<' '; cin>>math;
sum = chinese + english + math;
for(int i = 1; i <= n; i++)
{
if(name == stu[i].name)
{
stu[i].name = _name;
stu[i].num = num;
stu[i].chinese = chinese;
stu[i].math = math;
stu[i].english = english;
stu[i].sum = sum;
}
}
cout<<"Modification completed !(^v^)!"<<endl;
getch();
}
if(flag == 5)//删除成绩
{
string del_name;
// struct students _stu[100];
cout<<"Please enter the name of the student whose grade you wish to delete(请输入你要删除成绩的学生的姓名):";
cin>>del_name;
for(int i = 1; i <= n; i++)
{
if(del_name == stu[i].name)//找到了
{
// _stu[i].name = "\\";// \ -是转义字符
// _stu[i].num = "\\" ;
for(int j = 1; j <= i-1; j++)
{
_stu[j].name = stu[j].name;//将i前面的数据转移
_stu[j].num = stu[j].num;
_stu[j].chinese = stu[j].chinese;
_stu[j].math = stu[j].math;
_stu[j].english = stu[j].english;
_stu[j].sum = stu[j].sum;
}
//显然,_stu[i]中和i对应的数据是空的,但是没关系,重新排序就好了 ,此行必在最后面
//name = ' ',num = ' ', c = 0, m = 0, e = 0 ,sum = 0,r = ?
for(int j = i+1; j <= n; j++)
{
_stu[j-1].name = stu[j].name;//将i后面的数据转移
_stu[j-1].num = stu[j].num;
_stu[j-1].chinese = stu[j].chinese;
_stu[j-1].math = stu[j].math;
_stu[j-1].english = stu[j].english;
_stu[j-1].sum = stu[j].sum;
}
n = n - 1;//点睛之笔, 别忘了让整体的数组元素少一
for(int k = 1; k <= n; k++)
{
stu[k].name = _stu[k].name;//换回
stu[k].num = _stu[k].num;
stu[k].chinese = _stu[k].chinese;
stu[k].math = _stu[k].math;
stu[k].english = _stu[k].english;
stu[k].sum = _stu[k].sum;
}
}
}
cout<<"删除已完成,请重新排序(Delete finished, please reorder)!(^v^)!"<<endl;
getch();
}
if(flag == 6)//展示学生分数详情
{
system("cls");
cout<<"学生分数如下:"<<endl;
cout<<"学号\t姓名\t语文\t数学\t英语\t总分\t排名"<<endl;
for(int i = 1; i <= n; i++)
{
stu[i].rank = i;
cout<<stu[i].num<<'\t'<<stu[i].name<<'\t'<<stu[i].
chinese<<'\t'<<stu[i].math<<'\t'<<stu[i].english<<
'\t'<<stu[i].sum<<'\t'<<stu[i].rank<<endl;
}
getch();
}
}while(flag != 0);
return 0;
}
void cover()
{
cout<<"====================================================================================="<<endl<<"*"
<<" *"<<endl<<"*"
<<" *"<<endl<<"*"
<<" *"<<endl<<"*"
<<" *"<<endl<<"*"
<<" *"<<endl<<"*"
<<" *"<<endl<<"*"
<<" *"<<endl<<"*"
<<" *"<<endl<<"*"
<<" *"<<endl;
cout<<"* 欢迎使用学生成绩管理系统 *"<<endl;
cout<<"* ( 欢 迎 使 用 学 生 成 绩 管 理 系 统 ) *" <<endl;
cout<<"* *"<<endl
<<"* *"<<endl
<<"* *"<<endl
<<"* *"<<endl
<<"* *"<<endl
<<"* *"<<endl
<<"* *"<<endl
<<"* *"<<endl
<<"* *"<<endl
<<"* *"<<endl
<<"=====================================================================================";
}
void main_menu()//主页面菜单
{
system("cls");
cout<<endl;
cout<<"====================================================================================="<<endl;
cout<<"* 主页菜单 *"<<endl;
cout<<"====================================================================================="<<endl;
cout<<"* 1 : Add(添加) new classmate data; *"<<endl;
cout<<"* 2 : Student Performance Ranking(排名); *"<<endl;
cout<<"* 3 : Query(查询) student grade details; *"<<endl;
cout<<"* 4 : Modify(修改) student grade data; *"<<endl;
cout<<"* 5 : Remove(删除) student grade records; *"<<endl;
cout<<"* 6 : Show(展示) student details; *"<<endl;
cout<<"* 0 : Pressing '0' : Exit to system ; *"<<endl;
cout<<"====================================================================================="<<endl;
}
void menu_search()//查询菜单
{
system("cls");
cout<<endl;
cout<<"-------------------------------------------------------------------------------------"<<endl;
cout<<"* Inquiry Options: *"<<endl;
cout<<"-------------------------------------------------------------------------------------"<<endl;
cout<<"* 1:Student ID query; *"<<endl;
cout<<"* 2:Name search; *"<<endl;
cout<<"* 3:Total score inquiry; *"<<endl;
cout<<"* 4: Single subject grade(单科成绩) query *"<<endl;
cout<<"* and failure statistics(不及格统计); *"<<endl;
cout<<"* 0: Go back to main menu. *"<<endl;
cout<<"-------------------------------------------------------------------------------------"<<endl;
}
void menu_single()//单科查询
{
system("cls");
cout<<endl;
cout<<"-------------------------------------------------------------------------------------"<<endl;
cout<<"* 学科区域 : *"<<endl;
cout<<"-------------------------------------------------------------------------------------"<<endl;
cout<<"* 1: 语文成绩; *"<<endl;
cout<<"* 2: 数学成绩; *"<<endl;
cout<<"* 3: 英语成绩; *"<<endl;
cout<<"* 0: 返回上级目录; *"<<endl;
cout<<"* 注意:按1或2或3键选择; *"<<endl;
cout<<"-------------------------------------------------------------------------------------"<<endl;
}


雷达卡


京公网安备 11010802022788号







