实验4 类的组合、继承、模板类、标准库

实验任务2

代码

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 } 

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:成绩存储在派生类GradeCalc的基类std::vector<int>中。派生类方法sort、min、max、average、output是通过std::vector<int>的接口来访问成绩的。input方法通过调用基类std::vector<int>的push_back接口,将用户输入的                   成绩逐一存入基类数据结构中。

问题2:分母的功能是除以人数n来得到平均值。有影响。乘以1.0是为了将运算转化为浮点型,能得到更准确的平均值。

问题3:当前代码未对用户输入进行校验。例如,输入负数或非法字符时程序可能会崩溃。

 

实验任务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 } 

task3.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:GradeCalc使用组合方式,将std::vector<int>定义为一个私有成员变量grades,用来存储所有成绩。派生类方法sort、min、max、average、output是通过std::vector<int>的接口来访问成绩的。实验任务2用的继承方式,实                验任务3用的组合方式。

问题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 }

运行结果

 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 }

运行结果

 问题1:清空输入缓冲区:cin.ignore(numeric_limits<streamsize>::max(), '\n'); 用于跳过输入缓冲区中所有字符,直到遇到换行符。解决缓冲区遗留问题:保证后续getline方法可以正确读入用户数据,而不是直接读取缓冲区遗留                  的换行符。

问题2:line16用于清空输入缓冲区,确保getline能正确读取有效的用户输入。

 

实验任务5

代码

grm.hpp

 1 template <typename T>
 2 class GameResourceManager {
 3 private:
 4     T resource;
 5 public:
 6     GameResourceManager(T initValue) : resource(initValue) {}
 7     T get() const { return resource; }
 8     void update(T change) {
 9         resource += change;
10         if (resource < 0) resource = 0;
11     }
12 };

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 }

运行结果

 

实验任务6

代码

info.hpp

 1 #ifndef INFO_HPP
 2 #define INFO_HPP
 3 
 4 #include <string>
 5 #include <iostream>
 6 using namespace std;
 7 
 8 class Info {
 9 private:
10     string nickname;  // 昵称
11     string contact;   // 联系方式
12     string city;      // 所在城市
13     int attendees;    // 预定参加人数
14 public:
15     // 构造函数:初始化预约信息
16     Info(const string& name, const string& contactInfo, const string& location, int num)
17         : nickname(name), contact(contactInfo), city(location), attendees(num) {}
18 
19     // 显示预约信息
20     void display() const {
21         cout << "昵称: " << nickname 
22              << ", 联系方式: " << contact 
23              << ", 所在城市: " << city 
24              << ", 预定人数: " << attendees << endl;
25     }
26 
27     // 获取预约人数
28     int getAttendees() const {
29         return attendees;
30     }
31 };
32 
33 #endif

 

task.cpp

 1 #include "info.hpp"
 2 #include <vector>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 int main() {
 7     const int capacity = 100;  // 最大容纳人数
 8     vector<Info> audience;     // 存储预约听众信息
 9     int totalAttendees = 0;    // 当前总人数
10 
11     while (true) {
12         string name, contact, city;
13         int num;
14 
15         // 输入昵称
16         cout << "请输入昵称(或输入 q 退出):";
17         cin >> name;
18         if (name == "q") break;
19 
20         // 输入联系方式
21         cout << "请输入联系方式:";
22         cin >> contact;
23 
24         // 输入所在城市
25         cout << "请输入所在城市:";
26         cin >> city;
27 
28         // 输入预定人数
29         cout << "请输入预定人数:";
30         cin >> num;
31 
32         int remainingCapacity = capacity - totalAttendees;  // 剩余容量
33 
34         // 校验人数是否超过剩余容量
35         if (num > remainingCapacity) {
36             cout << "预定人数超过剩余容量。" << endl;
37             cout << "输入 'q' 退出预定,或输入 'u' 更新预定人数:";
38             char choice;
39             cin >> choice;
40 
41             if (choice == 'q') break;  // 退出
42             if (choice == 'u') {
43                 cout << "请输入更新后的预定人数:";
44                 cin >> num;
45                 if (num > remainingCapacity) {
46                     cout << "更新后人数仍超过剩余容量。\n";
47                     continue;
48                 }
49             }
50         }
51 
52         // 保存预约信息
53         audience.emplace_back(name, contact, city, num);
54         totalAttendees += num;
55 
56         // 检查是否已达到容量限制
57         if (totalAttendees >= capacity) {
58             cout << "已达到最大容量!停止输入。\n";
59             break;
60         }
61     }
62 
63     // 输出所有预约信息
64     cout << "\n预约信息如下:" << endl;
65     for (const auto& person : audience) {
66         person.display();
67     }
68 
69     return 0;
70 }

运行结果

 

实验任务7

代码

date.h

 1 #ifndef DATE_H
 2 #define DATE_H
 3 
 4 class Date {
 5 private:
 6     int year, month, day;
 7 
 8 public:
 9     Date(int y, int m, int d) : year(y), month(m), day(d) {}
10 
11     int getYear() const { return year; }
12     int getMonth() const { return month; }
13     int getDay() const { return day; }
14 
15     void show() const;
16     int diff(const Date& other) const;  // 计算两个日期之间的天数差
17 };
18 
19 #endif

date.cpp

#include "date.h"
#include <iostream>
using namespace std;

void Date::show() const {
    cout << year << "-" 
         << (month < 10 ? "0" : "") << month << "-" 
         << (day < 10 ? "0" : "") << day;
}

int Date::diff(const Date& other) const {
    // 简化计算,假设每个月30天
    int days1 = year * 365 + month * 30 + day;
    int days2 = other.year * 365 + other.month * 30 + other.day;
    return days1 - days2;
}

accumulator.h

 1 #ifndef ACCUMULATOR_H
 2 #define ACCUMULATOR_H
 3 
 4 #include "date.h"
 5 
 6 class Accumulator {
 7 private:
 8     double sum;  // 累积金额
 9     Date lastDate;
10 
11 public:
12     Accumulator(const Date& startDate, double initialSum = 0) 
13         : lastDate(startDate), sum(initialSum) {}
14 
15     void accumulate(const Date& currentDate, double amount);
16     double getSum() const { return sum; }
17 };
18 
19 #endif

accumulator.cpp

1 #include "accumulator.h"
2 
3 void Accumulator::accumulate(const Date& currentDate, double amount) {
4     int days = currentDate.diff(lastDate);  // 计算从 lastDate 到 currentDate 的天数
5     sum += days * amount;
6     lastDate = currentDate;
7 }

account.h

 1 #ifndef ACCOUNT_H
 2 #define ACCOUNT_H
 3 
 4 #include "date.h"
 5 #include "accumulator.h"
 6 #include <string>
 7 using namespace std;
 8 
 9 class Account {
10 private:
11     string owner;    // 账户拥有者
12     double balance;  // 当前余额
13     Accumulator acc; // 用于计算利息
14 
15 protected:
16     void record(const Date& date, double amount, const string& desc);
17 
18 public:
19     Account(const string& ownerName, const Date& startDate)
20         : owner(ownerName), balance(0), acc(startDate) {}
21 
22     void deposit(const Date& date, double amount, const string& desc);
23     void withdraw(const Date& date, double amount, const string& desc);
24     void settle(const Date& date, double rate);
25 
26     double getBalance() const { return balance; }
27     const string& getOwner() const { return owner; }
28 };
29 
30 #endif

account.cpp

 1 #include "account.h"
 2 #include <iostream>
 3 using namespace std;
 4 
 5 void Account::record(const Date& date, double amount, const string& desc) {
 6     balance += amount;
 7     acc.accumulate(date, balance);
 8     cout << "[";
 9     date.show();
10     cout << "] " << desc << ": " 
11          << (amount > 0 ? "+" : "") << amount 
12          << ", 余额: " << balance << endl;
13 }
14 
15 void Account::deposit(const Date& date, double amount, const string& desc) {
16     record(date, amount, desc);
17 }
18 
19 void Account::withdraw(const Date& date, double amount, const string& desc) {
20     if (amount > balance) {
21         cout << "余额不足,无法完成取款。" << endl;
22     } else {
23         record(date, -amount, desc);
24     }
25 }
26 
27 void Account::settle(const Date& date, double rate) {
28     double interest = acc.getSum() * rate / 365;  // 利息计算
29     if (interest > 0) {
30         record(date, interest, "利息结算");
31     }
32     acc.accumulate(date, 0);  // 重新开始累积
33 }

7_10.cpp

 1 #include "date.h"
 2 #include "account.h"
 3 #include <iostream>
 4 
 5 
 6 int main() {
 7     Date startDate(2024, 1, 1);
 8 
 9     Account account("Alice", startDate);
10 
11     // 存款操作
12     account.deposit(Date(2024, 3, 1), 1000, "存入1000元");
13     account.deposit(Date(2024, 6, 1), 2000, "存入2000元");
14 
15     // 取款操作
16     account.withdraw(Date(2024, 8, 1), 500, "取出500元");
17 
18     // 利息结算
19     account.settle(Date(2024, 12, 31), 0.03);
20 
21     // 查询余额
22     cout << "账户余额: " << account.getBalance() << endl;
23 
24     return 0;
25 }

运行结果

 

    

posted @ 2024-11-24 16:27  臣子民  阅读(1)  评论(0编辑  收藏  举报