實驗任務二
原始碼
GradeCalc.hpp
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <algorithm> 5 #include <numeric> 6 #include <iomanip> 7 8 using std::vector; 9 using std::string; 10 using std::cin; 11 using std::cout; 12 using std::endl; 13 14 class GradeCalc : public vector<int> { 15 public: 16 GradeCalc(const string& cname, int size); 17 void input(); // 錄入成績 18 void output() const; // 輸出成績 19 void sort(bool ascending = false); // 排序 (預設降序) 20 int min() const; // 返回最低分 21 int max() const; // 返回最高分 22 float average() const; // 返回平均分 23 void info(); // 輸出課程成績資訊 24 25 private: 26 void compute(); // 成績統計 27 28 private: 29 string course_name; // 課程名 30 int n; // 課程人數 31 vector<int> counts = vector<int>(5, 0); // 儲存各分數段人數([0, 60), [60, 70), [70, 80), [80, 90), [90, 100] 32 vector<double> rates = vector<double>(5, 0); // 儲存各分數段比例 33 }; 34 35 GradeCalc::GradeCalc(const string& cname, int size) : course_name{ cname }, n{ size } {} 36 37 void GradeCalc::input() { 38 int grade; 39 40 for (int i = 0; i < n; ++i) { 41 cin >> grade; 42 this->push_back(grade); 43 } 44 } 45 46 void GradeCalc::output() const { 47 for (auto ptr = this->begin(); ptr != this->end(); ++ptr) 48 cout << *ptr << " "; 49 cout << endl; 50 } 51 52 void GradeCalc::sort(bool ascending) { 53 if (ascending) 54 std::sort(this->begin(), this->end()); 55 else 56 std::sort(this->begin(), this->end(), std::greater<int>()); 57 } 58 59 int GradeCalc::min() const { 60 return *std::min_element(this->begin(), this->end()); 61 } 62 63 int GradeCalc::max() const { 64 return *std::max_element(this->begin(), this->end()); 65 } 66 67 float GradeCalc::average() const { 68 return std::accumulate(this->begin(), this->end(), 0) * 1.0 / n; 69 } 70 71 void GradeCalc::compute() { 72 for (int grade : *this) { 73 if (grade < 60) 74 counts.at(0)++; 75 else if (grade >= 60 && grade < 70) 76 counts.at(1)++; 77 else if (grade >= 70 && grade < 80) 78 counts.at(2)++; 79 else if (grade >= 80 && grade < 90) 80 counts.at(3)++; 81 else if (grade >= 90) 82 counts.at(4)++; 83 } 84 85 for (int i = 0; i < rates.size(); ++i) 86 rates.at(i) = counts.at(i) * 1.0 / n; 87 } 88 89 void GradeCalc::info() { 90 cout << "課程名稱:\t" << course_name << endl; 91 cout << "排序後成績: \t"; 92 sort(); output(); 93 cout << "最高分:\t" << max() << endl; 94 cout << "最低分:\t" << min() << endl; 95 cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl; 96 97 compute(); // 統計各分數段人數、比例 98 99 vector<string> tmp{ "[0, 60) ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]" }; 100 for (int i = tmp.size() - 1; i >= 0; --i) 101 cout << tmp[i] << "\t: " << counts[i] << "人\t" 102 << std::fixed << std::setprecision(2) << rates[i] * 100 << "%" << endl; 103 }
demo2.cpp
1 #include "GradeCalc.hpp" 2 #include <iomanip> 3 4 void test() { 5 int n; 6 cout << "輸入班級人數: "; 7 cin >> n; 8 9 GradeCalc c1("OOP", n); 10 11 cout << "錄入成績: " << endl;; 12 c1.input(); 13 cout << "輸出成績: " << endl; 14 c1.output(); 15 16 cout << string(20, '*') + "課程成績資訊" + string(20, '*') << endl; 17 c1.info(); 18 } 19 20 int main() { 21 test(); 22 }
執行結果截圖
問題1.貯存在this指標指向的地方,繼承的vector的at介面,使用的vector的push_back介面。
問題2.用於計算成績的累加和,有影響,乘1.0可以將資料轉變為double型別
問題3.還可以增加後續資料有改動的情況。
實驗任務三
原始碼
GradeCalc.hpp
1 #include <iostream> 2 #include <vector> 3 #include <string> 4 #include <algorithm> 5 #include <numeric> 6 #include <iomanip> 7 8 using std::vector; 9 using std::string; 10 using std::cin; 11 using std::cout; 12 using std::endl; 13 14 class GradeCalc { 15 public: 16 GradeCalc(const string& cname, int size); 17 void input(); // 錄入成績 18 void output() const; // 輸出成績 19 void sort(bool ascending = false); // 排序 (預設降序) 20 int min() const; // 返回最低分 21 int max() const; // 返回最高分 22 float average() const; // 返回平均分 23 void info(); // 輸出課程成績資訊 24 25 private: 26 void compute(); // 成績統計 27 28 private: 29 string course_name; // 課程名 30 int n; // 課程人數 31 vector<int> grades; // 課程成績 32 vector<int> counts = vector<int>(5, 0); // 儲存各分數段人數([0, 60), [60, 70), [70, 80), [80, 90), [90, 100] 33 vector<double> rates = vector<double>(5, 0); // 儲存各分數段比例 34 }; 35 36 GradeCalc::GradeCalc(const string& cname, int size) : course_name{ cname }, n{ size } {} 37 38 void GradeCalc::input() { 39 int grade; 40 41 for (int i = 0; i < n; ++i) { 42 cin >> grade; 43 grades.push_back(grade); 44 } 45 } 46 47 void GradeCalc::output() const { 48 for (int grade : grades) 49 cout << grade << " "; 50 cout << endl; 51 } 52 53 void GradeCalc::sort(bool ascending) { 54 if (ascending) 55 std::sort(grades.begin(), grades.end()); 56 else 57 std::sort(grades.begin(), grades.end(), std::greater<int>()); 58 59 } 60 61 int GradeCalc::min() const { 62 return *std::min_element(grades.begin(), grades.end()); 63 } 64 65 int GradeCalc::max() const { 66 return *std::max_element(grades.begin(), grades.end()); 67 } 68 69 float GradeCalc::average() const { 70 return std::accumulate(grades.begin(), grades.end(), 0) * 1.0 / n; 71 } 72 73 void GradeCalc::compute() { 74 for (int grade : grades) { 75 if (grade < 60) 76 counts.at(0)++; 77 else if (grade >= 60 && grade < 70) 78 counts.at(1)++; 79 else if (grade >= 70 && grade < 80) 80 counts.at(2)++; 81 else if (grade >= 80 && grade < 90) 82 counts.at(3)++; 83 else if (grade >= 90) 84 counts.at(4)++; 85 } 86 87 for (int i = 0; i < rates.size(); ++i) 88 rates.at(i) = counts.at(i) * 1.0 / n; 89 } 90 91 void GradeCalc::info() { 92 cout << "課程名稱:\t" << course_name << endl; 93 cout << "排序後成績: \t"; 94 sort(); output(); 95 cout << "最高分:\t" << max() << endl; 96 cout << "最低分:\t" << min() << endl; 97 cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl; 98 99 compute(); // 統計各分數段人數、比例 100 101 vector<string> tmp{ "[0, 60) ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]" }; 102 for (int i = tmp.size() - 1; i >= 0; --i) 103 cout << tmp[i] << "\t: " << counts[i] << "人\t" 104 << std::fixed << std::setprecision(2) << rates[i] * 100 << "%" << endl; 105 }
demo.cpp
1 #include "GradeCalc.hpp" 2 #include <iomanip> 3 4 void test() { 5 int n; 6 cout << "輸入班級人數: "; 7 cin >> n; 8 9 GradeCalc c1("OOP", n); 10 11 cout << "錄入成績: " << endl;; 12 c1.input(); 13 cout << "輸出成績: " << endl; 14 c1.output(); 15 16 cout << string(20, '*') + "課程成績資訊" + string(20, '*') << endl; 17 c1.info(); 18 } 19 20 int main() { 21 test(); 22 }
執行結果截圖
問題1.成績存在grades這個vector型別的物件中,是透過grades呼叫的vector中的at介面
問題2.物件導向設計時,可以繼承設計派生類,也可以設計物件型別來運用
實驗任務四
4.1
原始碼
1 #include <iostream> 2 #include <string> 3 #include <limits> 4 5 using namespace std; 6 7 void test1() { 8 string s1, s2; 9 cin >> s1 >> s2; // cin: 從輸入流讀取字串, 碰到空白符(空格/回車/Tab)即結束 10 cout << "s1: " << s1 << endl; 11 cout << "s2: " << s2 << endl; 12 } 13 14 void test2() { 15 string s1, s2; 16 getline(cin, s1); // getline(): 從輸入流中提取字串,直到遇到換行符 17 getline(cin, s2); 18 cout << "s1: " << s1 << endl; 19 cout << "s2: " << s2 << endl; 20 } 21 22 void test3() { 23 string s1, s2; 24 getline(cin, s1, ' '); //從輸入流中提取字串,直到遇到指定分隔符 25 getline(cin, s2); 26 cout << "s1: " << s1 << endl; 27 cout << "s2: " << s2 << endl; 28 } 29 30 int main() { 31 cout << "測試1: 使用標準輸入流物件cin輸入字串" << endl; 32 test1(); 33 cout << endl; 34 35 cin.ignore(numeric_limits<streamsize>::max(), '\n'); 36 37 cout << "測試2: 使用函式getline()輸入字串" << endl; 38 test2(); 39 cout << endl; 40 41 cout << "測試3: 使用函式getline()輸入字串, 指定字串分隔符" << endl; 42 test3(); 43 }
執行結果截圖
問題1.去掉後,測試2中的s1會變成空串,作用是清空輸入緩衝區,直到遇到換行符為止
4.2
原始碼
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <limits> 5 6 using namespace std; 7 8 void output(const vector<string>& v) { 9 for (auto& s : v) 10 cout << s << endl; 11 } 12 13 void test() { 14 int n; 15 while (cout << "Enter n: ", cin >> n) { 16 vector<string> v1; 17 18 for (int i = 0; i < n; ++i) { 19 string s; 20 cin >> s; 21 v1.push_back(s); 22 } 23 24 cout << "output v1: " << endl; 25 output(v1); 26 cout << endl; 27 } 28 } 29 30 int main() { 31 cout << "測試: 使用cin多組輸入字串" << endl; 32 test(); 33 }
執行結果截圖
4.3
原始碼
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <limits> 5 6 using namespace std; 7 8 void output(const vector<string>& v) { 9 for (auto& s : v) 10 cout << s << endl; 11 } 12 13 void test() { 14 int n; 15 while (cout << "Enter n: ", cin >> n) { 16 cin.ignore(numeric_limits<streamsize>::max(), '\n'); 17 18 vector<string> v2; 19 20 for (int i = 0; i < n; ++i) { 21 string s; 22 getline(cin, s); 23 v2.push_back(s); 24 } 25 cout << "output v2: " << endl; 26 output(v2); 27 cout << endl; 28 } 29 } 30 31 int main() { 32 cout << "測試: 使用函式getline()多組輸入字串" << endl; 33 test(); 34 }
執行結果截圖
問題1.清空輸入緩衝區
實驗任務五
原始碼
grm.hpp
1 template<typename T> 2 class GameResourceManager { 3 private: 4 T resource; 5 public: 6 GameResourceManager(T R); 7 T get()const; 8 void update(T newR); 9 }; 10 11 12 13 template<typename T> 14 GameResourceManager<T>::GameResourceManager(T R) :resource{ R } {} 15 template<typename T> 16 T GameResourceManager<T>::get()const { return resource; } 17 template<typename T> 18 void GameResourceManager<T>::update(T newR) { 19 resource += newR; 20 if (resource < 0) 21 resource = 0; 22 }
task5.cpp
1 #include "grm.hpp" 2 #include <iostream> 3 4 using std::cout; 5 using std::endl; 6 7 void test1() { 8 GameResourceManager<float> HP_manager(99.99); 9 cout << "當前生命值: " << HP_manager.get() << endl; 10 HP_manager.update(9.99); 11 cout << "增加9.99生命值後, 當前生命值: " << HP_manager.get() << endl; 12 HP_manager.update(-999.99); 13 cout << "減少999.99生命值後, 當前生命值: " << HP_manager.get() << endl; 14 } 15 16 void test2() { 17 GameResourceManager<int> Gold_manager(100); 18 cout << "當前金幣數量: " << Gold_manager.get() << endl; 19 Gold_manager.update(50); 20 cout << "增加50個金幣後, 當前金幣數量: " << Gold_manager.get() << endl; 21 Gold_manager.update(-99); 22 cout << "減少99個金幣後, 當前金幣數量: " << Gold_manager.get() << endl; 23 } 24 25 26 int main() { 27 cout << "測試1: 用float型別對類别範本GameResourceManager例項化" << endl; 28 test1(); 29 cout << endl; 30 31 cout << "測試2: 用int型別對類别範本GameResourceManager例項化" << endl; 32 test2(); 33 }
執行結果截圖
實驗任務六
原始碼
info.hpp
1 #include<iostream> 2 #include<string> 3 #include<iomanip> 4 5 using std::string; 6 using std::cout; 7 using std::endl; 8 9 10 class Info{ 11 private: 12 string nickname; 13 string contact; 14 string city; 15 int n; 16 17 public: 18 Info(string N,string C,string Ci,int n0):nickname{N},contact{C},city{Ci},n{n0}{} 19 void display() { 20 cout << "暱稱:\t\t" << nickname<<endl; 21 cout << "聯絡方式:\t" << contact<<endl; 22 cout << "所在城市:\t" << city<<endl; 23 cout << "預定人數:\t" << n << endl; 24 } 25 };
task6.cpp
1 #include"info.hpp" 2 #include<iostream> 3 #include<vector> 4 using namespace std; 5 int main() { 6 const int capacity = 100; 7 vector<Info> audience_lst; 8 string name, contact, city; 9 int n,num=0; 10 cout << "錄入使用者預約資訊" << endl; 11 cout << endl; 12 cout << "暱稱\t" << "聯絡方式(郵箱/手機號)\t" << "所在城市\t" << "預定參加人數\t"<<endl; 13 while (cin>>name>>contact>>city>>n) { 14 Info s(name, contact, city, n); 15 num += n; 16 if (num > capacity) 17 { 18 num -= n; 19 char ch; 20 cout << "對不起,只剩" << capacity-num << "個位置" << endl; 21 cout << "1.輸入u,更新(update)預定資訊" << endl; 22 cout << "2.輸入q,退出預定" << endl; 23 cout << "你的選擇:"; 24 cin >> ch; 25 if (ch == 'u') { 26 cout << "請重新輸入預定資訊" << endl; 27 continue; 28 } 29 else if (ch == 'q') 30 break; 31 32 33 } 34 else if (num == capacity) { 35 audience_lst.push_back(s); 36 break; 37 } 38 else 39 audience_lst.push_back(s); 40 } 41 cout << "截至目前一共有" << num << "位聽眾預約,預約聽眾資訊如下:" << endl; 42 43 for (auto i : audience_lst) { 44 45 cout<< string(20, '-') << endl; 46 i.display(); 47 } 48 49 50 51 52 53 }
執行結果截圖
實驗任務7
原始碼
date.h
1 #pragma once 2 3 class Date { 4 private: 5 int year; 6 int month; 7 int day; 8 int totalDays; 9 public: 10 Date(int year, int maonth, int day); 11 int getYear()const { return year; } 12 int getMonth()const { return month; } 13 int getDay()const { return day; } 14 int getMaxDay()const; 15 bool isLeapYear()const { 16 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 17 } 18 void show()const; 19 int diatance(const Date& date)const { 20 return totalDays - date.totalDays; 21 } 22 };
date.cpp
1 #include"date.h" 2 #include<iostream> 3 #include<cstdlib> 4 using namespace std; 5 namespace { 6 const int DATS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 }; 7 } 8 9 Date::Date(int year, int month, int day) :year{ year }, month{ month }, day{ day } { 10 if (day <= 0 || day > getMaxDay()) { 11 cout << "Invalid date: "; 12 show(); 13 exit(1); 14 } 15 int years = year - 1; 16 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DATS_BEFORE_MONTH[month - 1] + day; 17 if (isLeapYear() && month > 2)totalDays++; 18 } 19 20 int Date::getMaxDay()const { 21 if (isLeapYear() && month == 2) 22 return 29; 23 else 24 return DATS_BEFORE_MONTH[month] - DATS_BEFORE_MONTH[month - 1]; 25 } 26 27 void Date::show()const { 28 cout << getYear() << "-" << getMonth() << "-" << getDay(); 29 }
accumulator.h
1 #pragma once 2 #include"date.h" 3 class Accumulator { 4 private: 5 Date lastDate; 6 double value; 7 double sum; 8 public: 9 Accumulator(const Date &date,double value): 10 lastDate(date),value(value),sum(0){} 11 double getSum(const Date& date)const { 12 return sum + value * date.diatance(lastDate); 13 } 14 void change(const Date& date, double value) { 15 sum = getSum(date); 16 lastDate = date; 17 this->value = value; 18 } 19 void reset(const Date& date, double value) { 20 lastDate = date; 21 this->value = value; 22 sum = 0; 23 } 24 };
account.h
1 #pragma once 2 3 #include"date.h" 4 #include"accumulator.h" 5 #include<string> 6 7 class Account { 8 private: 9 std::string id; 10 double balance; 11 static double total; 12 protected: 13 Account(const Date& date, const std::string& id); 14 void record(const Date& date, double amount, const std::string& desc); 15 void error(const std::string& msg)const; 16 17 18 public: 19 20 const std::string& getId()const { return id; } 21 double getBalance()const { return balance; } 22 static double getTotal() { return total; } 23 void show()const; 24 }; 25 26 class SavingAccount :public Account { 27 private: 28 Accumulator acc; 29 double rate; 30 public: 31 SavingAccount(const Date& date, const std::string& id, double rate); 32 double getRate()const { return rate;} 33 void deposit(const Date& date, double amount, const std::string& desc); 34 void withdraw(const Date& date, double amount, const std::string& desc); 35 void settle(const Date& date); 36 }; 37 class CreditAccount :public Account { 38 private: 39 Accumulator acc; 40 double credit; 41 double rate; 42 double fee; 43 double getDebt()const { 44 double balance = getBalance(); 45 return (balance < 0 ? balance : 0); 46 } 47 public: 48 CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee); 49 double getCredit()const { return credit; } 50 double getRate() const { return rate; } 51 double getFee() const { return fee; } 52 double getAvsilbilableCredit() const { 53 if (getBalance() < 0) 54 return credit + getBalance(); 55 else 56 return credit; 57 } 58 void deposit(const Date& date, double amount, const std::string& desc); 59 void withdraw(const Date& date, double amount, const std::string& desc); 60 void settle(const Date& date); 61 void show()const; 62 };
account.cpp
1 #include"account.h" 2 #include<cmath> 3 #include<iostream> 4 using namespace std; 5 double Account::total = 0; 6 7 Account::Account(const Date& date, const string& id) :id{ id }, balance{ 0 }{ 8 date.show(); 9 cout << "\t#" << id << "created" << endl; 10 } 11 void Account::record(const Date& date, double amount, const string& desc) { 12 amount = floor(amount * 100 + 0.5) / 100; 13 balance += amount; 14 total += amount; 15 date.show(); 16 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 17 } 18 void Account::show()const { 19 cout << id << "\tBalance:" << balance; 20 } 21 void Account::error(const string& msg)const { 22 cout << "Error(#" << id << "): " << msg << endl; 23 } 24 25 SavingAccount::SavingAccount(const Date& date, const string& id, double rate) :Account{ date,id }, rate{ rate }, acc{ date,0 } {} 26 void SavingAccount::deposit(const Date& date, double amount, const string& desc) { 27 record(date, amount, desc); 28 acc.change(date, getBalance()); 29 } 30 void SavingAccount::withdraw(const Date& date, double amount, const string& desc) { 31 if (amount > getBalance()) 32 error("not enough money"); 33 else 34 record(date, -amount, desc); 35 acc.change(date, getBalance()); 36 } 37 38 void SavingAccount::settle(const Date& date) { 39 double interest = acc.getSum(date) * rate / date.diatance(Date(date.getYear() - 1, 1, 1)); 40 if (interest != 0) 41 record(date, interest, "interest"); 42 acc.reset(date, getBalance()); 43 44 } 45 46 47 CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee) :Account{ date,id }, credit{ credit }, rate { rate }, fee{ fee }, acc{ date, 0 } {} 48 49 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 50 record(date, amount, desc); 51 acc.change(date, getDebt()); 52 } 53 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 54 if (amount - getBalance()>credit) 55 error("not enough money"); 56 else 57 record(date, -amount, desc); 58 acc.change(date, getDebt()); 59 } 60 61 void CreditAccount::settle(const Date& date) { 62 double interest = acc.getSum(date) * rate; 63 if (interest != 0) 64 record(date, interest, "interest"); 65 if (date.getMonth() == 1) 66 record(date, -fee, "annual fee"); 67 acc.reset(date,getDebt()); 68 69 } 70 void CreditAccount::show()const { 71 Account::show(); 72 cout << "\tAvailable credit:"<<getAvsilbilableCredit(); 73 }
7_10.cpp
1 #include"account.h" 2 #include<iostream> 3 using namespace std; 4 int main() { 5 Date date(2008, 11, 1); 6 SavingAccount sa1(date, "s3755217", 0.015); 7 SavingAccount sa2(date, "02342342", 0.015); 8 CreditAccount ca(date, "c5392394", 10000, 0.0005, 50); 9 10 sa1.deposit(Date(2008, 11, 5), 5000, "salary"); 11 ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell"); 12 sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 13 14 ca.settle(Date(2008, 12, 1)); 15 16 ca.deposit(Date(2008, 12, 1), 2016, "repay the credit"); 17 sa1.deposit(Date(2008, 12, 5), 5500, "salary"); 18 19 sa1.settle(Date(2009, 1, 1)); 20 sa2.settle(Date(2009, 1, 1)); 21 ca.settle(Date(2009, 1, 1)); 22 23 cout << endl; 24 sa1.show();cout << endl; 25 sa2.show();cout << endl; 26 ca.show();cout << endl; 27 cout << "Total: " << Account::getTotal() << endl; 28 return 0; 29 }
執行結果截圖