实验5 继承和多态

任务三:

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 class MachinePets{
 6     protected:
 7         string nickname;
 8     public:
 9         MachinePets(const string& s=""):nickname(s){
10         }
11         string get_nickname()const{
12             return nickname;
13         }
14         virtual string talk()=0;
15 };
16 class PetCats:public MachinePets{
17     public:
18         PetCats(const string &s=""):MachinePets(s){
19         }
20         string talk(){
21             string a="miao wu~";
22             return a;
23         }
24 };
25 class PetDogs:public MachinePets{
26     public:
27         PetDogs(const string &s=""):MachinePets(s){
28         }
29         string talk(){
30             string a="wang wang~";
31             return a;
32         }
33 };
View Code
 1 #include <iostream>
 2 #include <vector>
 3 #include "pets.hpp"
 4 
 5 void test() {
 6     using namespace std;
 7 
 8     vector<MachinePets *> pets;
 9 
10     pets.push_back(new PetCats("miku"));
11     pets.push_back(new PetDogs("da huang"));
12 
13     for(auto &ptr: pets)
14         cout <<ptr->get_nickname() << " says " << ptr->talk() << endl;
15 }
16 
17 int main() {
18     test();
19 }
View Code

 

运行截图:

 

任务四:

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 class Film{
 6     public:
 7         Film(){}
 8         friend istream& operator>>(istream& in, Film& film){
 9             cout <<"录入片名:" ;          in>>film.name;
10             cout <<"录入导演:" ;          in>>film.director;
11             cout <<"录入制片国家、地区:" ;in>> film.country;
12             cout <<"录入上映年份:";       in>> film.time;
13             return in; 
14         }
15         friend ostream& operator<<(ostream& out,const Film& film){
16             out<<left;
17             out<<setw(8)<<film.name<<setw(10)<<film.director<<setw(8)<<film.country<<film.time;
18             return out;
19         }
20         int get_time()const{return time;}
21     private:
22         string name;//片名
23         string director;//导演
24         string country;//国家
25         int time;//上映时间 
26 }; 
View Code
 1 #include "film.hpp"
 2 #include <iostream>
 3 #include <string>
 4 #include <vector>
 5 #include <algorithm>
 6 bool compare_by_year(const Film& a,const Film& b){
 7     return a.get_time()<b.get_time();
 8 }
 9 void test() {
10     using namespace std;
11     
12     int n;
13     cout << "输入电影数目: ";
14     cin >> n;
15 
16     cout << "录入" << n << "部影片信息" << endl;
17     vector<Film> film_lst;
18     for(int i = 0; i < n; ++i) {
19         Film f;
20         cout << string(20, '-') << "" << i+1 << "部影片录入" << string(20, '-') << endl;
21         cin >> f;
22         film_lst.push_back(f);
23     }
24 
25     // 按发行年份升序排序
26     sort(film_lst.begin(), film_lst.end(), compare_by_year);
27 
28     cout << string(20, '=') + "电影信息(按发行年份)" +  string(20, '=')<< endl;
29     for(auto &f: film_lst)
30         cout << f << endl;
31 }
32 
33 int main() {
34     test();
35 }
View Code

 

运行截图:

 

任务五:

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 template<typename T>
 6 class Complex{
 7     private:
 8         T real,imag;
 9     public:
10         Complex(){}
11         Complex(T x,T y):real(x),imag(y){}
12         Complex(const Complex& a){
13             real=a.real;
14             imag=a.imag;
15         }
16         Complex operator+=(const Complex& a){
17             real+=a.real;
18             imag+=a.imag;
19             return *this;
20         }
21         friend Complex operator+(const Complex& a,const Complex& b){
22             return Complex(a.real+b.real,a.imag+b.imag);
23         }
24         friend istream& operator>>(istream& in,Complex& a){
25             in>>a.real>>a.imag;
26             return in;
27         } 
28         friend ostream& operator<<(ostream& out,const Complex& a){
29             if(a.imag>=0)
30             out<<a.real<<"+"<<a.imag<<"i";
31             else
32             out<<a.real<<"-"<<-a.imag<<"i";
33         }
34         T get_real(){
35             return real;
36         }
37         T get_imag(){
38             return imag;
39         }
40         friend bool operator==(const Complex& a,const Complex& b){
41             return a.real==b.real&&a.imag==b.imag;
42         }
43         
44 };
View Code
 1 #include "Complex.hpp"
 2 #include <iostream>
 3 
 4 using std::cin;
 5 using std::cout;
 6 using std::endl;
 7 using std::boolalpha;
 8 
 9 void test1() {
10     Complex<int> c1(2, -5), c2(c1);
11 
12     cout << "c1 = " << c1 << endl;
13     cout << "c2 = " << c2 << endl;
14     cout << "c1 + c2 = " << c1 + c2 << endl;
15     
16     c1 += c2;
17     cout << "c1 = " << c1 << endl;
18     cout << boolalpha << (c1 == c2) << endl;
19 }
20 
21 void test2() {
22     Complex<double> c1, c2;
23     cout << "Enter c1 and c2: ";
24     cin >> c1 >> c2;
25     cout << "c1 = " << c1 << endl;
26     cout << "c2 = " << c2 << endl;
27 
28     cout << "c1.real = " << c1.get_real() << endl;
29     cout << "c1.imag = " << c1.get_imag() << endl;
30 }
31 
32 int main() {
33     cout << "自定义类模板Complex测试1: " << endl;
34     test1();
35 
36     cout << endl;
37 
38     cout << "自定义类模板Complex测试2: " << endl;
39     test2();
40 }
View Code

 

运行截图:

 

任务六:

 1 #pragma once
 2 
 3 class Date {
 4     private:
 5         int year;
 6         int month;
 7         int day;
 8         int totalDays;
 9     public:
10         Date(int year, int month, int day);
11         int getYear() const {
12             return year;
13         }
14         int getMonth() const {
15             return month;
16         }
17         int getDay() const {
18             return day;
19         }
20         int getMaxDay() const;
21         bool isLeapYear() const {
22             return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
23         }
24         void show() const;
25         int operator-(const Date& date) const {
26             return totalDays - date.totalDays;
27         }
28 };
View Code

data.cpp:

 1 #include "date.h"
 2 #include <iostream>
 3 #include <cstdlib>
 4 
 5 using namespace std;
 6 
 7 namespace {
 8     const int DAYS_BEFIRE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
 9 }
10 
11 Date::Date(int year, int month, int day) : year(year), month(month), day(day) {
12     if (day <= 0 || day > getMaxDay()) {
13         cout << "Invalid date: ";
14         show();
15         cout << endl;
16         exit(1);
17     }
18     int years = year - 1;
19     totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFIRE_MONTH[month - 1] + day;
20     if (isLeapYear() && month > 2) totalDays++;
21 }
22 
23 int Date::getMaxDay() const {
24     if (isLeapYear() && month == 2)
25         return 29;
26     else
27         return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1];
28 }
29 
30 void Date::show() const {
31     cout << getYear() << "-" << getMonth() << "-" << getDay();
32 }
View Code

accumulater.h:

 1 #pragma once
 2 
 3 #include "date.h"
 4 
 5 class Accumulator {
 6 private:
 7     Date lastDate;
 8     double value;
 9     double sum;
10 
11 public:
12     Accumulator(const Date& date, double value) : lastDate(date), value(value), sum(0) {}
13 
14     double getSum(const Date& date) const {
15         return sum + value * (date - lastDate).getTotalDays(); // 假设Date类有getTotalDays()方法来计算日期差
16     }
17 
18     void change(const Date& date, double value) {
19         sum = getSum(date);
20         lastDate = date;
21         this->value = value;
22     }
23 
24     void reset(const Date& date, double value) {
25         lastDate = date;
26         this->value = value; // 修复了原始代码中的错误,这里应该赋值
27         sum = 0;
28     }
29 };
View Code

account.h:

 1 #pragma once
 2 
 3 #include "date.h"
 4 #include "accumulator.h"
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 class Account {
10 private:
11     string id;
12     double balance;
13     static double total;
14 protected:
15     Account(const Date& date, const string &id);
16     void record(const Date& date, double amount, const string& desc);
17     void error(const string& msg) const;
18 public:
19     const string& getId() {
20         return id;
21     }
22     double getBalance() const {
23         return balance;
24     }
25     static double getTotal() {
26         return total;
27     }
28     virtual void deposit(const Date& date, double amount, const string& desc) = 0;
29     virtual void withdraw(const Date& date, double amount, const string& desc) = 0;
30     virtual void settle(const Date& date) = 0;
31     virtual void show() const;
32 };
33 
34 class SavingsAccount : public Account {
35 private:
36     Accumulator acc;
37     double rate;
38 public:
39     SavingsAccount(const Date& date, const string& id, double rate);
40     double getRate() const {
41         return rate;
42     }
43     void deposit(const Date& date, double amount, const string& desc);
44     void withdraw(const Date& date, double amount, const string& desc);
45     void settle(const Date& date);
46 };
47 
48 class CreditAccount : public Account {
49 private:
50     Accumulator acc;
51     double credit;
52     double rate;
53     double fee;
54     double getDebt() const {
55         double balance = getBalance();
56         return (balance < 0 ? balance : 0);
57     }
58 public:
59     CreditAccount(const Date& date, const string& id, double credit, double rate, double fee);
60     double getCredit() const {
61         return credit;
62     }
63     double getRate() const {
64         return rate;
65     }
66     double getFee() const {
67         return fee;
68     }
69     double getAvailableCredit() const {
70         if (getBalance() < 0) return credit + getBalance();
71         else return credit;
72     }
73     void deposit(const Date& date, double amount, const string& desc);
74     void withdraw(const Date& date, double amount, const string& desc);
75     void settle(const Date& date);
76     void show() const;
77 };
View Code

account.cpp:

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

task6.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     Account* accounts[] = { &sa1,&sa2,&ca };
12     const int n = sizeof(accounts) / sizeof(Account*);
13     cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl;
14     char cmd;
15     do {
16         date.show();
17         cout << "\tTotal:" << Account::getTotal() << "\tcommand>";
18         int index, day;
19         double amount;
20         string desc;
21         cin >> cmd;
22         switch (cmd) {
23             case 'd':
24                 cin >> index >> amount;
25                 getline(cin, desc);
26                 accounts[index]->deposit(date, amount, desc);
27                 break;
28 
29             case 'w':
30                 cin >> index >> amount;
31                 getline(cin, desc);
32                 accounts[index]->withdraw(date, amount, desc);
33                 break;
34             case 's':
35                 for (int i = 0; i < n; i++) {
36                     cout << "[" << i << "]";
37                     accounts[i]->show();
38                     cout << endl;
39                 }
40                 break;
41 
42             case 'c':
43                 cin >> day;
44                 if (day < date.getDay()) {
45                     cout << "You cannot specify a previous day";
46                 } else if (day > date.getMaxDay())
47                     cout << "Invalid day";
48                 else date = Date(date.getYear(), date.getMonth(), day);
49                 break;
50             case 'n':
51                 if (date.getMonth() == 12)
52                     date = Date(date.getYear() + 1, 1, 1);
53                 else date = Date(date.getYear(), date.getMonth() + 1, 1);
54                 for (int i = 0; i < n; i++) {
55                     accounts[i]->settle(date);
56                 }
57                 break;
58         }
59     } while (cmd != 'e');
60     return 0;
61 }
View Code

 

运行截图:

posted @ 2024-12-08 23:44  白鸽nm海鸥  阅读(1)  评论(0编辑  收藏  举报