任務1
task1.cpp
1 #include "publisher.hpp" 2 #include <vector> 3 #include <typeinfo> 4 5 using std::vector; 6 7 void test() { 8 vector<Publisher *> v; 9 10 v.push_back(new Book("Harry Potter", "J.K. Rowling")); 11 v.push_back(new Film("The Godfather", "Francis Ford Coppola")); 12 v.push_back(new Music("Blowing in the wind", "Bob Dylan")); 13 14 for(auto &ptr: v) { 15 cout << "pointer type: " << typeid(ptr).name() << endl; // 輸出指標型別 16 cout << "RTTI type: " << typeid(*ptr).name() << endl; // 輸出指標指向的物件型別 17 ptr->publish(); 18 ptr->use(); 19 cout << endl; 20 } 21 } 22 23 int main() { 24 test(); 25 }
publisher.hpp
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 6 using std::cout; 7 using std::endl; 8 using std::string; 9 10 // 發行/出版物類:Publisher (抽象類) 11 class Publisher { 12 public: 13 Publisher(const string &s = ""); // 建構函式 14 15 public: 16 virtual void publish() const = 0; // 純虛擬函式,作為介面繼承 17 virtual void use() const = 0; // 純虛擬函式,作為介面繼承 18 19 protected: 20 string name; // 發行/出版物名稱 21 }; 22 23 Publisher::Publisher(const string &s): name {s} { 24 } 25 26 27 // 圖書類: Book 28 class Book: public Publisher { 29 public: 30 Book(const string &s = "", const string &a = ""); // 建構函式 31 32 public: 33 void publish() const override; // 介面 34 void use() const override; // 介面 35 36 private: 37 string author; // 作者 38 }; 39 40 Book::Book(const string &s, const string &a): Publisher{s}, author{a} { 41 } 42 43 void Book::publish() const { 44 cout << "Publishing book: 《" << name << "》 by " << author << endl; 45 } 46 47 void Book::use() const { 48 cout << "Reading book: " << name << " by " << author << endl; 49 } 50 51 52 // 電影類: Film 53 class Film: public Publisher { 54 public: 55 Film(const string &s = "", const string &d = ""); // 建構函式 56 57 public: 58 void publish() const override; // 介面 59 void use() const override; // 介面 60 61 private: 62 string director; // 導演 63 }; 64 65 Film::Film(const string &s, const string &d): Publisher{s}, director{d} { 66 } 67 68 void Film::publish() const { 69 cout << "Publishing film: <" << name << "> directed by " << director << endl; 70 } 71 72 void Film::use() const { 73 cout << "Watching film: " << name << " directed by " << director << endl; 74 } 75 76 77 // 音樂類:Music 78 class Music: public Publisher { 79 public: 80 Music(const string &s = "", const string &a = ""); 81 82 public: 83 void publish() const override; // 介面 84 void use() const override; // 介面 85 86 private: 87 string artist; // 音樂藝術家名稱 88 }; 89 90 Music::Music(const string &s, const string &a): Publisher{s}, artist{a} { 91 } 92 93 void Music::publish() const { 94 cout << "Publishing music <" << name << "> by " << artist << endl; 95 } 96 97 void Music::use() const { 98 cout << "Listening to music: " << name << " by " << artist << endl; 99 }
程式碼執行截圖:
任務2
task2.cpp
1 #include "booksale.hpp" 2 #include <iostream> 3 #include <string> 4 #include <vector> 5 #include <algorithm> 6 7 // 按圖書銷售數額比較 8 bool compare_by_amount(const BookSale &x1, const BookSale &x2) { 9 return x1.get_amount() > x2.get_amount(); 10 } 11 12 void test() { 13 using namespace std; 14 15 vector<BookSale> sales_lst; // 存放圖書銷售記錄 16 17 int books_number; 18 cout << "錄入圖書數量: "; 19 cin >> books_number; 20 21 cout << "錄入圖書銷售記錄" << endl; 22 for(int i = 0; i < books_number; ++i) { 23 string name, author, translator, isbn; 24 float price; 25 cout << string(20, '-') << "第" << i+1 << "本圖書資訊錄入" << string(20, '-') << endl; 26 cout << "錄入書名: "; cin >> name; 27 cout << "錄入作者: "; cin >> author; 28 cout << "錄入譯者: "; cin >> translator; 29 cout << "錄入isbn: "; cin >> isbn; 30 cout << "錄入定價: "; cin >> price; 31 32 Book book(name, author, translator, isbn, price); 33 34 float sales_price; 35 int sales_amount; 36 37 cout << "錄入售價: "; cin >> sales_price; 38 cout << "錄入銷售數量: "; cin >> sales_amount; 39 40 BookSale record(book, sales_price, sales_amount); 41 sales_lst.push_back(record); 42 } 43 44 // 按銷售冊數排序 45 sort(sales_lst.begin(), sales_lst.end(), compare_by_amount); 46 47 // 按銷售冊數降序輸出圖書銷售資訊 48 cout << string(20, '=') << "圖書銷售統計" << string(20, '=') << endl; 49 for(auto &t: sales_lst) { 50 cout << t << endl; 51 cout << string(40, '-') << endl; 52 } 53 } 54 55 int main() { 56 test(); 57 }
book.hpp
1 #pragma once 2 3 #include <string> 4 #include <iostream> 5 #include <iomanip> 6 7 using std::string; 8 using std::ostream; 9 using std::endl; 10 using std::setw; 11 using std::left; 12 13 class Book { 14 public: 15 Book(const string &name, const string &author, const string &translator, const string &isbn, float price); 16 17 friend ostream& operator<<(ostream &out, const Book &book); 18 19 private: 20 string name; // 書名 21 string author; // 作者 22 string translator; // 譯者 23 string isbn; // isbn號 24 float price; // 定價 25 }; 26 27 // 成員函式實現 28 Book::Book(const string &name, const string &author, const string &translator, const string &isbn, float price) { 29 this->name = name; 30 this->author = author; 31 this->translator = translator; 32 this->isbn = isbn; 33 this->price = price; 34 } 35 36 // 友元實現 37 ostream& operator<<(ostream &out, const Book &book) { 38 out << left; 39 out << setw(15) << "書名:" << book.name << endl 40 << setw(15) << "作者:" << book.author << endl 41 << setw(15) << "譯者:" << book.translator << endl 42 << setw(15) << "ISBN:" << book.isbn << endl 43 << setw(15) << "定價:" << book.price; 44 45 return out; 46 }
booksale.hpp
1 #pragma once 2 3 #include "book.hpp" 4 #include <iostream> 5 #include <string> 6 #include <iomanip> 7 8 using std::string; 9 using std::cout; 10 using std::endl; 11 using std::setw; 12 13 class BookSale { 14 public: 15 BookSale(const Book &b, float price, int amount); 16 int get_amount() const; 17 18 friend ostream& operator<<(ostream &out, const BookSale &item); 19 20 private: 21 Book rb; 22 float sales_price; // 售價 23 int sales_amount; // 銷售數量 24 float revenue; // 營收 25 }; 26 27 // 成員函式實現 28 BookSale::BookSale(const Book &b, float price, int amount): rb{b}, sales_price(price), sales_amount{amount} { 29 revenue = sales_amount * sales_price; 30 } 31 32 int BookSale::get_amount() const { 33 return sales_amount; 34 } 35 36 // 友元函式實現 37 ostream& operator<<(ostream &out, const BookSale &item) { 38 out << left; 39 out << item.rb << endl 40 << setw(15) << "售價:" << item.sales_price << endl 41 << setw(15) << "銷售數量:" << item.sales_amount << endl 42 << setw(15) << "營收:" << item.revenue; 43 44 return out; 45 }
程式碼執行截圖:
任務3
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 }
pets.hpp
1 //pets.hpp 2 3 #pragma onece 4 5 #ifndef PETS_HPP 6 #define PETS_HPP 7 8 #include<iostream> 9 #include<string> 10 11 using namespace std; 12 13 class MachinePets{ 14 public: 15 MachinePets(const string& Nickname):nickname(Nickname){} 16 17 string get_nickname()const{return nickname;} 18 19 virtual string talk() const = 0; 20 21 virtual ~MachinePets() = default; 22 23 private: 24 string nickname; 25 }; 26 27 28 class PetCats:public MachinePets{ 29 public: 30 PetCats(const string& s):MachinePets(s){} 31 string talk()const override{return cat_talk; } 32 private: 33 string cat_talk="miao wu~"; 34 }; 35 36 37 class PetDogs:public MachinePets{ 38 public: 39 PetDogs(const string& s):MachinePets(s){} 40 string talk()const override{return dog_talk; } 41 private: 42 string dog_talk="wang wang~"; 43 }; 44 45 46 #endif//PETS_HPP
程式碼執行截圖:
任務4
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 }
film.hpp
1 #pragma once 2 3 #ifndef FILM_HPP 4 #define FILM_HPP 5 6 #include<iostream> 7 #include<string> 8 #include<iomanip> 9 10 using namespace std; 11 12 class Film { 13 private: 14 string name; 15 string director; 16 string country; 17 int year; 18 public: 19 Film(){} 20 Film(const string& Name,const string& Director,const string& Country,const int& Year) 21 :name(Name),director(Director),country(Country),year(Year){} 22 23 24 friend bool compare_by_year(const Film& f1, const Film& f2); 25 friend ostream& operator<<(ostream& out, const Film& f); 26 friend istream& operator>>(istream &in,Film& f); 27 28 }; 29 30 istream& operator>>(istream& in, Film& f) { 31 cout << "錄入片名:";cin >> f.name; 32 cout << "錄入導演:";cin >> f.director; 33 cout << "錄入製片國家/地區:";cin >> f.country; 34 cout << "錄入上映年份:";cin>> f.year; 35 return in; 36 } 37 38 ostream& operator<<(ostream& out, const Film& f) { 39 40 out << left << setw(12) << f.name << setw(12) << f.director 41 << setw(12) << f.country << setw(12) << f.year; 42 return out; 43 } 44 45 bool compare_by_year(const Film& f1, const Film& f2) { 46 return f1.year < f2.year; 47 } 48 49 #endif //FILM_HPP
程式碼執行截圖:
任務5
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 }
Complex.hpp
1 #pragma once 2 3 #ifndef COMPLEX_HPP 4 #define COMPLEX_HPP 5 6 #include <iostream> 7 8 template <typename T> 9 class Complex { 10 private: 11 T real; 12 T imag; 13 14 public: 15 // 預設建構函式 16 Complex(T r = 0, T i = 0) : real(r), imag(i) {} 17 18 // 複製建構函式 19 Complex(const Complex& other) : real(other.real), imag(other.imag) {} 20 21 // 獲取實部 22 T get_real() const { return real; } 23 24 // 獲取虛部 25 T get_imag() const { return imag; } 26 27 // += 運算子過載 28 Complex& operator+=(const Complex& other) { 29 real += other.real; 30 imag += other.imag; 31 return *this; 32 } 33 34 // + 運算子過載 35 Complex operator+(const Complex& other) const { 36 return Complex(real + other.real, imag + other.imag); 37 } 38 39 // == 運算子過載 40 bool operator==(const Complex& other) const { 41 return real == other.real && imag == other.imag; 42 } 43 44 // 流插入運算子過載 45 friend std::ostream& operator<<(std::ostream& os, const Complex& c) { 46 os << c.real; 47 if (c.imag >= 0) 48 os << " + " << c.imag << "i"; 49 else 50 os << " - " << -c.imag << "i"; 51 return os; 52 } 53 54 // 流提取運算子過載 55 friend std::istream& operator>>(std::istream& is, Complex& c) { 56 is >> c.real >> c.imag; 57 return is; 58 } 59 }; 60 61 #endif // COMPLEX_HPP
程式碼執行截圖:
任務6
task6.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 }
date.h
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 { return year; } 12 int getMonth() const { return month; } 13 int getDay() const { return day; } 14 int getMaxDay() const; 15 bool isLeapYear() const { 16 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 17 } 18 void show()const; 19 int operator-(const Date& date)const { 20 return totalDays - date.totalDays; 21 } 22 };
date.cpp
1 #include"date.h" 2 #include<iostream> 3 #include<cstdlib> 4 using namespace std; 5 namespace { 6 const int DAYS_BEFIRE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304 ,334,365 }; 7 } 8 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 9 if (day <= 0 || day > getMaxDay()) { 10 cout << "Invalid date: "; 11 show(); 12 cout << endl; 13 exit(1); 14 } 15 int years = year - 1; 16 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFIRE_MONTH[month - 1] + day; 17 if (isLeapYear() && month > 2) totalDays++; 18 } 19 int Date::getMaxDay() const { 20 if (isLeapYear() && month == 2) 21 return 29; 22 else return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1]; 23 } 24 void Date::show() const { 25 cout << getYear() << "-" << getMonth() << "-" << getDay(); 26 }
accumulator.h
1 #pragma once 2 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; 22 sum = 0; 23 } 24 };
account.h
1 #pragma once 2 3 #include"date.h" 4 #include"accumulator.h" 5 #include<string> 6 using namespace std; 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 amoount, const string& desc); 15 void error(const string& msg) const; 16 public:const string& getId() { return id; } 17 double getBalance() const { return balance; } 18 static double getTotal() { return total; } 19 virtual void deposit(const Date& date, double amount, const string& desc) = 0; 20 virtual void withdraw(const Date& date, double amount, const string& desc) = 0; 21 virtual void settle(const Date& date) = 0; 22 virtual void show() const; 23 }; 24 class SavingsAccount :public Account { 25 private: 26 Accumulator acc; 27 double rate; 28 public: 29 SavingsAccount(const Date& date, const string& id, double rate); 30 double getRate() const { return rate; } 31 void deposit(const Date& date, double amount, const string& desc); 32 void withdraw(const Date& date, double amount, const string& desc); 33 void settle(const Date& date); 34 }; 35 class CreditAccount :public Account { 36 private: 37 Accumulator acc; 38 double credit; 39 double rate; 40 double fee; 41 double getDebt()const { 42 double balance = getBalance(); 43 return(balance < 0 ? balance : 0); 44 } 45 public:CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); 46 double getCredit()const { return credit; } 47 double getRate() const { return rate; } 48 double getFee() const { return fee; } 49 double getAvailableCredit() const { 50 if (getBalance() < 0)return credit + getBalance(); 51 else return credit; 52 } 53 void deposit(const Date& date, double amount, const string& desc); 54 void withdraw(const Date& date, double amount, const string& desc); 55 void settle(const Date& date); 56 void show() const; 57 };
account.cpp
1 #include "account.h" 2 #include <cmath> 3 #include<iostream> 4 using namespace std; 5 double Account::total = 0; 6 Account::Account(const Date& date, const string& id) :id(id), balance(0) { 7 date.show(); 8 cout << "\t#" << id << "created" << endl; 9 } 10 void Account::record(const Date& date, double amount, const string& desc) { 11 amount = floor(amount * 100 + 0.5) / 100; 12 balance += amount; 13 total += amount; 14 date.show(); 15 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 16 } 17 void Account::show()const { cout << id << "\tBalance:" << balance; } 18 void Account::error(const string& msg)const { 19 cout << "Error(#" << id << "):" << msg << endl; 20 } 21 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :Account(date, id), rate(rate), acc(date, 0) {} 22 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 23 record(date, amount, desc); 24 acc.change(date, getBalance()); 25 } 26 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 27 if (amount > getBalance()) { 28 error("not enough money"); 29 } 30 else { 31 record(date, -amount, desc); 32 acc.change(date, getBalance()); 33 } 34 } 35 void SavingsAccount::settle(const Date& date) { 36 double interest = acc.getSum(date) * rate / (date - Date(date.getYear() - 1, 1, 1)); 37 if (interest != 0)record(date, interest, "interest"); 38 acc.reset(date, getBalance()); 39 } 40 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) {} 41 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 42 record(date, amount, desc); 43 acc.change(date, getDebt()); 44 } 45 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 46 if (amount - getBalance() > credit) { 47 error("not enouogh credit"); 48 } 49 else { 50 record(date, -amount, desc); 51 acc.change(date, getDebt()); 52 } 53 } 54 void CreditAccount::settle(const Date& date) { 55 double interest = acc.getSum(date) * rate; 56 if (interest != 0) record(date, interest, "interest"); 57 if (date.getMonth() == 1)record(date, -fee, "annual fee"); 58 acc.reset(date, getDebt()); 59 } 60 void CreditAccount::show() const { 61 Account::show(); 62 cout << "\tAvailable credit:" << getAvailableCredit(); 63 }
程式碼執行截圖: