实验4 类的组成、继承、模板类、标准库
任务二:
代码:
task2.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 #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 }
运行截图:
问题1:成绩存储在Gradecalc类的内部,由于Gradecalc派生自 std::vector<int>
,它直接继承了这个向量,用于存储成绩。派生类中的方法 sort
, min
, max
, average
, 和 output
都是通过 this
指针来访问成绩的。在 C++ 中,当一个对象的方法被调用时,this
指针指向调用该方法的对象。由于 GradeCalc
派生自 std::vector<int>
,这些方法可以直接使用 this
指针来访问和操作存储的成绩(即 std::vector<int>
中的元素)。input
方法通过 this->push_back(grade)
将成绩添加到对象中。由于 GradeCalc
派生自 std::vector<int>
,它继承了 push_back
方法,该方法用于在向量的末尾添加一个元素。因此,input
方法通过调用 push_back
将每个输入的成绩添加到 GradeCalc
对象内部存储的向量中。
问题2:
这里的分母 n
代表课程人数,即成绩的总数。关于乘以1.0的部分,这里主要是为了确保计算结果的类型为浮点数。如果去掉乘以1.0的代码,重新编译、运行,结果确实会受到影响。具体来说,平均分将不再是一个浮点数,而是一个整数,因为整数除法会丢失小数部分。
问题3:
目前 input
方法只是简单地接收用户输入的成绩,并没有对输入进行合法性检查。在实际应用中,应该增加输入验证,确保用户输入的是有效的整数成绩,并且成绩在合理的范围内(如0到100之间)。
任务三:
代码:
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 }
task3.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(); }
运行截图:
问题1:
在GradeCalc
类中,成绩被存储在私有成员变量vector<int> grades
中。这个成员变量是一个整数向量,用于保存课程的所有成绩。
组合类方法sort
, min
, max
, average
, output
都需要访问这些成绩,它们通过成员函数指针来访问类的成员变量。
问题2:
通过继承和多态等机制,我们可以轻松地扩展现有类的功能或创建新的类来满足新的需求。
任务4:
代码:
task4_1.cpp:
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:
当程序运行到test2()
时,由于之前test1()
的输入后留下了一个换行符在输入缓冲区中,getline(cin, s1);
会立即读取到这个换行符并结束读取,因此s1
将为空字符串。随后,getline(cin, s2);
会等待用户输入下一个字符串,并将其赋值给s2。
task4_2.cpp:
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 }
运行截图:
task4_3.cpp:
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 }
运行截图:
问题2:
这行代码的作用是忽略输入缓冲区中直到下一个换行符(\n
)的所有字符。这通常用于清除输入缓冲区中残留的换行符或其他不需要的字符,以避免它们干扰后续的输入操作。
任务五:
代码:
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 }
1 #include<iostream> 2 using namespace std; 3 template<typename T> 4 class GameResourceManager{ 5 private: 6 T resource; 7 public: 8 GameResourceManager(T x); 9 T get(); 10 void update(T x1); 11 }; 12 template<typename T> 13 GameResourceManager<T>::GameResourceManager(T x):resource(x){} 14 template<typename T> 15 T GameResourceManager<T>::get() 16 { 17 return resource; 18 } 19 template<typename T> 20 void GameResourceManager<T>::update(T x1) 21 { 22 resource+=x1; 23 if(resource<0) 24 resource=0; 25 }
运行截图:
任务六:
代码:
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 class info{ 5 private: 6 string nickname; 7 string contact; 8 string city; 9 int n; 10 public: 11 info(string name,string node,string cs,int num); 12 void display(); 13 }; 14 info::info(string name,string node,string cs,int num):nickname(name),contact(node),city(cs),n(num){ 15 } 16 void info::display() 17 { 18 cout<<"昵称: \t"<<nickname<<endl; 19 cout<<"联系方式:\t"<<contact<<endl; 20 cout<<"所在城市:\t"<<city<<endl; 21 cout<<"预订人数:\t"<<n<<endl; 22 }
#include <iostream> #include <vector> #include <string> #include "info.hpp" using namespace std; const int capacity = 100; int main() { vector<info> audiences; int current = capacity; string nickname, contact, city; int n; cout << "请输入预约信息(按Ctrl+Z停止输入或人数达到上限):\n"; while (true) { cout << "昵称: "; cin >> nickname; cout << "联系方式 (email 或 手机号): "; cin >> contact; cout << "所在城市: "; cin >> city; cout << "预定参加人数: "; cin >> n; if (n > current) { cout<<"对不起,只剩下"<<current<<"个座位"<<endl; cout << "预定人数超过剩余容量,请输入q退出预定或输入u更新预定信息。\n"; char choice; cout<<"你的选择:"; cin >> choice; if (choice == 'q') { break; } else if (choice == 'u') { cout<<"请重新输入预订信息:"; continue; } else { cout << "无效选择,请输入q或u。\n"; continue; } } else { audiences.emplace_back(nickname, contact, city, n); current-= n; if (current <= 0) { break; } } } cout << "\n截至目前,一共有"<<100-current<<"位听众预约。预约听众信息如下:\n"; for ( auto& info : audiences) { info.display(); cout << "\n"; } return 0; }
运行截图:
任务七:
代码:
task7.cpp:
#include"account.h" #include<iostream> using namespace std; int main() { Date date(2008, 11, 1); SavingsAccount sa1(date, "S3755217", 0.015); SavingsAccount sa2(date, "02342342", 0.015); CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); sa1.deposit(Date(2008, 11, 5), 5000, "salary"); ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell"); sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); ca.settle(Date(2008, 12, 1)); ca.deposit(Date(2008, 12, 1), 2016, "repay the credit"); sa1.deposit(Date(2008, 12, 5), 5500, "salary"); sa1.settle(Date(2009, 1, 1)); sa2.settle(Date(2009, 1, 1)); ca.settle(Date(2009, 1, 1)); cout << endl; sa1.show(); cout << endl; sa2.show(); cout << endl; ca.show(); cout << endl; cout << "Total:" << Account::getTotal() << endl; return 0; }
data.h:
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 };
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 }
1 #pragma once 2 #ifndef ACCUMULATOR_H 3 #define ACCUMULATOR_H 4 5 #include "date.h" 6 7 class Accumulator { 8 private: 9 Date lastDate; 10 double value; 11 double sum; 12 13 public: 14 Accumulator(const Date& date, double value) : lastDate(date), value(value), sum(0) { 15 } 16 17 double getSum(const Date& date) const { 18 return sum + value * date.distance(lastDate); 19 } 20 21 void change(const Date& date, double value) { 22 sum = getSum(date); 23 lastDate = date; 24 this->value = value; 25 } 26 27 void reset(const Date& date, double value) { 28 lastDate = date; 29 this->value = value; 30 sum = 0; 31 } 32 };
#pragma once #ifndef ACCOUNT_H #define ACCOUNT_H #include "date.h" #include "accumulator.h" #include <string> class Account { private: std::string id; double balance; static double total; protected: Account(const Date& date, const std::string& id); void record(const Date& date, double amount, const std::string& desc); void error(const std::string& msg) const; public: const std::string& getId() const; double getBalance() const; static double getTotal(); void show() const; }; class SavingsAccount : public Account { private: Accumulator acc; double rate; public: SavingsAccount(const Date& date, const std::string& id, double rate); double getRate() const; void deposit(const Date& date, double amount, const std::string& desc); void withdraw(const Date& date, double amount, const std::string& desc); void settle(const Date& date); }; class CreditAccount : public Account { private: Accumulator acc; double credit; double rate; double fee; double getDebt() const; public: CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee); double getCredit() const; double getRate() const; double getAvailableCredit() const; void deposit(const Date& date, double amount, const std::string& desc); void withdraw(const Date& date, double amount, const std::string& desc); void settle(const Date& date); void show() const; };
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 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 33 record(date, amount, desc); 34 acc.change(date, getBalance()); 35 } 36 37 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 38 if (amount > getBalance()) { 39 error("not enough money"); 40 } else { 41 record(date, -amount, desc); 42 acc.change(date, getBalance()); 43 } 44 } 45 46 void SavingsAccount::settle(const Date& date) { 47 double interest = acc.getSum(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1)); 48 if (interest != 0) record(date, interest, "interest"); 49 acc.reset(date, getBalance()); 50 } 51 52 CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee) 53 : Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) {} 54 55 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 56 record(date, amount, desc); 57 acc.change(date, getDebt()); 58 } 59 60 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 61 if (amount - getBalance() > credit) { 62 error("not enough credit"); 63 } else { 64 record(date, -amount, desc); 65 acc.change(date, getDebt()); 66 } 67 } 68 69 void CreditAccount::settle(const Date& date) { 70 double interest = acc.getSum(date) * rate; 71 if (interest != 0) record(date, interest, "interest"); 72 if (date.getMonth() == 1) 73 record(date, -fee, "annual fee"); 74 acc.reset(date, getDebt()); 75 } 76 77 void CreditAccount::show() const { 78 Account::show(); 79 cout << "\tAvailable credit:" << getAvailableCredit(); 80 }
运行截图: