<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      OOP實(shí)驗(yàn)四

      任務(wù)2:

      源碼:

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

      結(jié)果:

       答:

      問(wèn)題1:派生類GradeCalc定義中,成績(jī)存儲(chǔ)在哪里?派生類方法sort, min, max, average,
      output都要訪問(wèn)成績(jī),是通過(guò)什么接口訪問(wèn)到每個(gè)成績(jī)的?input方法是通過(guò)什么接口實(shí)現(xiàn)數(shù)
      據(jù)存入對(duì)象的?
      基類vector。vector中的接口。vector::push_back。
      問(wèn)題2:代碼line68分母的功能是?去掉乘以1.0代碼,重新編譯、運(yùn)行,結(jié)果有影響嗎?為什
      么要乘以1.0?
      將數(shù)據(jù)轉(zhuǎn)換為浮點(diǎn)型。有。輸出小數(shù)而不是整數(shù)。
      問(wèn)題3:從真實(shí)應(yīng)用場(chǎng)景角度考慮,GradeCalc類在設(shè)計(jì)及代碼實(shí)現(xiàn)細(xì)節(jié)上,有哪些地方尚未
      考慮周全,仍需繼續(xù)迭代、完善?
      缺少查詢功能。

      任務(wù)3:

      源碼:

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

      結(jié)果:

       答:

      問(wèn)題1:組合類GradeCalc定義中,成績(jī)存儲(chǔ)在哪里?組合類方法sort, min, max, average,
      output都要訪問(wèn)成績(jī),是通過(guò)什么訪問(wèn)到每一個(gè)成績(jī)的?觀察與實(shí)驗(yàn)任務(wù)2在代碼寫(xiě)法細(xì)節(jié)上
      的差別。
      成員vector。vector本身的接口。
      問(wèn)題2:對(duì)比實(shí)驗(yàn)任務(wù)2和實(shí)驗(yàn)任務(wù)3,主體代碼邏輯(測(cè)試代碼)沒(méi)有變更,類GradeCalc的
      接口也沒(méi)變,變化的是類GradeCalc的設(shè)計(jì)及接口內(nèi)部實(shí)現(xiàn)細(xì)節(jié)。你對(duì)面向?qū)ο缶幊逃惺裁葱?/div>
      的理解和領(lǐng)悟嗎?
      封裝方式多樣。

      任務(wù)4:

      源碼:

       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)即結(jié)束
      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 << "測(cè)試1: 使用標(biāo)準(zhǔn)輸入流對(duì)象cin輸入字符串" << endl;
      32     test1();
      33     cout << endl;
      34 
      35     //cin.ignore(numeric_limits<streamsize>::max(), '\n');
      36 
      37     cout << "測(cè)試2: 使用函數(shù)getline()輸入字符串" << endl;
      38     test2();
      39     cout << endl;
      40 
      41     cout << "測(cè)試3: 使用函數(shù)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 << "測(cè)試: 使用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 << "測(cè)試: 使用函數(shù)getline()多組輸入字符串" << endl;
      33     test();
      34 }

      結(jié)果:

       

       

       答:

      問(wèn)題1:去掉task4_1.cpp的line35,重新編譯、運(yùn)行,給出此時(shí)運(yùn)行結(jié)果截圖。查閱資料,回
      答line35在這里的用途是什么?

       消除回車。

      問(wèn)題2:去掉task4_3.cpp的line16,重新編譯、運(yùn)行,給出此時(shí)運(yùn)行結(jié)果。查閱資料,回答
      line16在這里的用途是什么?

       消除回車。

      任務(wù)5:

      源碼:

       1 #include<iostream>
       2 using namespace std;
       3 template <typename T>
       4 class GameResourceManager {
       5 private:
       6     T resource;
       7 public:
       8     GameResourceManager(T data);
       9     T get() const;
      10     void update(T date);
      11 };
      12 template <typename T>
      13 GameResourceManager<T>::GameResourceManager(T data) :resource{ data } {};
      14 template <typename T>
      15 T GameResourceManager<T>::get() const {
      16     return resource;
      17 }
      18 template <typename T>
      19 void GameResourceManager<T>::update(T data) {
      20     resource += data;
      21     if (resource < 0) {
      22         resource = 0;
      23     }
      24 }
      25 
      26 
      27 #include "grm.hpp"
      28 #include <iostream>
      29 
      30 using std::cout;
      31 using std::endl;
      32 
      33 void test1() {
      34     GameResourceManager<float> HP_manager(99.99);
      35     cout << "當(dāng)前生命值: " << HP_manager.get() << endl;
      36     HP_manager.update(9.99);
      37     cout << "增加9.99生命值后, 當(dāng)前生命值: " << HP_manager.get() << endl;
      38     HP_manager.update(-999.99);
      39     cout << "減少999.99生命值后, 當(dāng)前生命值: " << HP_manager.get() << endl;
      40 }
      41 
      42 void test2() {
      43     GameResourceManager<int> Gold_manager(100);
      44     cout << "當(dāng)前金幣數(shù)量: " << Gold_manager.get() << endl;
      45     Gold_manager.update(50);
      46     cout << "增加50個(gè)金幣后, 當(dāng)前金幣數(shù)量: " << Gold_manager.get() << endl;
      47     Gold_manager.update(-99);
      48     cout << "減少99個(gè)金幣后, 當(dāng)前金幣數(shù)量: " << Gold_manager.get() << endl;
      49 }
      50 
      51 
      52 int main() {
      53     cout << "測(cè)試1: 用float類型對(duì)類模板GameResourceManager實(shí)例化" << endl;
      54     test1();
      55     cout << endl;
      56 
      57     cout << "測(cè)試2: 用int類型對(duì)類模板GameResourceManager實(shí)例化" << endl;
      58     test2();
      59 }

       

      結(jié)果:

       

      任務(wù)6:

      源碼:

       1 #include<iostream>
       2 using namespace std;
       3 
       4 class Info {
       5 private:
       6     string nikename;
       7     string contact;
       8     string city;
       9     int n;
      10     static int sum;
      11 public:
      12     Info(string name, string ct, string ci, int num);
      13     Info(const Info& info);
      14     string getName()const;
      15     string getContact()const;
      16     string getCity()const;
      17     int getN()const;
      18     void display()const;
      19     static int getSum();
      20     static void add(int num);
      21 };
      22 int Info::sum = 0;
      23 Info::Info(string name, string ct, string ci, int num) :nikename{ name }, contact{ ct }, city{ ci }, n{ num } {}
      24 Info::Info(const Info& info) {
      25     nikename = info.getName();
      26     contact = info.getContact();
      27     city = info.getCity();
      28     n = info.getN();
      29 }
      30 string Info::getName()const {
      31     return nikename;
      32 }
      33 string Info::getContact()const {
      34     return contact;
      35 }
      36 string Info::getCity()const {
      37     return city;
      38 }
      39 int Info::getN()const {
      40     return n;
      41 }
      42 void Info::display()const {
      43     cout << "昵稱:\t" << nikename << endl
      44         << "聯(lián)系方式:\t" << contact << endl
      45         << "所在城市:\t" << city << endl
      46         << "預(yù)定人數(shù):\t" << n << endl;
      47 }
      48 int Info::getSum() {
      49     return sum;
      50 }
      51 void Info::add(int num) {
      52     sum += num;
      53 }
      54 
      55 
      56 #include"Info.hpp"
      57 #include<vector>
      58 #define capacity 100
      59 int main() {
      60     vector<Info> audience_lst;
      61     cout << "錄入用戶預(yù)約信息:" << endl;
      62     cout << "昵稱\t\t聯(lián)系方式(郵箱/手機(jī)號(hào))\t\t所在城市\(zhòng)t\t預(yù)約參加人數(shù)" << endl;
      63     while (Info::getSum() < capacity) {
      64         string name, ct, ci;
      65         int num;
      66         if (!(cin >> name >> ct >> ci >> num)) {
      67             break;
      68         }
      69         if (num + Info::getSum() > capacity) {
      70             cout << "對(duì)不起,只剩" << capacity - Info::getSum() << "個(gè)位置" << endl;
      71             cout << "1.輸入u,更新(update)預(yù)定信息" << endl << "2.輸入q,放棄預(yù)定" << endl << "你的選擇:";
      72             char choice;
      73             cin >> choice;
      74             if (choice == 'u') {
      75                 cout << "請(qǐng)重新輸入預(yù)定信息:" << endl;
      76                 continue;
      77             }
      78             else{
      79                 break;
      80             }
      81         }
      82         else {
      83             Info audience(name, ct, ci, num);
      84             Info::add(num);
      85             audience_lst.push_back(audience);
      86         }
      87     }
      88     cout << "截至目前為止,一共有" << Info::getSum() << "位聽(tīng)眾預(yù)約。預(yù)約聽(tīng)眾信息如下:" << endl;
      89     cout << "-------------------------------------" << endl;
      90     for (auto i : audience_lst) {
      91         cout << "昵稱:\t\t" << i.getName() << endl 
      92             << "聯(lián)系方式:\t" << i.getContact() << endl 
      93             << "所在城市:\t" << i.getCity() << endl 
      94             << "預(yù)定人數(shù):\t" << i.getN() << endl;
      95     }
      96     return 0;
      97 }

      結(jié)果:

       

       

       

      任務(wù)7:

      源碼:

        1 #pragma once
        2 #ifndef  ACCOUNT H
        3 #define  ACCOUNT H
        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 getTotal() { return total; }
       20 
       21    void show()const;
       22 
       23 };
       24 class SavingsAccount :public Account {
       25 private:
       26      Accumulator acc;
       27      double rate;
       28 public:
       29     SavingsAccount(const Date & date, const std::string & id, double rate);
       30     double getRate()const { return rate; }
       31 
       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 class CreditAccount :public Account {
       37 private:
       38     Accumulator acc;
       39     double credit;
       40     double rate;
       41     double fee;
       42     double getDebt()const {
       43         double balance = getBalance();
       44         return (balance < 0 ? balance : 0);
       45     }
       46 public:
       47     CreditAccount(const Date & date, const std::string & id, double credit, double rate, double fee);
       48     double getCredit()const { return credit; }
       49     double getRate()const { return rate; }
       50     double getAvailableCredit()const {
       51      if (getBalance() < 0)
       52            return credit + getBalance();
       53      else
       54            return credit;
       55     }
       56     void deposit(const Date & date, double amount, const std::string & desc);
       57     void withdraw(const Date & date, double amount, const std::string & desc);
       58     void settle(const Date & date);
       59     void show()const;
       60 };
       61 #endif//ACCOUNT H
       62 
       63 
       64 #pragma once
       65 #ifndef  ACCUMULATOR H
       66 #define  ACCUMULATOR H
       67 #include"date.h"
       68 class Accumulator {
       69 private:
       70     Date lastDate;
       71     double value;
       72     double sum;
       73 public:
       74      Accumulator(const Date & date, double value) :lastDate(date), value(value), sum{ 0 } {}
       75      double getSum(const Date & date)const {
       76          return sum + value * date.distance(lastDate);
       77      }
       78      void change(const Date & date, double value) {
       79          sum = getSum(date);
       80          lastDate = date; this->value = value;
       81      }
       82 
       83      void reset(const Date & date, double value) {
       84          lastDate = date; this->value = value; sum = 0;
       85      }
       86 };
       87 #endif//ACCUMULATOR H
       88 
       89 
       90 #pragma once
       91 #ifndef  DATE H
       92 #define  DATE H
       93 class Date {
       94 private:
       95     int year;
       96     int month;
       97     int day;
       98     int totalDays;
       99 public:
      100      Date(int year, int month, int day);
      101      int getYear()const { return year; }
      102      int getMonth()const { return month; }
      103      int getDay()const { return day; }
      104      int getMaxDay()const;
      105      bool isLeapYear()const {
      106          return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
      107      }
      108      void show()const;
      109      int distance(const Date & date)const {
      110          return totalDays - date.totalDays;
      111      }
      112 };
      113 #endif//  DATE H
      114 
      115 
      116 
      117 #include"account.h"
      118 #include<cmath>
      119 #include<iostream>
      120 using namespace std;
      121 double Account::total = 0;
      122 
      123 Account::Account(const Date & date, const string & id) :id{ id }, balance{ 0 } {
      124      date.show(); cout << "\t#" << id << "created" << endl;
      125 }
      126 void Account::record(const Date & date, double amount, const string & desc) {
      127      amount = floor(amount * 100 + 0.5) / 100;
      128      balance += amount;
      129      total += amount;
      130      date.show();
      131      cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << endl;
      132 }
      133 void Account::show()const { cout << id << "\tBalance:" << balance; }
      134 void Account::error(const string & msg)const {
      135      cout << "Error(#" << id << "):" << msg << endl;
      136 }
      137 
      138 SavingsAccount::SavingsAccount(const Date & date, const string & id, double rate) :Account(date, id), rate(rate), acc(date, 0) {}
      139 
      140 void SavingsAccount::deposit(const Date & date, double amount, const string & desc) {
      141      record(date, amount, desc);
      142      acc.change(date, getBalance());
      143 }
      144 void SavingsAccount::withdraw(const Date & date, double amount, const string & desc) {
      145      if (amount > getBalance()) {
      146          error("not enough money");
      147     }
      148      else {
      149          record(date, -amount, desc);
      150          acc.change(date, getBalance());
      151     }
      152 }
      153 
      154 void SavingsAccount::settle(const Date & date) {
      155     double interest = acc.getSum(date) * rate / date.distance(Date(date.getYear() - 1, 1, 1));
      156     if (interest != 0)record(date, interest, "interest");
      157     acc.reset(date, getBalance());
      158 }
      159 
      160 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) {}
      161 
      162 void CreditAccount::deposit(const Date & date, double amount, const string & desc) {
      163      record(date, amount, desc);
      164      acc.change(date, getDebt());
      165 }
      166 
      167 void CreditAccount::withdraw(const Date & date, double amount, const string & desc) {
      168     if (amount - getBalance() > credit) {
      169          error("not enough credit");
      170     }
      171     else {
      172          record(date, -amount, desc);
      173          acc.change(date, getDebt());
      174     }
      175 }
      176 
      177 void CreditAccount::settle(const Date & date) {
      178     double interest = acc.getSum(date) * rate;
      179     if (interest != 0)record(date, interest, "interest");
      180     if (date.getMonth() == 1)
      181     record(date, -fee, "annual fee");
      182     acc.reset(date, getDebt());
      183 }
      184 void CreditAccount::show()const {
      185      Account::show();
      186      cout << "\tAvailable credit:" << getAvailableCredit();
      187 }
      188 
      189 
      190 
      191 #include"date.h"
      192 #include<iostream>
      193 #include<cstdlib>
      194 using namespace std;
      195 namespace {
      196      const int DAYS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
      197 }
      198 Date::Date(int year, int month, int day) :year{ year }, month{ month }, day{ day } {
      199     if (day <= 0 || day > getMaxDay()) {
      200          cout << "Invalid date:";
      201          show();
      202          cout << endl;
      203          exit(1);
      204 
      205     }
      206     int years = year - 1;
      207     totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day;
      208     if (isLeapYear() && month > 2)totalDays++;
      209 }
      210 int Date::getMaxDay()const {
      211      if (isLeapYear() && month == 2)
      212          return 29;
      213      else return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1];
      214 }
      215 void Date::show()const {
      216      cout << getYear() << "-" << getMonth() << "-" << getDay();
      217 }
      218 
      219 
      220 
      221 #include"account.h"
      222 #include<iostream>
      223 
      224 using namespace std;
      225 
      226 int main() {
      227     Date date(2008, 11, 1);
      228     SavingsAccount sa1(date, "S3755217", 0.015);
      229     SavingsAccount sa2(date, "02342342", 0.015);
      230     CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);
      231     
      232     sa1.deposit(Date(2008, 11, 5), 5000, "salary");
      233     ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell");
      234     sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323");
      235     
      236     ca.settle(Date(2008, 12, 1));
      237     
      238     ca.deposit(Date(2008, 12, 1), 2016, "repay the credit");
      239     sa1.deposit(Date(2008, 12, 5), 5500, "salary");
      240     
      241         sa1.settle(Date(2009, 1, 1));
      242     sa2.settle(Date(2009, 1, 1));
      243     ca.settle(Date(2009, 1, 1));
      244     
      245         cout << endl;
      246     sa1.show(); cout << endl;
      247     sa2.show(); cout << endl;
      248     ca.show(); cout << endl;
      249     cout << "Total:" << Account::getTotal() << endl;
      250     return 0;
      251 }

      結(jié)果:

       

      posted @ 2024-11-23 19:35  阿瓜不瓜  閱讀(14)  評(píng)論(0)    收藏  舉報(bào)
      主站蜘蛛池模板: 国产不卡免费一区二区| 精品国产中文字幕av| 亚洲国产精品人人做人人爱| 国产破外女出血视频| 国产一区二区三区九精品| 黄色段片一区二区三区| 精品国产中文字幕av| 亚洲精品岛国片在线观看| 亚洲精品区午夜亚洲精品区| 久久久久人妻精品一区三寸 | 国产午夜福利一区二区三区| 乱色欧美激惰| 久久99国产精一区二区三区!| 国产熟睡乱子伦视频在线播放 | 夏河县| 九九热在线精品免费视频 | 尹人香蕉久久99天天拍欧美p7| 熟女精品视频一区二区三区| 欧美日韩在线亚洲二区综二| 青青草久热这里只有精品| 国产精品福利自产拍在线观看| 国产做a爱片久久毛片a片| 精品无码三级在线观看视频| 久天啪天天久久99久孕妇| 熟女一区二区中文字幕| 成人av天堂男人资源站| 欧美人与zoxxxx另类| 欧美成人va免费大片视频| 亚洲欧美牲交| 北川| 久久国产免费观看精品3| 墨玉县| 国产亚洲精品黑人粗大精选| 国产免费午夜福利757| 日本亚洲一区二区精品| 狠狠综合久久av一区二| 国产成人精品1024免费下载| 国产美女午夜福利视频| 精品一区二区成人码动漫| caoporn免费视频公开| 国产熟女一区二区三区蜜臀 |