实验五
任务一
代码
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 } 100 101 102 #include "publisher.hpp" 103 #include <vector> 104 #include <typeinfo> 105 106 using std::vector; 107 108 void test() { 109 vector<Publisher *> v; 110 111 v.push_back(new Book("Harry Potter", "J.K. Rowling")); 112 v.push_back(new Film("The Godfather", "Francis Ford Coppola")); 113 v.push_back(new Music("Blowing in the wind", "Bob Dylan")); 114 115 for(auto &ptr: v) { 116 cout << "pointer type: " << typeid(ptr).name() << endl; // 输出指针类型 117 cout << "RTTI type: " << typeid(*ptr).name() << endl; // 输出指针指向的对象类型 118 ptr->publish(); 119 ptr->use(); 120 cout << endl; 121 } 122 } 123 124 int main() { 125 test(); 126 }
截图
任务二
代码
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 } 47 48 49 #pragma once 50 51 #include "book.hpp" 52 #include <iostream> 53 #include <string> 54 #include <iomanip> 55 56 using std::string; 57 using std::cout; 58 using std::endl; 59 using std::setw; 60 61 class BookSale { 62 public: 63 BookSale(const Book &b, float price, int amount); 64 int get_amount() const; 65 66 friend ostream& operator<<(ostream &out, const BookSale &item); 67 68 private: 69 Book rb; 70 float sales_price; // 售价 71 int sales_amount; // 销售数量 72 float revenue; // 营收 73 }; 74 75 // 成员函数实现 76 BookSale::BookSale(const Book &b, float price, int amount): rb{b}, sales_price(price), sales_amount{amount} { 77 revenue = sales_amount * sales_price; 78 } 79 80 int BookSale::get_amount() const { 81 return sales_amount; 82 } 83 84 // 友元函数实现 85 ostream& operator<<(ostream &out, const BookSale &item) { 86 out << left; 87 out << item.rb << endl 88 << setw(15) << "售价:" << item.sales_price << endl 89 << setw(15) << "销售数量:" << item.sales_amount << endl 90 << setw(15) << "营收:" << item.revenue; 91 92 return out; 93 } 94 95 96 #include "booksale.hpp" 97 #include <iostream> 98 #include <string> 99 #include <vector> 100 #include <algorithm> 101 102 // 按图书销售数额比较 103 bool compare_by_amount(const BookSale &x1, const BookSale &x2) { 104 return x1.get_amount() > x2.get_amount(); 105 } 106 107 void test() { 108 using namespace std; 109 110 vector<BookSale> sales_lst; // 存放图书销售记录 111 112 int books_number; 113 cout << "录入图书数量: "; 114 cin >> books_number; 115 116 cout << "录入图书销售记录" << endl; 117 for(int i = 0; i < books_number; ++i) { 118 string name, author, translator, isbn; 119 float price; 120 cout << string(20, '-') << "第" << i+1 << "本图书信息录入" << string(20, '-') << endl; 121 cout << "录入书名: "; cin >> name; 122 cout << "录入作者: "; cin >> author; 123 cout << "录入译者: "; cin >> translator; 124 cout << "录入isbn: "; cin >> isbn; 125 cout << "录入定价: "; cin >> price; 126 127 Book book(name, author, translator, isbn, price); 128 129 float sales_price; 130 int sales_amount; 131 132 cout << "录入售价: "; cin >> sales_price; 133 cout << "录入销售数量: "; cin >> sales_amount; 134 135 BookSale record(book, sales_price, sales_amount); 136 sales_lst.push_back(record); 137 } 138 139 // 按销售册数排序 140 sort(sales_lst.begin(), sales_lst.end(), compare_by_amount); 141 142 // 按销售册数降序输出图书销售信息 143 cout << string(20, '=') << "图书销售统计" << string(20, '=') << endl; 144 for(auto &t: sales_lst) { 145 cout << t << endl; 146 cout << string(40, '-') << endl; 147 } 148 } 149 150 int main() { 151 test(); 152 }
截图
任务三
代码
1 #include <iostream> 2 #include <vector> 3 #include<string> 4 5 using namespace std; 6 7 class MachinePets{ 8 public: 9 MachinePets(const string &s = ""); 10 const string get_nickname(); 11 virtual string talk() = 0; 12 13 private: 14 string nickname; 15 }; 16 17 MachinePets::MachinePets(const string &s): nickname{s}{} 18 const string MachinePets::get_nickname(){ 19 return this->nickname; 20 } 21 22 23 class PetCats: public MachinePets{ 24 public: 25 PetCats(const string &s = ""); 26 string talk() override; 27 28 }; 29 30 PetCats::PetCats(const string &s): MachinePets(s){} 31 string PetCats::talk(){ 32 return "miao wu~"; 33 } 34 35 36 37 class PetDogs: public MachinePets{ 38 public: 39 PetDogs(const string &s = ""); 40 string talk() override; 41 42 }; 43 44 PetDogs::PetDogs(const string &s): MachinePets(s){} 45 string PetDogs::talk(){ 46 return "wang wang~"; 47 } 48 49 50 #include <iostream> 51 #include <vector> 52 #include "pets.hpp" 53 54 void test() { 55 using namespace std; 56 57 vector<MachinePets *> pets; 58 59 pets.push_back(new PetCats("miku")); 60 pets.push_back(new PetDogs("da huang")); 61 62 for(auto &ptr: pets) 63 cout <<ptr->get_nickname() << " says " << ptr->talk() << endl; 64 } 65 66 int main() { 67 test(); 68 }
截图
任务四
代码
1 #pragma once 2 #pragma once 3 #include <string> 4 #include <iostream> 5 #include <iomanip> 6 using std::string; 7 using std::ostream; 8 using std::istream; 9 using std::endl; 10 using std::setw; 11 using std::left; 12 using std::cout; 13 14 class Film{ 15 private: 16 string name; 17 string director; 18 string country; 19 string year; 20 public: 21 friend istream& operator>>(istream& in,Film& film ); 22 friend ostream& operator<<(ostream& cout ,const Film& film); 23 string get_year() const; 24 25 26 }; 27 28 istream& operator >>(istream& in,Film& film){ 29 cout<<"录入片名:"; 30 in>>film.name; 31 cout<<"录入导演名:"; 32 in>>film.director; 33 cout<<"录入制片国家:"; 34 in>>film.country; 35 cout<<"录入上映年份:"; 36 in >>film.year; 37 return in; 38 39 } 40 ostream& operator <<(ostream& out,const Film& film ) 41 { 42 out<<left; 43 out<<setw(20)<<film.name<<setw(10)<<film.director<<setw(10)<<film.country<<setw(10)<<film.year; 44 return out; 45 46 } 47 string Film::get_year() const{ 48 return year; 49 } 50 //升序 51 bool compare_by_year(const Film& f1, const Film& f2) { 52 return f1.get_year() < f2.get_year(); 53 }
截图
任务五
代码
1 #pragma once 2 #include<iostream> 3 using namespace std; 4 5 template<typename T> 6 class Complex{ 7 public: 8 Complex(const T& r=0,const T& l=0): 9 real{r},imag{l}{} 10 Complex operator +=(const Complex &c) 11 { 12 real+=c.real; 13 imag+=c.imag; 14 return *this; 15 } 16 17 friend Complex operator+(const Complex &c1,const Complex &c2) 18 { 19 return Complex<T>(c1.real+c2.real,c1.imag+c2.imag); 20 } 21 22 friend bool operator==(const Complex 23 &c1,const Complex &c2) 24 { 25 return c1.real==c2.real&&c1.imag==c2.imag; 26 } 27 28 friend ostream& operator<<(ostream& out,const Complex& c) 29 { 30 if(c.imag>=0) 31 out<<c.real<<"+"<<c.imag<<"i"; 32 else 33 out<<c.real<<"-"<<-c.imag<<"i"; 34 return out; 35 } 36 37 friend istream& operator>>(istream& in,Complex& c) 38 { 39 in>>c.real>>c.imag; 40 return in; 41 } 42 43 T get_real() const{return real;} 44 T get_imag() const{return imag;} 45 46 private: 47 T real; 48 T imag; 49 };
截图
任务六
代码
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 } 27 28 29 30 #include"date.h" 31 #include<iostream> 32 #include<cstdlib> 33 using namespace std; 34 namespace { 35 const int DAYS_BEFIRE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304 ,334,365 }; 36 } 37 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 38 if (day <= 0 || day > getMaxDay()) { 39 cout << "Invalid date: "; 40 show(); 41 cout << endl; 42 exit(1); 43 } 44 int years = year - 1; 45 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFIRE_MONTH[month - 1] + day; 46 if (isLeapYear() && month > 2) totalDays++; 47 } 48 int Date::getMaxDay()const { 49 if (isLeapYear() && month == 2) 50 return 29; 51 else return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1]; 52 } 53 void Date::show()const { 54 cout << getYear() << "-" << getMonth() << "-" << getDay(); 55 } 56 57 58 #pragma once 59 #include"date.h" 60 #include"accumulator.h" 61 #include<string> 62 using namespace std; 63 class Account { 64 private: 65 string id; 66 double balance; 67 static double total; 68 protected: 69 Account(const Date& date, const string& id); 70 void record(const Date& date, double amount, const string& desc); 71 void error(const string& msg) const; 72 public:const string& getId() { return id; } 73 double getBalance()const { return balance; } 74 static double getTotal() { return total; } 75 virtual void deposit(const Date& date, double amount, const string& desc) = 0; 76 virtual void withdraw(const Date& date, double amount, const string& desc) = 0; 77 virtual void settle(const Date& date) = 0; 78 virtual void show()const; 79 }; 80 class SavingsAccount :public Account { 81 private: 82 Accumulator acc; 83 double rate; 84 public: 85 SavingsAccount(const Date& date, const string& id, double rate); 86 double getRate() const { return rate; } 87 void deposit(const Date& date, double amount, const string& desc); 88 void withdraw(const Date& date, double amount, const string& desc); 89 void settle(const Date& date); 90 }; 91 class CreditAccount :public Account { 92 private: 93 Accumulator acc; 94 double credit; 95 double rate; 96 double fee; 97 double getDebt()const { 98 double balance = getBalance(); 99 return(balance < 0 ? balance : 0); 100 } 101 public:CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); 102 double getCredit()const { return credit; } 103 double getRate()const { return rate; } 104 double getFee() const { return fee; } 105 double getAvailableCredit()const { 106 if (getBalance() < 0)return credit + getBalance(); 107 else return credit; 108 } 109 void deposit(const Date& date, double amount, const string& desc); 110 void withdraw(const Date& date, double amount, const string& desc); 111 void settle(const Date& date); 112 void show()const; 113 }; 114 115 116 #include "account.h" 117 #include <cmath> 118 #include<iostream> 119 using namespace std; 120 double Account::total = 0; 121 Account::Account(const Date& date, const string& id) :id(id), balance(0) { 122 date.show(); 123 cout << "\t#" << id << "created" << endl; 124 } 125 void Account::record(const Date& date, double amount, const string& desc) { 126 amount = floor(amount * 100 + 0.5) / 100; 127 balance += amount; 128 total += amount; 129 date.show(); 130 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 131 } 132 void Account::show()const { cout << id << "\tBalance:" << balance; } 133 void Account::error(const string& msg)const { 134 cout << "Error(#" << id << "):" << msg << endl; 135 } 136 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :Account(date, id), rate(rate), acc(date, 0) {} 137 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 138 record(date, amount, desc); 139 acc.change(date, getBalance()); 140 } 141 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 142 if (amount > getBalance()) { 143 error("not enough money"); 144 } 145 else { 146 record(date, -amount, desc); 147 acc.change(date, getBalance()); 148 } 149 } 150 void SavingsAccount::settle(const Date& date) { 151 if (date.getMonth() == 1) { 152 double interest = acc.getSum(date) * rate / (date - Date(date.getYear() - 1, 1, 1)); 153 if (interest != 0) record(date, interest, "interest"); 154 acc.reset(date, getBalance()); 155 } 156 } 157 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) {} 158 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 159 record(date, amount, desc); 160 acc.change(date, getDebt()); 161 } 162 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 163 if (amount - getBalance() > credit) { 164 error("not enouogh credit"); 165 } 166 else { 167 record(date, -amount, desc); 168 acc.change(date, getDebt()); 169 } 170 } 171 void CreditAccount::settle(const Date& date) { 172 double interest = acc.getSum(date) * rate; 173 if (interest != 0) record(date, interest, "interest"); 174 if (date.getMonth() == 1)record(date, -fee, "annual fee"); 175 acc.reset(date, getDebt()); 176 } 177 void CreditAccount::show()const { 178 Account::show(); 179 cout << "\tAvailable credit:" << getAvailableCredit(); 180 } 181 182 183 #include"account.h" 184 #include<iostream> 185 using namespace std; 186 int main() { 187 Date date(2008, 11, 1); 188 SavingsAccount sa1(date, "S3755217", 0.015); 189 SavingsAccount sa2(date, "02342342", 0.015); 190 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 191 Account* accounts[] = { &sa1,&sa2,&ca }; 192 const int n = sizeof(accounts) / sizeof(Account*); 193 cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl; 194 char cmd; 195 do { 196 date.show(); 197 cout << "\tTotal:" << Account::getTotal() << "\tcommand>"; 198 int index, day; 199 double amount; 200 string desc; 201 cin >> cmd; 202 switch (cmd) { 203 case 'd': 204 cin >> index >> amount; 205 getline(cin, desc); 206 accounts[index]->deposit(date, amount, desc); 207 break; 208 209 case 'w': 210 cin >> index >> amount; 211 getline(cin, desc); 212 accounts[index]->withdraw(date, amount, desc); 213 break; 214 case 's': 215 for (int i = 0; i < n; i++) { 216 cout << "[" << i << "]"; 217 accounts[i]->show(); 218 cout << endl; 219 } 220 break; 221 222 case 'c': 223 cin >> day; 224 if (day < date.getDay()) { 225 cout << "You cannot specify a previous day"; 226 } 227 else if (day > date.getMaxDay()) 228 cout << "Invalid day"; 229 else date = Date(date.getYear(), date.getMonth(), day); 230 break; 231 case 'n': 232 if (date.getMonth() == 12) 233 date = Date(date.getYear() + 1, 1, 1); 234 else date = Date(date.getYear(), date.getMonth() + 1, 1); 235 for (int i = 0; i < n; i++) { 236 accounts[i]->settle(date); 237 } 238 break; 239 } 240 } while (cmd != 'e'); 241 return 0; 242 } 243 244 245 #pragma once 246 class Date { 247 private: 248 int year; 249 int month; 250 int day; 251 int totalDays; 252 public: 253 Date(int year, int month, int day); 254 int getYear()const { return year; } 255 int getMonth()const { return month; } 256 int getDay()const { return day; } 257 int getMaxDay()const; 258 bool isLeapYear()const { 259 return (year % 4 == 0 && year % 100 != 0 )|| year % 400 == 0; 260 } 261 void show() const; 262 int operator-(const Date& date)const { 263 return totalDays - date.totalDays; 264 } 265 };
截图