实验5
task3:
1 #pragma 2 3 #include<iostream> 4 #include<string> 5 6 using namespace std; 7 8 class MachinePets{ 9 public: 10 MachinePets(const string &s=""); 11 public: 12 string get_nickname()const; 13 virtual string talk()const=0; 14 protected: 15 string nickname; 16 }; 17 18 MachinePets::MachinePets(const string &s):nickname{s}{ 19 } 20 21 string MachinePets::get_nickname()const{ 22 return nickname; 23 } 24 25 class PetCats:public MachinePets{ 26 public: 27 PetCats(const string &s="",const string &c="miao wu~"); 28 public: 29 string talk()const override; 30 private: 31 string cry; 32 }; 33 34 PetCats::PetCats(const string &s,const string &c):MachinePets{s},cry{c}{ 35 } 36 37 string PetCats::talk()const{ 38 return cry; 39 } 40 41 class PetDogs:public MachinePets{ 42 public: 43 PetDogs(const string &s="",const string &c="wang wang~"); 44 public: 45 string talk()const override; 46 private: 47 string cry; 48 }; 49 50 PetDogs::PetDogs(const string &s,const string &c):MachinePets{s},cry{c}{ 51 } 52 53 string PetDogs::talk()const{ 54 return cry; 55 }
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 #pragma once 2 3 #include<iostream> 4 #include<string> 5 #include<iomanip> 6 7 using namespace std; 8 9 class Film{ 10 public: 11 Film(); 12 Film(const string &name,const string &director,const string &area,const string &year); 13 friend istream& operator>>(istream& in,Film &film); 14 friend ostream& operator<<(ostream& out,const Film &film); 15 friend bool compare_by_year(const Film &f1,const Film &f2); 16 private: 17 string name; 18 string director; 19 string area; 20 string year; 21 }; 22 23 Film::Film(){ 24 } 25 26 Film::Film(const string &name,const string &director,const string &area,const string &year){ 27 this->name=name; 28 this->director=director; 29 this->area=area; 30 this->year=year; 31 } 32 33 istream& operator>>(istream& in,Film &film){ 34 cout<<"录入片名:"; 35 in>>film.name; 36 cout<<"录入导演:"; 37 in>>film.director; 38 cout<<"录入制片国家/地区:"; 39 in>>film.area; 40 cout<<"录入上映年份:"; 41 in>>film.year; 42 return in; 43 } 44 45 ostream& operator<<(ostream& out,const Film &film){ 46 out<<left; 47 out<<setw(15)<<film.name<<setw(15)<<film.director<<setw(15)<<film.area<<setw(15)<<film.year<<endl; 48 return out; 49 } 50 51 bool compare_by_year(const Film& f1,const Film& f2){ 52 return f1.year<f2.year; 53 }
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 #pragma once 2 3 #include<iostream> 4 5 using namespace std; 6 7 template<typename T> 8 class Complex{ 9 public: 10 Complex(T r=0,T i=0); 11 12 T get_real()const; 13 T get_imag()const; 14 15 template<typename U> 16 friend Complex<U>& operator+=(Complex<U> &c1,const Complex<U> &c2); 17 template<typename U> 18 friend Complex<U> operator+(const Complex<U> &c1,const Complex<U> &c2); 19 template<typename U> 20 friend bool operator==(const Complex<U> &c1,const Complex<U> &c2); 21 template<typename U> 22 friend ostream& operator<<(ostream &out,const Complex<U> &c); 23 template<typename U> 24 friend istream& operator>>(istream &in,Complex<U> &c); 25 private: 26 T real,imag; 27 }; 28 29 template<typename T> 30 Complex<T>::Complex(T r,T i):real{r},imag{i}{ 31 } 32 33 template<typename T> 34 T Complex<T>::get_real()const{ 35 return real; 36 } 37 38 template<typename T> 39 T Complex<T>::get_imag()const{ 40 return imag; 41 } 42 43 template<typename T> 44 Complex<T>& operator+=(Complex<T> &c1, const Complex<T> &c2) { 45 c1.real += c2.real; 46 c1.imag += c2.imag; 47 48 return c1; 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 template<typename T> 57 bool operator==(const Complex<T> &c1,const Complex<T> &c2){ 58 if(c1.real==c2.real&&c1.imag==c2.imag) 59 return true; 60 else 61 return false; 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 } 73 74 template<typename T> 75 istream& operator>>(istream &in,Complex<T> &c){ 76 in>>c.real>>c.imag; 77 78 return in; 79 }
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 #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 };
1 #include"date.h" 2 #include<iostream> 3 #include<cstdlib> 4 5 6 using namespace std; 7 8 namespace { 9 const int DAYS_BEFIRE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304 ,334,365 }; 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 int Date::getMaxDay()const { 23 if (isLeapYear() && month == 2) 24 return 29; 25 else return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1]; 26 } 27 void Date::show()const { 28 cout << getYear() << "-" << getMonth() << "-" << getDay(); 29 }
1 #pragma once 2 3 #include"date.h" 4 5 class Accumulator { 6 private: 7 Date lastDate; 8 double value; 9 double sum; 10 public: 11 Accumulator(const Date& date, double value) :lastDate(date), value(value), sum { 0 } {} 12 double getSum(const Date& date) const { 13 return sum + value * (date - lastDate); 14 } 15 void change(const Date& date, double value) { 16 sum = getSum(date); 17 lastDate = date; 18 this->value = value; 19 } 20 void reset(const Date& date, double value) { 21 lastDate = date; 22 this->value; 23 sum = 0; 24 } 25 };
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 amoount, 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 class SavingsAccount :public Account { 34 private: 35 Accumulator acc; 36 double rate; 37 public: 38 SavingsAccount(const Date& date, const string& id, double rate); 39 double getRate() const { 40 return rate; 41 } 42 void deposit(const Date& date, double amount, const string& desc); 43 void withdraw(const Date& date, double amount, const string& desc); 44 void settle(const Date& date); 45 }; 46 class CreditAccount :public Account { 47 private: 48 Accumulator acc; 49 double credit; 50 double rate; 51 double fee; 52 double getDebt()const { 53 double balance = getBalance(); 54 return(balance < 0 ? balance : 0); 55 } 56 public: 57 CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); 58 double getCredit()const { 59 return credit; 60 } 61 double getRate()const { 62 return rate; 63 } 64 double getFee() const { 65 return fee; 66 } 67 double getAvailableCredit()const { 68 if (getBalance() < 0)return credit + getBalance(); 69 else return credit; 70 } 71 void deposit(const Date& date, double amount, const string& desc); 72 void withdraw(const Date& date, double amount, const string& desc); 73 void settle(const Date& date); 74 void show()const; 75 };
1 #include "account.h" 2 #include <cmath> 3 #include<iostream> 4 5 using namespace std; 6 7 double Account::total = 0; 8 Account::Account(const Date& date, const string& id) :id(id), balance(0) { 9 date.show(); 10 cout << "\t#" << id << "created" << endl; 11 } 12 void Account::record(const Date& date, double amount, const string& desc) { 13 amount = floor(amount * 100 + 0.5) / 100; 14 balance += amount; 15 total += amount; 16 date.show(); 17 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 18 } 19 void Account::show()const { 20 cout << id << "\tBalance:" << balance; 21 } 22 void Account::error(const string& msg)const { 23 cout << "Error(#" << id << "):" << msg << endl; 24 } 25 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :Account(date, id), rate(rate), acc(date, 0) {} 26 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 27 record(date, amount, desc); 28 acc.change(date, getBalance()); 29 } 30 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 31 if (amount > getBalance()) { 32 error("not enough money"); 33 } else { 34 record(date, -amount, desc); 35 acc.change(date, getBalance()); 36 } 37 } 38 void SavingsAccount::settle(const Date& date) { 39 double interest = acc.getSum(date) * rate / (date-Date(date.getYear() - 1, 1, 1)); 40 if (interest != 0)record(date, interest, "interest"); 41 acc.reset(date, getBalance()); 42 } 43 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) {} 44 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 45 record(date, amount, desc); 46 acc.change(date, getDebt()); 47 } 48 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 49 if (amount - getBalance() > credit) { 50 error("not enouogh credit"); 51 } else { 52 record(date, -amount, desc); 53 acc.change(date, getDebt()); 54 } 55 } 56 void CreditAccount::settle(const Date& date) { 57 double interest = acc.getSum(date) * rate; 58 if (interest != 0) record(date, interest, "interest"); 59 if (date.getMonth() == 1)record(date, -fee, "annual fee"); 60 acc.reset(date, getDebt()); 61 } 62 void CreditAccount::show()const { 63 Account::show(); 64 cout << "\tAvailable credit:" << getAvailableCredit(); 65 }
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 }
在基类Account中将账户的公共操作皆声明为虚函数,通过基类的指针来执行各种操作,各类型的账户对象都可以通过一个基类指针的数组来访问,提供了极大的便利,使得我们可以通过统一方式来操作各个账户,从cin输入所需执行的操作就变得非常方便。