C++ 實現簡單命令行學生管理系統
阿新 • • 發佈:2018-07-26
什麽 cos wid 屏幕 cit 環境 iterator choice umeng
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++ 實現簡單命令行學生管理系統