实验五
实验任务三
代码
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 #pragma once 2 #include <iostream> 3 #include <string> 4 using std::cout; 5 using std::endl; 6 using std::string; 7 8 // 机器宠物类:MachinePets(抽象类) 9 class MachinePets { 10 public: 11 MachinePets(const string &s = ""); // 构造函数 12 virtual string talk() const = 0; // 纯虚函数,作为接口继承 13 string get_nickname() const; // 获取宠物昵称 14 protected: 15 string nickname; // 宠物的昵称 16 }; 17 18 // MachinePets 构造函数实现 19 MachinePets::MachinePets(const string &s) : nickname{s} { 20 } 21 22 // 获取宠物的昵称 23 string MachinePets::get_nickname() const { 24 return nickname; 25 } 26 27 // 电子宠物猫类:PetCats 28 class PetCats : public MachinePets { 29 public: 30 PetCats(const string &s = ""); // 构造函数 31 string talk() const override; // 实现叫声接口 32 }; 33 34 // PetCats 构造函数实现 35 PetCats::PetCats(const string &s) : MachinePets{s} { 36 } 37 38 // 实现宠物猫的叫声 39 string PetCats::talk() const { 40 return "Meow"; 41 } 42 43 // 电子宠物狗类:PetDogs 44 class PetDogs : public MachinePets { 45 public: 46 PetDogs(const string &s = ""); // 构造函数 47 string talk() const override; // 实现叫声接口 48 }; 49 50 // PetDogs 构造函数实现 51 PetDogs::PetDogs(const string &s) : MachinePets{s} { 52 } 53 54 // 实现宠物狗的叫声 55 string PetDogs::talk() const { 56 return "Woof"; 57 }
编译结果
实验任务四
代码
film.hpp
1 #ifndef FILM_HPP 2 #define FILM_HPP 3 4 #include <iostream> 5 #include <string> 6 using namespace std; 7 8 class Film { 9 private: 10 string title; 11 string director; 12 string country; 13 int year; 14 15 public: 16 Film() : title(""), director(""), country(""), year(0) {} 19 friend istream& operator>>(istream& is, Film& film) { 20 cout << "录入片名: "; 21 is.ignore(); 22 getline(is, film.title); 23 24 cout << "录入导演: "; 25 getline(is, film.director); 26 27 cout << "录入制片国家/地区: "; 28 getline(is, film.country); 29 30 cout << "录入上映年份: "; 31 is >> film.year; 32 33 return is; 34 } 35 37 friend ostream& operator<<(ostream& os, const Film& film) { 38 os << film.title << "\t" << film.director << "\t" << film.country << "\t" << film.year; 39 return os; 40 } 41 43 int get_year() const { 44 return year; 45 } 46 }; 47 49 bool compare_by_year(const Film& a, const Film& b) { 50 return a.get_year() < b.get_year(); 51 } 52 53 #endif // FILM_HPP
film.cpp
1 #include "film.hpp"
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 }
编译结果
实验任务5
代码
complex.hpp
1 #ifndef COMPLEX_HPP 2 #define COMPLEX_HPP 3 4 #include <iostream> 5 #include <cmath> 6 using namespace std; 7 8 template<typename T> 9 class Complex { 10 private: 11 T real; 12 T imag; 13 14 public: 15 Complex(T r = 0, T i = 0) : real(r), imag(i) {} 16 17 T get_real() const { return real; } 18 T get_imag() const { return imag; } 19 20 Complex& operator+=(const Complex& other) { 21 real += other.real; 22 imag += other.imag; 23 return *this; 24 } 25 26 Complex operator+(const Complex& other) const { 27 return Complex(real + other.real, imag + other.imag); 28 } 29 30 bool operator==(const Complex& other) const { 31 return real == other.real && imag == other.imag; 32 } 33 34 friend istream& operator>>(istream& is, Complex& c) { 35 is >> c.real >> c.imag; 36 return is; 37 } 38 39 friend ostream& operator<<(ostream& os, const Complex& c) { 40 os << c.real << (c.imag < 0 ? " - " : " + ") << std::abs(c.imag) << "i"; 41 return os; 42 } 43 }; 44 45 #endif
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 }
编译结果
实验任务6
代码
account.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 }
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 };
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 };
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 }
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 };
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 }
编译结果