实验五
实验任务3
pets.hpp
1 #pragma once; 2 3 #include <iostream> 4 #include <string> 5 6 using namespace std; 7 8 class MachinePets{ 9 private: 10 string nickname; 11 public: 12 MachinePets(const string &s); 13 string get_nickname() const; 14 virtual string talk() = 0; 15 }; 16 17 MachinePets::MachinePets(const string &s): nickname(s){} 18 19 string MachinePets::get_nickname()const{ 20 return nickname; 21 } 22 23 class PetCats: public MachinePets{ 24 public: 25 PetCats(const string &s); 26 string talk()override; 27 }; 28 29 PetCats::PetCats(const string &s): MachinePets(s) {} 30 31 string PetCats::talk(){ 32 return "miao wu~"; 33 } 34 35 class PetDogs: public MachinePets{ 36 public: 37 PetDogs(const string &s); 38 string talk() override; 39 }; 40 41 PetDogs::PetDogs(const string &s): MachinePets(s){} 42 43 string PetDogs::talk(){ 44 return "wang wang~"; 45 }
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 3 #include <iostream> 4 #include <string> 5 #include <iomanip> 6 7 using namespace std; 8 9 class Film{ 10 public: 11 Film(): name("didi"), director("fuck"), country("china"), year(1984){} 12 13 Film(const string &name, const string &director, const string &country, const int &year); 14 15 friend istream& operator>>(istream &in, Film &film); 16 17 friend ostream& operator<<(ostream &out, const Film &film); 18 19 friend bool compare_by_year(const Film &f1, const Film &f2); 20 21 private: 22 string name; 23 string director; 24 string country; 25 int year; 26 }; 27 28 Film::Film(const string &name, const string &director, const string &country, const int &year){ 29 this->name = name; 30 this->director = director; 31 this->country = country; 32 this->year = year; 33 } 34 35 istream& operator>>(istream &in, Film &film){ 36 37 cout << "录入片名:"; in >> film.name; 38 cout << "录入导演:"; in >> film.director; 39 cout << "录入制片国家/地区:"; in >> film.country; 40 cout << "录入制片年份"; in >> film.year; 41 42 return in; 43 } 44 45 ostream& operator<<(ostream &out, const Film &film){ 46 out << left; 47 out << setw(15) << film.name 48 << setw(15) << film.director 49 << setw(15) << film.country 50 << setw(15) << film.year; 51 52 return out; 53 } 54 55 bool compare_by_year(const Film &f1, const Film &f2){ 56 return f1.year > f2.year; 57 }
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 3 #include <iostream> 4 #include <string> 5 #include <cmath> 6 7 using namespace std; 8 9 template <typename T> 10 class Complex{ 11 private: 12 T real; 13 T imag; 14 public: 15 Complex<T>(T x = 0, T y = 0): real(x), imag(y) {} 16 Complex<T>(const Complex<T> &other){ 17 real = other.real; 18 imag = other.imag; 19 } 20 T get_real(){ 21 return real; 22 } 23 T get_imag(){ 24 return imag; 25 } 26 27 friend Complex<T> operator+(const Complex<T> &other1, const Complex<T> &other2){ 28 return Complex<T>(other1.real + other2.real, other1.imag + other2.imag); 29 } 30 31 friend istream& operator>>(istream &in, Complex<T> &c){ 32 in >> c.real; 33 in >> c.imag; 34 35 return in; 36 } 37 38 friend ostream& operator<<(ostream &out, const Complex<T> &c){ 39 if (c.imag >= 0) { out << c.real << " + " << abs(c.imag) << "i"; } 40 else { out << c.real << " - " << abs(c.imag) << "i"; } 41 return out; 42 } 43 44 bool operator== (const Complex<T>&c){ 45 return real == c.real && imag == c.imag; 46 } 47 48 Complex<T> operator+=(const Complex<T>& c){ 49 real += c.real; 50 imag += c.imag; 51 return *this; 52 } 53 };
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 //date.h 2 #ifndef _DATE_H_ 3 #define _DATE_H_ 4 class Date 5 { 6 private: 7 int year; 8 int month; 9 int day; 10 int totalDays; 11 public: 12 Date(int year,int month,int day); 13 int getYear()const{return year;} 14 int getMonth()const{return month;} 15 int getDay()const {return day;} 16 int getMaxDay()const; 17 bool isLeapYear()const 18 { 19 return year%4==0&&year%100!=0||year%400==0; 20 } 21 void show()const; 22 int operator-(const Date&date)const 23 { 24 return totalDays-date.totalDays; 25 } 26 }; 27 28 #endif//_DATE_H_ date.h
date.cpp
1 #include"date.h" 2 #include<iostream> 3 #include<cstdlib> 4 using namespace std; 5 namespace 6 { 7 const int DAYS_BEFORE_MONTH[]={0,31,59,90,120,151,181,212,243,273,304,334,365}; 8 } 9 10 Date::Date(int year,int month,int day):year{year},month{month},day{day} 11 { 12 if(day<=0||day>getMaxDay()) 13 { 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+DAYS_BEFORE_MONTH[month-1]+day; 21 if(isLeapYear()&&month>2)totalDays++; 22 } 23 24 int Date::getMaxDay()const 25 { 26 if(isLeapYear()&&month==2){return 29; 27 } 28 else{return DAYS_BEFORE_MONTH[month]-DAYS_BEFORE_MONTH[month-1]; 29 } 30 } 31 32 void Date::show()const 33 { 34 cout<<getYear()<<"-"<<getMonth()<<"-"<<getDay(); 35 } date.cpp
accumulator.h
1 //accumulator.h 2 #ifndef _ACCUMULATOR_H_ 3 #define _ACCUMULATOR_H_ 4 #include "date.h" 5 6 class Accumulator { 7 //将某个数值按日累加 8 private: 9 Date lastDate; //上次变更数值的时期 10 double value; //数值的当前值 11 double sum; //数值按日累加之和 12 public: 13 //构造函数,date为开始累加的日期,value为初始值 14 Accumulator(const Date &date, double value) 15 : lastDate(date), value(value), sum(0) {} 16 //获得日期date的累加结果 17 double getSum(const Date &date) const { 18 return sum + value * (date-lastDate); 19 } 20 //在date将数值变更为value 21 void change(const Date &date, double value) { 22 sum = getSum(date); 23 lastDate = date; 24 this->value = value; 25 } 26 //初始化,将日期变为date,数值变为value,累加器清零 27 void reset(const Date &date, double value) { 28 lastDate = date; 29 this->value = value; 30 sum = 0; 31 } 32 }; 33 #endif //_ACCUMULATOR_H_ accumulator.h
account.h
1 //accouth.h 2 #ifndef _ACCOUNT_H_ 3 #define _ACCOUNT_H_ 4 #include "date.h" 5 #include "accumulator.h" 6 #include <string> 7 8 class Account { 9 private: 10 std::string id; 11 double balance; 12 static double total; 13 protected: 14 // 构造函数 15 Account(const Date& date, const std::string& id); 16 void record(const Date& date, double amount, const std::string& desc); 17 // 报告错误信息 18 void error(const std::string& msg) const; 19 public: 20 // 获取账户ID 21 const std::string& getId() const; 22 // 获取账户余额 23 double getBalance() const; 24 // 获取所有账户的总金额 25 static double getTotal(); 26 // 存入现金,date为日期,amount为金额,desc为款项说明 27 virtual void deposit(const Date& date, double amount, const std::string& desc)=0; 28 // 取出现金,date为日期,amount为金额,desc为款项说明 29 virtual void withdraw(const Date& date, double amount, const std::string& desc)=0; 30 // 结算(计算利息、年费等),每月结算一次,date为结算日期 31 virtual void settle(const Date& date)=0; 32 // 显示账户信息 33 virtual void show() const; 34 }; 35 class SavingsAccount:public Account 36 { 37 private: 38 Accumulator acc; //辅助计算利息的累加器 39 double rate; //存款的年利率 40 public: 41 //构造函数 42 SavingsAccount(const Date &date, const std::string &id, double rate); 43 double getRate() const { return rate; } 44 //存入现金 45 void deposit(const Date &date, double amount, const std::string &desc); 46 //取出现金 47 void withdraw(const Date &date, double amount, const std::string &desc); 48 //结算利息,每年1月1日调用一次该函数 49 void settle(const Date &date); 50 }; 51 52 class CreditAccount : public Account { //信用账户类 53 private: 54 Accumulator acc; //辅助计算利息的累加器 55 double credit; //信用额度 56 double rate; //欠款的日利率 57 double fee; //信用卡年费 58 double getDebt() const { 59 double balance = getBalance(); 60 return (balance < 0? balance : 0); 61 } 62 public: 63 //构造函数 64 CreditAccount(const Date &date, const std::string &id, double credit, double rate, double fee); 65 double getCredit() const { return credit; } 66 double getRate() const { return rate; } 67 double getFee() const { return fee; } 68 double getAvailableCredit() const { 69 if (getBalance() < 0) 70 return credit + getBalance(); 71 else 72 return credit; 73 } 74 //存入现金 75 void deposit(const Date &date, double amount, const std::string &desc); 76 //取出现金 77 void withdraw(const Date &date, double amount, const std::string &desc); 78 void settle(const Date &date); 79 //显示账户信息 80 void show() const; 81 }; 82 #endif account.h
account.cpp
1 //account.cpp 2 #include "account.h" 3 #include <cmath> 4 #include <iostream> 5 using namespace std; 6 7 double Account::total = 0; 8 9 //Account类的实现 10 Account::Account(const Date &date, const 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 double Account::getBalance()const 33 { 34 return balance; 35 } 36 37 double Account::getTotal() 38 { 39 return total; 40 } 41 42 //SavingsAccount类相关成员函数的实现 43 SavingsAccount::SavingsAccount(const Date &date, const string &id, double rate) 44 : Account(date, id), rate(rate), acc(date, 0) {} 45 46 void SavingsAccount::deposit(const Date &date, double amount, const string &desc) { 47 record(date, amount, desc); 48 acc.change(date, getBalance()); 49 } 50 void SavingsAccount::withdraw(const Date &date, double amount, const string &desc) { 51 if (amount > getBalance()) { 52 error("not enough money"); 53 } else { 54 record(date, -amount, desc); 55 acc.change(date, getBalance()); 56 } 57 } 58 59 void SavingsAccount::settle(const Date &date) { 60 if(date.getMonth()==1) 61 { 62 double interest=acc.getSum(date)*rate/(date-Date(date.getYear()-1,1,1)); 63 if(interest!=0) 64 { 65 record(date,interest,"interest"); 66 } 67 acc.reset(date,getBalance()); 68 } 69 } 70 71 //CreditAccount类相关成员函数的实现 72 CreditAccount::CreditAccount(const Date &date, const string &id, double credit, double rate, double fee) 73 : Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) {} 74 75 void CreditAccount::deposit(const Date &date, double amount, const string &desc) { 76 record(date, amount, desc); 77 acc.change(date, getDebt()); 78 } 79 80 void CreditAccount::withdraw(const Date &date, double amount, const string &desc) { 81 if (amount - getBalance() > credit) { 82 error("not enough credit"); 83 } else { 84 record(date, -amount, desc); 85 acc.change(date, getDebt()); 86 } 87 } 88 89 void CreditAccount::settle(const Date &date) { 90 double interest = acc.getSum(date) * rate; 91 if (interest!= 0) 92 record(date, interest, "interest"); 93 if (date.getMonth() == 1) 94 record(date, -fee, "annual fee"); 95 acc.reset(date, getDebt()); 96 } 97 98 void CreditAccount::show() const { 99 Account::show(); 100 cout << "\tAvailable credit: " << getAvailableCredit(); 101 } account.cpp
8_8.cpp
1 // 8_8.cpp 2 #include "account.h" 3 #include <iostream> 4 using namespace std; 5 6 int main() { 7 Date date(2008, 11, 1); // 起始日期 8 9 // 建立几个账户 10 SavingsAccount sa1(date, "S3755217", 0.015); 11 SavingsAccount sa2(date, "02342342", 0.015); 12 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 13 Account *accounts[] = {&sa1, &sa2, &ca}; 14 15 const int n = sizeof(accounts) / sizeof(Account *); 16 17 cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl; 18 19 char cmd; 20 do { 21 // 显示日期和总金额 22 date.show(); 23 cout << "\tTotal: " << Account::getTotal() << "\tcommand>"; 24 25 int index, day; 26 double amount; 27 string desc; 28 cin >> cmd; 29 30 switch (cmd) { 31 case 'd': // 存入现金 32 cin >> index >> amount; 33 getline(cin, desc); 34 accounts[index]->deposit(date, amount, desc); 35 break; 36 case 'w': // 取出现金 37 cin >> index >> amount; 38 getline(cin, desc); 39 accounts[index]->withdraw(date, amount, desc); 40 break; 41 case 's': // 查询各账户信息 42 for (int i = 0; i < n; i++) { 43 cout << "[" << i << "] "; 44 accounts[i]->show(); 45 cout << endl; 46 } 47 break; 48 case 'c': // 改变日期 49 cin >> day; 50 if (day < date.getDay()) 51 cout << "You cannot specify a previous day"; 52 else if (day > date.getMaxDay()) 53 cout << "Invalid day"; 54 else 55 date = Date(date.getYear(), date.getMonth(), day); 56 break; 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 for (int i = 0; i < n; i++) 63 accounts[i]->settle(date); 64 break; 65 } 66 } while (cmd!= 'e'); 67 68 return 0; 69 } 8_8.cpp