~cpp #include <iostream> #include <vector> #include <algorithm> #include <string> #include <fstream> using namespace std; class ScoresTable { private : string _StudentName; vector<int> _Scores; int _TotalScore; double _AverageScore; public : ScoresTable(const string& aStudentName) : _StudentName(aStudentName) { _TotalScore = 0; _AverageScore = 0.0; } void addScoreToVector(int aScore) { _Scores.push_back(aScore); } void calculateTotalScore() { for(unsigned int i = 0 ; i < _Scores.size() ; ++i) _TotalScore += _Scores[i]; } void calculateAverageScore() { _AverageScore = (double)_TotalScore / 4.0; } const string& getName() const { return _StudentName; } int getnthScore(int n) const { return _Scores[n]; } int getTotalScore() const { return _TotalScore; } double getAverageScore() const { return _AverageScore; } }; class ScoreProcessor { private : vector<ScoresTable*> _StudentList; class ScoreSort { public : bool operator()(ScoresTable* stu1, ScoresTable* stu2) const { return stu1->getTotalScore() > stu2->getTotalScore(); } }; class NameSort { public : bool operator()(ScoresTable* stu1, ScoresTable* stu2) const { return stu1->getName() < stu2->getName(); } }; void showNameAndCuri() { cout << "이름\t국어\t영어\t수학\t과학\t총점\t평균\n"; } public : virtual ~ScoreProcessor() { for(unsigned int i = 0 ; i < _StudentList.size() ; ++i) delete _StudentList[i]; } void dataReader() { ifstream anInputer("test.txt"); ScoresTable* aTable; while(!anInputer.eof()) { string aName; int aScore; anInputer >> aName; aTable = new ScoresTable(aName); for(int i = 0 ; i < 4 ; ++i) { anInputer >> aScore; aTable->addScoreToVector(aScore); } _StudentList.push_back(aTable); aTable->calculateTotalScore(); aTable->calculateAverageScore(); } } void showAllStudentsInfo() { showNameAndCuri(); for(unsigned int i = 0 ; i < _StudentList.size() ; ++i) { cout << _StudentList[i]->getName() << "\t"; for(int j = 0 ; j < 4 ; ++j) cout << _StudentList[i]->getnthScore(j) << "\t"; cout << _StudentList[i]->getTotalScore() << "\t"; cout << _StudentList[i]->getAverageScore() << endl; } cout << endl; } void scoreSortAndShow() { sort(_StudentList.begin(), _StudentList.end(), ScoreSort()); showAllStudentsInfo(); } void nameSortAndShow() { sort(_StudentList.begin(), _StudentList.end(), NameSort()); showAllStudentsInfo(); } }; class Admin { private : ScoreProcessor _aScoreProcessor; void showMenu() { cout << "STL을 이용한 성적관리 프로그램" << endl; cout << "1. 현재 목록 보기" << endl; cout << "2. 이름순으로 소트해서 보기" << endl; cout << "3. 점수순으로 소트해서 보기" << endl; cout << "4. 끝" << endl; cout << "Select : " ; } int getInputNumber() { int ch; cin >> ch; return ch; } public : Admin() { _aScoreProcessor.dataReader(); } void process() { while(1) { showMenu(); switch(getInputNumber()) // 별루 맘에 안들긴 하지만--; 그냥 보여주는 거니깐 { case 1: _aScoreProcessor.showAllStudentsInfo(); break; case 2: _aScoreProcessor.nameSortAndShow(); break; case 3: _aScoreProcessor.scoreSortAndShow(); break; case 4: return; } } } }; int main() { Admin gogo; gogo.process(); return 0; }