实验5
实验任务3
1 #pragma once 2 #include <string> 3 #include <iostream> 4 5 class MachinePets { 6 public: 7 MachinePets(const std::string& s); 8 virtual ~MachinePets() = default; 9 10 const std::string& get_nickname() const; 11 12 virtual std::string talk() const = 0; 13 14 protected: 15 std::string nickname; 16 }; 17 18 class PetCats : public MachinePets { 19 public: 20 PetCats(const std::string& s); 21 ~PetCats() override = default; 22 23 std::string talk() const override; 24 }; 25 26 class PetDogs : public MachinePets { 27 public: 28 PetDogs(const std::string& s); 29 ~PetDogs() override = default; 30 31 std::string talk() const override; 32 }; 33 34 MachinePets::MachinePets(const std::string& s) : nickname(s) {} 35 36 const std::string& MachinePets::get_nickname() const { 37 return nickname; 38 } 39 40 PetCats::PetCats(const std::string& s) : MachinePets(s) {} 41 42 std::string PetCats::talk() const { 43 return "miao wu~"; 44 } 45 46 PetDogs::PetDogs(const std::string& s) : MachinePets(s) {} 47 48 std::string PetDogs::talk() const { 49 return "wang wang~"; 50 }
1 #include <iostream> 2 #include "pets.hpp" 3 void play(MachinePets& obj) { 4 std::cout << obj.get_nickname() << " says " << obj.talk() << std::endl; 5 } 6 void test() { 7 PetCats cat("miku"); 8 PetDogs dog("da huang"); 9 play(cat); 10 play(dog); 11 } 12 int main() { 13 test(); 14 }
实验任务4
1 #pragma once 2 #include <iostream> 3 #include <string> 4 5 class Person { 6 public: 7 Person(); 8 Person(const std::string& name, const std::string& telephone, const std::string& email = ""); 9 10 11 Person(const Person& other); 12 13 14 void update_telephone(); 15 void update_email(); 16 17 friend std::ostream& operator<<(std::ostream& os, const Person& p); 18 friend std::istream& operator>>(std::istream& is, Person& p); 19 friend bool operator==(const Person& p1, const Person& p2); 20 21 private: 22 std::string name; 23 std::string telephone; 24 std::string email; 25 }; 26 27 Person::Person() : name(""), telephone(""), email("") {} 28 29 Person::Person(const std::string& name, const std::string& telephone, const std::string& email) 30 : name(name), telephone(telephone), email(email) {} 31 32 Person::Person(const Person& other) : name(other.name), telephone(other.telephone), email(other.email) {} 33 34 void Person::update_telephone() { 35 std::cout << "Enter new telephone number: "; 36 std::cin >> telephone; 37 } 38 39 void Person::update_email() { 40 std::cout << "Enter new email address: "; 41 std::cin >> email; 42 } 43 44 std::ostream& operator<<(std::ostream& os, const Person& p) { 45 os << "Name: " << p.name << "\nTelephone: " << p.telephone << "\nEmail: " << p.email << "\n"; 46 return os; 47 } 48 49 std::istream& operator>>(std::istream& is, Person& p) { 50 std::cout << "Enter name: "; 51 is >> p.name; 52 std::cout << "Enter telephone number: "; 53 is >> p.telephone; 54 std::cout << "Enter email address: "; 55 is >> p.email; 56 return is; 57 } 58 59 bool operator==(const Person& p1, const Person& p2) { 60 return (p1.name == p2.name) && (p1.telephone == p2.telephone); 61 }
1 #include <iostream> 2 #include <vector> 3 #include "Person.hpp" 4 void test() { 5 using namespace std; 6 vector<Person> phone_book; 7 Person p; 8 cout << "输入一组联系人的联系方式,E直至按下Ctrl+Z终止\n"; 9 while (cin >> p) 10 phone_book.push_back(p); 11 cout << "\n更新phone_book中索引为0的联系人的手机号、邮箱:\n"; 12 phone_book.at(0).update_telephone(); 13 phone_book.at(0).update_email(); 14 cout << "\n测试两个联系人是否是同一个:\n"; 15 cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl; 16 } 17 int main() { 18 test(); 19 }
实验任务5
1 #pragma once 2 #include<iostream> 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 };
1 #include"date.h" 2 #include<iostream> 3 #include<cstdlib> 4 using namespace std; 5 namespace{ 6 const int DAYS_BEFORE_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_BEFORE_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 23 return DAYS_BEFORE_MONTH[month]-DAYS_BEFORE_MONTH[month-1]; 24 } 25 void Date::show() const{ 26 cout << getYear() << "-" << getMonth() << "-" <<getDay(); 27 }
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;this->value=value; 17 } 18 void reset(const Date &date,double value) { 19 lastDate=date;this->value=value;sum=0; 20 } 21 };
1 #pragma once 2 #include"date.h" 3 #include"accumulator.h" 4 #include<string> 5 6 class Account{ 7 private: 8 std::string id; 9 double balance; 10 static double total; 11 protected: 12 Account(const Date &date,const std::string &id); 13 void record(const Date &date,double amount,const std::string &desc); 14 void error(const std::string &msg) const; 15 public: 16 const std::string &getId() const {return id;}; 17 double getBalance() const {return balance;} 18 static double getTotal() {return total;} 19 virtual void deposit(const Date &date,double amount,const std::string &desc)=0; 20 virtual void withdraw(const Date &date,double amount,const std::string &desc)=0; 21 virtual void settle(const Date &date)=0; 22 virtual void show() const; 23 }; 24 25 class SavingsAccount : public Account{ 26 private: 27 Accumulator acc; 28 double rate; 29 public: 30 SavingsAccount(const Date &date,const std::string &id,double rate); 31 double getRate() const {return rate;} 32 void deposit(const Date &date,double amount,const std::string &desc); 33 void withdraw(const Date &date,double amount,const std::string &desc); 34 void settle (const Date &date); 35 }; 36 37 class CreditAccount : public Account{ 38 private: 39 Accumulator acc; 40 double credit; 41 double rate; 42 double fee; 43 double getDebt() const { 44 double balance=getBalance(); 45 return (balance<0 ? balance : 0); 46 } 47 public: 48 CreditAccount(const Date &date,const std::string &id,double credit,double rate,double fee); 49 double getCredit() const {return credit;} 50 double getRate() const {return rate;} 51 double getFee() const {return fee;} 52 double getAvailableCredit() const{ 53 if(getBalance() < 0) 54 return credit+getBalance(); 55 else 56 return credit; 57 } 58 void deposit(const Date &date,double amount,const std::string &desc); 59 void withdraw(const Date &date,double amount,const std::string &desc); 60 void settle(const Date &date); 61 void show() const; 62 };
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) 8 :id(id),balance(0) { 9 date.show();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;total+=amount; 14 date.show(); 15 cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl; 16 } 17 void Account::show() const {cout << id << "\tBalance:" << balance;} 18 void Account::error(const string &msg) const { 19 cout << "Error( #"<<id<<"):"<< msg << endl; 20 } 21 22 SavingsAccount::SavingsAccount(const Date &date,const string &id,double rate) 23 :Account(date,id),rate(rate),acc(date,0) {} 24 void SavingsAccount::deposit(const Date &date,double amount,const string &desc) { 25 record(date,amount,desc); 26 acc.change(date,getBalance()); 27 } 28 void SavingsAccount::withdraw(const Date &date,double amount,const string &desc) { 29 if(amount>getBalance()) { 30 error("not enough money"); 31 }else { 32 record(date,-amount,desc); 33 acc.change(date,getBalance()); 34 } 35 } 36 void SavingsAccount::settle(const Date &date) { 37 if(date.getMonth()==1) { 38 double interest=acc.getSum(date) * rate 39 /(date-Date(date.getYear()-1,1,1)); 40 if(interest !=0) 41 record(date,interest,"interest"); 42 acc.reset(date,getBalance()); 43 } 44 } 45 46 CreditAccount::CreditAccount(const Date& date,const string& id,double credit, double rate, double fee) 47 :Account(date,id),credit(credit),rate(rate),fee(fee),acc(date,0) {} 48 void CreditAccount::deposit(const Date &date,double amount, const string &desc) { 49 record(date,amount,desc); 50 acc.change(date,getDebt()); 51 } 52 void CreditAccount::withdraw(const Date &date,double amount,const string &desc) { 53 if(amount-getBalance()>credit) { 54 error("not enough credit"); 55 }else{ 56 record(date,-amount,desc); 57 acc.change(date,getDebt()); 58 } 59 } 60 void CreditAccount::settle(const Date &date) { 61 double interest=acc.getSum(date)*rate; 62 if(interest !=0) record (date,interest,"interest"); 63 if(date.getMonth()==1) 64 record(date,-fee,"annual fee"); 65 acc.reset(date,getDebt()); 66 } 67 void CreditAccount::show() const { 68 Account::show(); 69 cout << "\tAvailable credit:" << getAvailableCredit(); 70 }
1 #include"account.h" 2 #include<iostream> 3 using namespace std; 4 int main() { 5 Date date(2008,11,1); 6 SavingsAccount sa1(date,"S3755217",0.015); 7 SavingsAccount sa2(date,"02342342",0.015); 8 CreditAccount ca(date,"C5392394",10000,0.0005,50); 9 Account * accounts[]={&sa1,&sa2,&ca}; 10 const int n=sizeof(accounts)/sizeof(Account*); 11 cout << "(d)deposit (w)withdraw (s)show (c)change day (n)next month (e)exit" << endl; 12 char cmd; 13 do { 14 date.show(); 15 cout << "\tTotal:" << Account::getTotal() << "\tcommand>"; 16 int index,day; 17 double amount; 18 string desc; 19 cin>>cmd; 20 switch(cmd) { 21 case'd': 22 cin >> index >> amount; 23 getline(cin,desc); 24 accounts[index]->deposit(date,amount,desc); 25 break; 26 case'w': 27 cin >> index >> amount; 28 getline(cin,desc); 29 accounts[index]->withdraw(date,amount,desc); 30 break; 31 case's': 32 for(int i=0; i<n;i++) { 33 cout << "[]"<<i<<"]"; 34 accounts[i] ->show(); 35 cout << endl; 36 } 37 break; 38 case'c': 39 cin>>day; 40 if(day < date.getDay()) 41 cout << "You cannot specify a previous day"; 42 else if (day > date.getMaxDay()) 43 cout << "Invalid day"; 44 else 45 date=Date(date.getYear(),date.getMonth(),day); 46 break; 47 case'n': 48 if(date.getMonth()==12) 49 date=Date(date.getYear()+1,1,1); 50 else 51 date=Date(date.getYear(),date.getMonth()+1,1); 52 for(int i = 0;i<n;i++) 53 accounts[i]->settle(date); 54 break; 55 } 56 }while(cmd !='e'); 57 return 0; 58 }