实验任务3:
1.代码:
pets.hpp:
1 pragma once 2 3 #include<iostream> 4 #include<string> 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 using std::string; 9 10 class MachinePets{ 11 private: 12 string nickname; 13 public: 14 MachinePets(const string &s=" "):nickname{s}{} 15 string get_nickname(){ 16 return nickname; 17 } 18 public: 19 virtual string talk()=0; 20 21 }; 22 23 class PetCats:public MachinePets{ 24 private: 25 const string m="miao wu~" ; 26 27 public: 28 PetCats(const string &a):MachinePets{a}{} 29 string talk(){ 30 return m; 31 32 } 33 }; 34 35 class PetDogs:public MachinePets{ 36 private: 37 const string d="wang wang~" ; 38 39 public: 40 PetDogs(const string &a):MachinePets{a}{} 41 string talk(){ 42 return d; 43 44 } 45 };View Code
task3.cpp:
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 }View Code
2.图片:
实验任务4:
1.代码:
person.hpp:
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 using namespace std; 6 class Person { 7 private: 8 string name; 9 string telephone; 10 string email; 11 12 public: 13 Person() {} 14 Person(const string& name, const string& telephone, const string& email = "") : name(name), telephone(telephone), email(email) {} 15 Person(const Person& other) : name(other.name), telephone(other.telephone), email(other.email) {} 16 17 void update_telephone() { 18 cout << "请输入新的手机号码:"; 19 cin >> telephone; 20 cout<<"电话号码已更新..."; 21 cout<<endl; 22 } 23 24 void update_email() { 25 cout << "请输入新的邮箱地址:"; 26 cin >> email; 27 cout<<"邮箱地址已更新..."; 28 cout<<endl; 29 } 30 31 friend std::ostream& operator<<(std::ostream& os, const Person& person) { 32 os << "姓名:" << person.name << "\n手机号码:" << person.telephone << "\n邮箱地址:" << person.email; 33 return os; 34 } 35 36 friend std::istream& operator>>(std::istream& is, Person& person) { 37 cout << "请输入姓名: "; 38 getline(is, person.name); 39 if (is.eof()) { 40 return is; 41 } 42 cout << "请输入电话号码: "; 43 getline(is, person.telephone); 44 cout << "请输入邮箱 (可不填): "; 45 getline(is, person.email); 46 cout<<endl; 47 return is; 48 } 49 50 51 friend bool operator==(const Person& person1, const Person& person2) { 52 return person1.name == person2.name && person1.telephone == person2.telephone; 53 } 54 }; 55View Code
task4.cpp:
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 cin.clear(); 12 cout << "\n更新phone_book中索引为0的联系人的手机号、邮箱:\n"; 13 phone_book.at(0).update_telephone(); 14 phone_book.at(0).update_email(); 15 cout << "\n测试两个联系人是否是同一个:\n"; 16 cout << boolalpha << (phone_book.at(0) == phone_book.at(1)) << endl; 17 } 18 int main() { 19 test(); 20 }View Code
2.图片:
实验任务5:
1.代码:
account.h:
1 #pragma once 2 3 4 #include"date.h" 5 #include"accumulator.h" 6 #include<string> 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 public: 17 const std::string& getId() const { return id; } 18 double getBalance() const { return balance; } 19 static double getToal() { return total; } 20 virtual void deposit(const Date& date, double amount, const std::string& desc) = 0; 21 virtual void withdraw(const Date& date, double amount, const std::string& desc) = 0; 22 virtual void settle(const Date& date)=0; 23 virtual void show() const; 24 }; 25 26 class SavingsAccount :public Account { 27 private: 28 Accumulator acc; 29 double rate; 30 public: 31 SavingsAccount(const Date& date, const std::string& id, double rate); 32 double getRate() const { return rate; } 33 void deposit(const Date& date, double amount, const std::string& desc); 34 void withdraw(const Date& date, double amount, const std::string& desc); 35 void settle(const Date& date); 36 }; 37 38 class CreditAccount :public Account { 39 private: 40 Accumulator acc; 41 double credit; 42 double rate; 43 double fee; 44 double getDebt() const 45 { 46 double balance = getBalance(); 47 return(balance < 0 ? balance : 0); 48 } 49 public: 50 CreditAccount(const Date& date, const std::string& id, double credit, double rate, double fee); 51 double getCredit() const { return credit; } 52 double getRate() const { return rate; } 53 double getFee() const { return fee; } 54 double getAvailableCredit() const { 55 if (getBalance() < 0) 56 return credit + getBalance(); 57 else 58 return credit; 59 } 60 void deposit(const Date& date, double amount, const std::string& desc); 61 void withdraw(const Date& date, double amount, const std::string& desc); 62 void settle(const Date& date); 63 void show() const; 64 };View Code
accumulator.h:
1 #pragma once 2 3 #include"date.h" 4 class Accumulator { 5 private: 6 Date lastDate; 7 double value; 8 double sum; 9 public: 10 Accumulator(const Date& date, double value) :lastDate(date), value(value), sum(0) {} 11 double getSum(const Date& date) const 12 { 13 return sum + value * (date - lastDate); 14 } 15 void change(const Date& date, double value) 16 { 17 sum = getSum(date); 18 lastDate = date; this->value = value; sum = 0; 19 } 20 void reset(const Date& date, double value) 21 { 22 lastDate = date; this->value = value; sum = 0; 23 } 24 25 26 };View Code
date.h:
1 #pragma once 2 3 4 class Date { 5 private: 6 int year; 7 int month; 8 int day; 9 int totalDays; 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 void show() const; 20 int operator-(const Date& date) const { 21 return totalDays - date.totalDays; 22 } 23 24 };View Code
account.cpp:
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 std::string& id) :id(id), balance(0) { 7 date.show(); cout << "\t#" << id << "created" << endl; 8 } 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 } 15 void Account::show() const { cout << id << "\tBalance:" << balance; } 16 void Account::error(const string& msg) const { 17 cout << "Error(#" << id << "):" << msg << endl; 18 } 19 20 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :Account(date, id), rate(rate), acc(date, 0) { 21 } 22 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) { 23 record(date, amount, desc); 24 acc.change(date, getBalance()); 25 } 26 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) { 27 if (amount > getBalance()) 28 { 29 error("not enough money"); 30 } 31 else 32 { 33 record(date, -amount, desc); 34 acc.change(date, getBalance()); 35 } 36 } 37 void SavingsAccount::settle(const Date& date) 38 { 39 if (date.getMonth() == 1) 40 { 41 double interest = acc.getSum(date) * rate / (date - Date(date.getYear() - 1, 1, 1)); 42 if (interest != 0) record(date, interest, "interest"); 43 acc.reset(date, getBalance()); 44 } 45 46 } 47 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) { 48 } 49 void CreditAccount::deposit(const Date& date, double amount, const string& desc) 50 { 51 record(date, amount, desc); 52 acc.change(date, getDebt()); 53 } 54 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) 55 { 56 if (amount - getBalance() > credit) 57 { 58 error("not enough credit"); 59 } 60 else 61 { 62 record(date, -amount, desc); 63 acc.change(date, getDebt()); 64 } 65 } 66 void CreditAccount::settle(const Date& date) 67 { 68 double interest = acc.getSum(date) * rate; 69 if (interest != 0) record(date, interest, "interest"); 70 if (date.getMonth() == 1) record(date, -fee, "annual fee"); 71 acc.reset(date, getDebt()); 72 } 73 void CreditAccount::show() const 74 { 75 Account::show(); 76 cout << "\tAvailable credit:" << getAvailableCredit(); 77 }View Code
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 Date::Date(int year, int month, int day) :year(year), month(month), day(day) { 11 if (day <= 0 || day > getMaxDay()) { 12 cout << "Invalid date: "; 13 show(); 14 cout << endl; 15 exit(1); 16 } 17 int years = year - 1; 18 totalDays = year * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day; 19 if (isLeapYear() && month > 2)totalDays++; 20 21 } 22 23 int Date::getMaxDay() const { 24 if (isLeapYear() && month == 2)return 29; 25 else 26 return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; 27 28 } 29 30 void Date::show() const { 31 cout << getYear() << "-" << getMonth() << "-" << getDay(); 32 33 }View Code
8.8.cpp:
1 #include"account.h" 2 #include<iostream> 3 using namespace std; 4 int main() 5 { 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::getToal() << "\tcommand>"; 17 int index, day; 18 double amount; string desc; 19 cin >> cmd; 20 switch (cmd) 21 { 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 { 35 cout << "[" << i << "]"; 36 accounts[i]->show(); 37 cout << endl; 38 } 39 break; 40 case'c': 41 cin >> day; 42 if (day < date.getDay()) 43 cout << "You cannot specify a previous day"; 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 } 58 } while (cmd != 'e'); 59 return 0; 60 61 }View Code
2.图片:
标签:std,const,string,继承,double,多态,Date,实验,date From: https://www.cnblogs.com/yili123/p/17860515.html