实验四

任务2:

代码:

#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();
}
#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:成绩存储在Gradecalc类的内部,由于Gradecalc派生自 std::vector<int>,它直接继承了这个向量,用于存储成绩。

派生类中的方法 sortminmaxaverage, 和 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的部分,这里主要是为了确保计算结果的类型为浮点数,而不是整数。在C++中,当两个整数进行除法运算时,结果也会是一个整数,小数部分会被截断。为了得到包含小数部分的平均分,我们需要确保至少有一个操作数是浮点数。

如果去掉乘以1.0的代码,重新编译、运行,结果确实会受到影响。具体来说,平均分将不再是一个浮点数,而是一个整数,因为整数除法会丢失小数部分。

乘以1.0的目的是进行类型强制转换,确保除法运算的结果是一个浮点数。在C++中,当整数与浮点数进行运算时,整数会被隐式转换为浮点数,然后执行浮点数除法,从而得到包含小数部分的结果。

问题3:

目前 input 方法只是简单地接收用户输入的成绩,并没有对输入进行合法性检查。在实际应用中,应该增加输入验证,确保用户输入的是有效的整数成绩,并且成绩在合理的范围内(如0到100之间)。

任务3:

代码:

#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; 
} 
#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中。这个成员变量是一个整数向量,用于保存课程的所有成绩。

组合类方法sortminmaxaverageoutput都需要访问这些成绩,它们通过成员函数(也称为方法)的隐式this指针来访问类的成员变量。

问题2:

通过继承和多态等机制,我们可以轻松地扩展现有类的功能或创建新的类来满足新的需求。

任务4:

代码:

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

task4.1运行截图:

 问题1:

如果去掉这行代码并重新编译运行程序,运行结果将会因为输入缓冲区的残留换行符而出现问题。

当程序运行到test1()时,它会正常工作,因为cin >> s1 >> s2;只会读取到空白符之前的输入,并在遇到空白符时停止读取。

当程序运行到test2()时,由于之前test1()的输入后留下了一个换行符在输入缓冲区中,getline(cin, s1);会立即读取到这个换行符并结束读取,因此s1将为空字符串。随后,getline(cin, s2);会等待用户输入下一个字符串,并将其赋值给s2

task4.2运行截图:

 task4.3运行截图:

 问题2:

这行代码的作用是忽略输入缓冲区中直到下一个换行符(\n)的所有字符。这通常用于清除输入缓冲区中残留的换行符或其他不需要的字符,以避免它们干扰后续的输入操作。如果去掉那会对后面的输入造成影响。

任务5:

代码:

#include<iostream>
using namespace std;
template<typename T>
class GameResourceManager{
    private:
        T resource;
    public:
        GameResourceManager(T x);
        T get();
        void update(T x1);
};
template<typename T>
GameResourceManager<T>::GameResourceManager(T x):resource(x){}
template<typename T>
T GameResourceManager<T>::get()
{
    return resource;
}
template<typename T>
void GameResourceManager<T>::update(T x1)
{
    resource+=x1;
    if(resource<0)
    resource=0;
}

 

运行截图:

 任务6:

 代码:

#include<iostream>
#include<string>
using namespace std;
class info{
    private:
        string nickname;
        string contact;
        string city;
        int n;
    public:
        info(string name,string node,string cs,int num);
        void display();
};
info::info(string name,string node,string cs,int num):nickname(name),contact(node),city(cs),n(num){
}
void info::display()
{
    cout<<"昵称:    \t"<<nickname<<endl;
    cout<<"联系方式:\t"<<contact<<endl;
    cout<<"所在城市:\t"<<city<<endl;
    cout<<"预订人数:\t"<<n<<endl;
}
#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;
}

运行截图:

 任务7:

data.h代码

#ifndef __DATE_H__
#define __DATE_H__

class Date {
private:
    int year;
    int month;
    int day;
    int totalDays;
public:
    Date(int year, int month, int day);
    int getYear() const { return year; }
    int getMonth() const { return month; }
    int getDay() const { return day; }
    int getMaxDay() const;
    bool isLeapYear() const {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }
    void show() const;

    int distance(const Date& date) const {
        return totalDays - date.totalDays;
    }
};

#endif //__DATE_H__
#include "date.h"
#include <iostream>
#include <cstdlib>
using namespace std;
namespace {
    const int DAYS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
}

Date::Date(int year, int month, int day) : year(year), month(month), day(day) {
    if (day <= 0 || day > getMaxDay()) {
        cout << "Invalid date:";
        show();
        cout << endl;
        exit(1);
    }
    int years = year - 1;
    totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day;
    if (isLeapYear() && month > 2) totalDays++;
}

int Date::getMaxDay() const {
    if (isLeapYear() && month == 2)
        return 29;
    else
        return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
}

void Date::show() const {
    cout << getYear() << "-" << getMonth() << "-" << getDay();
}
#pragma once
#ifndef ACCUMULATOR_H
#define ACCUMULATOR_H

#include "date.h"

class Accumulator {
private:
    Date lastDate;
    double value;
    double sum;

public:
    Accumulator(const Date& date, double value) : lastDate(date), value(value), sum(0) {
    }

    double getSum(const Date& date) const {
        return sum + value * date.distance(lastDate);
    }

    void change(const Date& date, double value) {
        sum = getSum(date);
        lastDate = date;
        this->value = value;
    }

    void reset(const Date& date, double value) {
        lastDate = date;
        this->value = value;
        sum = 0;
    }
};

#endif // ACCUMULATOR_H
#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;
};

#endif // ACCOUNT_H
#include "account.h"
#include <cmath>
#include <iostream>
using namespace std;

double Account::total = 0;

Account::Account(const Date& date, const string& id) : id(id), balance(0) {
    date.show(); 
    cout << "\t#" << id << " created" << endl;
}

void Account::record(const Date& date, double amount, const string& desc) {
    amount = floor(amount * 100 + 0.5) / 100; // 四舍五入到两位小数
    balance += amount;
    total += amount;
    date.show();
    cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
}

void Account::show() const {
    cout << id << "\tBalance:" << balance;
}

void Account::error(const string& msg) const {
    cout << "Error(#" << id << "):" << msg << endl;
}

SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate)
    : Account(date, id), rate(rate), acc(date, 0) {}

void SavingsAccount::deposit(const Date& date, double amount, const string& desc) {
    record(date, amount, desc);
    acc.change(date, getBalance());
}

void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) {
    if (amount > getBalance()) {
        error("not enough money");
    } else {
        record(date, -amount, desc);
        acc.change(date, getBalance());
    }
}

void SavingsAccount::settle(const Date& date) {
    double interest = acc.getSum(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1));
    if (interest != 0) record(date, interest, "interest");
    acc.reset(date, getBalance());
}

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) {}

void CreditAccount::deposit(const Date& date, double amount, const string& desc) {
    record(date, amount, desc);
    acc.change(date, getDebt());
}

void CreditAccount::withdraw(const Date& date, double amount, const string& desc) {
    if (amount - getBalance() > credit) {
        error("not enough credit");
    } else {
        record(date, -amount, desc);
        acc.change(date, getDebt());
    }
}

void CreditAccount::settle(const Date& date) {
    double interest = acc.getSum(date) * rate;
    if (interest != 0) record(date, interest, "interest");
    if (date.getMonth() == 1)
        record(date, -fee, "annual fee");
    acc.reset(date, getDebt());
}

void CreditAccount::show() const {
    Account::show();
    cout << "\tAvailable credit:" << getAvailableCredit();
}
#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;
}

运行截图:

 

posted @ 2024-11-19 00:16  忘忧熊  阅读(4)  评论(0编辑  收藏  举报