实验五
Task3:
1 #ifndef PETS_HPP 2 #define PETS_HPP 3 4 #include <string> 5 6 class MachinePets { 7 private: 8 std::string nickname; 9 10 public: 11 MachinePets(const std::string s); 12 virtual std::string talk() = 0; 13 std::string get_nickname() const { return nickname; } 14 }; 15 16 class PetCats : public MachinePets { 17 public: 18 PetCats(const std::string &s); 19 std::string talk() override; 20 }; 21 22 class PetDogs : public MachinePets { 23 public: 24 PetDogs(const std::string &s); 25 std::string talk() override; 26 }; 27 28 #endif
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 }
Task4:
1 #ifndef FILM_HPP 2 #define FILM_HPP 3 4 #include <iostream> 5 #include <string> 6 class Film { 7 private: 8 std::string title; // 片名 9 std::string director; // 导演 10 std::string country; // 制片国家/地区 11 int year; // 上映年份 12 public: 13 // 构造函数 14 Film() = default; 15 16 // 重载输入运算符>> 17 friend std::istream& operator>>(std::istream& is, Film& f); 18 19 // 重载输出运算符<< 20 friend std::ostream& operator<<(std::ostream& os, const Film& f); 21 22 // 获取上映年份,用于排序 23 int getYear() const { return year; } 24 }; 25 std::istream& operator>>(std::istream& is, Film& f) { 26 std::cout << "输入片名: "; 27 is >> f.title; 28 std::cout << "输入导演: "; 29 is >> f.director; 30 std::cout << "输入制片国家/地区: "; 31 is >> f.country; 32 std::cout << "输入上映年份: "; 33 is >> f.year; 34 return is; 35 } 36 std::ostream& operator<<(std::ostream& os, const Film& f) { 37 os << "片名: " << f.title << std::endl; 38 os << "导演: " << f.director << std::endl; 39 os << "制片国家/地区: " << f.country << std::endl; 40 os << "上映年份: " << f.year << std::endl; 41 return os; 42 } 43 bool compare_by_year(const Film& a, const Film& b) { 44 return a.getYear() < b.getYear(); 45 } 46 #endif
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 // 按发行年份升序排序 24 sort(film_lst.begin(), film_lst.end(), compare_by_year); 25 26 cout << string(20, '=') + "电影信息(按发行年份)" + string(20, '=')<< endl; 27 for(auto &f: film_lst) 28 cout << f << endl; 29 } 30 31 int main() { 32 test(); 33 }
Task5:
1 #ifndef COMPLEX_HPP 2 #define COMPLEX_HPP 3 4 #include <iostream> 5 #include <sstream> 6 #include <string> 7 template <typename T> 8 class Complex { 9 private: 10 T real; // 实部 11 T imag; // 虚部 12 public: 13 // 构造函数 14 Complex(T r = 0, T i = 0) : real(r), imag(i) {} 15 16 // 获取实部 17 T get_real() const { return real; } 18 19 // 获取虚部 20 T get_imag() const { return imag; } 21 22 // 重载 += 运算符 23 Complex<T>& operator+=(const Complex<T>& other) { 24 real += other.real; 25 imag += other.imag; 26 return *this; 27 } 28 29 // 重载 + 运算符 30 Complex<T> operator+(const Complex<T>& other) const { 31 return Complex<T>(real + other.real, imag + other.imag); 32 } 33 34 // 重载 == 运算符 35 bool operator==(const Complex<T>& other) const { 36 return (real == other.real) && (imag == other.imag); 37 } 38 }; 39 template <typename T> 40 std::istream& operator>>(std::istream& is, Complex<T>& c) { 41 std::string line; 42 std::getline(is, line); 43 std::stringstream ss(line); 44 char plus_or_minus; 45 T real, imag; 46 ss >> real; 47 if (ss.peek() == '+' || ss.peek() == '-') { 48 ss >> plus_or_minus; 49 ss >> imag; 50 if (plus_or_minus == '-') { 51 imag = -imag; 52 } 53 } else { 54 imag = 0; 55 } 56 c = Complex<T>(real, imag); 57 return is; 58 } 59 template <typename T> 60 std::ostream& operator<<(std::ostream& os, const Complex<T>& c) { 61 if (c.get_imag() >= 0) { 62 os << c.get_real() << "+" << c.get_imag() << "i"; 63 } else { 64 os << c.get_real() << c.get_imag() << "i"; 65 } 66 return os; 67 } 68 #endif
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 }
Task6:
1 #ifndef __DATE_H__ 2 #define __DATE_H__ 3 class Date { 4 private: 5 int year; 6 int month; 7 int day; 8 int totalDays; 9 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 operator-(const Date& date) const { 21 return totalDays - date.totalDays; 22 } 23 }; 24 #endif
1 #include "date.h" 2 #include <iostream> 3 #include <cstdlib> 4 using namespace std; 5 6 //命名空间使下面的定义只在当前文件中有效 7 namespace { 8 //存储平年中的某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项 9 const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; 10 } 11 12 Date::Date(int year, int month, int day) : year(year), month(month), day(day) { 13 if (day <= 0 || day > getMaxDay()) { 14 cout << "Invalid date."; 15 show(); 16 cout << endl; 17 exit(1); 18 } 19 int years = year - 1; 20 totalDays = years * 365 + years / 4 - years / 100 + years / 400 21 + DAYS_BEFORE_MONTH[month - 1] + day; 22 if (isLeapYear() && month > 2) totalDays++; 23 } 24 25 26 int Date::getMaxDay()const { 27 if (isLeapYear() && month == 2) { 28 return 29; 29 } 30 else { 31 return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; 32 } 33 } 34 35 void Date::show() const { 36 cout << getYear() << '-' << getMonth() << '-' << getDay(); 37 }
1 #ifndef __ACCUMULATOR_H__ 2 #define __ACCUMULATOR_H__ 3 #include "date.h" 4 class Accumulator { 5 private: 6 Date lastDate; 7 double value; 8 double sum; 9 public: 10 Accumulator(const Date& date, double value) : lastDate(date), value(value), sum{ 0 } { } 11 double getSum(const Date& date) const { 12 return sum + value * (date - lastDate); 13 } 14 void change(const Date& date, double value) { 15 sum = getSum(date); 16 lastDate = date; 17 this->value = value; 18 } 19 void reset(const Date& date, double value) { 20 lastDate = date; 21 this->value = value; 22 sum = 0; 23 } 24 }; 25 #endif
1 #ifndef __ACCOUNT_H__ 2 #define __ACCOUNT_H__ 3 #include "date.h" 4 #include "accumulator.h" 5 #include <string> 6 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() { return id; } 18 double getBalance() const { return balance; } 19 static double getTotal() { return total; } 20 virtual void deposit(const Date& date, double amount, const std::string& desc) = 0; 21 virtual void withdraw(const Date& date, double amount, const std::string& desc) = 0; 22 virtual void settle(const Date& date) = 0; 23 virtual void show() const; 24 }; 25 using namespace std; 26 class SavingsAccount : public Account { 27 private: 28 Accumulator acc; 29 double rate; 30 public: 31 SavingsAccount(const Date& date, const string& id, double rate); 32 double getRate() const { return rate; } 33 void deposit(const Date& date, double amount, const string& desc); 34 void withdraw(const Date& date, double amount, const string& desc); 35 void settle(const Date& date); 36 }; 37 38 class CreditAccount : public Account { 39 private: 40 Accumulator acc; 41 double credit; 42 double rate; 43 double fee; 44 double getDebt() const { 45 double balance = getBalance(); 46 return (balance < 0 ? balance : 0); 47 } 48 public: 49 CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); 50 double getCredit() const { return credit; } 51 double getRate() const { return rate; } 52 double getFee() const { return fee; } 53 double getAvailableCredit() const { 54 if (getBalance() < 0) return credit + getBalance(); 55 else return credit; 56 } 57 void deposit(const Date& date, double amount, const string& desc); 58 void withdraw(const Date& date, double amount, const string& desc); 59 void settle(const Date& date); 60 void show() const; 61 }; 62 #endif
1 #include "account.h" 2 #include <cmath> 3 #include<iostream> 4 using namespace std; 5 6 double Account::total = 0; 7 8 Account::Account(const Date& date, const string& id) : id(id), balance(0) { 9 date.show(); 10 cout << "\t#" << id << " created" << endl; 11 } 12 13 void Account::record(const Date& date, double amount, const string& desc) { 14 amount = floor(amount * 100 + 0.5) / 100; 15 balance += amount; 16 total += amount; 17 date.show(); 18 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 19 } 20 21 void Account::show() const { 22 cout << id << "\tBalance: " << balance; 23 } 24 25 void Account::error(const string& msg) const { 26 cout << "Error (#" << id << "): " << msg << endl; 27 } 28 29 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) 30 : Account(date, id), rate(rate), acc(date, 0) {} 31 32 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 33 record(date, amount, desc); 34 acc.change(date, getBalance()); 35 } 36 37 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 38 if (amount > getBalance()) { 39 error("not enough money"); 40 } 41 else { 42 record(date, -amount, desc); 43 acc.change(date, getBalance()); 44 } 45 } 46 47 void SavingsAccount::settle(const Date& date) { 48 if (date.getMonth() == 1) { 49 double interest = acc.getSum(date) * rate / (date - Date(date.getYear() - 1, 1, 1)); 50 if (interest != 0) record(date, interest, "interest"); 51 acc.reset(date, getBalance()); 52 } 53 } 54 55 CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee) 56 : Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) {} 57 58 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 59 record(date, amount, desc); 60 acc.change(date, getDebt()); 61 } 62 63 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 64 if (amount - getBalance() > credit) { 65 error("not enough credit"); 66 } 67 else { 68 record(date, -amount, desc); 69 acc.change(date, getDebt()); 70 } 71 } 72 73 void CreditAccount::settle(const Date& date) { 74 double interest = acc.getSum(date) * rate; 75 if (interest != 0) record(date, interest, "interest"); 76 if (date.getMonth() == 1) record(date, -fee, "annual fee"); 77 acc.reset(date, getDebt()); 78 } 79 80 void CreditAccount::show() const { 81 Account::show(); 82 cout << "\tAvailable credit: " << getAvailableCredit(); 83 }
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 }