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