实验四

task2

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;
}

task2.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>的对象中(即 this 所指向的对象)。派生类方法 sort、min、max、average、output 通过迭代器访问成绩,例如 begin () 和 end () 获取迭代器范围来遍历成绩。input 方法通过 push_back 接口实现数据存入对象。

问题 2:代码 line68 分母 n 的功能是计算成绩的平均值,用于将总成绩除以班级人数得到平均成绩。去掉乘以 1.0 代码,重新编译、运行,结果会有影响。因为 accumulate 函数返回的是整数类型的累加和,如果不乘以 1.0,在计算平均值时会进行整数除法,导致结果不准确,乘以 1.0 是为了将结果转换为浮点数,以得到正确的平均值。

问题 3:从真实应用场景角度考虑,GradeCalc 类在设计及代码实现细节上可能存在以下不足:没有考虑成绩录入的合法性检查,例如录入负数或大于 100 的数;对于大规模数据的排序效率可能较低,可考虑使用更高效的排序算法;没有提供修改成绩的功能;在分数段统计中,对于边界值的处理可能不够灵活,例如 60 分既属于 [0, 60) 又属于 [60, 70) 的情况没有明确规定;没有考虑多门课程成绩管理的情况,可进一步扩展为可以管理多门课程成绩的类1

task3

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;
}

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 定义中,成绩存储在私有数据成员 grades 中,它是一个 vector<int>类型的对象。组合类方法 sort、min、max、average、output 通过直接访问 grades 对象来获取每个成绩,例如在 sort 方法中直接使用 grades.begin () 和 grades.end () 获取迭代器范围进行排序操作。与实验任务 2 在代码写法细节上的差别在于,实验任务 2 是通过继承 vector<int>,利用继承关系访问成绩,而本任务是通过组合的方式,将 vector<int>作为类的一个成员来访问成绩。

问题 2:对比实验任务 2 和实验任务 3,对面向对象编程有以下新的理解和领悟:继承和组合是实现代码复用的两种重要方式。继承可以直接复用基类的功能,但可能会导致类之间的耦合度较高,如果基类发生变化,可能会影响到派生类。而组合则更加灵活,通过将其他类的对象作为成员,可以在不影响外部类的情况下修改内部成员类的实现,降低了类之间的耦合度。在实际编程中,应根据具体需求选择合适的方式来设计类,有时可能需要综合使用继承和组合来达到更好的设计效果。

task4

task4_1.cpp 

#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();
}

问题 1:去掉 task4_1.cpp 的 line35 后,重新编译、运行,当连续进行测试 1 和测试 2 时,会出现测试 2 直接跳过输入 s1 的情况。line35 的用途是忽略输入缓冲区中的剩余字符,直到遇到换行符 '\n'。在测试 1 中使用 cin 输入字符串时,输入完字符串后按下回车键,回车键会留在输入缓冲区中。如果不忽略这个回车键,在测试 2 中使用 getline () 函数读取字符串时,它会首先读取到这个回车键,导致直接跳过输入 s1 的步骤。

task2.cpp

#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_3.cpp 

#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();
}

问题 2:去掉 task4_3.cpp 的 line16 后,重新编译、运行,当输入数字后直接按回车键,然后输入字符串时,会出现程序直接结束的情况。line16 的用途是在每次输入数字后,忽略输入缓冲区中的剩余字符,直到遇到换行符 '\n'。这是因为在使用 cin 读取数字后,回车键会留在输入缓冲区中。如果不忽略这个回车键,当使用 getline () 函数读取字符串时,它会首先读取到这个回车键,导致 getline () 函数认为已经读取到了空行,从而直接结束本次循环,使程序无法正常输入字符串。

实验五

grm.hpp 

template<typename T>
class GameResourceManager {
private:
    T resource;

public:
    GameResourceManager(T initialResource) : resource(initialResource) {}

    T get() const {
        return resource;
    }

    void update(T change) {
        resource += change;
        if (resource < 0) {
            resource = 0;
        }
    }
};

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>

class Info {
private:
    std::string nickname;
    std::string contact;
    std::string city;
    int n;

public:
    Info(const std::string &nick, const std::string &cont, const std::string &cty, int num) :
    nickname(nick), contact(cont), city(cty), n(num) {}

    void display() const {
        std::cout << "昵称: " << nickname << std::endl;
        std::cout << "联系方式: " << contact << std::endl;
        std::cout << "所在城市: " << city << std::endl;
        std::cout << "预定人数: " << n << std::endl;
    }
};

task6.cpp

#include "info.hpp"
#include <iostream>
#include <vector>
#include <limits>

const int capacity = 100;

int main() {
    std::vector<Info> audience_list;
    int total = 0;
    while (true) {
        std::string nick, cont, city;
        int num;
        std::cout << "录入用户预约信息: " << std::endl;
        std::cout << "昵称 联系方式(邮箱/手机号) 所在城市 预定参加人数" << std::endl;
        if (!(std::cin >> nick >> cont >> city >> num)) {
            break;
        }
        if (total + num > capacity) {
            std::cout << "对不起,只剩 " << capacity - total << " 个位置. 输入u,更新(update)预定信息,输入q,退出预定" << std::endl;
            std::string choice;
            std::cin >> choice;
            if (choice == "u") {
                std::cout << "请重新输入预定信息: " << std::endl;
                continue;
            } else if (choice == "q") {
                break;
            }
        }
        audience_list.push_back(Info(nick, cont, city, num));
        total += num;
        if (total >= capacity) {
            std::cout << "截至目前,一共有 " << total << " 位听众预约。预约听众信息如下: " << std::endl;
            for (const auto &info : audience_list) {
                info.display();
            }
            break;
        }
    }
    return 0;
}

task7

date.h

1 #pragma once
 2 #ifndef  DATE H
 3 #define  DATE H
 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     int distance(const Date& date)const {
21         return totalDays - date.totalDays;
22     }
23 };
24 #endif//  DATE H

date.h

date.cpp

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 Date::Date(int year, int month, int day) :year{ year }, month{ month }, day{ day } {
 9     if (day <= 0 || day > getMaxDay()) {
10         cout << "Invalid date:";
11         show();
12         cout << endl;
13         exit(1);
14     }
15     int years = year - 1;
16     totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day;
17     if (isLeapYear() && month > 2)totalDays++;
18 }
19 int Date::getMaxDay()const {
20     if (isLeapYear() && month == 2)
21         return 29;
22     else return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
23 }
24 
25 void Date::show()const {
26     cout << getYear() << "-" << getMonth() << "-" << getDay();
27 }

date.cpp

accumulator.h

1 #pragma once
 2 #ifndef  ACCUMULATOR H
 3 #define  ACCUMULATOR H
 4 #include"date.h"
 5 class Accumulator {
 6 private:
 7     Date lastDate;
 8     double value;
 9     double sum;
10 public:
11     Accumulator(const Date& date, double value) :lastDate(date), value(value), sum{ 0 } {
12 }
13 
14     double getSum(const Date& date)const {
15         return sum + value * date.distance(lastDate);
16     }
17 
18     void change(const Date& date, double value) {
19         sum = getSum(date);
20         lastDate = date; this->value = value;
21     }
22 
23     void reset(const Date& date, double value) {
24         lastDate = date; this->value = value; sum = 0;
25     }
26 };
27 #endif//ACCUMULATOR H

accumulator.h

account.h

1 #pragma once
 2 #ifndef  ACCOUNT H
 3 #define  ACCOUNT H
 4 #include"date.h"
 5 #include"accumulator.h"
 6 #include<string>
 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 public:
17     const std::string& getId()const { return id; }
18     double getBalance()const { return balance; }
19     static double getTotal() { return total; }
20 
21     void show()const;
22 };
23 class SavingsAccount :public Account {
24 private:
25     Accumulator acc;
26     double rate;
27 public:
28     SavingsAccount(const Date& date, const std::string& id, double rate);
29     double getRate()const { return rate; }
30 
31     void deposit(const Date& date, double amount, const std::string& desc);
32     void withdraw(const Date& date, double amount, const std::string& desc);
33     void settle(const Date& date);
34 };
35 class CreditAccount :public Account {
36 private:
37     Accumulator acc;
38     double credit;
39     double rate;
40     double fee;
41     double getDebt()const {
42         double balance = getBalance();
43         return (balance < 0 ? balance : 0);
44     }
45 public:
46     CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee);
47     double getCredit()const { return credit; }
48     double getRate()const { return rate;}
49     double getAvailableCredit()const {
50         if (getBalance() < 0)
51             return credit + getBalance();
52         else
53             return credit;
54     }
55     void deposit(const Date& date, double amount, const std::string& desc);
56     void withdraw(const Date& date, double amount, const std::string& desc);
57     void settle(const Date& date);
58     void show()const;
59 };
60 #endif//ACCOUNT H

account.h:

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(); cout << "\t#" << id << "created" << endl;
 9 }
10 
11 
12 void Account::record(const Date& date, double amount, const string& desc) {
13     amount = floor(amount * 100 + 0.5) / 100;
14     balance += amount;
15     total += amount;
16     date.show();
17     cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
18 }
19 
20 void Account::show()const { cout << id << "\tBalance:" << balance; }
21 void Account::error(const string& msg)const {
22     cout << "Error(#" << id << "):" << msg << endl;
23 }
24 
25 SavingsAccount::SavingsAccount(const Date&date,const string&id,double rate):Account(date,id),rate(rate), acc(date,0){}
26 
27 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) {
28     record(date, amount, desc);
29     acc.change(date, getBalance());
30 }
31 
32 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) {
33     if (amount > getBalance()) {
34         error("not enough money");
35     }
36     else {
37         record(date, -amount, desc);
38         acc.change(date, getBalance());
39     }
40 }
41 
42 void SavingsAccount::settle(const Date& date) {
43     double interest = acc.getSum(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1));
44     if (interest != 0)record(date, interest, "interest");
45     acc.reset(date, getBalance());
46 }
47 
48 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){}
49 
50 void CreditAccount::deposit(const Date& date, double amount, const string& desc) {
51     record(date, amount, desc);
52     acc.change(date, getDebt());
53 }
54 
55 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) {
56     if (amount - getBalance() > credit) {
57         error("not enough credit");
58     }
59     else {
60         record(date, -amount, desc);
61         acc.change(date, getDebt());
62     }
63 }
64 
65 void CreditAccount::settle(const Date& date) {
66     double interest = acc.getSum(date) * rate;
67     if (interest != 0)record(date, interest, "interest");
68     if (date.getMonth() == 1)
69         record(date, -fee, "annual fee");
70     acc.reset(date, getDebt());
71 }
72 
73 void CreditAccount::show()const {
74     Account::show();
75     cout << "\tAvailable credit:" << getAvailableCredit();
76 }

account.cpp

task7_10.cpp

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     return 0;
31 }

task7_10.cpp

使用了类的继承,通过基类account继承两个派生类SavingAccounts和CreditAccounts,模块抽象更清晰。

代码复杂度增加。

 

posted @ 2024-11-24 11:51  连跪也能称雄  阅读(13)  评论(0)    收藏  举报