实验5
实验任务3:
1 #pragma once 2 #include<iostream> 3 using namespace std; 4 class MachinePets{ 5 string nickname; 6 public: 7 MachinePets(const string &s){ 8 nickname=s; 9 } 10 string get_nickname()const{ 11 return nickname; 12 } 13 virtual string talk()const=0; 14 }; 15 class PetCats:public MachinePets{ 16 public: 17 PetCats(const string &s):MachinePets(s){ 18 19 } 20 string talk()const override{ 21 return "miao wu~"; 22 } 23 }; 24 class PetDogs:public MachinePets{ 25 public: 26 PetDogs(const string &s):MachinePets(s){ 27 28 } 29 string talk()const override{ 30 return "wang wang~"; 31 } 32 };
实验任务4:
1 #pragma once 2 #include<iostream> 3 #include<iomanip> 4 using namespace std; 5 class Film{ 6 string name; 7 string director; 8 string country; 9 string time; 10 11 public: 12 Film(const string& name="",const string& director="",const string& country="",const string& time=""){ 13 this->name=name;this->director=director;this->country=country;this->time=time; 14 } 15 friend ostream& operator<<(ostream& out,const Film& f); 16 friend istream& operator>>(istream& in,Film& f); 17 string get_year()const{ 18 return time; 19 } 20 }; 21 ostream& operator<<(ostream& out,const Film &f){ 22 out<<f.name<<setw(15)<<f.director<<setw(10)<<f.country<<setw(10)<<f.time<<endl; 23 return out; 24 } 25 istream& operator>>(istream& in,Film &f){ 26 in>>f.name>>f.director>>f.country>>f.time; 27 return in; 28 } 29 bool compare_by_year(const Film &f1,const Film &f2){ 30 return f1.get_year()<f2.get_year(); 31 }
实验任务5:
1 #pragma once 2 #include<iostream> 3 using namespace std; 4 template<typename T> 5 class Complex{ 6 T real; 7 T imag; 8 public: 9 Complex(const T &r=0,const T &i=0){ 10 real=r; 11 imag=i; 12 } 13 Complex operator+=(const Complex &c){ 14 real+=c.real; 15 imag+=c.imag; 16 return *this; 17 } 18 friend Complex operator+(const Complex &c1,const Complex &c2){ 19 return Complex<T>(c1.real+c2.real,c1.imag+c2.imag); 20 } 21 friend bool operator==(const Complex &c1,const Complex &c2){ 22 return c1.real==c2.real&&c1.imag==c2.imag; 23 } 24 friend ostream& operator<<(ostream &out,const Complex &c){ 25 if(c.imag>=0){ 26 out<<c.real<<"+"<<c.imag<<"i"; 27 } 28 else 29 out<<c.real<<"-"<<-c.imag<<"i"; 30 return out; 31 } 32 friend istream &operator>>(istream &in,Complex &c){ 33 in>>c.real>>c.imag; 34 return in; 35 } 36 T get_real(){ 37 return real; 38 } 39 T get_imag(){ 40 return imag; 41 } 42 };
实验任务6:
1 #pragma once 2 class Date { 3 private: 4 int year; 5 int month; 6 int day; 7 int totalDays; 8 public: 9 Date(int year, int month, int day); 10 int getYear()const { return year; } 11 int getMonth()const { return month; } 12 int getDay()const { return day; } 13 int getMaxDay()const; 14 bool isLeapYear()const { 15 return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; 16 } 17 void show()const; 18 int operator-(const Date& date)const { 19 return totalDays - date.totalDays; 20 } 21 }; 22 23 24 25 #pragma once 26 #include"date.h" 27 class Accumulator { 28 private: 29 Date lastDate; 30 double value; 31 double sum; 32 public: 33 Accumulator(const Date& date, double value) :lastDate(date), value(value), sum{ 0 } {} 34 double getSum(const Date& date) const{ 35 return sum + value * (date - lastDate); 36 } 37 void change(const Date& date, double value) { 38 sum = getSum(date); 39 lastDate = date; 40 this->value = value; 41 } 42 void reset(const Date& date, double value) { 43 lastDate = date; 44 this->value; 45 sum = 0; 46 } 47 }; 48 49 50 51 #pragma once 52 #include"date.h" 53 #include"accumulator.h" 54 #include<string> 55 using namespace std; 56 class Account { 57 private: 58 string id; 59 double balance; 60 static double total; 61 protected: 62 Account(const Date& date, const string &id); 63 void record(const Date& date, double amoount, const string& desc); 64 void error(const string& msg) const; 65 public:const string& getId() { return id; } 66 double getBalance()const { return balance; } 67 static double getTotal() { return total; } 68 virtual void deposit(const Date& date, double amount, const string& desc) = 0; 69 virtual void withdraw(const Date& date, double amount, const string& desc) = 0; 70 virtual void settle(const Date& date) = 0; 71 virtual void show()const; 72 }; 73 class SavingsAccount :public Account { 74 private: 75 Accumulator acc; 76 double rate; 77 public: 78 SavingsAccount(const Date& date, const string& id, double rate); 79 double getRate() const { return rate; } 80 void deposit(const Date& date, double amount, const string& desc); 81 void withdraw(const Date& date, double amount, const string& desc); 82 void settle(const Date& date); 83 }; 84 class CreditAccount :public Account { 85 private: 86 Accumulator acc; 87 double credit; 88 double rate; 89 double fee; 90 double getDebt()const { 91 double balance = getBalance(); 92 return(balance < 0 ? balance : 0); 93 } 94 public:CreditAccount(const Date& date, const string& id, double credit, double rate, double fee); 95 double getCredit()const { return credit; } 96 double getRate()const { return rate; } 97 double getFee() const { return fee; } 98 double getAvailableCredit()const { 99 if (getBalance() < 0)return credit + getBalance(); 100 else return credit; 101 } 102 void deposit(const Date& date, double amount, const string& desc); 103 void withdraw(const Date& date, double amount, const string& desc); 104 void settle(const Date& date); 105 void show()const; 106 }; 107 108 109 110 #include"date.h" 111 #include<iostream> 112 #include<cstdlib> 113 using namespace std; 114 namespace { 115 const int DAYS_BEFIRE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304 ,334,365 }; 116 } 117 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 118 if (day <= 0 || day > getMaxDay()) { 119 cout << "Invalid date: "; 120 show(); 121 cout << endl; 122 exit(1); 123 } 124 int years = year - 1; 125 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFIRE_MONTH[month - 1] + day; 126 if (isLeapYear() && month > 2) totalDays++; 127 } 128 int Date::getMaxDay()const { 129 if (isLeapYear() && month == 2) 130 return 29; 131 else return DAYS_BEFIRE_MONTH[month] - DAYS_BEFIRE_MONTH[month - 1]; 132 } 133 void Date::show()const { 134 cout << getYear() << "-" << getMonth() << "-" << getDay(); 135 } 136 137 138 139 #include "account.h" 140 #include <cmath> 141 #include<iostream> 142 using namespace std; 143 double Account::total = 0; 144 Account::Account(const Date& date, const string& id) :id(id), balance(0) { 145 date.show(); 146 cout << "\t#" << id << "created" << endl; 147 } 148 void Account::record(const Date& date, double amount, const string& desc) { 149 amount = floor(amount * 100 + 0.5) / 100; 150 balance += amount; 151 total += amount; 152 date.show(); 153 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 154 } 155 void Account::show()const { cout << id << "\tBalance:" << balance; } 156 void Account::error(const string& msg)const { 157 cout << "Error(#" << id << "):" << msg << endl; 158 } 159 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :Account(date, id), rate(rate), acc(date, 0) {} 160 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 161 record(date, amount, desc); 162 acc.change(date, getBalance()); 163 } 164 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 165 if (amount > getBalance()) { 166 error("not enough money"); 167 } 168 else { 169 record(date, -amount, desc); 170 acc.change(date, getBalance()); 171 } 172 } 173 void SavingsAccount::settle(const Date& date) { 174 double interest = acc.getSum(date) * rate / (date-Date(date.getYear() - 1, 1, 1)); 175 if (interest != 0)record(date, interest, "interest"); 176 acc.reset(date, getBalance()); 177 } 178 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) {} 179 void CreditAccount::deposit(const Date& date, double amount, const string& desc) { 180 record(date, amount, desc); 181 acc.change(date, getDebt()); 182 } 183 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) { 184 if (amount - getBalance() > credit) { 185 error("not enouogh credit"); 186 } 187 else { 188 record(date, -amount, desc); 189 acc.change(date, getDebt()); 190 } 191 } 192 void CreditAccount::settle(const Date& date) { 193 double interest = acc.getSum(date) * rate; 194 if (interest != 0) record(date, interest, "interest"); 195 if (date.getMonth() == 1)record(date, -fee, "annual fee"); 196 acc.reset(date, getDebt()); 197 } 198 void CreditAccount::show()const { 199 Account::show(); 200 cout << "\tAvailable credit:" << getAvailableCredit(); 201 } 202 203 204 #include"account.h" 205 #include<iostream> 206 using namespace std; 207 int main() { 208 Date date(2008, 11, 1); 209 SavingsAccount sa1(date, "S3755217", 0.015); 210 SavingsAccount sa2(date, "02342342", 0.015); 211 CreditAccount ca(date, "C5392394", 10000, 0.0005, 50); 212 Account* accounts[] = { &sa1,&sa2,&ca }; 213 const int n = sizeof(accounts) / sizeof(Account*); 214 cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl; 215 char cmd; 216 do { 217 date.show(); 218 cout << "\tTotal:" << Account::getTotal() << "\tcommand>"; 219 int index, day; 220 double amount; 221 string desc; 222 cin >> cmd; 223 switch (cmd) { 224 case 'd': 225 cin >> index >> amount; 226 getline(cin, desc); 227 accounts[index]->deposit(date, amount, desc); 228 break; 229 230 case 'w': 231 cin >> index >> amount; 232 getline(cin, desc); 233 accounts[index]->withdraw(date, amount, desc); 234 break; 235 case 's': 236 for (int i = 0; i < n; i++) { 237 cout << "[" << i << "]"; 238 accounts[i]->show(); 239 cout << endl; 240 } 241 break; 242 243 case 'c': 244 cin >> day; 245 if (day < date.getDay()) { 246 cout << "You cannot specify a previous day"; 247 } 248 else if (day > date.getMaxDay()) 249 cout << "Invalid day"; 250 else date = Date(date.getYear(), date.getMonth(), day); 251 break; 252 case 'n': 253 if (date.getMonth() == 12) 254 date = Date(date.getYear() + 1, 1, 1); 255 else date = Date(date.getYear(), date.getMonth() + 1, 1); 256 for (int i = 0; i < n; i++) { 257 accounts[i]->settle(date); 258 } 259 break; 260 } 261 } while (cmd != 'e'); 262 return 0; 263 }