初学C++做的一个小系统,这是一个简单的控制台系统,运行之后所有的操作都在控制台中完成。
有人指出代码中使用了C语言库的函数,
scanf()
是C语言库#include <stdio.h>
中的函数,下面程序将scanf()
改成cin >> a
这种方式。
#include <iostream>
//这里注释掉C语言库
//#include <stdio.h>
#include <string>
#include <algorithm>
#include <vector>
#include <sstream>
#include <iomanip>
using namespace std;
//定义二维数组,最多8名学生,每名学生拥有一个学号和三门科目成绩
const int a = 8;
const int b = 4;
const int c = 2;
int info[a][b];
//菜单ID
int menuId;
//学号
int stuId;
//数学
int math;
//英语
int eng;
//政治
int pol;
//定义函数
//主菜单
void menu();
//选择菜menuId
void searchMenu(int id);
//信息录入
void saveStu();
//信息显示
void stuInfo();
//查找功能
void selectStu();
//信息修改
void updateStu();
//成绩排序
void sortScore();
//成绩评定
void grade();
//返回主菜单
void fullBack();
bool cmp(vector<float> a, vector<float> b){
if(a[1] != b[1]) return a[1] > b[1];
if(a[0] != b[0]) return a[0] > b[0];
}
int main() {
menu();
return 0;
}
void menu(){
cout << "┌─────────────────────────────────────────────────────────────┐" << endl;
cout << "| 欢迎使用学生成绩管理系统 |" << endl;
cout << "├─────────────────────────────────────────────────────────────┤" << endl;
cout << "| 请输入菜单编号进行操作! |" << endl;
cout << "├─────────────────────────────────────────────────────────────┤" << endl;
cout << "|1.信息录入:录入学生成绩信息 |" << endl;
cout << "├─────────────────────────────────────────────────────────────┤" << endl;
cout << "|2.信息显示:显示每位学生记录 |" << endl;
cout << "├─────────────────────────────────────────────────────────────┤" << endl;
cout << "|3.查找功能:根据学号查找学生的相关记录,并显示 |" << endl;
cout << "├─────────────────────────────────────────────────────────────┤" << endl;
cout << "|4.信息修改:根据学号查找学生并修改学生成绩 |" << endl;
cout << "├─────────────────────────────────────────────────────────────┤" << endl;
cout << "|5.成绩排序:算各位同学的加权平均成绩,并根据平均成绩进行排序 |" << endl;
cout << "├─────────────────────────────────────────────────────────────┤" << endl;
cout << "|6.成绩评定:根据学生平均成绩进行综合成绩评定 |" << endl;
cout << "└─────────────────────────────────────────────────────────────┘" << endl;
cout << "请输入菜单序号进行操作:" << endl;
//scanf("%d",&menuId);
cin >> menuId;
searchMenu(menuId);
}
//选择菜单
void searchMenu(int id){
switch (id) {
case 0 :
menu();
break;
case 1 :
saveStu();
fullBack();
break;
case 2:
stuInfo();
fullBack();
break;
case 3:
selectStu();
fullBack();
break;
case 4:
stuInfo();
updateStu();
fullBack();
break;
case 5:
sortScore();
fullBack();
break;
case 6:
grade();
fullBack();
break;
default:
cout << "您输入的序号有误,请重新输入!" << endl;
menu();
}
}
//学生信息
void stuInfo(){
cout << "┌───────┬───────┬────────┬───────┬───────┐" << endl;
cout << "| 序号 | 学号 | 高数 | 英语 | 政治 |" << endl;
cout << "├───────┼───────┼────────┼───────┼───────┤" << endl;
cout << "| 1 | "+to_string(info[0][0])+" | "+to_string(info[0][1])+" | "+to_string(info[0][2])+" | "+to_string(info[0][3])+" |" << endl;
cout << "├───────┼───────┼────────┼───────┼───────┤" << endl;
cout << "| 2 | "+to_string(info[1][0])+" | "+to_string(info[1][1])+" | "+to_string(info[1][2])+" | "+to_string(info[1][3])+" |" << endl;
cout << "├───────┼───────┼────────┼───────┼───────┤" << endl;
cout << "| 3 | "+to_string(info[2][0])+" | "+to_string(info[2][1])+" | "+to_string(info[2][2])+" | "+to_string(info[2][3])+" |" << endl;
cout << "├───────┼───────┼────────┼───────┼───────┤" << endl;
cout << "| 4 | "+to_string(info[3][0])+" | "+to_string(info[3][1])+" | "+to_string(info[3][2])+" | "+to_string(info[3][3])+" |" << endl;
cout << "├───────┼───────┼────────┼───────┼───────┤" << endl;
cout << "| 5 | "+to_string(info[4][0])+" | "+to_string(info[4][1])+" | "+to_string(info[4][2])+" | "+to_string(info[4][3])+" |" << endl;
cout << "├───────┼───────┼────────┼───────┼───────┤" << endl;
cout << "| 6 | "+to_string(info[5][0])+" | "+to_string(info[5][1])+" | "+to_string(info[5][2])+" | "+to_string(info[5][3])+" |" << endl;
cout << "├───────┼───────┼────────┼───────┼───────┤" << endl;
cout << "| 7 | "+to_string(info[6][0])+" | "+to_string(info[6][1])+" | "+to_string(info[6][2])+" | "+to_string(info[6][3])+" |" << endl;
cout << "├───────┼───────┼────────┼───────┼───────┤" << endl;
cout << "| 8 | "+to_string(info[7][0])+" | "+to_string(info[7][1])+" | "+to_string(info[7][2])+" | "+to_string(info[7][3])+" |" << endl;
cout << "└────────────────────────────────────────┘" << endl;
}
//录入学生信息
void saveStu(){
cout << "请输入学生学号,学号必须大于0:" << endl;
//scanf("%d",&stuId);
cin >> stuId;
if(stuId==0){
cout << "学号输入有误,请重新输入!:" << endl;
return;
}
cout << "请输入学生数学成绩:" << endl;
//scanf("%d",&math);
cin >> math;
cout << "请输入学生英语成绩:" << endl;
//scanf("%d",&eng);
cin >> eng;
cout << "请输入学生政治成绩:" << endl;
//scanf("%d",&pol);
cin >> pol;
printf("学号=%d,数学=%d,英语=%d,政治=%d\n",stuId,math,eng,pol);
int infoTemp[4] = {stuId,math,eng,pol};
for(int i = 0;i<8;i++){
if(info[i][0]==stuId){
cout << "学号已存在,请重新输入!:" << endl;
break;
}
if((info[i][0]==0||info[i][0]<0)&&info[i][0]!=stuId){
for(int j = 0 ; j < 4;j++){
info[i][j]=infoTemp[j];
}
break;
}
}
stuInfo();
}
//查询学生信息
void selectStu(){
cout << "请输入需要查询的学生学号,学号必须大于0:" << endl;
//scanf("%d",&stuId);
cin >> stuId;
if(stuId==0){
cout << "学号输入有误,请重新输入!:" << endl;
return;
}
int num;
for(int i = 0 ; i < 8 ; i++){
if(info[i][0]==stuId){
cout << "┌───────┬───────┬────────┬───────┬───────┐" << endl;
cout << "| 序号 | 学号 | 高数 | 英语 | 政治 |" << endl;
cout << "├───────┼───────┼────────┼───────┼───────┤" << endl;
cout << "| "+to_string(i+1)+" | "+to_string(info[i][0])+" | "+to_string(info[i][1])+" | "+to_string(info[i][2])+" | "+to_string(info[i][3])+" |" << endl;
cout << "└────────────────────────────────────────┘" << endl;
num+=1;
}
}
if(num<=0){
cout << "很抱歉,未查询到结果!" << endl;
}
}
//修改学生信息
void updateStu(){
cout << "请输入需要修改的学生学号,学号必须大于0:" << endl;
//scanf("%d",&stuId);
cin >> stuId;
if(stuId==0){
cout << "学号输入有误,请重新输入!:" << endl;
return;
}
int updateFlag=0;
for(int i = 0 ; i < 8 ; i++){
if(info[i][0]==stuId){
cout << "请输入学生数学成绩:" << endl;
//scanf("%d",&math);
cin >> math;
cout << "请输入学生英语成绩:" << endl;
//scanf("%d",&eng);
cin >> eng;
cout << "请输入学生政治成绩:" << endl;
//scanf("%d",&pol);
cin >> pol;
printf("学号=%d,数学=%d,英语=%d,政治=%d\n",stuId,math,eng,pol);
int infoTemp[3] = {math,eng,pol};
for(int j = 0 ; j < 3;j++){
info[i][j+1]=infoTemp[j];
}
updateFlag=1;
break;
}
}
if(updateFlag==0){
cout << "输入的学号不存在!" << endl;
searchMenu(4);
}else{
stuInfo();
}
};
//加权平均值排序
void sortScore(){
vector<vector<float>> vec;
for(int i = 0 ; i < 8 ; i++){
if(info[i][0]!=0 && info[i][0]>0) {
//计算加权平均值
float wavg = info[i][1]*0.3+info[i][2]*0.4+info[i][3]*0.3;
vec.insert(vec.begin()+i,{(float)info[i][0],wavg});
}
}
if(vec.size()==0){
cout << "暂无学生成绩信息,请返回添加!" << endl;
return;
}
sort(vec.begin(), vec.end(), cmp);
cout << "┌───────┬───────┬──────────┐" << endl;
cout << "| 序号 | 学号 | 平均分 |" << endl;
cout << "├───────┼───────┼──────────┤" << endl;
for(int i=0;i<vec.size();i++){
string str = to_string(vec[i][1]);
stringstream ss;
ss << std::setprecision(3) << vec[i][1];
str = ss.str();
cout << "| "+to_string(i+1)+" | "+to_string((int)vec[i][0])+" | "+str+" |" << endl;
}
cout << "└──────────────────────────┘" << endl;
};
void grade(){
vector<vector<float>> vec;
for(int i = 0 ; i < 8 ; i++){
if(info[i][0]!=0 && info[i][0]>0) {
//计算加权平均值
float wavg = info[i][1]*0.3+info[i][2]*0.4+info[i][3]*0.3;
vec.insert(vec.begin()+i,{(float)info[i][0],wavg});
}
}
if(vec.size()==0){
cout << "暂无学生成绩信息,请返回添加!" << endl;
return;
}
sort(vec.begin(), vec.end(), cmp);
cout << "┌───────┬───────┬────────────┐" << endl;
cout << "| 序号 | 学号 | 成绩评定 |" << endl;
cout << "├───────┼───────┼────────────┤" << endl;
for(int i=0;i<vec.size();i++){
string str = "";
if(vec[i][1]>=90){str = "优秀";}
if(vec[i][1]<90&&vec[i][1]>=70){str = "良好";}
if(vec[i][1]<70&&vec[i][1]>=60){str = "及格";}
if(vec[i][1]<60){str = "不及格";}
cout << "| "+to_string(i+1)+" | "+to_string((int)vec[i][0])+" | "+str+" |" << endl;
}
cout << "└──────────────────────────┘" << endl;
};
void fullBack(){
cout << "输入“0”可返回主菜单:" << endl;
//scanf("%d",&menuId);
cin >> menuId;
searchMenu(menuId);
};