实验四
1 #include "GradeCalc_2.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 } 23 //问题1:派生类GradeCalc定义中,成绩存储在哪里?派生类方法sort, min, max, average, output都要访问成绩,是通过什么接口访问到每个成绩的?input方法是通过什么接口实现数据存入对象的? 24 //答:存储在GradeCalc对象内部的vector中。vector中的this指针访问成绩。通过调用std::vector<int>的push_back成员函数。 25 //问题2:代码line68分母的功能是?去掉乘以1.0代码,重新编译、运行,结果有影响吗?为什么要乘以1.0? 26 //答:求平均值;有,只保留整数部分;* 1.0是为了把int转化为float 27 //问题3:从真实应用场景角度考虑,GradeCalc类在设计及代码实现细节上,有哪些地方尚未考虑周全,仍需继续迭代、完善? 28 //1.输入成绩时没有验证数据合法性 29 //2.代码中没有考虑极端情况,例如: 30 //用户创建 GradeCalc 对象时将学生人数 n 设置为 0,可能导致除零错误。 31 //vector<int> 容器为空时调用 min() 和 max() 函数会产生未定义行为。 32 //3.分数段设置的灵活性:分数段被硬编码为固定的 5 个区间,无法适应不同需求(如一些考试可能采用不同分数等级划分) 33 //4.数据封装:GradeCalc 类继承了 std::vector<int>,但 std::vector 是一种通用容器,不适合作为逻辑类的基类。容易破坏封装性,用户可以直接通过 GradeCalc 对象调用 vector 的方法修改数据,可能导致逻辑不一致。 34 //5.GradeCalc 类当前只支持整数成绩,但在某些场景(如 GPA 计算)可能需要支持浮点数。
1 #include<iostream> 2 #include<vector> 3 #include<string> 4 5 6 using std::vector; 7 using std::string; 8 using std::cin; 9 using std::cout; 10 using std::endl; 11 12 #include "GradeCalc_3.hpp" 13 #include <iomanip> 14 15 void test() { 16 int n; 17 cout << "输入班级人数: "; 18 cin >> n; 19 20 GradeCalc c1("OOP", n); 21 22 cout << "录入成绩: " << endl;; 23 c1.input(); 24 cout << "输出成绩: " << endl; 25 c1.output(); 26 27 cout << string(20, '*') + "课程成绩信息" + string(20, '*') << endl; 28 c1.info(); 29 } 30 31 int main() { 32 test(); 33 } 34 ////问题1:组合类GradeCalc定义中,成绩存储在哪里?组合类方法sort, min, max, average, output都要访问成绩,是通过什么接口访问到每个成绩的?观察与实验任务2在代码写法细节上的差别。 35 //答:public的vector中;通过vector直接访问 36 //问题2:对比实验任务2和实验任务3,主体代码逻辑(测试代码)没有变更,类GradeCalc的 接口也没变,变化的是类GradeCalc的设计及接口内部实现细节。你对面向对象编程有什么新的理解和领悟吗? 37 //答:类有多种设计:有时可直接将基类作为自定义类的数据元素,而使用类的公有继承则可以调用基类的接口,各有利弊
#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; }
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 }
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 #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 }
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 } 35 //问题1:去掉task4_1.cpp的line35,重新编译、运行,给出此时运行结果截图。查阅资料,回 答line35在这里的用途是什么? 36 //答:cin.ignore(numeric_limits<streamsize>::max(), '\n');尤其用于处理输入流,直到下一个换行符(\n)之前,忽略(或“跳过”)输入缓冲区中的所有字符,最多忽略 numeric_limits<streamsize>::max() (一个极大的常量)个字符。 37 //清除/跳过缓存区剩下的\n 38 //问题2:去掉task4_3.cpp的line16,重新编译、运行,给出此时运行结果。查阅资料,回答line16 在这里的用途是什么? 39 //答:in.ignore(numeric_limits<streamsize>::max(), '\n');直到下一个换行符(\n)之前,忽略输入流(cin)中的所有字符,忽略字符数量小于等于<>指定的最大值。 40 //处理输入后的残留字符,特别是在读取之前已经读取了其他数据时。
1 #pragma once 2 #include<iostream> 3 using namespace std; 4 template <typename T> 5 class GameResourceManager { 6 private: 7 T resouce; 8 public: 9 GameResourceManager(T Resource) :resouce(Resource) {} 10 T get()const { 11 return resouce; 12 } 13 void update(T value) { 14 resouce += value; 15 if (resouce < 0) resouce = 0; 16 } 17 };
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 }
1 #include<iostream> 2 #include<string> 3 4 using std::cout; 5 class info { 6 private: 7 std::string nickname; 8 std::string contact; 9 std::string city; 10 11 int n; 12 public: 13 info(const std::string & nick, const std::string & cont, const std::string & cit, int num): nickname(nick), contact(cont), city(cit), n(num){} 14 15 void display()const { 16 cout << "昵称: " << nickname << "\n"; 17 cout << "联系方式: " << contact << "\n"; 18 cout << "所在城市: " << city << "\n"; 19 cout<< "预定参加人数: " << n << "\n"; 20 } 21 22 int get_num() const { 23 return n; 24 25 } 26 };
1 #include<iostream> 2 #include"info.hpp" 3 using namespace std; 4 #include<iomanip> 5 #include<vector> 6 #include<limits> 7 int main() { 8 const int capacity = 100; 9 cout << "录入用户信息:" << endl; 10 cout << left << setw(15) << "昵称" << left << setw(20) << "联系方式" << left << setw(15) << "所在城市" << left << setw(15) << "预定参加人数" << endl; 11 vector<info> audience_list; 12 13 string nickname, city, contact; 14 int n; 15 int now_cap = 0; 16 while (true) { 17 if (!(cin >> nickname >> contact >> city >> n)) 18 break; 19 20 if (now_cap + n > capacity) 21 { 22 cout << "对不起,只剩80个位置。" << endl; 23 cout << "1.输入u,更新预约信息" << endl << "2.输入q,退出预定" << endl << "你的选择:" << endl; 24 string a; 25 cin >> a; 26 if (a == "u") 27 { 28 cout << "请重新输入预约信息:" << endl; 29 cin >> nickname >> contact >> city >> n; 30 audience_list.push_back(info(nickname, contact, city, n)); 31 } 32 else if (a == "q") 33 break; 34 } 35 else { 36 now_cap += n; 37 audience_list.push_back(info(nickname, contact, city, n)); 38 } 39 } 40 cout << "截至目前,一共有" << now_cap << "位听众预约。预约听众信息如下:" << endl; 41 for (info& i : audience_list) 42 i.display(); 43 }
1 #ifndef __DATE_H__ 2 #define __DATE_H__ 3 4 class Date { 5 private: 6 int year; 7 int month; 8 int day; 9 int totalDays; 10 public: 11 Date(int year, int month, int day); 12 int getYear() const { return year; } 13 int getMonth() const { return month; } 14 int getDay() const { return day; } 15 int getMaxDay() const; 16 bool isLeapYear() const { 17 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 18 } 19 void show() const; 20 21 int distance(const Date& date) const { 22 return totalDays - date.totalDays; 23 } 24 }; 25 26 #endif
1 #include "date.h" 2 #include <iostream> 3 #include <cstdlib> 4 using namespace std; 5 namespace { 6 const int DAYS_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 cout << endl; 14 exit(1); 15 } 16 int years = year - 1; 17 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day; 18 if (isLeapYear() && month > 2) totalDays++; 19 } 20 21 int Date::getMaxDay() const { 22 if (isLeapYear() && month == 2) 23 return 29; 24 else 25 return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; 26 } 27 28 void Date::show() const { 29 cout << getYear() << "-" << getMonth() << "-" << getDay(); 30 }
account.h
1 #ifndef ACCUMULATOR H 2 #define ACCUMULATOR H 3 #include"date.h" 4 class Accumulator { 5 private: 6 Date lastDate; 7 double value; 8 double sum; 9 public: 10 Accumulator(const Date & date, double value) :lastDate(date), value(value), sum{ 0 } { 11 12 } 13 14 double getSum(const Date & date)const { 15 return sum + value * date.distance(lastDate); 16 17 } 18 19 void change(const Date & date, double value) { 20 sum = getSum(date); 21 lastDate = date; this->value = value; 22 23 } 24 25 void reset(const Date & date, double value) { 26 lastDate = date; this->value = value; sum = 0; 27 28 } 29 30 }; 31 #endif
1 #include "account.h" 2 #include <cmath> 3 #include <iostream> 4 using namespace std; 5 6 double Account::total = 0; 7 8 Account::Account(const Date& date, const string& id) : id(id), balance(0) { 9 date.show(); 10 cout << "\t#" << id << " created" << endl; 11 } 12 13 void Account::record(const Date& date, double amount, const string& desc) { 14 amount = floor(amount * 100 + 0.5) / 100; // 四舍五入到两位小数 15 balance += amount; 16 total += amount; 17 date.show(); 18 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 19 } 20 21 void Account::show() const { 22 cout << id << "\tBalance:" << balance; 23 } 24 25 void Account::error(const string& msg) const { 26 cout << "Error(#" << id << "):" << msg << endl; 27 } 28 29 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) 30 : Account(date, id), rate(rate), acc(date, 0) { 31 } 32 33 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 34 record(date, amount, desc); 35 acc.change(date, getBalance()); 36 } 37 38 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 39 if (amount > getBalance()) { 40 error("not enough money"); 41 } 42 else { 43 record(date, -amount, desc); 44 acc.change(date, getBalance()); 45 } 46 } 47 48 void SavingsAccount::settle(const Date& date) { 49 double interest = acc.getSum(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1)); 50 if (interest != 0) record(date, interest, "interest"); 51 acc.reset(date, getBalance()); 52 } 53 54 CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee) 55 : Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) { 56 } 57 58 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 59 record(date, amount, desc); 60 acc.change(date, getDebt()); 61 } 62 63 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 64 if (amount - getBalance() > credit) { 65 error("not enough credit"); 66 } 67 else { 68 record(date, -amount, desc); 69 acc.change(date, getDebt()); 70 } 71 } 72 73 void CreditAccount::settle(const Date& date) { 74 double interest = acc.getSum(date) * rate; 75 if (interest != 0) record(date, interest, "interest"); 76 if (date.getMonth() == 1) 77 record(date, -fee, "annual fee"); 78 acc.reset(date, getDebt()); 79 } 80 81 void CreditAccount::show() const { 82 Account::show(); 83 cout << "\tAvailable credit:" << getAvailableCredit(); 84 }
1 #include"account.h" 2 #include<iostream> 3 4 using namespace std; 5 6 int main() { 7 Date date(2008, 11, 1); 8 SavingsAccount sa1(date, "S3755217", 0.015); 9 SavingsAccount sa2(date, "02342342", 0.015); 10 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 11 12 sa1.deposit(Date(2008, 11, 5), 5000, "salary"); 13 ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell"); 14 sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); 15 16 ca.settle(Date(2008, 12, 1)); 17 18 ca.deposit(Date(2008, 12, 1), 2016, "repay the credit"); 19 sa1.deposit(Date(2008, 12, 5), 5500, "salary"); 20 21 sa1.settle(Date(2009, 1, 1)); 22 sa2.settle(Date(2009, 1, 1)); 23 ca.settle(Date(2009, 1, 1)); 24 25 cout << endl; 26 sa1.show(); cout << endl; 27 sa2.show(); cout << endl; 28 ca.show(); cout << endl; 29 cout << "Total:" << Account::getTotal() << endl; 30 31 return 0; 32 }