實驗4

易安135發表於2024-11-24

task1:

 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 
 3 #include <iostream>
 4 
 5 using std::cout;
 6 using std::endl;
 7 
 8 // 類A的定義
 9 class A {
10 public:
11     A(int x0, int y0);
12     void display() const;
13 
14 private:
15     int x, y;
16 };
17 
18 A::A(int x0, int y0) : x{ x0 }, y{ y0 } {
19 }
20 
21 void A::display() const {
22     cout << x << ", " << y << endl;
23 }
24 
25 // 類B的定義
26 class B {
27 public:
28     B(double x0, double y0);
29     void display() const;
30 
31 private:
32     double x, y;
33 };
34 
35 B::B(double x0, double y0) : x{ x0 }, y{ y0 } {
36 }
37 
38 void B::display() const {
39     cout << x << ", " << y << endl;
40 }
41 
42 void test() {
43     cout << "測試類A: " << endl;
44     A a(3, 4);
45     a.display();
46 
47     cout << "\n測試類B: " << endl;
48     B b(3.2, 5.6);
49     b.display();
50 }
51 
52 int main() {
53     test();
54 }

 1 #include <iostream>
 2 #include <string>
 3 
 4 using std::cout;
 5 using std::endl;
 6 using std::string;
 7 
 8 // 定義類别範本
 9 template<typename T>
10 class X{
11 public:
12     X(T x0, T y0);
13     void display();
14 
15 private:
16     T x, y;
17 };
18 
19 template<typename T>
20 X<T>::X(T x0, T y0): x{x0}, y{y0} {
21 }
22 
23 template<typename T>
24 void X<T>::display() {
25     cout << x << ", " << y << endl;
26 }
27 
28 
29 void test() {
30     cout << "測試1: 類别範本X中的抽象型別T用int例項化" << endl;
31     X<int> x1(3, 4);
32     x1.display();
33     
34     cout << endl;
35 
36     cout << "測試2: 類别範本X中的抽象型別T用double例項化" << endl;
37     X<double> x2(3.2, 5.6);
38     x2.display();
39 
40     cout << endl;
41 
42     cout << "測試3: 類别範本X中的抽象型別T用string例項化" << endl;
43     X<string> x3("hello", "oop");
44     x3.display();
45 }
46 
47 int main() {
48     test();
49 }

task2:

#pragma once

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <iomanip>

using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;

class GradeCalc : public vector<int> {
public:
    GradeCalc(const string& cname, int size);
    void input();                             // 錄入成績
    void output() const;                      // 輸出成績
    void sort(bool ascending = false);        // 排序 (預設降序)
    int min() const;                          // 返回最低分
    int max() const;                          // 返回最高分
    float average() const;                    // 返回平均分
    void info();                              // 輸出課程成績資訊 

private:
    void compute();     // 成績統計

private:
    string course_name;     // 課程名
    int n;                  // 課程人數
    vector<int> counts = vector<int>(5, 0);      // 儲存各分數段人數([0, 60), [60, 70), [70, 80), [80, 90), [90, 100]
    vector<double> rates = vector<double>(5, 0); // 儲存各分數段比例 
};

GradeCalc::GradeCalc(const string& cname, int size) : course_name{ cname }, n{ size } {}

void GradeCalc::input() {
    int grade;

    for (int i = 0; i < n; ++i) {
        cin >> grade;
        this->push_back(grade);
    }
}

void GradeCalc::output() const {
    for (auto ptr = this->begin(); ptr != this->end(); ++ptr)
        cout << *ptr << " ";
    cout << endl;
}

void GradeCalc::sort(bool ascending) {
    if (ascending)
        std::sort(this->begin(), this->end());
    else
        std::sort(this->begin(), this->end(), std::greater<int>());
}

int GradeCalc::min() const {
    return *std::min_element(this->begin(), this->end());
}

int GradeCalc::max() const {
    return *std::max_element(this->begin(), this->end());
}

float GradeCalc::average() const {
    return std::accumulate(this->begin(), this->end(), 0) * 1.0 / n;
}

void GradeCalc::compute() {
    for (int grade : *this) {
        if (grade < 60)
            counts.at(0)++;
        else if (grade >= 60 && grade < 70)
            counts.at(1)++;
        else if (grade >= 70 && grade < 80)
            counts.at(2)++;
        else if (grade >= 80 && grade < 90)
            counts.at(3)++;
        else if (grade >= 90)
            counts.at(4)++;
    }

    for (int i = 0; i < rates.size(); ++i)
        rates.at(i) = counts.at(i) * 1.0 / n;
}

void GradeCalc::info() {
    cout << "課程名稱:\t" << course_name << endl;
    cout << "排序後成績: \t";
    sort();  output();
    cout << "最高分:\t" << max() << endl;
    cout << "最低分:\t" << min() << endl;
    cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl;

    compute();  // 統計各分數段人數、比例

    vector<string> tmp{ "[0, 60)  ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]" };
    for (int i = tmp.size() - 1; i >= 0; --i)
        cout << tmp[i] << "\t: " << counts[i] << "人\t"
        << std::fixed << std::setprecision(2) << rates[i] * 100 << "%" << endl;
}
 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 
 3 #include "GradeCalc.hpp"
 4 #include <iomanip>
 5 
 6 void test() {
 7     int n;
 8     cout << "輸入班級人數: ";
 9     cin >> n;
10 
11     GradeCalc c1("OOP", n);
12 
13     cout << "錄入成績: " << endl;;
14     c1.input();
15     cout << "輸出成績: " << endl;
16     c1.output();
17 
18     cout << string(20, '*') + "課程成績資訊" + string(20, '*') << endl;
19     c1.info();
20 }
21 
22 int main() {
23     test();
24 }

問題1:

儲存在vector模板類中,透過vector模板類提供的介面,透過vector.push_back.

問題2:

求平均數。有影響。因為整數與整數做除法,會捨去小數部分,乘以1.0是為了讓分子變成小數,從而保留結果的小數部分。

問題3:

應該檢查輸入成績是否符合程式要求。

task3:

  1 #pragma once
  2 
  3 #include <iostream>
  4 #include <vector>
  5 #include <string>
  6 #include <algorithm>
  7 #include <numeric>
  8 #include <iomanip>
  9 
 10 using std::vector;
 11 using std::string;
 12 using std::cin;
 13 using std::cout;
 14 using std::endl;
 15 
 16 class GradeCalc {
 17 public:
 18     GradeCalc(const string& cname, int size);
 19     void input();                             // 錄入成績
 20     void output() const;                      // 輸出成績
 21     void sort(bool ascending = false);        // 排序 (預設降序)
 22     int min() const;                          // 返回最低分
 23     int max() const;                          // 返回最高分
 24     float average() const;                    // 返回平均分
 25     void info();                              // 輸出課程成績資訊 
 26 
 27 private:
 28     void compute();     // 成績統計
 29 
 30 private:
 31     string course_name;     // 課程名
 32     int n;                  // 課程人數
 33     vector<int> grades;     // 課程成績
 34     vector<int> counts = vector<int>(5, 0);      // 儲存各分數段人數([0, 60), [60, 70), [70, 80), [80, 90), [90, 100]
 35     vector<double> rates = vector<double>(5, 0); // 儲存各分數段比例 
 36 };
 37 
 38 GradeCalc::GradeCalc(const string& cname, int size) : course_name{ cname }, n{ size } {}
 39 
 40 void GradeCalc::input() {
 41     int grade;
 42 
 43     for (int i = 0; i < n; ++i) {
 44         cin >> grade;
 45         grades.push_back(grade);
 46     }
 47 }
 48 
 49 void GradeCalc::output() const {
 50     for (int grade : grades)
 51         cout << grade << " ";
 52     cout << endl;
 53 }
 54 
 55 void GradeCalc::sort(bool ascending) {
 56     if (ascending)
 57         std::sort(grades.begin(), grades.end());
 58     else
 59         std::sort(grades.begin(), grades.end(), std::greater<int>());
 60 
 61 }
 62 
 63 int GradeCalc::min() const {
 64     return *std::min_element(grades.begin(), grades.end());
 65 }
 66 
 67 int GradeCalc::max() const {
 68     return *std::max_element(grades.begin(), grades.end());
 69 }
 70 
 71 float GradeCalc::average() const {
 72     return std::accumulate(grades.begin(), grades.end(), 0) * 1.0 / n;
 73 }
 74 
 75 void GradeCalc::compute() {
 76     for (int grade : grades) {
 77         if (grade < 60)
 78             counts.at(0)++;
 79         else if (grade >= 60 && grade < 70)
 80             counts.at(1)++;
 81         else if (grade >= 70 && grade < 80)
 82             counts.at(2)++;
 83         else if (grade >= 80 && grade < 90)
 84             counts.at(3)++;
 85         else if (grade >= 90)
 86             counts.at(4)++;
 87     }
 88 
 89     for (int i = 0; i < rates.size(); ++i)
 90         rates.at(i) = counts.at(i) * 1.0 / n;
 91 }
 92 
 93 void GradeCalc::info() {
 94     cout << "課程名稱:\t" << course_name << endl;
 95     cout << "排序後成績: \t";
 96     sort();  output();
 97     cout << "最高分:\t" << max() << endl;
 98     cout << "最低分:\t" << min() << endl;
 99     cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl;
100 
101     compute();  // 統計各分數段人數、比例
102 
103     vector<string> tmp{ "[0, 60)  ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]" };
104     for (int i = tmp.size() - 1; i >= 0; --i)
105         cout << tmp[i] << "\t: " << counts[i] << "人\t"
106         << std::fixed << std::setprecision(2) << rates[i] * 100 << "%" << endl;
107 }
 1 #include "GradeCalc.hpp"
 2 #include <iomanip>
 3 
 4 void test() {
 5     int n;
 6     cout << "輸入班級人數: ";
 7     cin >> n;
 8 
 9     GradeCalc c1("OOP", n);
10 
11     cout << "錄入成績: " << endl;;
12     c1.input();
13     cout << "輸出成績: " << endl;
14     c1.output();
15 
16     cout << string(20, '*') + "課程成績資訊" + string(20, '*') << endl;
17     c1.info();
18 }
19 
20 int main() {
21     test();
22 }

問題1:

儲存在vector模板類中。透過vector提供的介面。

問題2:

類的繼承可以直接呼叫父類(基類)的public等符合許可權的方法,類的組合則需要透過包含的類來間接呼叫。

task4:

 1 #include <iostream>
 2 #include <string>
 3 #include <limits>
 4 
 5 using namespace std;
 6 
 7 void test1() {
 8     string s1, s2;
 9     cin >> s1 >> s2;  // cin: 從輸入流讀取字串, 碰到空白符(空格/回車/Tab)即結束
10     cout << "s1: " << s1 << endl;
11     cout << "s2: " << s2 << endl;
12 }
13 
14 void test2() {
15     string s1, s2;
16     getline(cin, s1);  // getline(): 從輸入流中提取字串,直到遇到換行符
17     getline(cin, s2);
18     cout << "s1: " << s1 << endl;
19     cout << "s2: " << s2 << endl;
20 }
21 
22 void test3() {
23     string s1, s2;
24     getline(cin, s1, ' '); //從輸入流中提取字串,直到遇到指定分隔符
25     getline(cin, s2);
26     cout << "s1: " << s1 << endl;
27     cout << "s2: " << s2 << endl;
28 }
29 
30 int main() {
31     cout << "測試1: 使用標準輸入流物件cin輸入字串" << endl;
32     test1();
33     cout << endl;
34 
35     cin.ignore(numeric_limits<streamsize>::max(), '\n');
36 
37     cout << "測試2: 使用函式getline()輸入字串" << endl;
38     test2();
39     cout << endl;
40 
41     cout << "測試3: 使用函式getline()輸入字串, 指定字串分隔符" << endl;
42     test3();
43 }

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <limits>
 5 
 6 using namespace std;
 7 
 8 void output(const vector<string>& v) {
 9     for (auto& s : v)
10         cout << s << endl;
11 }
12 
13 void test() {
14     int n;
15     while (cout << "Enter n: ", cin >> n) {
16         vector<string> v1;
17 
18         for (int i = 0; i < n; ++i) {
19             string s;
20             cin >> s;
21             v1.push_back(s);
22         }
23 
24         cout << "output v1: " << endl;
25         output(v1);
26         cout << endl;
27     }
28 }
29 
30 int main() {
31     cout << "測試: 使用cin多組輸入字串" << endl;
32     test();
33 }

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <limits>
 5 
 6 using namespace std;
 7 
 8 void output(const vector<string>& v) {
 9     for (auto& s : v)
10         cout << s << endl;
11 }
12 
13 void test() {
14     int n;
15     while (cout << "Enter n: ", cin >> n) {
16         cin.ignore(numeric_limits<streamsize>::max(), '\n');
17 
18         vector<string> v2;
19 
20         for (int i = 0; i < n; ++i) {
21             string s;
22             getline(cin, s);
23             v2.push_back(s);
24         }
25         cout << "output v2: " << endl;
26         output(v2);
27         cout << endl;
28     }
29 }
30 
31 int main() {
32     cout << "測試: 使用函式getline()多組輸入字串" << endl;
33     test();
34 }

問題1:

讀取換行符,讓下一個輸入正確讀取。

問題2:

多次輸入,去除緩衝區的換行符,避免有一行輸入輸入失敗。

task5:

 1 #pragma once
 2 #include<iostream>
 3 #include<string>
 4 using namespace std;
 5 
 6 template<typename T>
 7 class GameResourceManager {
 8 private:
 9     T resource;
10 public:
11     GameResourceManager(T x) :resource{x} {
12     }
13     T get() {
14         return resource;
15     }
16     void update(T data) {
17         resource = resource + data;
18         if (resource <= 0) {
19             resource = 0;
20         }
21     }
22 };
 1 #include "grm.hpp"
 2 #include <iostream>
 3 
 4 using std::cout;
 5 using std::endl;
 6 
 7 void test1() {
 8     GameResourceManager<float> HP_manager(99.99);
 9     cout << "當前生命值: " << HP_manager.get() << endl;
10     HP_manager.update(9.99);
11     cout << "增加9.99生命值後, 當前生命值: " << HP_manager.get() << endl;
12     HP_manager.update(-999.99);
13     cout << "減少999.99生命值後, 當前生命值: " << HP_manager.get() << endl;
14 }
15 
16 void test2() {
17     GameResourceManager<int> Gold_manager(100);
18     cout << "當前金幣數量: " << Gold_manager.get() << endl;
19     Gold_manager.update(50);
20     cout << "增加50個金幣後, 當前金幣數量: " << Gold_manager.get() << endl;
21     Gold_manager.update(-99);
22     cout << "減少99個金幣後, 當前金幣數量: " << Gold_manager.get() << endl;
23 }
24 
25 
26 int main() {
27     cout << "測試1: 用float型別對類别範本GameResourceManager例項化" << endl;
28     test1();
29     cout << endl;
30 
31     cout << "測試2: 用int型別對類别範本GameResourceManager例項化" << endl;
32     test2();
33 }

task6:

 1 #pragma once
 2 
 3 #include<iostream>
 4 #include<string>
 5 #include<iomanip>
 6 
 7 #define MaxSize 100
 8 using namespace std;
 9 class Info {
10 private:
11     string nickName;
12     string contact;
13     string city;
14     double n;
15 public:
16     Info(string name, string con, string ci, double n1) :nickName{ name }, contact{ con }, city{ ci }, n{n1} {
17     }
18     void display() {
19         cout << left << setw(10) << nickName << left << setw(30) << contact << left << setw(30) << city << left << setw(30) << n << endl;;
20         cout << "------" << endl;
21     }
22     string getName() {
23         return nickName;
24     }
25 };
 1 #include "info.hpp"
 2 #include<iostream>
 3 #include<vector>
 4 #include<iomanip>
 5 
 6 int main() {
 7     const double capacity = MaxSize;
 8     vector<Info> audience_lst;
 9     int num = 0;
10     cout << "錄入使用者預約資訊:" << endl;
11     cout << endl;
12     cout << left <<setw(10) <<"暱稱" << left << setw(30) << "聯絡方式(郵箱/手機號)" << left << setw(30) << "所在城市" << left << setw(30) << "預定參加人數"  << endl;;
13     
14     string name, con, ci;
15     double n1;
16     double usedSize = 0;
17     int flag = -1;
18     while (true) {
19         cin >> name;
20         flag = -1;
21         if (name == "^Z") {
22             break;
23         }
24         if (usedSize == capacity) {
25             break;
26         }
27         cin >> con >> ci >> n1;
28         while (usedSize + n1 > capacity) {
29             cout << "對不起,只剩" << capacity - usedSize << "個位置." << endl;
30             cout << "1.輸入u,更新(update)預定資訊" << endl;
31             cout << "2.輸入q,退出預定" << endl;
32             cout << "你的選擇:";
33             string s;
34             cin >> s;
35             if (s == "q") {
36                 return 0;
37             }
38             if (s == "u") {
39                 cout << "請重新輸入預定資訊:" << endl;
40                 cin >> name >> con >> ci >> n1;
41                 flag = 1;
42                 usedSize += n1;
43                 audience_lst.push_back(Info(name, con, ci, n1));
44                 num++;
45                 break;
46             }
47         }
48         if (flag == 1) {
49             break;
50         }
51         usedSize += n1;
52         audience_lst.push_back(Info(name, con, ci, n1));
53         num++;
54     }
55     cout << "截至目前,一共有" << usedSize << "位觀眾預約。預約觀眾資訊如下:" << endl;
56     cout << "------" << endl;
57     for (Info& i : audience_lst) {
58         i.display();
59     }
60 }

task7:

 1 #pragma once
 2 
 3 #pragma once
 4 #ifndef  DATE H
 5 #define  DATE H
 6 class Date {
 7 private:
 8     int year;
 9     int month;
10     int day;
11     int totalDays;
12 public:
13     Date(int year, int month, int day);
14     int getYear()const { return year; }
15     int getMonth()const { return month; }
16     int getDay()const { return day; }
17     int getMaxDay()const;
18     bool isLeapYear()const {
19         return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
20     }
21     void show()const;
22     int distance(const Date& date)const {
23         return totalDays - date.totalDays;
24     }
25 };
26 #endif//  DATE H
 1 #pragma once
 2 
 3 #pragma once
 4 #ifndef  ACCOUNT H
 5 #define  ACCOUNT H
 6 #include"date.h"
 7 #include"accumulator.h"
 8 #include<string>
 9 class Account {
10 private:
11     std::string id;
12     double balance;
13     static double total;
14 protected:
15     Account(const Date& date, const std::string& id);
16     void record(const Date& date, double amount, const std::string& desc);
17     void error(const std::string& msg)const;
18 public:
19     const std::string& getId()const { return id; }
20     double getBalance()const { return balance; }
21     static double getTotal() { return total; }
22 
23     void show()const;
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 
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 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 getAvailableCredit()const {
52         if (getBalance() < 0)
53             return credit + getBalance();
54         else
55             return credit;
56     }
57     void deposit(const Date& date, double amount, const std::string& desc);
58     void withdraw(const Date& date, double amount, const std::string& desc);
59     void settle(const Date& date);
60     void show()const;
61 };
62 #endif//ACCOUNT H
 1 #pragma once
 2 
 3 #pragma once
 4 #ifndef  ACCUMULATOR H
 5 #define  ACCUMULATOR H
 6 #include"date.h"
 7 class Accumulator {
 8 private:
 9     Date lastDate;
10     double value;
11     double sum;
12 public:
13     Accumulator(const Date& date, double value) :lastDate(date), value(value), sum{ 0 } {
14     }
15 
16     double getSum(const Date& date)const {
17         return sum + value * date.distance(lastDate);
18     }
19 
20     void change(const Date& date, double value) {
21         sum = getSum(date);
22         lastDate = date; this->value = value;
23     }
24 
25     void reset(const Date& date, double value) {
26         lastDate = date; this->value = value; sum = 0;
27     }
28 };
29 #endif//ACCUMULATOR H
 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 
 3 #include"account.h"
 4 #include<cmath>
 5 #include<iostream>
 6 using namespace std;
 7 double Account::total = 0;
 8 
 9 Account::Account(const Date& date, const string& id) :id{ id }, balance{ 0 } {
10     date.show(); cout << "\t#" << id << "created" << endl;
11 }
12 
13 
14 void Account::record(const Date& date, double amount, const string& desc) {
15     amount = floor(amount * 100 + 0.5) / 100;
16     balance += amount;
17     total += amount;
18     date.show();
19     cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
20 }
21 
22 void Account::show()const { cout << id << "\tBalance:" << balance; }
23 void Account::error(const string& msg)const {
24     cout << "Error(#" << id << "):" << msg << endl;
25 }
26 
27 SavingsAccount::SavingsAccount(const Date& date, const string& id, double rate) :Account(date, id), rate(rate), acc(date, 0) {}
28 
29 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) {
30     record(date, amount, desc);
31     acc.change(date, getBalance());
32 }
33 
34 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) {
35     if (amount > getBalance()) {
36         error("not enough money");
37     }
38     else {
39         record(date, -amount, desc);
40         acc.change(date, getBalance());
41     }
42 }
43 
44 void SavingsAccount::settle(const Date& date) {
45     double interest = acc.getSum(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1));
46     if (interest != 0)record(date, interest, "interest");
47     acc.reset(date, getBalance());
48 }
49 
50 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) {}
51 
52 void CreditAccount::deposit(const Date& date, double amount, const string& desc) {
53     record(date, amount, desc);
54     acc.change(date, getDebt());
55 }
56 
57 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) {
58     if (amount - getBalance() > credit) {
59         error("not enough credit");
60     }
61     else {
62         record(date, -amount, desc);
63         acc.change(date, getDebt());
64     }
65 }
66 
67 void CreditAccount::settle(const Date& date) {
68     double interest = acc.getSum(date) * rate;
69     if (interest != 0)record(date, interest, "interest");
70     if (date.getMonth() == 1)
71         record(date, -fee, "annual fee");
72     acc.reset(date, getDebt());
73 }
74 
75 void CreditAccount::show()const {
76     Account::show();
77     cout << "\tAvailable credit:" << getAvailableCredit();
78 }
 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 
 3 #include"date.h"
 4 #include<iostream>
 5 #include<cstdlib>
 6 using namespace std;
 7 namespace {
 8     const int DAYS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
 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 = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day;
19     if (isLeapYear() && month > 2)totalDays++;
20 }
21 int Date::getMaxDay()const {
22     if (isLeapYear() && month == 2)
23         return 29;
24     else return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
25 }
26 
27 void Date::show()const {
28     cout << getYear() << "-" << getMonth() << "-" << getDay();
29 }
 1 #define _CRT_SECURE_NO_WARNINGS 1
 2 
 3 #include"account.h"
 4 #include<iostream>
 5 
 6 using namespace std;
 7 
 8 int main() {
 9     Date date(2008, 11, 1);
10     SavingsAccount sa1(date, "S3755217", 0.015);
11     SavingsAccount sa2(date, "02342342", 0.015);
12     CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);
13 
14     sa1.deposit(Date(2008, 11, 5), 5000, "salary");
15     ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell");
16     sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323");
17 
18     ca.settle(Date(2008, 12, 1));
19 
20     ca.deposit(Date(2008, 12, 1), 2016, "repay the credit");
21     sa1.deposit(Date(2008, 12, 5), 5500, "salary");
22 
23     sa1.settle(Date(2009, 1, 1));
24     sa2.settle(Date(2009, 1, 1));
25     ca.settle(Date(2009, 1, 1));
26 
27     cout << endl;
28     sa1.show(); cout << endl;
29     sa2.show(); cout << endl;
30     ca.show(); cout << endl;
31     cout << "Total:" << Account::getTotal() << endl;
32     return 0;
33 }