实验5 C++

任务三:

pets.hpp

 1 #pragma once
 2 #include <iostream>
 3 #include <string>
 4 
 5 using namespace std;
 6 
 7 class MachinePets{
 8 private:
 9     string nickname;
10 public:
11     MachinePets(const string s): nickname{s} {}
12     string get_nickname() const;
13     virtual string talk() = 0;                                                                                                                                                                                                                               
14 };
15 string MachinePets::get_nickname() const {
16     return nickname;
17 }
18 
19 class PetCats: public MachinePets {
20 public:
21     PetCats(const string s) : MachinePets{s} {}
22     string talk();
23 };
24 string PetCats::talk() {
25     string sound{"meow~"};
26     return sound;
27 }
28 
29 class PetDogs: public MachinePets {
30 public:
31     PetDogs(const string s) : MachinePets{s} {}
32     string talk();
33 };
34 string PetDogs::talk() {
35     string sound{"woof!"};
36     return sound;
37 }

task3.cpp

 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 }

运行结果:

 

任务四:

film.hpp

 1 #pragma once
 2 #include <iostream>
 3 #include <string>
 4 #include <iomanip>
 5 
 6 using namespace std;
 7 
 8 class Film{
 9 private:
10     string name, derector, region ,year;
11 public:
12     string get_year() const;
13     friend istream& operator>>(istream &in, Film &f);
14     friend ostream& operator<<(ostream &out, const Film &f);
15 };
16 
17 
18 string Film::get_year() const {
19     return year;
20 }
21 istream& operator>>(istream &in, Film &f) {
22     cout << "录入片名:";
23     in >> f.name;
24     cout << "录入导演:";
25     in >> f.derector;
26     cout << "录入制片国家/地区:";
27     in >> f.region;
28     cout << "录入上映年份:";
29     in >> f.year;
30     return in;
31 }
32 ostream& operator<<(ostream &out, const Film &f) {
33     out << left;
34     out << setw(16) << f.name 
35         << setw(16) << f.derector 
36         << setw(16) << f.region 
37         <<setw(16) << f.year ;
38     return out; 
39 }

task4.cpp

 1 #include "film.hpp"
 2 #include <iostream>
 3 #include <string>
 4 #include <vector>
 5 #include <algorithm>
 6 
 7 void test() {
 8     using namespace std;
 9     
10     int n;
11     cout << "输入电影数目: ";
12     cin >> n;
13 
14     cout << "录入" << n << "部影片信息" << endl;
15     vector<Film> film_lst;
16     for(int i = 0; i < n; ++i) {
17         Film f;
18         cout << string(20, '-') << "" << i+1 << "部影片录入" << string(20, '-') << endl;
19         cin >> f;
20         film_lst.push_back(f);
21     }
22 
23     sort(film_lst.begin(), film_lst.end(), compare_by_year);
24 
25     cout << string(20, '=') + "电影信息(按发行年份)" +  string(20, '=')<< endl;
26     for(auto &f: film_lst)
27         cout << f << endl;
28 }
29 
30 int main() {
31     test();
32 }

 

运行结果:

 


任务五:

Complex.hpp

 1 #pragma once
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 template<typename T>
 7 class Complex{
 8 private:
 9     T imag, real;
10 public:
11     Complex() : imag{0}, real{0} {}
12     Complex(T i, T r) : imag{i}, real{r} {}
13     Complex(const Complex &c) {
14         imag = c.imag;
15         real = c.real;
16     }
17     T get_imag() const { return imag; }
18     T get_real() const { return real; }
19 
20     friend istream& operator>>(istream &in, Complex &c) {
21         in >> c.imag >> c.real;
22         return in;
23     }
24     friend ostream& operator<<(ostream &out, const Complex &c) {
25         if(c.real >= 0){
26             out << c.imag <<" + " << c.real << 'i';
27         }
28         else
29             out << c.imag <<" - " << -c.real << 'i';
30         return out;
31     }
32     friend void operator+=(Complex &c1, Complex c2) {
33         c1.imag += c2.imag;
34         c1.real += c2.real;
35     }
36     friend Complex operator+(Complex c1, Complex c2) {
37         Complex c;
38         c.imag = c1.imag + c2.imag;
39         c.real = c1.real + c2.real;
40         return c;
41     }
42     friend bool operator==(Complex c1, Complex c2) { 
43         if(c1.imag == c2.imag && c1.real == c2.real)
44             return true;
45         else
46             return false;
47         
48     }
49 };

task5.cpp

 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 }

运行结果:

 

 

任务六:

date.h

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

accumulator.h

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

account.h

 1 #pragma once
 2 #include"date.h"
 3 #include"accumulator.h"
 4 #include<string>
 5 using namespace std;
 6 class Account {
 7 private:
 8     string id;
 9     double balance;
10     static double total;
11 protected:
12     Account(const Date& date, const string &id);
13     void record(const Date& date, double amoount, const string& desc);
14     void error(const string& msg) const;
15 public:const string& getId() { return id; }
16       double getBalance() const { return balance; }
17       static double getTotal() { return total; }
18       virtual void deposit(const Date& date, double amount, const string& desc) = 0;
19       virtual void withdraw(const Date& date, double amount, const string& desc) = 0;
20       virtual void settle(const Date& date) = 0;
21       virtual 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 string& id, double rate);
29     double getRate() const { return  rate; }
30     void deposit(const Date& date, double amount, const string& desc);
31     void withdraw(const Date& date, double amount, const string& desc);
32     void settle(const Date& date);
33 };
34 class CreditAccount :public Account {
35  private:
36     Accumulator acc;
37     double credit;
38     double rate;
39     double fee;
40     double getDebt()const {
41         double balance = getBalance();
42         return(balance < 0 ? balance : 0);
43     }
44 public:CreditAccount(const Date& date, const string& id, double credit, double rate, double fee);
45       double getCredit()const { return credit; }
46       double getRate() const { return rate; }
47       double getFee() const { return fee; }
48       double getAvailableCredit() const {
49           if (getBalance() < 0)return credit + getBalance();
50           else return credit;
51         }
52       void deposit(const Date& date, double amount, const string& desc);
53       void withdraw(const Date& date, double amount, const string& desc);
54       void settle(const Date& date);
55       void show() const;
56 };

date.cpp

 1 #include"date.h"
 2 #include<iostream>
 3 #include<cstdlib>
 4 using namespace std;
 5 namespace {
 6     const int DAYS_BEFIRE_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_BEFIRE_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_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1];
23 }
24 void Date::show() const {
25     cout << getYear() << "-" << getMonth() << "-" << getDay();
26 }

account.cpp

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

task6.cpp

 1 #include"account.h"
 2 #include<iostream>
 3 using namespace std;
 4 int main() {
 5     Date date(2008, 11, 1);
 6     SavingsAccount sa1(date, "S3755217", 0.015);
 7     SavingsAccount sa2(date, "02342342", 0.015);
 8     CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);
 9     Account* accounts[] = { &sa1,&sa2,&ca };
10     const int n = sizeof(accounts) / sizeof(Account*);
11     cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl;
12     char cmd;
13     do {
14         date.show();
15         cout << "\tTotal:" << Account::getTotal() << "\tcommand>";
16         int index, day;
17         double amount;
18         string desc;
19         cin >> cmd;
20         switch (cmd) {
21         case 'd':
22             cin >> index >> amount;
23             getline(cin, desc);
24             accounts[index]->deposit(date, amount, desc);
25             break; 
26         case 'w':
27             cin >> index >> amount;
28             getline(cin, desc);
29             accounts[index]->withdraw(date, amount, desc);
30             break;
31         case 's':
32             for (int i = 0; i < n; i++) {
33                 cout << "[" << i << "]";
34                 accounts[i]->show();
35                 cout << endl;
36             }
37             break; 
38          case 'c':
39             cin >> day;
40             if (day < date.getDay()) {
41                 cout << "You cannot specify a previous day";
42             }
43             else if (day > date.getMaxDay())
44                 cout << "Invalid day";
45             else date = Date(date.getYear(), date.getMonth(), day);
46             break;
47         case 'n':
48             if (date.getMonth() == 12)
49                 date = Date(date.getYear() + 1, 1, 1);
50             else date = Date(date.getYear(), date.getMonth() + 1, 1);
51             for (int i = 0; i < n; i++) {
52                 accounts[i]->settle(date);
53             }
54             break;
55         }
56     } while (cmd != 'e');
57     return 0;
58 }

运行结果:

 

posted @ 2024-12-02 21:33  Tt3n  阅读(13)  评论(0编辑  收藏  举报