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

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

预览:

1395949-20180726180538959-1427539214.png

1395949-20180726180544771-34480178.png

1395949-20180726180550330-327230770.png

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

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

class Student {
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          changeSum(int value){ scores_sum += value; }
    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);
void     error(char choice);
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");
        switch(choice){
            case 'A':append(stus);
                break;
            case 'B':change(stus);
                break;
            case 'C':del(stus);
                break;
            case 'D':search(stus);
                getchar();
                break;
            case 'E':displayAllInfos(stus);
                getchar();
                break;
            case 'Q':quitSys(stus);
                return;
            default: error(choice);
                getchar();
                break;
        }
        system("clear");
        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);
    bool conti = true;
    while(temp != nullptr && conti == true){
        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;
            int sub = stoi(tmp) - stoi(temp->getEng());
            temp->changeSum(sub);
            Student::ENGSUM += sub;
            temp->changeEng(tmp);
        }else if(ch == 'B'){
            cout << "请输入新的语文成绩: ";
            cin >> tmp;
            int sub = stoi(tmp) - stoi(temp->getChi());
            temp->changeSum(sub);
            Student::CHISUM += sub;
            temp->changeChi(tmp);
        }else{
            cout << "请输入新的数学成绩: ";
            cin >> tmp;
            int sub = stoi(tmp) - stoi(temp->getMat());
            temp->changeSum(sub);
            Student::MATSUM += sub;
            temp->changeMat(tmp);
        }
        cout << "\n\n------------------------------------------------------------\n搞定.输入y继续修改,输入n返回菜单\n";
        cout << "--> ";
        string t;
        cin >> t;
        if(t.at(0)=='y')
             conti = true;
        else conti = false;
        cout << conti << endl;
        if(conti)
            temp = displaySingleInfo(stus,target);
    }
    if(temp == nullptr)
        cout << "未查找到,按enter返回!";
}
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);
            break;
        }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);
    cout<< "按下enter键以继续" <<endl;
    getchar();
}
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 << endl;
    }else{
        cout << "未查找到,请重试!" << endl << endl; //TODO
        return nullptr;
    }
    return temp;
}
void displayAllInfos(vector<Student *>& stus){
    int rank = 1;
    int len = static_cast<int>(stus.size());
    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); break;
        }
    }
    return ptr;
}
void error(char choice){
    cout<< "\n------------------------------------------------------------\n\n";
    cout << "抱歉,无选项 "<<choice<<" ,请重试。" << endl;
    cout<< "\n按下enter键返回" <<endl;
    cout<< "\n------------------------------------------------------------\n\n";
    getchar();
}

转载于:https://www.cnblogs.com/1Kasshole/p/9372607.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值