实验四

实验任务1:

(1)代码部分:

  1 task1_1
  2 #include <iostream>
  3 
  4 using std::cout;
  5 using std::endl;
  6 
  7 // 类A的定义
  8 class A {
  9 public:
 10     A(int x0, int y0);
 11     void display() const;
 12 
 13 private:
 14     int x, y;
 15 };
 16 
 17 A::A(int x0, int y0): x{x0}, y{y0} {
 18 }
 19 
 20 void A::display() const {
 21     cout << x << ", " << y << endl;
 22 }
 23 
 24 // 类B的定义
 25 class B {
 26 public:
 27     B(double x0, double y0);
 28     void display() const;
 29 
 30 private:
 31     double x, y;
 32 };
 33 
 34 B::B(double x0, double y0): x{x0}, y{y0} {
 35 }
 36 
 37 void B::display() const {
 38     cout << x << ", " << y << endl;
 39 }
 40 
 41 void test() {
 42     cout << "测试类A: " << endl;
 43     A a(3, 4);
 44     a.display();
 45 
 46     cout << "\n测试类B: " << endl;
 47     B b(3.2, 5.6);
 48     b.display();
 49 }
 50 
 51 int main() {
 52     test();
 53 }
 54 
 55 task1_2
 56 #include <iostream>
 57 #include <string>
 58 
 59 using std::cout;
 60 using std::endl;
 61 using std::string;
 62 
 63 // 定义类模板
 64 template<typename T>
 65 class X{
 66 public:
 67     X(T x0, T y0);
 68     void display();
 69 
 70 private:
 71     T x, y;
 72 };
 73 
 74 template<typename T>
 75 X<T>::X(T x0, T y0): x{x0}, y{y0} {
 76 }
 77 
 78 template<typename T>
 79 void X<T>::display() {
 80     cout << x << ", " << y << endl;
 81 }
 82 
 83 
 84 void test() {
 85     cout << "测试1: 类模板X中的抽象类型T用int实例化" << endl;
 86     X<int> x1(3, 4);
 87     x1.display();
 88     
 89     cout << endl;
 90 
 91     cout << "测试2: 类模板X中的抽象类型T用double实例化" << endl;
 92     X<double> x2(3.2, 5.6);
 93     x2.display();
 94 
 95     cout << endl;
 96 
 97     cout << "测试3: 类模板X中的抽象类型T用string实例化" << endl;
 98     X<string> x3("hello", "oop");
 99     x3.display();
100 }
101 
102 int main() {
103     test();
104 }
View Code

 

(2)运行结果:

 

 

实验任务2:

(1)代码部分:

  1 GradeCalc.hpp
  2 #include <iostream>
  3 #include <vector>
  4 #include <string>
  5 #include <algorithm>
  6 #include <numeric>
  7 #include <iomanip>
  8 
  9 using std::vector;
 10 using std::string;
 11 using std::cin;
 12 using std::cout;
 13 using std::endl;
 14 
 15 class GradeCalc: public vector<int> {
 16 public:
 17     GradeCalc(const string &cname, int size);      
 18     void input();                             // 录入成绩
 19     void output() const;                      // 输出成绩
 20     void sort(bool ascending = false);        // 排序 (默认降序)
 21     int min() const;                          // 返回最低分
 22     int max() const;                          // 返回最高分
 23     float average() const;                    // 返回平均分
 24     void info();                              // 输出课程成绩信息 
 25 
 26 private:
 27     void compute();     // 成绩统计
 28 
 29 private:
 30     string course_name;     // 课程名
 31     int n;                  // 课程人数
 32     vector<int> counts = vector<int>(5, 0);      // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100]
 33     vector<double> rates = vector<double>(5, 0); // 保存各分数段比例 
 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         this->push_back(grade);
 44     } 
 45 }  
 46 
 47 void GradeCalc::output() const {
 48     for(auto ptr = this->begin(); ptr != this->end(); ++ptr)
 49         cout << *ptr << " ";
 50     cout << endl;
 51 } 
 52 
 53 void GradeCalc::sort(bool ascending) {
 54     if(ascending)
 55         std::sort(this->begin(), this->end());
 56     else
 57         std::sort(this->begin(), this->end(), std::greater<int>());
 58 }  
 59 
 60 int GradeCalc::min() const {
 61     return *std::min_element(this->begin(), this->end());
 62 }  
 63 
 64 int GradeCalc::max() const {
 65     return *std::max_element(this->begin(), this->end());
 66 }    
 67 
 68 float GradeCalc::average() const {
 69     return std::accumulate(this->begin(), this->end(), 0) * 1.0 / n;
 70 }   
 71 
 72 void GradeCalc::compute() {
 73     for(int grade: *this) {
 74         if(grade < 60)
 75             counts.at(0)++;
 76         else if(grade >= 60 && grade < 70)
 77             counts.at(1)++;
 78         else if(grade >= 70 && grade < 80)
 79             counts.at(2)++;
 80         else if(grade >= 80 && grade < 90)
 81             counts.at(3)++;
 82         else if(grade >= 90)
 83             counts.at(4)++;
 84     }
 85 
 86     for(int i = 0; i < rates.size(); ++i)
 87         rates.at(i) = counts.at(i) * 1.0 / n;
 88 }
 89 
 90 void GradeCalc::info()  {
 91     cout << "课程名称:\t" << course_name << endl;
 92     cout << "排序后成绩: \t";
 93     sort();  output();
 94     cout << "最高分:\t" << max() << endl;
 95     cout << "最低分:\t" << min() << endl;
 96     cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl;
 97     
 98     compute();  // 统计各分数段人数、比例
 99 
100     vector<string> tmp{"[0, 60)  ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]"};
101     for(int i = tmp.size()-1; i >= 0; --i)
102         cout << tmp[i] << "\t: " << counts[i] << "人\t" 
103              << std::fixed << std::setprecision(2) << rates[i]*100 << "%" << endl; 
104 } 
105 
106 demo2.cpp
107 #include "GradeCalc.hpp"
108 #include <iomanip>
109 
110 void test() {
111     int n;
112     cout << "输入班级人数: ";
113     cin >> n;
114 
115     GradeCalc c1("OOP", n);
116 
117     cout << "录入成绩: " << endl;;
118     c1.input();
119     cout << "输出成绩: " << endl;
120     c1.output();
121 
122     cout << string(20, '*') + "课程成绩信息"  + string(20, '*') << endl;
123     c1.info();
124 }
125 
126 int main() {
127     test();
128 }
View Code

 

(2)运行结果:

 

(3)问题1:在派生类GradeCalc的定义中,成绩储存在int类型的vector数组中;通过vector提供的接口来访问成绩;input()是通过vector提供的push_back()接口向队尾插入新元素;

    问题2:分母的功能是计算平均分;结果有影响,因为没有乘以1.0结果为int类型,小数部分会四舍五入,精度缺失;乘以1.0将数据变为double类型避免精度缺失;

    问题3:没有对输入成绩进行合法性判断,可能会输入超出总分范围的成绩;考虑支持输入其他类型数据,如浮点数等;

 

实验任务3:

(1)代码部分:

  1 GradeCalc.hpp
  2 #include <iostream>
  3 #include <vector>
  4 #include <string>
  5 #include <algorithm>
  6 #include <numeric>
  7 #include <iomanip>
  8 
  9 using std::vector;
 10 using std::string;
 11 using std::cin;
 12 using std::cout;
 13 using std::endl;
 14 
 15 class GradeCalc {
 16 public:
 17     GradeCalc(const string &cname, int size);      
 18     void input();                             // 录入成绩
 19     void output() const;                      // 输出成绩
 20     void sort(bool ascending = false);        // 排序 (默认降序)
 21     int min() const;                          // 返回最低分
 22     int max() const;                          // 返回最高分
 23     float average() const;                    // 返回平均分
 24     void info();                              // 输出课程成绩信息 
 25 
 26 private:
 27     void compute();     // 成绩统计
 28 
 29 private:
 30     string course_name;     // 课程名
 31     int n;                  // 课程人数
 32     vector<int> grades;     // 课程成绩
 33     vector<int> counts = vector<int>(5, 0);      // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100]
 34     vector<double> rates = vector<double>(5, 0); // 保存各分数段比例 
 35 };
 36 
 37 GradeCalc::GradeCalc(const string &cname, int size): course_name{cname}, n{size} {}   
 38 
 39 void GradeCalc::input() {
 40     int grade;
 41 
 42     for(int i = 0; i < n; ++i) {
 43         cin >> grade;
 44         grades.push_back(grade);
 45     } 
 46 }  
 47 
 48 void GradeCalc::output() const {
 49     for(int grade: grades)
 50         cout << grade << " ";
 51     cout << endl;
 52 } 
 53 
 54 void GradeCalc::sort(bool ascending) {
 55     if(ascending)
 56         std::sort(grades.begin(), grades.end());
 57     else
 58         std::sort(grades.begin(), grades.end(), std::greater<int>());
 59         
 60 }  
 61 
 62 int GradeCalc::min() const {
 63     return *std::min_element(grades.begin(), grades.end());
 64 }  
 65 
 66 int GradeCalc::max() const {
 67     return *std::max_element(grades.begin(), grades.end());
 68 }    
 69 
 70 float GradeCalc::average() const {
 71     return std::accumulate(grades.begin(), grades.end(), 0) * 1.0 / n;
 72 }   
 73 
 74 void GradeCalc::compute() {
 75     for(int grade: grades) {
 76         if(grade < 60)
 77             counts.at(0)++;
 78         else if(grade >= 60 && grade < 70)
 79             counts.at(1)++;
 80         else if(grade >= 70 && grade < 80)
 81             counts.at(2)++;
 82         else if(grade >= 80 && grade < 90)
 83             counts.at(3)++;
 84         else if(grade >= 90)
 85             counts.at(4)++;
 86     }
 87 
 88     for(int i = 0; i < rates.size(); ++i)
 89         rates.at(i) = counts.at(i) *1.0 / n;
 90 }
 91 
 92 void GradeCalc::info()  {
 93     cout << "课程名称:\t" << course_name << endl;
 94     cout << "排序后成绩: \t";
 95     sort();  output();
 96     cout << "最高分:\t" << max() << endl;
 97     cout << "最低分:\t" << min() << endl;
 98     cout << "平均分:\t" << std::fixed << std::setprecision(2) << average() << endl;
 99     
100     compute();  // 统计各分数段人数、比例
101 
102     vector<string> tmp{"[0, 60)  ", "[60, 70)", "[70, 80)","[80, 90)", "[90, 100]"};
103     for(int i = tmp.size()-1; i >= 0; --i)
104         cout << tmp[i] << "\t: " << counts[i] << "人\t" 
105              << std::fixed << std::setprecision(2) << rates[i]*100 << "%" << endl; 
106 } 
107 
108 demo3.cpp
109 #include "GradeCalc.hpp"
110 #include <iomanip>
111 
112 void test() {
113     int n;
114     cout << "输入班级人数: ";
115     cin >> n;
116 
117     GradeCalc c1("OOP", n);
118 
119     cout << "录入成绩: " << endl;;
120     c1.input();
121     cout << "输出成绩: " << endl;
122     c1.output();
123 
124     cout << string(20, '*') + "课程成绩信息"  + string(20, '*') << endl;
125     c1.info();
126 }
127 
128 int main() {
129     test();
130 }
View Code

 

(2)运行结果:

 

(3)问题1:在派生类GradeCalc中,成绩存储在类的私有成员grades中;sort,input等函数是通过vector提供的接口访问成绩的;

    问题2:面向对象允许我们通过多种方式实现同一功能,可以通过不断改进提升代码质量;

 

 

实验任务4:

(1)代码部分:

  1 task4_1
  2 #include <iostream>
  3 #include <string>
  4 #include <limits>
  5 
  6 using namespace std;
  7 
  8 void test1() {
  9     string s1, s2;
 10     cin >> s1 >> s2;  // cin: 从输入流读取字符串, 碰到空白符(空格/回车/Tab)即结束
 11     cout << "s1: " << s1 << endl;
 12     cout << "s2: " << s2 << endl;
 13 }
 14 
 15 void test2() {
 16     string s1, s2;
 17     getline(cin, s1);  // getline(): 从输入流中提取字符串,直到遇到换行符
 18     getline(cin, s2);
 19     cout << "s1: " << s1 << endl;
 20     cout << "s2: " << s2 << endl;
 21 }
 22 
 23 void test3() {
 24     string s1, s2;
 25     getline(cin, s1, ' '); //从输入流中提取字符串,直到遇到指定分隔符
 26     getline(cin, s2);
 27     cout << "s1: " << s1 << endl;
 28     cout << "s2: " << s2 << endl;
 29 }
 30 
 31 int main() {
 32     cout << "测试1: 使用标准输入流对象cin输入字符串" << endl;
 33     test1();
 34     cout << endl;
 35 
 36     cin.ignore(numeric_limits<streamsize>::max(), '\n');
 37 
 38     cout << "测试2: 使用函数getline()输入字符串" << endl;
 39     test2();
 40     cout << endl;
 41 
 42     cout << "测试3: 使用函数getline()输入字符串, 指定字符串分隔符" << endl;
 43     test3();
 44 }
 45 
 46 task4_2
 47 #include <iostream>
 48 #include <string>
 49 #include <vector>
 50 #include <limits>
 51 
 52 using namespace std;
 53 
 54 void output(const vector<string> &v) {
 55     for(auto &s: v)
 56         cout << s << endl;
 57 }
 58 
 59 void test() {
 60     int n;
 61     while(cout << "Enter n: ", cin >> n) {
 62         vector<string> v1;
 63 
 64         for(int i = 0; i < n; ++i) {
 65             string s;
 66             cin >> s;
 67             v1.push_back(s);
 68         }
 69 
 70         cout << "output v1: " << endl;
 71         output(v1); 
 72         cout << endl;
 73     }
 74 }
 75 
 76 int main() {
 77     cout << "测试: 使用cin多组输入字符串" << endl;
 78     test();
 79 }
 80 
 81 task4_3
 82 #include <iostream>
 83 #include <string>
 84 #include <vector>
 85 #include <limits>
 86 
 87 using namespace std;
 88 
 89 void output(const vector<string> &v) {
 90     for(auto &s: v)
 91         cout << s << endl;
 92 }
 93 
 94 void test() {
 95     int n;
 96     while(cout << "Enter n: ", cin >> n) {
 97         cin.ignore(numeric_limits<streamsize>::max(), '\n');
 98 
 99         vector<string> v2;
100 
101         for(int i = 0; i < n; ++i) {
102             string s;
103             getline(cin, s);
104             v2.push_back(s);
105         }
106         cout << "output v2: " << endl;
107         output(v2); 
108         cout << endl;
109     }
110 }
111 
112 int main() {
113     cout << "测试: 使用函数getline()多组输入字符串" << endl;
114     test();
115 }
View Code

 

(2)运行结果:

 

 

 

(3)问题1:用来清除以回车结束的输入缓冲区的内容,消除上一次输入对下一次输入的影响;

    问题2:用来消除输入n的值时输入的回车键对下方输入字符串的影响;

 

实验任务5

(1)代码部分:

 1 grm.hpp
 2 #pragma once
 3 
 4 #include<bits/stdc++.h>
 5 using namespace std;
 6 
 7 template<typename T>
 8 class GameResourceManager{
 9 public:
10     GameResourceManager(T n):num{n}{}
11     T get(){
12         return num;
13     }
14     void update(T n){
15         num+=n;
16         if(num<0)
17         num=0;
18     }
19 private:
20     T num;
21 }; 
22 task5.cpp
23 #include "grm.hpp"
24 #include <iostream>
25 
26 using std::cout;
27 using std::endl;
28 
29 void test1() {
30     GameResourceManager<float> HP_manager(99.99);
31     cout << "当前生命值: " << HP_manager.get() << endl;
32     HP_manager.update(9.99);
33     cout << "增加9.99生命值后, 当前生命值: " << HP_manager.get() << endl;
34     HP_manager.update(-999.99);
35     cout <<"减少999.99生命值后, 当前生命值: " << HP_manager.get() << endl;
36 }
37 
38 void test2() {
39     GameResourceManager<int> Gold_manager(100);
40     cout << "当前金币数量: " << Gold_manager.get() << endl;
41     Gold_manager.update(50);
42     cout << "增加50个金币后, 当前金币数量: " << Gold_manager.get() << endl;
43     Gold_manager.update(-99);
44     cout <<"减少99个金币后, 当前金币数量: " << Gold_manager.get() << endl;
45 }
46 
47 
48 int main() {
49     cout << "测试1: 用float类型对类模板GameResourceManager实例化" << endl;
50     test1();
51     cout << endl;
52 
53     cout << "测试2: 用int类型对类模板GameResourceManager实例化" << endl;
54     test2();
55 }
View Code

 

(2)运行结果:

 

 

实验任务6

(1)代码部分:

 1 #pragma once
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 
 5 class info{
 6 public:
 7     info(string s1,string s2,string s3,int num):nickname{s1},contact{s2},city{s3},member{num}{}
 8     void dispaly(){
 9         cout<<"昵称:"<<"\t"<<nickname<<endl;
10         cout<<"联系方式:"<<"\t"<<contact<<endl;
11         cout<<"所在城市:"<<"\t"<<city<<endl;
12         cout<<"预定参加人数:"<<"\t"<<member<<endl;
13         int i=30;
14         while(i){
15             cout<<'-';
16             i--;
17         }
18         cout<<endl; 
19     }
20 private:
21     string nickname;
22     string contact;
23     string city;
24     int member;
25 };
26 #include"info.hpp"
27 #include<bits/stdc++.h>
28 using namespace std;
29 
30 int main(){
31     int sum=0,n;
32     string s1,s2,s3; 
33     const int capacity=100;
34     vector<info> audience_info_list;
35     cout<<"录入用户预约信息:"<<"\n"<<endl;
36     cout<<"昵称"<<"\t"<<"联系方式"<<"\t"<<"所在城市"<<"\t"<<"预定参加人数"<<endl;
37     while(cin>>s1){
38         if(s1=="End")break;cin>>s2>>s3>>n;
39         if(sum+n>capacity){
40             cout<<"对不起,只剩"<<capacity-sum<<"个位置"<<endl;
41             cout<<"输入u,更新(update)预定信息"<<endl;
42             cout<<"输入q,退出预定"<<endl;
43             cout<<"你的选择:"; 
44             char ch;
45             cin>>ch;
46             if(ch=='u'){
47                 cout<<"请重新输入预定信息:"<<endl; 
48                 continue;
49             }
50             else
51             break; 
52         }
53         info member(s1,s2,s3,n);
54         sum+=n;
55         audience_info_list.push_back(member);
56         if(sum==capacity)break;
57     }
58     cout<<"截至目前,一共"<<sum<<"位听众预约。预约听众信息如下:"<<endl;
59     for(int i=1;i<=30;i++)
60     cout<<'-';
61     cout<<endl;
62     for(int i=0;i<audience_info_list.size();i++)
63     audience_info_list[i].dispaly();
64     return 0;
65 }
View Code

 

(2)运行结果:

 

 

实验任务7:

(1)代码部分:

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

 

(2)运行结果:

 

posted @ 2024-11-24 17:46  刘晔yyy  阅读(7)  评论(0编辑  收藏  举报