task1:
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 }View Code
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 }View Code
task2:
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 }View Code
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 }View Code
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 }View Code
问题一:运用运算符重载,返回值为流对象ostream的引用形式,可以实现连续输出,<<作为双目运算符,一个形参是流对象,一个是自定义的类
问题二: sort(sales_lst.begin(), sales_lst.end(), compare_by_amount); 用迭代器和自定义的函数来实现,compare_by_amount是自定义的函数,如果>,则返回真的一个bool函数
问题三:rb{b},列表初始化
task3:
Pets.hpp:
1 #pragma once 2 #include<iostream> 3 #include<string> 4 using namespace std; 5 6 class MachinePets 7 { 8 private: 9 string nickname; 10 11 public: 12 MachinePets(const string& name) :nickname(name) {} 13 string get_nickname()const; 14 virtual string talk() = 0; 15 16 }; 17 18 string MachinePets::get_nickname()const 19 { 20 return nickname; 21 } 22 23 24 class PetCats :public MachinePets 25 { 26 public: 27 PetCats(const string& s) :MachinePets(s) {} 28 string talk()override; 29 30 }; 31 32 string PetCats::talk() 33 { 34 return "miao wu~"; 35 36 } 37 38 39 class PetDogs :public MachinePets 40 { 41 public: 42 PetDogs(const string& s) :MachinePets(s) {} 43 string talk()override; 44 45 }; 46 47 string PetDogs::talk() 48 { 49 return "wang wang~"; 50 }View Code
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 }View Code
task4:
film.hpp:
1 #pragma once 2 #include<iostream> 3 #include<string> 4 #include<iomanip> 5 using namespace std; 6 7 class Film 8 { 9 private: 10 string pianming; 11 string daoyan; 12 string diqu; 13 int nianfen; 14 15 public: 16 friend istream& operator>>(istream& in, Film& f); 17 friend ostream& operator<<(ostream& out, const Film& f); 18 friend bool compare_by_year(const Film& f1, const Film& f2); 19 20 }; 21 22 istream& operator>>(istream &in,Film& f) 23 { 24 cout << "录入片名:"; cin >> f.pianming; 25 cout << "录入导演:"; cin >> f.daoyan; 26 cout << "录入制片国家/地区:"; cin >> f.diqu; 27 cout << "录入上映年份:"; cin >> f.nianfen; 28 return in; 29 30 } 31 32 ostream& operator<<(ostream& out, const Film& f) 33 { 34 out << left; 35 out << setw(15) << f.pianming << setw(15) << f.daoyan << setw(15) << f.diqu << setw(15) << f.nianfen; 36 return out; 37 } 38 39 40 bool compare_by_year(const Film& f1, const Film& f2) 41 { 42 return f1.nianfen < f2.nianfen; 43 }View Code
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 }View Code
task5:
Complex.hpp:
1 #pragma once 2 #include<iostream> 3 using namespace std; 4 5 template <typename T> 6 class Complex 7 { 8 private: 9 T real; 10 T imag; 11 public: 12 Complex(T shi = 0, T xu = 0); 13 Complex(const Complex& c); 14 15 T get_real(); 16 T get_imag(); 17 18 friend istream& operator>>(istream& in, Complex& c) 19 { 20 in >> c.real >> c.imag; 21 return in; 22 } 23 friend ostream& operator<<(ostream& out, const Complex& c) 24 { 25 if (c.imag > 0) 26 { 27 out << c.real << " + " << c.imag<<"i"; 28 return out; 29 } 30 else 31 { 32 out << c.real << " - " << -c.imag<<"i"; 33 return out; 34 } 35 } 36 37 Complex& operator+=(const Complex& c) 38 { 39 real += c.real; 40 imag += c.imag; 41 return *this; 42 43 } 44 45 Complex operator+(const Complex& c) //返回值类型不能为引用,这只是创建了一个临时无名对象 46 { 47 return Complex(real + c.real, imag + c.imag); 48 } 49 50 bool operator==(const Complex& c) 51 { 52 if (real == c.real) 53 { 54 return true; 55 } 56 else 57 { 58 return false; 59 } 60 } 61 62 }; 63 64 template <typename T> 65 Complex<T>::Complex(T shi, T xu):real(shi),imag(xu){} 66 67 template <typename T> 68 Complex<T>::Complex(const Complex& c) 69 { 70 real = c.real; 71 imag = c.imag; 72 } 73 74 template <typename T> 75 T Complex<T>::get_real() 76 { 77 return real; 78 } 79 80 template <typename T> 81 T Complex<T>::get_imag() 82 { 83 return imag; 84 }View Code
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 }View Code
task6:
date.h:
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 };View Code
date.cpp:
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 }View Code
accumulator.h:
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 };View Code
account.h:
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 };View Code
account.cpp:
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 }View Code
task6.cpp:
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 }View Code
改进:使用了运算符重载,更加直观,使用了虚函数,实现运行时多态,可以直接用循环来输出,方便快捷;用户的输入窗口比较简陋,可以改进一下,使其更加美观;还有一些信息不全面,银行系统还应有预约等操作
标签:const,string,double,c++,实验,date,include,cout From: https://www.cnblogs.com/jiangyuhui/p/18582323