实验四

实验一代码:

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

运行截图:

 

 

实验二代码:

  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();                             // 录入成绩
 18     void output() const;                      // 输出成绩
 19     void sort(bool ascending = false);        // 排序 (默认降序)
 20     int min() const;                          // 返回最低分
 21     int max() const;                          // 返回最高分
 22     float average() const;                    // 返回平均分
 23     void info();                              // 输出课程成绩信息 
 24 
 25 private:
 26     void compute();     // 成绩统计
 27 
 28 private:
 29     string course_name;     // 课程名
 30     int n;                  // 课程人数
 31     vector<int> counts = vector<int>(5, 0);      // 保存各分数段人数([0, 60), [60, 70), [70, 80), [80, 90), [90, 100]
 32     vector<double> rates = vector<double>(5, 0); // 保存各分数段比例 
 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 << "排序后成绩: \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();  // 统计各分数段人数、比例
 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 } 
104 
105 
106 
107 
108 
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

 

运行截图:

问题1:利用this指针储存在派生类的动态数组vector中,通过基类的外部接口,派生类去访问基类的外部接口

问题2:求和储存的所有成绩,有,将求和的整形数转化为float类型,后面求平均数才会保留分数

问题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();                             // 录入成绩
 18     void output() const;                      // 输出成绩
 19     void sort(bool ascending = false);        // 排序 (默认降序)
 20     int min() const;                          // 返回最低分
 21     int max() const;                          // 返回最高分
 22     float average() const;                    // 返回平均分
 23     void info();                              // 输出课程成绩信息 
 24 
 25 private:
 26     void compute();     // 成绩统计
 27 
 28 private:
 29     string course_name;     // 课程名
 30     int n;                  // 课程人数
 31     vector<int> grades;     // 课程成绩
 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         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 << "排序后成绩: \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();  // 统计各分数段人数、比例
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 } 
106 
107 
108 
109 
110 #include "GradeCalc.hpp"
111 #include <iomanip>
112 
113 void test() {
114     int n;
115     cout << "输入班级人数: ";
116     cin >> n;
117 
118     GradeCalc c1("OOP", n);
119 
120     cout << "录入成绩: " << endl;;
121     c1.input();
122     cout << "输出成绩: " << endl;
123     c1.output();
124 
125     cout << string(20, '*') + "课程成绩信息"  + string(20, '*') << endl;
126     c1.info();
127 }
128 
129 int main() {
130     test();
131 }
View Code

 

运行截图:

问题1:储存在派生类的私有成员grades动态数组中,直接访问派生类的私有成员

问题2:派生类可以直接使用基类里的成员函数

 

实验四代码:

  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 }
 44 
 45 
 46 
 47 
 48 
 49 
 50 #include <iostream>
 51 #include <string>
 52 #include <vector>
 53 #include <limits>
 54 
 55 using namespace std;
 56 
 57 void output(const vector<string> &v) {
 58     for(auto &s: v)
 59         cout << s << endl;
 60 }
 61 
 62 void test() {
 63     int n;
 64     while(cout << "Enter n: ", cin >> n) {
 65         vector<string> v1;
 66 
 67         for(int i = 0; i < n; ++i) {
 68             string s;
 69             cin >> s;
 70             v1.push_back(s);
 71         }
 72 
 73         cout << "output v1: " << endl;
 74         output(v1); 
 75         cout << endl;
 76     }
 77 }
 78 
 79 int main() {
 80     cout << "测试: 使用cin多组输入字符串" << endl;
 81     test();
 82 }
 83 
 84 
 85 
 86 
 87 
 88 #include <iostream>
 89 #include <string>
 90 #include <vector>
 91 #include <limits>
 92 
 93 using namespace std;
 94 
 95 void output(const vector<string> &v) {
 96     for(auto &s: v)
 97         cout << s << endl;
 98 }
 99 
100 void test() {
101     int n;
102     while(cout << "Enter n: ", cin >> n) {
103         cin.ignore(numeric_limits<streamsize>::max(), '\n');
104 
105         vector<string> v2;
106 
107         for(int i = 0; i < n; ++i) {
108             string s;
109             getline(cin, s);
110             v2.push_back(s);
111         }
112         cout << "output v2: " << endl;
113         output(v2); 
114         cout << endl;
115     }
116 }
117 
118 int main() {
119     cout << "测试: 使用函数getline()多组输入字符串" << endl;
120     test();
121 }
View Code

 

运行截图

 

 问题1:忽略剩余的输入行,包括换行符

 

问题2:忽略剩余的输入行,包括换行符

 

 

实验五代码:

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

 

运行截图:

 

 

实验六代码:

 1 #include<iostream>
 2 #include<iomanip>
 3 
 4 using namespace std;
 5 
 6 
 7 string s(32,'-');
 8 class info{
 9     public:
10         info(string nick,string con,string cit,int num);
11         void display();
12     private:
13         string nickname,contact,city;
14         int n;
15 };
16 info::info(string nick,string con,string cit,int num):nickname(nick),contact(con),city(cit),n(num){}
17 void info::display(){
18     cout<<s<<endl; 
19     cout<<"昵称:"<<setw(15)<<nickname<<endl;
20     cout<<"联系方式:"<<setw(25)<<contact<<endl;
21     cout<<"所在城市:"<<setw(25)<<city<<endl;
22     cout<<"预定人数:"<<setw(25)<<n<<endl;
23 }
24 
25 
26 
27 
28 
29 
30 #include<iostream>
31 #include "info.hpp"
32 #include<vector>
33 #include<iomanip>
34 
35 
36 using namespace std;
37 
38 int main(){
39     const int capacity=100;
40     vector<info>oudience_list;
41     string nickname,contact,city;
42     char s;
43     int num,sum=0,i;
44     cout<<left<<setw(15)<<"昵称"<<left<<setw(30)<<"联系方式(邮箱/手机号)";
45     cout<<left<<setw(20)<<"所在城市"<<left<<setw(25)<<"预定参加人数"<<endl;
46     while(cin>>nickname){
47         cin>>contact;
48         cin>>city;
49         cin>>num;
50         oudience_list.push_back({nickname,contact,city,num});
51         sum+=num;
52         if(sum>capacity){
53             cout<<"对不起,只剩"<<capacity-sum+num<<"个位置."<<endl;
54             cout<<"1.输入u,更新(update)预定信息"<<endl;
55             cout<<"2.输入q,退出预定"<<endl;
56             cout<<"你的选择:";
57             cin>>s;
58             if(s='u'){
59                 oudience_list.pop_back();
60                 cout<<"请重新输入预定信息:"<<endl;
61                 sum=sum-num; 
62             }
63             else{
64                 oudience_list.pop_back();
65                 sum=sum-num;
66                 break;
67             }
68         }
69         else if(sum==capacity)
70                 break;
71     }
72     cout<<"截至目前,一共有"<<sum<<"位观众预约,预约观众信息如下:" <<endl;
73     for(auto i:oudience_list)
74         i.display();
75     return 0;
76 }
View Code

运行截图:

 

实验七代码:

1 #pragma once
 2 class Date{
 3     private:
 4         int year;
 5         int month;
 6         int day;
 7         int totalDays;
 8     public:
 9         Date(int year,int month,int day);
10         int getYear()const {return year;}
11         int getMonth()const{return month;}
12         int getDay()const{return day;}
13         int getMaxDay()const;
14         bool isLeapYear()const{
15             return year%4==0&&year%100!=0||year%400==0;
16         }
17         void show()const;
18         int distance(const Date&date)const{
19             return totalDays-date.totalDays;
20         }
21 };

date.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 Date::Date(int year,int month,int day):year(year),month(month),day(day){
 9     if(day<=0||day>getMaxDay()){
10         cout<<"Invalid date: ";
11         show();
12         cout<<endl;
13         exit(1);
14     }
15     int years=year-1;
16     totalDays=years*365+years/4-years/100+years/400+DAYS_BEFORE_MONTH[month-1]+day;
17     if(isLeapYear()&&month>2)totalDays++;
18 }
19 int Date::getMaxDay()const{
20     if(isLeapYear()&&month==2)
21         return 29;
22     else
23         return DAYS_BEFORE_MONTH[month]-DAYS_BEFORE_MONTH[month-1];
24 }
25 void Date::show()const{
26     cout<<getYear()<<"-"<<getMonth()<<"-"<<getDay();
27 }

date.cpp



1 #pragma once
 2 #include "date.h"
 3 class Accumulator {
 4 private:
 5     Date lastDate;
 6     double value;
 7     double sum;
 8 public:
 9     Accumulator(const Date&date,double value):lastDate(date),value(value),sum{0}{}
10     double getSum(const Date& date) const {
11         return sum + value * date.distance(lastDate);
12     }
13     void change(const Date& date, double value) {
14         sum = getSum(date);
15         lastDate = date;
16         this->value = value;
17     }
18     void reset(const Date& date, double value) {
19         lastDate = date;
20         this->value;
21         sum = 0;
22     }
23 };

accumulator.h




1 #pragma once
 2 #include"date.h"
 3 #include"accumulator.h"
 4 #include<string>
 5 using namespace std;
 6 class Account {
 7 private:
 8     string id;
 9     double balance;
10     static double total;
11 protected:
12     Account(const Date& date, const string &id);
13     void record(const Date& date, double amount, const string& desc);
14     void error(const string& msg) const;
15 public:const string& getId() { return id; }
16       double getBalance()const { return balance; }
17       static double getTotal() { return total; }
18       void show()const;
19 };
20 class SavingsAccount:public Account {
21 private:
22     Accumulator acc;
23     double rate;
24 public:
25     SavingsAccount(const Date& date, const string& id, double rate);
26     double getRate() const { return  rate; }
27     void deposit(const Date& date, double amount, const string& desc);
28     void withdraw(const Date& date, double amount, const string& desc);
29     void settle(const Date& date);
30 };
31 class CreditAccount :public Account {
32 private:
33     Accumulator acc;
34     double credit;
35     double rate;
36     double fee;
37     double getDebt()const {
38         double balance = getBalance();
39         return(balance < 0 ? balance : 0);
40     }
41 public:CreditAccount(const Date& date, const string& id, double credit, double rate, double fee);
42       double getCredit()const { return credit; }
43       double getRate()const { return rate; }
44       double getFee() const { return fee; }
45       double getAvailableCredit()const {
46           if (getBalance() < 0)return credit + getBalance();
47           else return credit;
48       }
49       void deposit(const Date& date, double amount, const string& desc);
50       void withdraw(const Date& date, double amount, const string& desc);
51       void settle(const Date& date);
52       void show()const;
53 };

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)
 7     :id(id),balance(0){
 8         date.show();cout<<"\t#"<<id<<"created"<<endl;
 9     }
10 void Account::record(const Date &date,double amount,const string &desc){
11     amount=floor(amount*100+0.5)/100;
12     balance+=amount;total+=amount;
13     date.show();
14     cout<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<"\t"<<desc<<endl;
15 }
16 void Account::show()const{cout<<id<<"\tBalance:"<<balance;}
17 void Account::error(const string &msg)const{
18     cout<<"Error(#"<<id<<"):"<<msg<<endl;
19 }
20 SavingsAccount::SavingsAccount(const Date&date,const string &id,double rate):Account(date,id),rate(rate),acc(date,0){}
21 void SavingsAccount::deposit(const Date& date, double amount, const string& desc) {
22     record(date, amount, desc);
23     acc.change(date, getBalance());
24 }
25 void SavingsAccount::withdraw(const Date& date, double amount, const string& desc) {
26     if (amount > getBalance()) {
27         error("not enough money");
28     }
29     else {
30         record(date, -amount, desc);
31         acc.change(date, getBalance());
32     }
33 }
34 void SavingsAccount::settle(const Date& date) {
35     double interest = acc.getSum(date) * rate/date.distance(Date(date.getYear()-1,1,1));
36     if (interest != 0)record(date, interest, "interest");
37     acc.reset(date, getBalance());
38 }
39 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){}
40 void CreditAccount::deposit(const Date& date, double amount, const string& desc) {
41     record(date, amount, desc);
42     acc.change(date, getDebt());
43 }
44 void CreditAccount::withdraw(const Date& date, double amount, const string& desc) {
45     if (amount - getBalance() > credit) {
46         error("not enouogh 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)record(date, -fee, "annual fee");
57     acc.reset(date, getDebt());
58 }
59 void CreditAccount::show()const {
60     Account::show();
61     cout << "\tAvailable credit:" << getAvailableCredit();
62 }
account.cpp




1 #include "account.h"
 2 #include<iostream>
 3 using namespace std;
 4 int main() {
 5     Date date(2008, 11, 1);
 6     SavingsAccount sa1(date, "S3755217", 0.015);
 7     SavingsAccount sa2(date, "02342342", 0.015);
 8     CreditAccount ca(date, "C5392394", 10000, 0.0005, 50);
 9     sa1.deposit(Date(2008, 11, 5), 5000, "Salary");
10     ca.withdraw(Date(2008, 11, 15), 2000, "buy a cell");
11     sa2.deposit(Date(2008, 11, 25), 10000, "sell stock 0323");
12     ca.settle(Date(2008, 12, 1));
13     ca.deposit(Date(2008, 12,1), 2016, "repay the credit");
14     sa1.deposit(Date(2008, 12, 5), 5500, "salary");
15     sa1.settle(Date(2009, 1, 1));
16     sa2.settle(Date(2009, 1, 1));
17     ca.settle(Date(2009, 1, 1));
18     cout << endl;
19     sa1.show(); cout << endl;
20     sa2.show(); cout << endl;
21     ca.show(); cout << endl;
22     cout << "Total: " << Account::getTotal() << endl;
23     return 0;
24 }

7_10.cpp
View Code

 

运行截图:

 

posted @ 2024-11-24 13:28  严文奇  阅读(2)  评论(0编辑  收藏  举报