实验五
task3源代码:

1 #pragma once 2 3 #include<iostream> 4 #include<string> 5 6 using namespace std; 7 8 class MachinePets { 9 public: 10 MachinePets(const string s); 11 string get_nickname() const; 12 virtual string talk() = 0; 13 14 private: 15 string nickname; 16 }; 17 18 class PetsCats : public MachinePets{ 19 public: 20 PetsCats(const string s); 21 string talk() override; 22 }; 23 24 class PetsDogs : public MachinePets { 25 public: 26 PetsDogs(const string s); 27 string talk() override; 28 }; 29 30 MachinePets::MachinePets(const string s): nickname{s} {} 31 32 string MachinePets::get_nickname() const { 33 return nickname; 34 } 35 36 PetsCats::PetsCats(const string s): MachinePets{s} {} 37 38 string PetsCats::talk() { 39 return "Nya~"; 40 } 41 42 PetsDogs::PetsDogs(const string s): MachinePets{s} {} 43 44 string PetsDogs::talk() { 45 return "wang!"; 46 }

1 #include<iostream> 2 #include"pets.hpp" 3 4 void play(MachinePets &obj) { 5 std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl; 6 } 7 8 void test() { 9 PetsCats cat("miku"); 10 PetsDogs dog("da huang"); 11 12 play( cat ); 13 play( dog ); 14 } 15 16 int main() { 17 test(); 18 }
task3运行结果:
task4源代码:

1 #pragma once 2 3 #include<iostream> 4 #include<string> 5 6 using namespace std; 7 8 class Person { 9 public: 10 Person() = default; 11 Person(const string name, const string telephone, const string email = ""); 12 Person(const Person &p); 13 ~Person() = default; 14 15 void update_telephone() { 16 cout << "输入电话号码:"; 17 cout << telephone; 18 cout << endl << "电话号码已更新。。" << endl; 19 } 20 21 void update_email() { 22 cout << "输入emai:"; 23 cout << email; 24 cout << endl << "email已更新。。" << endl; 25 } 26 27 friend std::ostream& operator<<(std::ostream& out, const Person& t) 28 { 29 out << "name:" << t.name << endl; 30 out << "telephone:" << t.telephone << endl; 31 out << "email:" << t.email << endl; 32 return out; 33 } 34 35 friend std::istream& operator>>(std::istream& in, Person& t) 36 { 37 in >> t.name >> t.telephone >> t.email; 38 cout << endl; 39 return in; 40 } 41 42 friend bool operator==(const Person& s1, const Person& s2) 43 { 44 if (s1.name == s2.name && s1.telephone == s2.telephone && s1.email == s2.email) 45 return true; 46 else 47 return false; 48 } 49 50 private: 51 string name; 52 string telephone; 53 string email; 54 }; 55 56 Person::Person(const string name, const string telephone, const string email): name{name}, telephone{telephone}, email{email} {} 57 58 Person::Person(const Person &p): name{p.name}, telephone{p.telephone}, email{p.email} {}

1 #include<iostream> 2 #include<vector> 3 #include"Person.hpp" 4 5 void test() { 6 using namespace std; 7 vector<Person> phone_book; 8 Person p; 9 10 cout << "输入一组联系人的联系方式,E直至按下Crtl+Z终止\n"; 11 while(cin >> p) { 12 phone_book.push_back(p); 13 } 14 15 cout << "\n更新phone_book中索引为0的联系人的手机号、邮箱:\n"; 16 phone_book.at(0).update_telephone(); 17 phone_book.at(0).update_email(); 18 19 cout << "\n测试两个联系人是否是同一个:\n"; 20 cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl; 21 } 22 23 int main() { 24 test(); 25 }
task4运行结果:
task5源代码:

1 #include"date.h" 2 #include"accumulator.h" 3 #include<string> 4 5 class Account { 6 private: 7 std::string id; 8 double balance; 9 static double total; 10 11 protected: 12 Account(const Date& date, const std::string& id); 13 void record(const Date& date, double amount, const std::string& desc); 14 void error(const std::string& msg) const; 15 16 public: 17 const std::string& getId() const { 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); 23 virtual void show() const; 24 }; 25 26 class SavingsAccount : public Account { 27 private: 28 Accumulator acc; 29 double rate; 30 31 public: 32 SavingsAccount(const Date& date, const std::string& id, double rate); 33 double getRate() const { return rate; } 34 void deposit(const Date& date, double amount, const std::string& desc); 35 void withdraw(const Date& date, double amount, const std::string& desc); 36 void settle(const Date& date); 37 }; 38 39 class CreditAccount :public Account { 40 private: 41 Accumulator acc; 42 double credit; 43 double rate; 44 double fee; 45 double getDebt() const 46 { 47 double balance = getBalance(); 48 return(balance < 0 ? balance : 0); 49 } 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 57 double getAvailableCredit() const { 58 if (getBalance() < 0) 59 return credit + getBalance(); 60 else 61 return credit; 62 } 63 64 void deposit(const Date& date, double amount, const std::string& desc); 65 void withdraw(const Date& date, double amount, const std::string& desc); 66 void settle(const Date& date); 67 void show() const; 68 };

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

1 #include"date.h" 2 #include<iostream> 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):lastDate{date},value{value},sum{0}{} 12 13 double getSum(const Date& date)const { 14 return sum + value * (date - lastDate); 15 } 16 17 void change(const Date& date, double value) { 18 sum = getSum(date); 19 lastDate = date; this->value = value; 20 } 21 22 void reset(const Date& date, double value) { 23 lastDate = date; this->value = value; sum = 0; 24 } 25 };

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

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 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_BEFORE_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_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; 28 } 29 30 void Date::show()const { 31 cout << getYear() << "-" << getMonth() << "-" << getDay(); 32 }

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