实验任务3
代码:
pets.hpp
1 #pragma once 2 #include <iostream> 3 4 using namespace std; 5 6 class MachinePets { 7 public: 8 MachinePets(const string& s); 9 string get_nickname() const; 10 virtual string talk() = 0; 11 12 private: 13 string nickname; 14 }; 15 16 class PetCats : public MachinePets { 17 public: 18 PetCats(const string& s); 19 string talk(); 20 }; 21 22 class PetDogs : public MachinePets { 23 public: 24 PetDogs(const string& s); 25 string talk(); 26 }; 27 28 MachinePets::MachinePets(const string& s) : nickname{ s } {} 29 30 string MachinePets::get_nickname() const { 31 return nickname; 32 } 33 34 PetCats::PetCats(const string& s) : MachinePets{ s } {} 35 36 PetDogs::PetDogs(const string& s) : MachinePets{ s } {} 37 38 string PetCats::talk() { 39 return "miao wu~"; 40 } 41 42 string PetDogs::talk() { 43 return "wang wang~"; 44 }
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 }
运行测试截图:
实验任务4
代码:
film.hpp
1 #pragma once 2 #include <iostream> 3 #include <iomanip> 4 5 using namespace std; 6 7 class Film { 8 public: 9 Film(); 10 11 int getyear() const; 12 13 friend istream& operator>>(istream& in, Film& f); 14 friend ostream& operator<<(ostream& out, const Film& f); 15 16 private: 17 string name; 18 string director; 19 string nation; 20 int year; 21 }; 22 23 Film::Film() {} 24 25 int Film::getyear() const { 26 return year; 27 } 28 29 istream& operator>>(istream& in, Film& f) { 30 cout << "录入片名:"; in >> f.name; 31 cout << "录入导演:"; in >> f.director; 32 cout << "录入制片国家/地区:"; in >> f.nation; 33 cout << "录入上映年份:"; in >> f.year; 34 35 return in; 36 } 37 38 ostream& operator<<(ostream& out, const Film& f) { 39 out << left; 40 out << setw(15) << f.name 41 << setw(20) << f.director 42 << setw(15) << f.nation 43 << setw(15) << f.year; 44 45 return out; 46 } 47 48 49 bool compare_by_year(const Film& f1, const Film& f2) { 50 return f1.getyear() > f2.getyear(); 51 }
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 // 按发行年份升序排序 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 }
运行测试截图:
实验任务5
代码:
Complex.hpp
1 #pragma once 2 #include <iostream> 3 4 using namespace std; 5 6 template <typename T> 7 class Complex { 8 public: 9 Complex(T r = 0, T i = 0); 10 Complex(const Complex& c); 11 T get_real() const; 12 T get_imag() const; 13 Complex operator+=(const Complex& c); 14 bool operator==(const Complex& c); 15 16 template <typename T> friend Complex<T> operator+(const Complex<T>& c1, const Complex<T>& c2); 17 template <typename T> friend istream& operator>>(istream& in, Complex<T>& c); 18 template <typename T> friend ostream& operator<<(ostream& out, const Complex<T>& c); 19 20 private: 21 T real, imag; 22 }; 23 24 template <typename T> 25 Complex<T>::Complex(T r, T i) : real{ r }, imag{ i } {} 26 27 template <typename T> 28 Complex<T>::Complex(const Complex& c) : real{ c.real }, imag{c.imag} {} 29 30 template <typename T> 31 T Complex<T>::get_real() const { return real; } 32 33 template <typename T> 34 T Complex<T>::get_imag() const { return imag; } 35 36 template <typename T> 37 Complex<T> Complex<T>::operator+=(const Complex& c) { 38 real += c.real; 39 imag += c.imag; 40 41 return *this; 42 } 43 44 template <typename T> 45 bool Complex<T>::operator==(const Complex& c) { 46 if (real == c.real && imag == c.imag) 47 return true; 48 else return false; 49 } 50 51 template <typename T> 52 Complex<T> operator+(const Complex<T>& c1, const Complex<T>& c2) { 53 return Complex<T>(c1.real + c2.real, c1.imag + c2.imag); 54 } 55 56 57 template <typename T> 58 istream& operator>>(istream& in, Complex<T>& c) { 59 in >> c.real >> c.imag; 60 61 return in; 62 } 63 64 template <typename T> 65 ostream& operator<<(ostream& out, const Complex<T>& c) { 66 if (c.imag >= 0) 67 out << c.real << " + " << c.imag << "i"; 68 else 69 out << c.real << " - " << -c.imag << "i"; 70 71 return out; 72 }
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 }
运行测试截图()
实验任务6
代码:
date.h
1 #pragma once 2 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 };
date.cpp
1 #include "date.h" 2 #include <iostream> 3 #include <cstdlib> 4 5 using namespace std; 6 7 namespace { 8 const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; 9 } 10 11 int Date::getMaxDay() const { 12 if (isLeapYear() && month == 2) 13 return 29; 14 else 15 return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; 16 } 17 18 void Date::show() const { 19 cout << getYear() << "-" << getMonth() << "-" << getDay(); 20 } 21 22 Date::Date(int year, int month, int day) : year{ year }, month{ month }, day{ day } { 23 if (day <= 0 || day > getMaxDay()) { 24 cout << "Invalid date: "; 25 show(); 26 cout << endl; 27 exit(1); 28 } 29 int years = year - 1; 30 totalDays = years * 365 + years / 4 - years / 100 + years / 400 31 + DAYS_BEFORE_MONTH[month - 1] + day; 32 if (isLeapYear() && month > 2) totalDays++; 33 }
accumulator.h
1 #pragma once 2 #include "date.h" 3 4 class Accumulator { 5 private: 6 Date lastDate; //上次变更数值的日期 7 double value; 8 double sum; 9 10 public: 11 Accumulator(const Date& date, double value) 12 :lastDate{date}, value{value}, sum{0}{} 13 14 double getSum(const Date& date) const { 15 return sum + value * (date - lastDate); 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 30 };
account.h
1 #pragma once 2 #include "date.h" 3 #include "Accumulator.h" 4 #include <string> 5 6 class Account { //账户类 7 private: 8 std::string id; //账号 9 double balance; //余额 10 static double total; //所有账户的总金额 11 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 17 public: 18 const std::string& getId() const { return id; } 19 double getBalance() const { return balance; } 20 static double getTotal() { return total; } 21 22 virtual void deposit(const Date& date, double amount, const std::string& desc) = 0; //存入现金 23 virtual void withdraw(const Date& date, double amount, const std::string& desc) = 0; //取出现金 24 virtual void settle(const Date& date) = 0; //结算(利息、年费) 25 virtual void show() const; //显示账户信息 26 }; 27 28 class SavingsAccount : public Account { //储蓄类账户 29 private: 30 Accumulator acc; //辅助计算利息的累加器 31 double rate; //存款的年利率 32 33 public: 34 SavingsAccount(const Date& date, const std::string& id, double rate); //构造函数 35 double getRate() const { return rate; } 36 void deposit(const Date& date, double amount, const std::string& desc); //存入现金 37 void withdraw(const Date& date, double amount, const std::string& desc); //取出现金 38 void settle(const Date& date); //结算利息 39 }; 40 41 class CreditAccount : public Account { //信用账户类 42 private: 43 Accumulator acc; //辅助计算利息的累加器 44 double credit; //信用额度 45 double rate; //欠款的日利率 46 double fee; //信用卡年费 47 double getDebt() const { //获得欠款额 48 double balance = getBalance(); 49 return (balance < 0 ? balance : 0); 50 } 51 public: 52 CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee); 53 double getCredit() const { return credit; } 54 double getRate() const { return rate; } 55 double getFee() const { return fee; } 56 double getAvailableCredit() const { //获得可用信用 57 if (getBalance() < 0) 58 return credit + getBalance(); 59 else 60 return credit; 61 } 62 void deposit(const Date& date, double amount, const std::string& desc); //存入现金 63 void withdraw(const Date& date, double amount, const std::string& desc); //取出现金 64 void settle(const Date& date); //结算利息 65 void show() const; 66 };
account.cpp
1 #include "account.h" 2 #include <cmath> 3 #include <iostream> 4 5 using namespace std; 6 7 //Account类的实现 8 double Account::total = 0; 9 10 Account::Account(const Date& date, const std::string& id) 11 : id{ id }, balance{ 0 } { 12 date.show(); 13 cout << "\t#" << id << "created" << endl; 14 } 15 16 void Account::record(const Date& date, double amount, const string& desc) { 17 amount = floor(amount * 100 + 0.5) / 100; 18 balance += amount; 19 total += amount; 20 date.show(); 21 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 22 } 23 24 void Account::show() const { 25 cout << id << "\tBalance:" << balance; 26 } 27 28 void Account::error(const string& msg) const { 29 cout << "Error(#" << id << "):" << msg << endl; 30 } 31 32 33 //SavingsAccount类的实现 34 SavingsAccount::SavingsAccount(const Date& date, const std::string& id, double rate) 35 : Account{ date, id }, rate{ rate }, acc{date, 0} {} 36 37 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 38 record(date, amount, desc); 39 acc.change(date, getBalance()); 40 } 41 42 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 43 if (amount > getBalance()) 44 error("not enough money"); 45 else { 46 record(date, -amount, desc); 47 acc.change(date, getBalance()); 48 } 49 } 50 51 void SavingsAccount::settle(const Date& date) { 52 if (date.getMonth() == 1) { 53 double interest = acc.getSum(date) * rate //计算年息 54 / (date - Date(date.getYear() - 1, 1, 1)); 55 if (interest != 0) 56 record(date, interest, "interest"); 57 acc.reset(date, getBalance()); 58 } 59 } 60 61 //CreditAccount类的实现 62 CreditAccount::CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee) 63 : Account{ date, id }, credit{ credit }, rate{ rate }, fee{fee}, acc{ date, 0 } {} 64 65 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 66 record(date, amount, desc); 67 acc.change(date, getDebt()); 68 } 69 70 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 71 if (amount - getBalance() > credit) 72 error("not enough credit"); 73 else { 74 record(date, -amount, desc); 75 acc.change(date, getDebt()); 76 } 77 } 78 79 void CreditAccount::settle(const Date& date) { 80 double interest = acc.getSum(date) * rate; 81 if (interest != 0) 82 record(date, interest, "interest"); 83 if (date.getMonth() == 1) 84 record(date, -fee, "annual fee"); 85 acc.reset(date, getDebt()); 86 } 87 88 void CreditAccount::show() const{ 89 Account::show(); 90 cout << "\tAvailable credit:" << getAvailableCredit(); 91 }
8_8.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 12 Account* accounts[] = { &sa1, &sa2, &ca }; 13 const int n = sizeof(accounts) / sizeof(Account*); 14 15 cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl; 16 char cmd; 17 do { 18 date.show(); 19 cout << "\tTotal:" << Account::getTotal() << "\tcommand>"; 20 21 int index, day; 22 double amount; 23 string desc; 24 cin >> cmd; 25 26 switch (cmd) { 27 case'd': //存入现金 28 cin >> index >> amount; 29 getline(cin, desc); 30 accounts[index]->deposit(date, amount, desc); 31 break; 32 33 case'w': //取出现金 34 cin >> index >> amount; 35 getline(cin, desc); 36 accounts[index]->withdraw(date, amount, desc); 37 break; 38 39 case's': //查询各账户信息 40 for (int i = 0; i < n; i++) { 41 cout << "[" << i << "]"; 42 accounts[i]->show(); 43 cout << endl; 44 } 45 break; 46 47 case'c': //改变日期 48 cin >> day; 49 if (day < date.getDay()) 50 cout << "You cannot specify a previous day"; 51 else if (day > date.getMaxDay()) 52 cout << "Invalid day"; 53 else 54 date = Date(date.getYear(), date.getMonth(), day); 55 break; 56 57 case'n': //进入下个月 58 if (date.getMonth() == 12) 59 date = Date(date.getYear() + 1, 1, 1); 60 else 61 date = Date(date.getYear(), date.getMonth() + 1, 1); 62 63 for (int i = 0; i < n; i++) 64 accounts[i]->settle(date); 65 66 break; 67 } 68 69 } while (cmd != 'e'); 70 }
运行测试截图:
总结:
1.改进:使用虚函数和抽象类,使项目实现了运行时多态,可以把各个账户放入数组中,简化了操作;将distance的求值,用“-”运算符重载,代码更易读,操作也更简单
2.问题:现实中利息不会一直固定不变
实验总结
1.在进行实验5的撰写时,代码报出了LN2019的错误,被足足硬控了好几个小时。
还好最后找到了官方的错误原因解释,以及修改方式:
看来自己在使用模板类时犯了常见且经典的错误,这一次的debug真是让人印象深刻。