实验5c++
实验任务三
pets.hpp
1 #pragma once 2 #include<iostream> 3 #include<string> 4 5 using std::string; 6 using std::cout; 7 class MachinePets { 8 protected: 9 string nickname; 10 public: 11 MachinePets(const string &s):nickname{s}{} 12 string get_nickname()const { return nickname; } 13 virtual string talk() = 0; 14 }; 15 16 class PetCats :public MachinePets{ 17 public: 18 PetCats(const string& s) :MachinePets{ s } {} 19 string talk() { 20 return talk1; 21 } 22 private: 23 string talk1 = "miao wu~"; 24 }; 25 26 class PetDogs :public MachinePets { 27 public: 28 PetDogs(const string& s) :MachinePets{ s } {} 29 string talk() { 30 return talks; 31 } 32 private: 33 string talks = "wang wang"; 34 };
tack.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 }
运行结果截图
实验任务四
film.hpp
1 #pragma once 2 #include<iostream> 3 #include<iomanip> 4 using std::string; 5 using std::ostream; 6 using std::istream; 7 using std::setw; 8 using std::cout; 9 using std::left; 10 11 class Film { 12 private: 13 string name; 14 string director; 15 string area; 16 int year; 17 public: 18 Film() {} 19 // Film(const string n,const string d,const string a,int y):name{n},director{d},area{a},year{y}{} 20 int get_year()const { return year; } 21 friend ostream& operator<<(ostream& out, const Film& f); 22 friend istream& operator>>(istream& in, Film& f); 23 }; 24 25 istream& operator>>(istream& in, Film& f) { 26 cout << "录入片名:"; in >> f.name; 27 cout << "录入导演:"; in >> f.director; 28 cout << "录入制片国家/地区:"; in >> f.area; 29 cout << "录入上映年份:"; in >> f.year; 30 return in; 31 } 32 33 ostream& operator<<(ostream& out, const Film& f) { 34 out << left<<setw(20) << f.name 35 << setw(20) << f.director 36 << setw(20) << f.area 37 << setw(20) << f.year; 38 return out; 39 } 40 41 bool compare_by_year(const Film& x1, const Film& x2) { 42 return x1.get_year() > x2.get_year(); 43 }
task5.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 34 35 36 }
运行结果截图
实验任务五
Complex.hpp
1 #pragma once 2 #include<iostream> 3 4 using namespace std; 5 6 template <typename T> 7 class Complex { 8 public: 9 Complex(T r=0, T i=0) :real{ r }, imag{ i } {} 10 Complex(const Complex& c) { 11 real = c.real; 12 imag = c.imag; 13 } 14 void operator += (Complex &c) { 15 real += c.real; 16 imag += c.imag; 17 } 18 T get_real()const { 19 return real; 20 } 21 T get_imag()const { 22 return imag; 23 } 24 template <typename T> 25 friend Complex<T> operator+(Complex<T> c1, Complex<T> c2); 26 template <typename T> 27 friend bool operator==(Complex<T> c1, Complex<T> c2); 28 template <typename T> 29 friend istream& operator>>(istream& in, Complex<T>& c); 30 template <typename T> 31 friend ostream& operator<<(ostream& out, Complex<T> c); 32 private: 33 T real; 34 T imag; 35 }; 36 37 template <typename T> 38 Complex<T> operator+(Complex<T> c1, Complex<T> c2) { 39 return Complex<T>(c1.real + c2.real, c1.imag + c2.imag); 40 } 41 template <typename T> 42 bool operator==(Complex<T> c1, Complex<T> c2) { 43 if (c1.real == c2.real && c1.imag == c2.imag) 44 return true; 45 else return false; 46 } 47 template <typename T> 48 istream& operator>>(istream& in, Complex<T>& c) { 49 in >> c.real >> c.imag; 50 return in; 51 } 52 53 template <typename T> 54 ostream& operator<<(ostream& out, Complex<T> c) { 55 if (c.imag > 0) 56 out << c.real << " + " << c.imag << "i"; 57 else if (c.imag < 0) 58 out << c.real << " - " << abs(c.imag) << "i"; 59 else 60 out << c.real; 61 return out; 62 }
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 }
运行结果截图
实验任务六
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 DATS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 }; 7 } 8 9 Date::Date(int year, int month, int day) :year{ year }, month{ month }, day{ day } { 10 if (day <= 0 || day > getMaxDay()) { 11 cout << "Invalid date: "; 12 show(); 13 exit(1); 14 } 15 int years = year - 1; 16 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DATS_BEFORE_MONTH[month - 1] + day; 17 if (isLeapYear() && month > 2)totalDays++; 18 } 19 20 int Date::getMaxDay()const { 21 if (isLeapYear() && month == 2) 22 return 29; 23 else 24 return DATS_BEFORE_MONTH[month] - DATS_BEFORE_MONTH[month - 1]; 25 } 26 27 void Date::show()const { 28 cout << getYear() << "-" << getMonth() << "-" << getDay(); 29 }
account.h
1 #pragma once 2 3 #include"date.h" 4 #include"accumulator.h" 5 #include<string> 6 7 class Account { 8 private: 9 std::string id; 10 double balance; 11 static double total; 12 protected: 13 Account(const Date& date, const std::string& id); 14 void record(const Date& date, double amount, const std::string& desc); 15 void error(const std::string& msg)const; 16 17 18 public: 19 20 const std::string& getId()const { return id; } 21 double getBalance()const { return balance; } 22 static double getTotal() { return total; } 23 virtual void deposit(const Date& date, double amount, const std::string& desc) = 0; 24 virtual void withdraw(const Date& date, double amount, const std::string& desc) = 0; 25 virtual void settle(const Date& date) = 0; 26 virtual void show()const; 27 }; 28 29 class SavingAccount :public Account { 30 private: 31 Accumulator acc; 32 double rate; 33 public: 34 SavingAccount(const Date& date, const std::string& id, double rate); 35 double getRate()const { return rate; } 36 void deposit(const Date& date, double amount, const std::string& desc); 37 void withdraw(const Date& date, double amount, const std::string& desc); 38 void settle(const Date& date); 39 }; 40 class CreditAccount :public Account { 41 private: 42 Accumulator acc; 43 double credit; 44 double rate; 45 double fee; 46 double getDebt()const { 47 double balance = getBalance(); 48 return (balance < 0 ? balance : 0); 49 } 50 public: 51 CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee); 52 double getCredit()const { return credit; } 53 double getRate() const { return rate; } 54 double getFee() const { return fee; } 55 double getAvsilbilableCredit() const { 56 if (getBalance() < 0) 57 return credit + getBalance(); 58 else 59 return credit; 60 } 61 void deposit(const Date& date, double amount, const std::string& desc); 62 void withdraw(const Date& date, double amount, const std::string& desc); 63 void settle(const Date& date); 64 void show()const; 65 };
account.cpp
1 #include"account.h" 2 #include<cmath> 3 #include<iostream> 4 using namespace std; 5 double Account::total = 0; 6 7 Account::Account(const Date& date, const string& id) :id{ id }, balance{ 0 } { 8 date.show(); 9 cout << "\t#" << id << "created" << endl; 10 } 11 void Account::record(const Date& date, double amount, const string& desc) { 12 amount = floor(amount * 100 + 0.5) / 100; 13 balance += amount; 14 total += amount; 15 date.show(); 16 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 17 } 18 void Account::show()const { 19 cout << id << "\tBalance:" << balance; 20 } 21 void Account::error(const string& msg)const { 22 cout << "Error(#" << id << "): " << msg << endl; 23 } 24 25 SavingAccount::SavingAccount(const Date& date, const string& id, double rate) :Account{ date,id }, rate{ rate }, acc{ date,0 } {} 26 void SavingAccount::deposit(const Date& date, double amount, const string& desc) { 27 record(date, amount, desc); 28 acc.change(date, getBalance()); 29 } 30 void SavingAccount::withdraw(const Date& date, double amount, const string& desc) { 31 if (amount > getBalance()) 32 error("not enough money"); 33 else 34 record(date, -amount, desc); 35 acc.change(date, getBalance()); 36 } 37 38 void SavingAccount::settle(const Date& date) { 39 if (date.getMonth() == 1) { 40 double interest = acc.getSum(date) * rate / (date - Date(date.getYear() - 1, 1, 1)); 41 if (interest != 0) 42 record(date, interest, "interest"); 43 acc.reset(date, getBalance()); 44 45 } 46 } 47 48 49 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 } {} 50 51 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 52 record(date, amount, desc); 53 acc.change(date, getDebt()); 54 } 55 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 56 if (amount - getBalance() > credit) 57 error("not enough money"); 58 else 59 record(date, -amount, desc); 60 acc.change(date, getDebt()); 61 } 62 63 void CreditAccount::settle(const Date& date) { 64 double interest = acc.getSum(date) * rate; 65 if (interest != 0) 66 record(date, interest, "interest"); 67 if (date.getMonth() == 1) 68 record(date, -fee, "annual fee"); 69 acc.reset(date, getDebt()); 70 71 } 72 void CreditAccount::show()const { 73 Account::show(); 74 cout << "\tAvailable credit:" << getAvsilbilableCredit(); 75 }
accumulator.h
1 #pragma once 2 #include"date.h" 3 class Accumulator { 4 private: 5 Date lastDate; 6 double value; 7 double sum; 8 public: 9 Accumulator(const Date& date, double value) : 10 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 = value; 22 sum = 0; 23 } 24 };
运行结果截图