C/C++知识点之C++ 实现简单命令行学生管理系统
小标 2018-08-10 来源 : 阅读 1215 评论 0

摘要:本文主要向大家介绍了C/C++知识点之C++ 实现简单命令行学生管理系统,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

本文主要向大家介绍了C/C++知识点之C++ 实现简单命令行学生管理系统,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。

编译环境是macOS。system(“clear”) 在windows下请换成 system(“cls”)

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
using namespace std;

class Student {                                                     // 学生base类
public:
    static int ENGSUM;
    static int CHISUM;
    static int MATSUM;
    explicit Student(string &Nname,string &NID,string &Neng,string &Nchi,string &Nmat)
            :name(Nname),ID(NID),eng_score(Neng),chi_score(Nchi),math_score(Nmat)
    { sumup();}
    const string& getID() const{ return ID; }
    const string& getName() const{ return name; }
    const string& getEng() const{ return eng_score; }
    const string& getChi() const{ return chi_score; }
    const string& getMat() const{ return math_score; }
    const int     getsum() const{ return scores_sum; }
    void          changeEng(string &newEng){ eng_score = newEng; }
    void          changeChi(string &newChi){ chi_score = newChi; }
    void          changeMat(string &newMat){ math_score = newMat; }
    void          sumup(){
        int e = stoi(eng_score); int c = stoi(chi_score); int m = stoi(math_score); scores_sum = e+c+m;
    }
private:
    string name;
    string ID;
    string eng_score;
    string chi_score;
    string math_score;
    int scores_sum;                                                 // 单个学生的成绩总和
};
int Student::ENGSUM = 0;                                            // 静态变量,代表学生这个整体的某科成绩总和
int Student::CHISUM = 0;
int Student::MATSUM = 0;

Student* __lookup(vector<Student *>& stus,string &target);          // 核心查找函数
void     launcher();                                                // 主逻辑实现
void     display(int currentSize);                                  // 界面
void     append(vector<Student *>& stus);                           // 添加
Student* displaySingleInfo(vector<Student *>& stus,string target);  // 查找并显示信息
void     change(vector<Student *>& stus);                           // 修改
void     del(vector<Student *>& stus);                              // 删除
void     search(vector<Student *>& stus);                           // 查找
void     quitSys(vector<Student *>& stus);                          // 关闭并清理内存
void     displayAllInfos(vector<Student *>& stus);                  // 显示所有学生信息,被用作子函数
bool     comp(const Student* a,const Student* b){                   // 排序依据实现
    return a->getsum()>b->getsum();
}

int main(){                                                         /**** 系统入口 ****/
    launcher();
    cout<< "再见" << endl;
    return 0;
}

// ****************************** 功能实现 ***************************************

void launcher(){
    vector<Student *> stus;
    string            _input;
    display(static_cast<int>(stus.size()));
    while(cin>>_input){
        char choice = _input.at(0);
        system("clear");                                            // 操作后清理屏幕,windows下请换成system("cls")
        switch(choice){
            case 'A':append(stus);
                break;
            case 'B':change(stus);
                break;
            case 'C':del(stus);
                break;
            case 'D':search(stus);
                break;
            case 'E':displayAllInfos(stus);
                getchar();
                break;
            case 'Q':quitSys(stus);
                return;
            default: cout << "无选项"<<choice<<",请重试。" << endl;
                break;
        }
        system("clear");                                            // 操作后清理屏幕,windows下请换成system("cls")
        display(static_cast<int>(stus.size()));
    }
}
void display(int currentSize){
    cout << "\n\n --------------------------------------------------------------------------------" << endl;
    cout << "| ~~~                            输入Q退出系统                               ~~~" << endl;
    cout << " --------------------------------------------------------------------------------" << endl;
    cout << "|\n";
    cout << "|                           * 正在运行学生管理系统 *" << endl;
    cout << "|\n";
    cout << " --------------------------------------------------------------------------------" << endl;
    cout << "|" << endl;
    cout << "|              * 已记录" << currentSize << "个学生的档案 *       * 你还可以录入" << 100 - currentSize
         << "个学生 *" << endl;
    cout << "|\n";
    cout << " --------------------------------------------------------------------------------" << endl;
    cout << "|\n";
    cout << "|                   接下来,你想进行什么操作? (输入对应序号)" << endl;
    cout << "|\n";
    cout << "|  (A).添加       (B).修改       (C).删除       (D).查找      (E).查看所有学生档案" << endl;
    cout << "|\n";
    cout << " --------------------------------------------------------------------------------" << endl;
    cout<< "--> ";
}
void append(vector<Student *>& stus){                                   // 添加
    cout << "\n\n好的,现在开始添加学生: \n\n";
    cout << "请输入新学生的姓名、学号、英语成绩、语文成绩、数学成绩\n\n"
         << "例如: 小明 1704010625 129 120 134\n";
    cout << " ------------------------------------------------------" << endl;
    cout<< "--> ";
    string n,i,e,c,m;
    cin >> n >> i >> e >> c >> m;
    Student *newStu = new Student(n,i,e,c,m);
    stus.push_back(newStu);
    Student::ENGSUM += stoi(e); Student::CHISUM += stoi(c); Student::MATSUM += stoi(m);
}
void change(vector<Student *>& stus){                                   // 修改
    cout << "\n\n好的,现在开始进行修改操作: \n\n";
    cout << "请输入需要修改的学生的学号: \n"<<endl;
    cout<< "--> ";
    string target;
    cin >> target;
    Student *temp = displaySingleInfo(stus,target);
    if(temp != nullptr){
        cout << "\n你想改动" << temp->getName() << "的哪个成绩?" << endl;
        cout << "(A).English  (B).Chinese  (C).Math" << endl;
        cout << "--> ";
        string _input;
        cin >> _input;
        char ch = _input.at(0);
        string tmp;
        if(ch == 'A'){
            cout << "请输入新的英语成绩: ";
            cin >> tmp;
            temp->changeEng(tmp);
        }else if(ch == 'B'){
            cout << "请输入新的语文成绩: ";
            cin >> tmp;
            temp->changeChi(tmp);
        }else{
            cout << "请输入新的数学成绩: ";
            cin >> tmp;
            temp->changeMat(tmp);
        }
    }
}
void del(vector<Student *>& stus){                                      // 删除
    cout << "\n\n好的,现在开始进行删除操作: \n\n";
    cout << "请输入需要删除的学生的学号: \n"<<endl;
    cout<< "--> ";
    string target;
    cin >> target;
    vector<Student *>::iterator it;
    bool findIt = false;
    for(it = stus.begin();it != stus.end();){
        if((*it)->getID() == target){
            findIt = true;
            Student::ENGSUM -= stoi((*it)->getEng());
            Student::CHISUM -= stoi((*it)->getChi());
            Student::MATSUM -= stoi((*it)->getMat());
            it = stus.erase(it);
        }else
            ++it;
    }
    if(!findIt)
        cout << "未查找到,请重试!" << endl;
}
void search(vector<Student *>& stus){                                   // 查找
    cout << "\n\n好的,现在开始进行查找操作: \n\n";
    cout << "请输入需要查找的学生的学号: \n"<<endl;
    cout<< "--> ";
    string target;
    cin >> target;
    displaySingleInfo(stus,target);
}
void quitSys(vector<Student *>& stus){                                  // 清理内存
    vector<Student *>::iterator it;
    for(it = stus.begin();it != stus.end();it ++){
        delete (*it);
    }
}
Student* displaySingleInfo(vector<Student *>& stus,string target)       // 显示单个学生信息
{
    Student *temp = __lookup(stus,target);
    if(temp != nullptr){
        cout << "\n学生姓名:" << temp->getName() << "  学号:" << temp->getID() << endl;
        cout<< "--scores:"<<endl;
        cout << "English: " << temp->getEng() << " Chinese: " << temp->getChi()
             << " Math: " << temp->getMat() << endl;
    }else{
        cout << "未查找到,请重试!" << endl; //TODO
        return nullptr;
    }
    return temp;
}
void displayAllInfos(vector<Student *>& stus){                          // 显示总体信息
    int rank = 1;
    int len = static_cast<int>(stus.size());
    int sumEng = 0,sumChi = 0,sumMat = 0;
    sort(stus.begin(),stus.end(),comp);
    cout<< "\n\n\n------------------------------------------------------------\n";
    cout<< "                                             scores\n";
    cout<< "rank  "<<"Name          "<<"ID           "<<"English  "<<"Chinese  "<< "Math  "<<"Sum  \n";
    for(auto &stu : stus){
        cout.setf(ios::left);
        cout.width(6);  cout<<rank++;
        cout.width(14); cout<< stu->getName();
        cout.width(13); cout<< stu->getID();
        cout.width(9);  cout<< stu->getEng();
        cout.width(9);  cout<< stu->getChi();
        cout.width(6);  cout<< stu->getMat();
        cout<< stu->getsum()<<endl;
    }
    cout<< "\nsum                              ";
    cout.width(9); cout<<Student::ENGSUM;
    cout.width(9); cout<<Student::CHISUM;
    cout.width(6); cout<<Student::MATSUM<<endl;
    cout<< "average                          ";
    cout.width(9); cout<<Student::ENGSUM/len;
    cout.width(9); cout<<Student::CHISUM/len;
    cout.width(6); cout<<Student::MATSUM/len;
    cout<< "\n------------------------------------------------------------\n\n";
    cout<< "按下enter键以继续" <<endl;
    getchar();
}
Student* __lookup(vector<Student *>& stus,string &target){
    vector<Student *>::iterator it;
    Student *ptr = nullptr;
    for(it = stus.begin();it != stus.end();it ++){
        if((*it)->getID() == target)
            ptr = (*it);
    }
    return ptr;
}    

本文由职坐标整理并发布,了解更多内容,请关注职坐标编程语言C/C+频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程