GradeCalc.hpp
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <numeric> #include <iomanip> using std::vector; using std::string; using std::cin; using std::cout; using std::endl; class GradeCalc: public vector<int> { public: GradeCalc(const string &cname, int size); void input(); // 錄入成績 void output() const; // 輸出成績 void sort(bool ascending = false); // 排序 (預設降序) int min() const; // 返回最低分 int max() const; // 返回最高分 float average() const; // 返回平均分 void info(); // 輸出課程成績資訊 private: void compute(); // 成績統計 private: string course_name; // 課程名 int n; // 課程人數 vector<int> counts = vector<int>(5, 0); // 儲存各分數段人數([0, 60), [60, 70), [70, 80), [80, 90), [90, 100] vector<double> rates = vector<double>(5, 0); // 儲存各分數段比例 }; GradeCalc::GradeCalc(const string &cname, int size): course_name{cname}, n{size} {} void GradeCalc::input() { int grade; for(int i = 0; i < n; ++i) { cin >> grade; this->push_back(grade); } } void GradeCalc::output() const { for(auto ptr = this->begin(); ptr != this->end(); ++ptr) cout << *ptr << " "; cout << endl; } void GradeCalc::sort(bool ascending) { if(ascending) std::sort(this->begin(), this->end()); else std::sort(this->begin(), this->end(), std::greater<int>()); } int GradeCalc::min() const { return *std::min_element(this->begin(), this->end()); } int GradeCalc::max() const { return *std::max_element(this->begin(), this->end()); } float GradeCalc::average() const { return std::accumulate(this->begin(), this->end(), 0) * 1.0 / n; } void GradeCalc::compute() { for(int grade: *this) { if(grade < 60) counts.at(0)++; else if(grade >= 60 && grade < 70) counts.at(1)++; else if(grade >= 70 && grade < 80) counts.at(2)++; else if(grade >= 80 && grade < 90) counts.at(3)++; else if(grade >= 90) counts.at(4)++; } for(int i = 0; i < rates.size(); ++i) rates.at(i) = counts.at(i) * 1.0 / n; } void GradeCalc::info() { cout << "課程名稱:\t" << course_name << endl; cout << "排序後成績: \t"; sort(); output(); cout << "最高分:\t" << max() << endl; cout << "最低分:\t" << min() << endl; cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl; compute(); // 統計各分數段人數、比例 vector<string> tmp{"[0, 60) ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]"}; for(int i = tmp.size()-1; i >= 0; --i) cout << tmp[i] << "\t: " << counts[i] << "人\t" << std::fixed << std::setprecision(2) << rates[i]*100 << "%" << endl; }
demo2.cpp
#include "GradeCalc.hpp" #include <iomanip> void test() { int n; cout << "輸入班級人數: "; cin >> n; GradeCalc c1("OOP", n); cout << "錄入成績: " << endl;; c1.input(); cout << "輸出成績: " << endl; c1.output(); cout << string(20, '*') + "課程成績資訊" + string(20, '*') << endl; c1.info(); } int main() { test(); }
在基類vector<int>裡
this指標
從頭加到尾乘1.0再除以n求平均值。有。讓運算結果變為浮點型 ,可以有小數。
GradeCalc.hpp
#include <iostream> #include <vector> #include <string> #include <algorithm> #include <numeric> #include <iomanip> using std::vector; using std::string; using std::cin; using std::cout; using std::endl; class GradeCalc { public: GradeCalc(const string &cname, int size); void input(); // 錄入成績 void output() const; // 輸出成績 void sort(bool ascending = false); // 排序 (預設降序) int min() const; // 返回最低分 int max() const; // 返回最高分 float average() const; // 返回平均分 void info(); // 輸出課程成績資訊 private: void compute(); // 成績統計 private: string course_name; // 課程名 int n; // 課程人數 vector<int> grades; // 課程成績 vector<int> counts = vector<int>(5, 0); // 儲存各分數段人數([0, 60), [60, 70), [70, 80), [80, 90), [90, 100] vector<double> rates = vector<double>(5, 0); // 儲存各分數段比例 }; GradeCalc::GradeCalc(const string &cname, int size): course_name{cname}, n{size} {} void GradeCalc::input() { int grade; for(int i = 0; i < n; ++i) { cin >> grade; grades.push_back(grade); } } void GradeCalc::output() const { for(int grade: grades) cout << grade << " "; cout << endl; } void GradeCalc::sort(bool ascending) { if(ascending) std::sort(grades.begin(), grades.end()); else std::sort(grades.begin(), grades.end(), std::greater<int>()); } int GradeCalc::min() const { return *std::min_element(grades.begin(), grades.end()); } int GradeCalc::max() const { return *std::max_element(grades.begin(), grades.end()); } float GradeCalc::average() const { return std::accumulate(grades.begin(), grades.end(), 0) * 1.0 / n; } void GradeCalc::compute() { for(int grade: grades) { if(grade < 60) counts.at(0)++; else if(grade >= 60 && grade < 70) counts.at(1)++; else if(grade >= 70 && grade < 80) counts.at(2)++; else if(grade >= 80 && grade < 90) counts.at(3)++; else if(grade >= 90) counts.at(4)++; } for(int i = 0; i < rates.size(); ++i) rates.at(i) = counts.at(i) *1.0 / n; } void GradeCalc::info() { cout << "課程名稱:\t" << course_name << endl; cout << "排序後成績: \t"; sort(); output(); cout << "最高分:\t" << max() << endl; cout << "最低分:\t" << min() << endl; cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl; compute(); // 統計各分數段人數、比例 vector<string> tmp{"[0, 60) ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]"}; for(int i = tmp.size()-1; i >= 0; --i) cout << tmp[i] << "\t: " << counts[i] << "人\t" << std::fixed << std::setprecision(2) << rates[i]*100 << "%" << endl; }
demo3.cpp
#include "GradeCalc.hpp" #include <iomanip> void test() { int n; cout << "輸入班級人數: "; cin >> n; GradeCalc c1("OOP", n); cout << "錄入成績: " << endl;; c1.input(); cout << "輸出成績: " << endl; c1.output(); cout << string(20, '*') + "課程成績資訊" + string(20, '*') << endl; c1.info(); } int main() { test(); }
vector<int> grades裡。直接使用grade的vectory函式。
task4_1
#include <iostream> #include <string> #include <limits> using namespace std; void test1() { string s1, s2; cin >> s1 >> s2; // cin: 從輸入流讀取字串, 碰到空白符(空格/回車/Tab)即結束 cout << "s1: " << s1 << endl; cout << "s2: " << s2 << endl; } void test2() { string s1, s2; getline(cin, s1); // getline(): 從輸入流中提取字串,直到遇到換行符 getline(cin, s2); cout << "s1: " << s1 << endl; cout << "s2: " << s2 << endl; } void test3() { string s1, s2; getline(cin, s1, ' '); //從輸入流中提取字串,直到遇到指定分隔符 getline(cin, s2); cout << "s1: " << s1 << endl; cout << "s2: " << s2 << endl; } int main() { cout << "測試1: 使用標準輸入流物件cin輸入字串" << endl; test1(); cout << endl; cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "測試2: 使用函式getline()輸入字串" << endl; test2(); cout << endl; cout << "測試3: 使用函式getline()輸入字串, 指定字串分隔符" << endl; test3(); }
防止test1輸入末尾的 '\n' 被getline錯誤錄入
task4_2
#include <iostream> #include <string> #include <vector> #include <limits> using namespace std; void output(const vector<string> &v) { for(auto &s: v) cout << s << endl; } void test() { int n; while(cout << "Enter n: ", cin >> n) { vector<string> v1; for(int i = 0; i < n; ++i) { string s; cin >> s; v1.push_back(s); } cout << "output v1: " << endl; output(v1); cout << endl; } } int main() { cout << "測試: 使用cin多組輸入字串" << endl; test(); }
task4_2
#include <iostream> #include <string> #include <vector> #include <limits> using namespace std; void output(const vector<string> &v) { for(auto &s: v) cout << s << endl; } void test() { int n; while(cout << "Enter n: ", cin >> n) { cin.ignore(numeric_limits<streamsize>::max(), '\n'); vector<string> v2; for(int i = 0; i < n; ++i) { string s; getline(cin, s); v2.push_back(s); } cout << "output v2: " << endl; output(v2); cout << endl; } } int main() { cout << "測試: 使用函式getline()多組輸入字串" << endl; test(); }
防止各種輸入末尾的 '\n' 被getline錯誤錄入
grm.hpp
#include <iostream> using std::cout; using std::endl; template<typename T> class GameResourceManager { private: T resource; public: GameResourceManager(T n); T get(); void update(T i); }; template<typename T> GameResourceManager<T>::GameResourceManager(T n): resource{n} {} template<typename T> T GameResourceManager<T>::get() { return resource; } template<typename T> void GameResourceManager<T>::update(T i) { if(resource+i<0) resource=0; else resource += i; }
task5.cpp
#include "grm.hpp" #include <iostream> using std::cout; using std::endl; void test1() { GameResourceManager<float> HP_manager(99.99); cout << "當前生命值: " << HP_manager.get() << endl; HP_manager.update(9.99); cout << "增加9.99生命值後, 當前生命值: " << HP_manager.get() << endl; HP_manager.update(-999.99); cout <<"減少999.99生命值後, 當前生命值: " << HP_manager.get() << endl; } void test2() { GameResourceManager<int> Gold_manager(100); cout << "當前金幣數量: " << Gold_manager.get() << endl; Gold_manager.update(50); cout << "增加50個金幣後, 當前金幣數量: " << Gold_manager.get() << endl; Gold_manager.update(-99); cout <<"減少99個金幣後, 當前金幣數量: " << Gold_manager.get() << endl; } int main() { cout << "測試1: 用float型別對類别範本GameResourceManager例項化" << endl; test1(); cout << endl; cout << "測試2: 用int型別對類别範本GameResourceManager例項化" << endl; test2(); }
task6
info.hpp
#include <string> #include <iostream> #include <iomanip> using namespace std; using std::string; class info { private: string name; string contact; string city; int n; public: static int total; info(string NAM="NU*LL",string CON="NU*LL",string CIT="NU*LL",int N=0); void display(); }; int info::total=0; info::info(string NAM,string CON,string CIT,int N): name{NAM},contact{CON},city{CIT},n{N} { total+=n; } void info::display(){ cout << setw(20) << "暱稱:" << name << endl; cout << setw(20) << "聯絡方式:" << contact << endl; cout << setw(20) << "所在城市:" << city << endl; cout << setw(20) << "預定人數:" << n << endl; cout << "-----------------------------------------------------" <<endl; }
demo6.cpp
#include <string> #include <iostream> #include <iomanip> #include <vector> #include "info.hpp" using namespace std; using std::string; int main() { const int max = 100; cout << "錄入預約資訊:" << endl; cout << endl; cout << left << setw(20) << "暱稱" << setw(35) << "聯絡方式(手機/郵箱)" << setw(30) << "所在城市" << setw(15) << "預定參加人數" << endl; int NUM; string A,B,C; vector<info> audience_lst; while( cin >> A >> B >> C >> NUM ) { if( info::total+NUM > max ) { int o = max-info::total; char choic; cout << "對不起,只剩" << o << "個位置" << endl; cout << "輸入u,更新預定資訊" << endl; cout << "輸入q,退出預定" << endl; cout << "你的選擇:" << endl; cin >> choic; if(choic=='q') break; else if(choic=='u') { cout << "請重新輸入預定資訊" << endl; continue; } else { cout << "無效資訊" << endl; break; } } else { info man(A,B,C,NUM); audience_lst.push_back(man); if( info::total == max ) break; } } cout << "截至目前,共" << info::total << "名聽眾預約。預約聽眾資訊如下" << endl; cout << "-----------------------------------------------------" <<endl; for(auto i: audience_lst) i.display(); cout << "-----------------------------------------------------" <<endl; }