码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 实现简单命令行学生管理系统

时间:2018-07-26 18:50:37      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:code   命令   ace   sys   news   tor   choice   gets   sum   

C++ 实现简单命令行学生管理系统

贴吧ID: 这把问题不大

编译环境是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++ 实现简单命令行学生管理系统

标签:code   命令   ace   sys   news   tor   choice   gets   sum   

原文地址:https://www.cnblogs.com/1Kasshole/p/9372607.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!