实验5
task3
代码:
#pragma once #include <iostream> #include <string> class MachinePets { protected: std::string nickname; public: MachinePets(const std::string &s) : nickname(s) {} std::string get_nickname() const { return nickname; } virtual std::string talk() = 0; virtual ~MachinePets() {} }; class PetCats : public MachinePets { public: PetCats(const std::string &s) : MachinePets(s) {} std::string talk() override { return "maio wu~"; } }; class PetDogs : public MachinePets { public: PetDogs(const std::string &s) : MachinePets(s) {} std::string talk() override { return "wang wang~"; } };
#include "pets.hpp" #include <iostream> #include <vector> void test() { using namespace std; vector<MachinePets *> pets; pets.push_back(new PetCats("miku")); pets.push_back(new PetDogs("da huang")); for(auto &ptr: pets) cout <<ptr->get_nickname() << " says " << ptr->talk() << endl; } int main() { test(); }
运行截图:
task4
代码:
#pragma once #include<iostream> #include<iomanip> #include<string> using namespace std; class Film { private: string name; string director; string area; string time; public: Film(const string &name="",const string &director="",const string &area="",const string &time=""); friend ostream& operator<<(ostream& out, const Film& f); friend istream& operator>>(istream& in, Film& f); string get_year()const { return time; } }; Film::Film(const string &name,const string &director,const string &area,const string &time){ this->name=name; this->director=director; this->area=area; this->time=time; } ostream& operator<<(ostream& out, const Film& f) { out << f.name<< setw(15) << f.director <<setw(10) << f.area << setw(10) << f.time << endl; return out; } istream& operator>>(istream& in, Film& f) { in >> f.name >> f.director >> f.area >> f.time; return in; } bool compare_by_year(const Film& f1, const Film& f2) { return f1.get_year() < f2.get_year(); } film.hpp
#pragma once #include<iostream> #include<iomanip> #include<string> using namespace std; class Film { private: string name; string director; string area; string time; public: Film(const string &name="",const string &director="",const string &area="",const string &time=""); friend ostream& operator<<(ostream& out, const Film& f); friend istream& operator>>(istream& in, Film& f); string get_year()const { return time; } }; Film::Film(const string &name,const string &director,const string &area,const string &time){ this->name=name; this->director=director; this->area=area; this->time=time; } ostream& operator<<(ostream& out, const Film& f) { out << f.name<< setw(15) << f.director <<setw(10) << f.area << setw(10) << f.time << endl; return out; } istream& operator>>(istream& in, Film& f) { in >> f.name >> f.director >> f.area >> f.time; return in; } bool compare_by_year(const Film& f1, const Film& f2) { return f1.get_year() < f2.get_year(); } task4.cpp task4.cpp
运行截图:
task5
代码:
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include <iostream> using std::istream; using std::ostream; template <typename T> class Complex { private: T real; T imag; public: // 默认 Complex(T r = 0, T i = 0) : real(r), imag(i) {} // 复制 Complex(const Complex<T>& other) : real(other.real), imag(other.imag) {} // +重载 Complex<T> operator+(const Complex<T>& other) const { return Complex<T>(real + other.real, imag + other.imag); } // +=重载 Complex<T>& operator+=(const Complex<T>& other) { real += other.real; imag += other.imag; return *this; } // ==重载 bool operator==(const Complex<T>& other) const { return real == other.real && imag == other.imag; } // 得到实部 T get_real() const { return real; } // 得到虚部 T get_imag() const { return imag; } // 输出重载 friend ostream& operator<<(ostream& out, const Complex<T>& c) { out << c.real; if (c.imag >= 0) { out<< " + " << c.imag << "i"; } else { out << " - " << -c.imag << "i"; } return out; } // 输入重载 friend istream& operator>>(istream& in, Complex<T>& c) { in >> c.real >>c.imag; return in; } }; #endif
#include "Complex.hpp" #include <iostream> using std::cin; using std::cout; using std::endl; using std::boolalpha; void test1() { Complex<int> c1(2, -5), c2(c1); cout << "c1 = " << c1 << endl; cout << "c2 = " << c2 << endl; cout << "c1 + c2 = " << c1 + c2 << endl; c1 += c2; cout << "c1 = " << c1 << endl; cout << boolalpha << (c1 == c2) << endl; } void test2() { Complex<double> c1, c2; cout << "Enter c1 and c2: "; cin >> c1 >> c2; cout << "c1 = " << c1 << endl; cout << "c2 = " << c2 << endl; cout << "c1.real = " << c1.get_real() << endl; cout << "c1.imag = " << c1.get_imag() << endl; } int main() { cout << "自定义类模板Complex测试1: " << endl; test1(); cout << endl; cout << "自定义类模板Complex测试2: " << endl; test2(); }
运行截图:
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 date.h
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 } date.cpp
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 accumulator.h
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 acount.h
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 } account.cpp
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 } 8.8
运行截图: