C++ 實現簡單命令行學生管理系統
預覽:
編譯環境是macOS。system(“clear”) 在windows下請換成 system(“cls”)
#include
#include
#include
#include
#include
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
void launcher(); // 主邏輯實現
void display(int currentSize); // 界面
void append(vector
Student* displaySingleInfo(vector
void change(vector
void del(vector
void search(vector
void quitSys(vector
void displayAllInfos(vector
bool comp(const Student* a,const Student* b){ // 排序依據實現
return a->getsum()>b->getsum();
}
int main(){ /**** 系統入口 ****/
launcher();
cout<< "再見" << endl;
return 0;
}
// ****************************** 功能實現 ***************************************
void launcher(){
vector
string _input;
display(static_cast
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 << "無選項"< break; } system("clear"); // 操作後清理屏幕,windows下請換成system("cls") display(static_cast } } 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
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
cout << "\n\n好的,現在開始進行修改操作: \n\n";
cout << "請輸入需要修改的學生的學號: \n"< 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
cout << "\n\n好的,現在開始進行刪除操作: \n\n";
cout << "請輸入需要刪除的學生的學號: \n"<
cout<< "--> ";
string target;
cin >> target;
vector
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
cout << "\n\n好的,現在開始進行查找操作: \n\n";
cout << "請輸入需要查找的學生的學號: \n"< cout<< "--> "; string target; cin >> target; displaySingleInfo(stus,target); } void quitSys(vector vector for(it = stus.begin();it != stus.end();it ++){ delete (*it); } } Student* displaySingleInfo(vector { Student *temp = __lookup(stus,target); if(temp != nullptr){ cout << "\n學生姓名:" << temp->getName() << " 學號:" << temp->getID() << endl; cout<< "--scores:"< cout << "English: " << temp->getEng() << " Chinese: " << temp->getChi() << " Math: " << temp->getMat() << endl; }else{ cout << "未查找到,請重試!" << endl; //TODO return nullptr; } return temp; } void displayAllInfos(vector int rank = 1; int len = static_cast int sumEng = 0,sumChi = 0,sumMat = 0; sort(stus.begin(),stus.end(),comp); cout<< "\n\n\n------------------------------------------------------------\n"; cout<< " scores\n"; cout<< "rank "< for(auto &stu : stus){ cout.setf(ios::left); cout.width(6); cout< 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()< } cout<< "\nsum "; cout.width(9); cout<<:engsum> cout.width(9); cout<<:chisum> cout.width(6); cout<<:matsum> cout<< "average "; cout.width(9); cout<<:engsum> cout.width(9); cout<<:chisum> cout.width(6); cout<<:matsum> cout<< "\n------------------------------------------------------------\n\n"; cout<< "按下enter鍵以繼續" < getchar(); } Student* __lookup(vector vector Student *ptr = nullptr; for(it = stus.begin();it != stus.end();it ++){ if((*it)->getID() == target) ptr = (*it); } return ptr; } 今天給大家帶來的迷宮製作希望大家喜歡,有不懂的多多討論,指教。你覺得我們的文章對你有幫助,多多在文章下方評論。希望久伴可以伴隨你從入門到專家.。
閱讀更多 前端勤學者一勞永逸 的文章