实验5

task3

hpp

#pragma once
#include <iostream>
#include <string>

using std::string;

class MachinePets {
public:
    MachinePets(const string& s);
    string get_nickname() const;
    virtual string talk() const = 0;

protected:
    string nickname;
};

MachinePets::MachinePets(const string& s) : nickname(s) {}

string MachinePets::get_nickname() const {
    return nickname;
}

class PetCats : public MachinePets {
public:
    PetCats(const string& s);
    string talk() const override;
};

PetCats::PetCats(const string& s) : MachinePets(s) {}

string PetCats::talk() const {
    return "miao wu~";
}

class PetDogs : public MachinePets {
public:
    PetDogs(const string& s);
    string talk() const override;
};

PetDogs::PetDogs(const string& s) : MachinePets(s) {}

string PetDogs::talk() const {
    return "wang wang~";
}

task4

hpp

#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using std::string;
using std::vector;

class Film {
public:
    Film();
    friend std::istream& operator>>(std::istream& in, Film& f);
    friend std::ostream& operator<<(std::ostream& out, const Film& f);
    int get_year() const;

private:
    string title;
    string director;
    string country;
    int year;
};

Film::Film() : title(""), director(""), country(""), year(0) {}

std::istream& operator>>(std::istream& in, Film& f) {
    std::cout << "录入片名: ";
    in >> f.title;
    std::cout << "录入导演: ";
    in >> f.director;
    std::cout << "录入制片国家/地区: ";
    in >> f.country;
    std::cout << "录入上映年份: ";
    in >> f.year;
    return in;
}

std::ostream& operator<<(std::ostream& out, const Film& f) {
    out << f.title << " " << f.director << " " << f.country << " " << f.year << std::endl;
    return out;
}

int Film::get_year() const {
    return year;
}

// 比较函数,用于按发行年份升序排序
bool compare_by_year(const Film& f1, const Film& f2) {
    return f1.get_year() < f2.get_year();
}

task5

hpp

#pragma once
#include <iostream>

using namespace std;

template <typename T>
class Complex {
public:
    Complex(T r = 0, T i = 0);
    Complex(const Complex& c);
    T get_real() const;
    T get_imag() const;
    Complex operator+=(const Complex& c);
    bool operator==(const Complex& c);

    template <typename T> friend Complex<T> operator+(const Complex<T>& c1, const Complex<T>& c2);
    template <typename T> friend istream& operator>>(istream& in, Complex<T>& c);
    template <typename T> friend ostream& operator<<(ostream& out, const Complex<T>& c);

private:
    T real, imag;
};

template <typename T>
Complex<T>::Complex(T r, T i) : real{ r }, imag{ i } {}

template <typename T>
Complex<T>::Complex(const Complex& c) : real{ c.real }, imag{c.imag} {}

template <typename T>
T Complex<T>::get_real() const { return real; }

template <typename T>
T Complex<T>::get_imag() const { return imag; }

template <typename T>
Complex<T> Complex<T>::operator+=(const Complex& c) {
    real += c.real;
    imag += c.imag;

    return *this;
}

template <typename T>
bool Complex<T>::operator==(const Complex& c) {
    if (real == c.real && imag == c.imag)
        return true;
    else return false;
}

template <typename T>
Complex<T> operator+(const Complex<T>& c1, const Complex<T>& c2) {
    return Complex<T>(c1.real + c2.real, c1.imag + c2.imag);
}


template <typename T>
istream& operator>>(istream& in, Complex<T>& c) {
    in >> c.real >> c.imag;

    return in;
}

template <typename T>
ostream& operator<<(ostream& out, const Complex<T>& c) {
    if (c.imag >= 0)
        out << c.real << " + " << c.imag << "i";
    else
        out << c.real << " - " << -c.imag << "i";

    return out;
}

task6

data.h

#pragma once

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 operator-(const Date& date) const {
        return totalDays - date.totalDays;
    }
};

data.cpp

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

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

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

accumulator.h

#pragma once
#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 - 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;
    }

};

account.h

#pragma once
#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 { return id; }
    double getBalance() const { return balance; }
    static double getTotal() { return total; }

    virtual void deposit(const Date& date, double amount, const std::string& desc) = 0;  //存入现金
    virtual void withdraw(const Date& date, double amount, const std::string& desc) = 0;  //取出现金
    virtual void settle(const Date& date) = 0;  //结算(利息、年费)
    virtual 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 { return rate; }
    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 {  //获得欠款额
        double balance = getBalance();
        return (balance < 0 ? balance : 0);
    }
public:
    CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee);
    double getCredit() const { return credit; }
    double getRate() const { return rate; }
    double getFee() const { return fee; }
    double getAvailableCredit() const {  //获得可用信用
        if (getBalance() < 0)
            return credit + getBalance();
        else
            return credit;
    }
    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;
};

account.cpp

#include "account.h"
#include <cmath>
#include <iostream>

using namespace std;

//Account类的实现
double Account::total = 0;

Account::Account(const Date& date, const std::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::SavingsAccount(const Date& date, const std::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) {
    if (date.getMonth() == 1) {
        double interest = acc.getSum(date) * rate  //计算年息
            / (date - Date(date.getYear() - 1, 1, 1));
        if (interest != 0)
            record(date, interest, "interest");
        acc.reset(date, getBalance());
    }
}

//CreditAccount类的实现
CreditAccount::CreditAccount(const Date& date, const std::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();
}

8_8.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 };

    Account* accounts[] = { &sa1, &sa2, &ca };
    const int n = sizeof(accounts) / sizeof(Account*);

    cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl;
    char cmd;
    do {
        date.show();
        cout << "\tTotal:" << Account::getTotal() << "\tcommand>";

        int index, day;
        double amount;
        string desc;
        cin >> cmd;

        switch (cmd) {
        case'd':  //存入现金
            cin >> index >> amount;
            getline(cin, desc);
            accounts[index]->deposit(date, amount, desc);
            break;

        case'w':  //取出现金
            cin >> index >> amount;
            getline(cin, desc);
            accounts[index]->withdraw(date, amount, desc);
            break;

        case's':  //查询各账户信息
            for (int i = 0; i < n; i++) {
                cout << "[" << i << "]";
                accounts[i]->show();
                cout << endl;
            }
            break;

        case'c':  //改变日期
            cin >> day;
            if (day < date.getDay())
                cout << "You cannot specify a previous day";
            else if (day > date.getMaxDay())
                cout << "Invalid day";
            else
                date = Date(date.getYear(), date.getMonth(), day);
            break;

        case'n':  //进入下个月
            if (date.getMonth() == 12)
                date = Date(date.getYear() + 1, 1, 1);
            else
                date = Date(date.getYear(), date.getMonth() + 1, 1);

            for (int i = 0; i < n; i++)
                accounts[i]->settle(date);

            break;
        }

    } while (cmd != 'e');
}

posted @ 2024-12-09 11:37  连跪也能称雄  阅读(1)  评论(0编辑  收藏  举报