任务5:
1 #ifndef_ _DATE_H_ _ 2 #define_ _DATE_H_ _ 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 year); 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 void show() const; 18 int distance(const Date& date) const{ 19 return totalDays-date.totalDays;} 20 int operator-(const Date& date) const{ 21 return tptalDays-date.totalDays;} 22 }; 23 #endifdate.h
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 } 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 cout<<endl; 14 exit(1); 15 } 16 int years=year-1; 17 totalDays=years*356+years/4-years/100+years/400+DAYS_BEFORE_MONTH[month-1]+day; 18 if(isLeapYear()&&month>2) totalDays++; 19 } 20 int Date::getMaxDay() const{ 21 if(isLeapYear()&&month==2) 22 return 29; 23 else 24 return DAYS_BEFORE_MONTH[month]-DAYS_BEFORE_MONTH[month-1];} 25 void Date::show() const { 26 cout<<getYear()<<"-"<<getMonth()<<"-"<<getDay();}date.cpp
1 #ifndef_ _ACCUMULATOR_H_ _ 2 #define_ _ACCUMULATOR_H_ _ 3 #include"date.h" 4 class Accumulator{ 5 private: 6 Date lastDate; 7 double value; 8 double sum; 9 public: 10 double getSum(const Date &date) const{ 11 return sum+value*(date-lastDate);} 12 Accumulator(const Date &date,double value) 13 :lastDate(date),value(value),sum(0){ 14 } 15 double getSum(const Date& date) const{ 16 return sum+value*date.distance(lastDate);} 17 void change(const Date &date,double value){ 18 lastDate=date;this->value=value; 19 } 20 void reset(const Date& date,double value){ 21 lastDate=date;this->value=value;sum=0; 22 } 23 }; 24 #endifaccumulator.h
1 #ifndef_ _ACCOUNT_H_ _ 2 #define_ _ACCOUNT_H_ _ 3 #include"date.h" 4 #include"accumulator.h" 5 #include<string> 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 public: 18 const std::string &getId() const{return id;} 19 double getBalance() const{return balance;} 20 static double getTotal() {return total; 21 } 22 virtual void deposit(const Date &date,double amount,const std::string &desc)=0; 23 virtual void withdraw(const Date &date,double amount,const std::string &desc)=0; 24 virtual void settle(const Date &date)=0; 25 virtual void show() const; 26 }; 27 class SavingAccount:public Account{ 28 private: 29 Accumulator acc; 30 double rate; 31 public: 32 SavingAccount(const Date &date,const std::string &id,double rate); 33 double getRate() const{return rate;} 34 void deposit(const Date &date,double amount,const std::string &desc); 35 void withdraw(const Date &date,double amount,const std::string &desc); 36 void settle(const Date &date); 37 38 }; 39 class CreditAccount:public Account{ 40 private: 41 Accumulator acc; 42 double credit; 43 double rate; 44 double fee; 45 double getDebt() const{ 46 double balance=getBalance(); 47 return (balance<0?balance:0);} 48 public: 49 CreditAccount(const Date &date,const std::string &id,double credit,double rate,double fee); 50 double getCredit() const{return credit;} 51 double getRate() const{return rate;} 52 double getFee() const{return fee;} 53 double getAvailableCredit() const{ 54 if(getBalance()<0) 55 return credit+getBalance(); 56 else 57 return credit;} 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 63 };account.h
1 #include"account.h" 2 #include<cmath> 3 #include<iostream> 4 using namespace std; 5 double Account::total=0; 6 Account::Account(const Date &date,const string &id):id(id),balance(0){ 7 date.show() 8 ;cout<<"\t#"<<id<<"created"<<endl;} 9 void Account::record(const Date &date,double amount ,const string &desc){ 10 amount=floor(amount*100+0.5)/100; 11 balance+=amount;total+=amount; 12 date.show() 13 ;cout<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<"\t"<<desc<<endl;} 14 void Account::show() const{cout<<id<<"\tBalance: "<<balance;} 15 void Account::error(const string &msg) const{ 16 cout<<"Error(#"<<id<<"): "<<msg<<endl;} 17 SavingsAccount::SavingsAccount(const Date &date,const string &id,double rate): 18 Account(date,id),rate(rate),acc(date,0){ 19 } 20 void SavingsAccount::deposit(const Date &date,double amount,const string &desc){ 21 record(date,amount,desc); 22 acc.change(date,getBalance()); 23 } 24 void SavingsAccount::withdraw(const Date &date,double amount,const string &desc){ 25 if(amount>getBalance()){ 26 error("not enough money"); 27 28 }else{ 29 record(date,-amount,desc); 30 acc.change(date,getBalance()); 31 } 32 } 33 void SavingsAccount::settle(const Date &date){ 34 if(date.getMonth()==1){ 35 double interest=acc.getSum(date)*rate/(date-Date(date.getYear()-1,1,1)); 36 if(interest!=0) 37 record(date,interest,"interest"); 38 acc.reset(date,getBalance()); 39 } 40 } 41 CreditAccount::CreditAccount(const Date& date,const string &id,double credit,double rate,double fee) 42 :Account(date,id),credit(credit),rate(rate),fee(fee),acc(date,0){ 43 } 44 void CreditAccount::deposit(const Date &date,double amount,const string &desc){ 45 if(amount-getBalance()>credit){ 46 error("not enough credit"); 47 48 }else { 49 record(date,-amount,desc); 50 acc.change(date,getDebt()); 51 } 52 } 53 void CreditAccount::settle(const Date &date){ 54 double interest=acc.getSum(date)*rate; 55 if(interest!=0)record(date,interest,"interest"); 56 if(date.getMonth()==1) 57 record(date,-fee,"annual fee"); 58 acc.reset(date,getDebt()); 59 } 60 void CreditAccount::show() const{ 61 Account::show(); 62 cout<<"\tAvailable credit: "<<getAvailableCredit();} 63 64account.cpp
1 #include"account.h" 2 #include<iostream> 3 using namespace std; 4 int main(){ 5 Date date(2008,11,1); 6 SavingAccount sa1(date,"S3755217",0.015); 7 SavingAccount 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 swith(cmd){ 21 case'd': 22 cin>>index>>amount; 23 getline(cin,desc); 24 accounts[index]->withdraw(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 } 39 case'c': 40 cin>>day; 41 if(day<date.getDay()) 42 cout<<"You cannot specify a previous day"; 43 44 else if(day>date.getMaxDay()) 45 cout<<"Invalid day"; 46 else 47 date=Date(date.getYear(),date.getMonth(),day); 48 break; 49 case'n': 50 if(date.getMonth()==12) 51 date=Date(date.getYear()+1,1,1); 52 else 53 date=Date(date.getYear(),date.getMonth()+1,1); 54 for(int i=0;i<n;i++) 55 accounts[i]->settle(date); 56 break;} 57 }while(cmd!='e'); 58 return 0; 59 }8_8.cpp
任务3:
#pragma once #include<iostream> #include<string> using namespace std; class MachinePets{ public: MachinePets(const string s):nickname(s){} public: virtual string talk()=0; string get_nickname(){ return nickname; } private: string nickname; }; class PetCats: public MachinePets{ public: PetCats(const string s):MachinePets(s){ } string talk() override{ return "miao wu~"; } }; class PetDogs:public MachinePets{ public: PetDogs(const string s):MachinePets(s){ } string talk() override{ return "wang wang ~"; } };pets.hpp
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 }task3.cpp
任务4:
1 #pragma once 2 #include<string> 3 #include<iostream> 4 5 6 class Person{ 7 private: 8 string name,telephone,email; 9 public: 10 Person(); 11 Person(const string &a,const string &b,const string &c=" "):name(a),telephone(b),email(c){ 12 } 13 Person(const Person& c):name(c.name),telephone(c.telephone),email(c.email){ 14 } 15 void update_telephone(){ 16 cout<<"请输入新的手机号码:"<<endl; 17 cin>>telephone; 18 } 19 void update_email(){ 20 cout<<"请输入新的邮箱地址:"<<endl; 21 cin>>email; 22 } 23 friend std::ostream& operator<<(std::ostream& out,const Person& p){ 24 out<<"name: "<<p.name<<"telephone: "<<p.telephone<<"email: "<<p.email<<endl; 25 return out; 26 } 27 //重载插入运算符<<为友元函数,支持使用诸如 cout << p << endl; 的方式输出联系人信息 28 friend std::istream& operator>>(std::istream& in, Person& p){ 29 in>>p.name>>p.telephone>>p.email; 30 return in; 31 } 32 //重载提取运算符>>为友元函数,支持使用诸如 cin >> p 的方式输入联系人信息 33 friend bool operator==(const Person& p1,const Person &p2){ 34 return p1.name==p2.name&&p1.telephone==p2.telephone&&p1.email==p2.email; 35 } 36 // 重载关系运算符==为友元函数,支持对两个联系人信息进行关系判断。 37 //只有当姓名和手机号都相同时,才返回true,否则,返回false。 38 };Person.hpp
1 #include <iostream> 2 #include <vector> 3 #include "Person.hpp" 4 5 void test() { 6 using namespace std; 7 8 vector<Person> phone_book; 9 Person p; 10 11 cout << "输入一组联系人的联系方式,E直至按下Ctrl+Z终止\n"; 12 while(cin >> p) 13 phone_book.push_back(p); 14 15 cout << "\n更新phone_book中索引为0的联系人的手机号、邮箱:\n"; 16 phone_book.at(0).update_telephone(); 17 phone_book.at(0).update_email(); 18 19 cout << "\n测试两个联系人是否是同一个:\n"; 20 cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl; 21 } 22 23 int main() { 24 test(); 25 }task4.cpp 标签:const,string,double,Date,实验,date,return From: https://www.cnblogs.com/shaoshuai-nuist/p/17873435.html