实验5
任务3:
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 12 public: 13 MachinePets(const string &name):nickname(name) {} 14 15 string get_nickname() const { 16 return nickname; 17 } 18 19 virtual string talk() = 0; 20 }; 21 22 23 24 class PetCats: public MachinePets { 25 public: 26 PetCats(const string& name):MachinePets(name){} 27 28 string talk() override { 29 return "meow"; 30 } 31 }; 32 33 class PetDogs: public MachinePets { 34 public: 35 PetDogs(const string& name):MachinePets(name){} 36 37 string talk() override{ 38 return "woof"; 39 } 40 };
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:
1 #include<iostream> 2 #include<string> 3 #include<iomanip> 4 5 using namespace std; 6 7 class Film{ 8 public: 9 Film(const string &name="",const string &director="",const string &area="",const int &year=0); 10 friend istream& operator>>(istream &in,Film &film); 11 friend ostream& operator<<(ostream &out,const Film &film); 12 int get_year() const; 13 private: 14 string name; 15 string director; 16 string area; 17 int year; 18 }; 19 20 Film::Film(const string &name,const string &director,const string &area,const int &year){ 21 } 22 23 istream& operator>>(istream &in,Film &film){ 24 in>>film.name>>film.director>>film.area>>film.year; 25 return in; 26 } 27 28 ostream& operator<<(ostream &out,const Film &film){ 29 out<<left; 30 out<<setw(15) << film.name << setw(15) << film.director << setw(15) << film.area << setw(15) << film.year << endl; 31 return out; 32 } 33 34 int Film::get_year() const{ 35 return year; 36 } 37 38 bool compare_by_year(const Film &f1,const Film &f2){ 39 return f1.get_year()>f2.get_year(); 40 }
1 #include "film.hpp" 2 3 #include <iostream> 4 #include <string> 5 #include <vector> 6 #include <algorithm> 7 8 void test() { 9 using namespace std; 10 11 int n; 12 cout << "输入电影数目: "; 13 cin >> n; 14 cout << "录入" << n << "部影片信息" << endl; 15 16 vector<Film> film_lst; 17 18 for(int i = 0; i < n; ++i) { 19 Film f; 20 cout << string(20, '-') << "第" << i+1 << "部影片录入" << string(20, 21 '-') << endl; 22 cin >> f; 23 film_lst.push_back(f); 24 } 25 26 // 按发行年份升序排序 27 sort(film_lst.begin(), film_lst.end(), compare_by_year); 28 cout << string(20, '=') + "电影信息(按发行年份)" + string(20, '=')<< endl; 29 for(auto &f: film_lst) 30 cout << f << endl; 31 } 32 33 int main() { 34 test(); 35 }
结果:
任务5:
1 #pragma once 2 3 #include <iostream> 4 5 using namespace std; 6 7 template <typename T> 8 class Complex { 9 private: 10 T real; 11 T imag; 12 13 public: 14 Complex(T r = 0, T i = 0) : real(r), imag(i) {} 15 16 Complex(const Complex<T>& other) : real(other.real), imag(other.imag) {} 17 18 Complex<T> operator+(const Complex<T>& other) const { 19 return Complex<T>(real + other.real, imag + other.imag); 20 } 21 22 Complex<T>& operator+=(const Complex<T>& other) { 23 real += other.real; 24 imag += other.imag; 25 return *this; 26 } 27 28 bool operator==(const Complex<T>& other) const { 29 return real == other.real && imag == other.imag; 30 } 31 32 T get_real() const { 33 return real; 34 } 35 36 T get_imag() const { 37 return imag; 38 } 39 40 friend ostream& operator<<(ostream& os, const Complex<T>& c) { 41 os << c.real; 42 if (c.imag >= 0) { 43 os << " + " << c.imag << "i"; 44 } else { 45 os << " - " << -c.imag << "i"; 46 } 47 return os; 48 } 49 50 friend istream& operator>>(istream& is, Complex<T>& c) { 51 is >> c.real >>c.imag; 52 return is; 53 } 54 };
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 cout << "c1 = " << c1 << endl; 12 cout << "c2 = " << c2 << endl; 13 cout << "c1 + c2 = " << c1 + c2 << endl; 14 c1 += c2; 15 cout << "c1 = " << c1 << endl; 16 cout << boolalpha << (c1 == c2) << endl; 17 } 18 19 void test2() { 20 Complex<double> c1, c2; 21 cout << "Enter c1 and c2: "; 22 cin >> c1 >> c2; 23 cout << "c1 = " << c1 << endl; 24 cout << "c2 = " << c2 << endl; 25 cout << "c1.real = " << c1.get_real() << endl; 26 cout << "c1.imag = " << c1.get_imag() << endl; 27 } 28 29 int main() { 30 cout << "自定义类模板Complex测试1: " << endl; 31 test1(); 32 cout << endl; 33 cout << "自定义类模板Complex测试2: " << endl; 34 test2(); 35 }
结果:
任务6:
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 } 20 void show() const; 21 int operator-(const Date & date) const { 22 return totalDays - date.totalDays; 23 24 } 25 26 };
1 #include "date.h" 2 #include <iostream> 3 #include <cstdlib> 4 using namespace std; 5 6 namespace { 7 const int DAYS_BEFORE_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; 8 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 } 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 25 int Date::getMaxDay() const { 26 if (isLeapYear() && month == 2) 27 return 29; 28 else return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; 29 30 } 31 32 void Date::show() const { 33 cout << getYear() << "-" << getMonth() << "-" << getDay(); 34 35 }
1 #pragma once 2 #include "date.h" 3 #include "accumulator.h" 4 #include <string> 5 using namespace std; 6 7 class Account { 8 private: 9 string id; 10 double balance; 11 static double total; 12 protected: 13 Account(const Date& date, const string& id); 14 void record(const Date& date, double amount, const string& desc); 15 void error(const string& msg) const; 16 17 public: 18 const string& getId() { return id; } 19 double getBalance() const { return balance; } 20 static double getTotal() { return total; } 21 virtual void deposit(const Date& date, double amount, const string& desc) = 0; 22 virtual void withdraw(const Date& date, double amount, const string& desc) = 0; 23 virtual void settle(const Date& date) = 0; 24 virtual void show() const; 25 26 }; 27 28 class SavingsAccount : public Account { 29 private: 30 Accumulator acc; 31 double rate; 32 public: 33 SavingsAccount(const Date& date, const string& id, double rate); 34 double getRate() const { return rate; } 35 void deposit(const Date& date, double amount, const string& desc); 36 void withdraw(const Date& date, double amount, const string& desc); 37 void settle(const Date& date); 38 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 } 52 public: 53 CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); 54 double getCredit() const { return credit; } 55 double getRate() const { return rate; } 56 double getFee() const { return fee; } 57 double getAvailableCredit() const { 58 if (getBalance() < 0) return credit + getBalance(); 59 else return credit; 60 61 } 62 void deposit(const Date& date, double amount, const string& desc); 63 void withdraw(const Date& date, double amount, const string& desc); 64 void settle(const Date& date); 65 void show() const; 66 67 };
1 #include "account.h" 2 #include <cmath> 3 #include<iostream> 4 using namespace std; 5 6 double Account::total = 0; 7 Account::Account(const Date& date, const string& id) : id(id), balance(0) { 8 date.show(); 9 cout << "\t#" << id << " created" << endl; 10 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 22 void Account::show() const { 23 cout << id << "\tBalance: " << balance; 24 25 } 26 27 void Account::error(const string& msg) const { 28 cout << "Error (#" << id << "): " << msg << endl; 29 30 } 31 32 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) 33 : Account(date, id), rate(rate), acc(date, 0) {} 34 35 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 36 record(date, amount, desc); 37 acc.change(date, getBalance()); 38 39 } 40 41 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 42 if (amount > getBalance()) { 43 error("not enough money"); 44 45 } 46 else { 47 record(date, -amount, desc); 48 acc.change(date, getBalance()); 49 50 } 51 52 } 53 54 void SavingsAccount::settle(const Date& date) { 55 if (date.getMonth() == 1) 56 { 57 double interest = acc.getSum(date) * rate / (date - Date(date.getYear() - 1, 1, 1)); 58 if (interest != 0) record(date, interest, "interest"); 59 acc.reset(date, getBalance()); 60 } 61 } 62 63 CreditAccount::CreditAccount(const Date& date, const string& id, double credit, double rate, double fee) 64 : Account(date, id), credit(credit), rate(rate), fee(fee), acc(date, 0) {} 65 66 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 67 record(date, amount, desc); 68 acc.change(date, getDebt()); 69 70 } 71 72 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 73 if (amount - getBalance() > credit) { 74 error("not enough credit"); 75 76 } 77 else { 78 record(date, -amount, desc); 79 acc.change(date, getDebt()); 80 81 } 82 83 } 84 85 void CreditAccount::settle(const Date& date) { 86 double interest = acc.getSum(date) * rate; 87 if (interest != 0) record(date, interest, "interest"); 88 if (date.getMonth() == 1) record(date, -fee, "annual fee"); 89 acc.reset(date, getDebt()); 90 91 } 92 93 void CreditAccount::show() const { 94 Account::show(); 95 cout << "\tAvailable credit: " << getAvailableCredit(); 96 97 }
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 12 double getSum(const Date& date) const { 13 return sum + value * (date - lastDate); 14 15 } 16 Accumulator(const Date& date, double value) : lastDate(date), value(value), sum{ 0 } {} 17 18 void change(const Date& date, double value) { 19 sum = getSum(date); 20 lastDate = date; 21 this->value = value; 22 23 } 24 25 void reset(const Date& date, double value) { 26 lastDate = date; 27 this->value = value; 28 sum = 0; 29 } 30 31 };
1 #include "account.h" 2 #include<iostream> 3 using namespace std; 4 5 int main() { 6 Date date(2008, 11, 1); 7 SavingsAccount sa1(date, "S3755217", 0.015); 8 SavingsAccount sa2(date, "02342342", 0.015); 9 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 10 Account* accounts[] = { &sa1, &sa2, &ca }; 11 const int n = sizeof(accounts) / sizeof(Account*); 12 cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl; 13 char cmd; 14 do { 15 date.show(); 16 cout << "\tTotal:" << Account::getTotal() << "\tcommand>"; 17 int index, day; 18 double amount; 19 string desc; 20 cin >> cmd; 21 switch (cmd) { 22 case 'd': 23 cin >> index >> amount; 24 getline(cin, desc); 25 accounts[index]->deposit(date, amount, desc); 26 break; 27 case 'w': 28 cin >> index >> amount; 29 getline(cin, desc); 30 accounts[index]->withdraw(date, amount, desc); 31 break; 32 case 's': 33 for (int i = 0; i < n; i++) { 34 cout << "[" << i << "]"; 35 accounts[i]->show(); 36 cout << endl; 37 38 } 39 break; 40 case 'c': 41 cin >> day; 42 if (day < date.getDay()) { 43 cout << "You cannot specify a previous day"; 44 45 } 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 54 { 55 date = Date(date.getYear(), date.getMonth() + 1, 1); 56 } 57 for (int i = 0; i < n; i++) { 58 accounts[i]->settle(date); 59 60 } 61 break; 62 63 } 64 65 } while (cmd != 'e'); 66 return 0; 67 68 }
结果:
问题:
改进:重载了运算符,书写更加简洁,程序安全性更高;
完善:输入前应有提示,功能不是特别全面。