forgiver

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

task 1

代码:

t.h

 1 #pragma once
 2 
 3 #include <string>
 4 
 5 // 类T: 声明
 6 class T {
 7 // 对象属性、方法
 8 public:
 9     T(int x = 0, int y = 0);   // 普通构造函数
10     T(const T &t);  // 复制构造函数
11     T(T &&t);       // 移动构造函数
12     ~T();           // 析构函数
13 
14     void adjust(int ratio);      // 按系数成倍调整数据
15     void display() const;           // 以(m1, m2)形式显示T类对象信息
16 
17 private:
18     int m1, m2;
19 
20 // 类属性、方法
21 public:
22     static int get_cnt();          // 显示当前T类对象总数
23 
24 public:
25     static const std::string doc;       // 类T的描述信息
26     static const int max_cnt;           // 类T对象上限
27 
28 private:
29     static int cnt;         // 当前T类对象数目
30 
31 // 类T友元函数声明
32     friend void func();
33 };
34 
35 // 普通函数声明
36 void func();

t.cpp

 1 // 类T: 实现
 2 // 普通函数实现
 3 
 4 #include "t.h"
 5 #include <iostream>
 6 #include <string>
 7 
 8 using std::cout;
 9 using std::endl;
10 using std::string;
11 
12 // static成员数据类外初始化
13 const std::string T::doc{"a simple class sample"};
14 const int T::max_cnt = 999;
15 int T::cnt = 0;
16 
17 
18 // 对象方法
19 T::T(int x, int y): m1{x}, m2{y} { 
20     ++cnt; 
21     cout << "T constructor called.\n";
22 } 
23 
24 T::T(const T &t): m1{t.m1}, m2{t.m2} {
25     ++cnt;
26     cout << "T copy constructor called.\n";
27 }
28 
29 T::T(T &&t): m1{t.m1}, m2{t.m2} {
30     ++cnt;
31     cout << "T move constructor called.\n";
32 }    
33 
34 T::~T() {
35     --cnt;
36     cout << "T destructor called.\n";
37 }           
38 
39 void T::adjust(int ratio) {
40     m1 *= ratio;
41     m2 *= ratio;
42 }    
43 
44 void T::display() const {
45     cout << "(" << m1 << ", " << m2 << ")" ;
46 }     
47 
48 // 类方法
49 int T::get_cnt() {
50    return cnt;
51 }
52 
53 // 友元
54 void func() {
55     T t5(42);
56     t5.m2 = 2049;
57     cout << "t5 = "; t5.display(); cout << endl;
58 }

task1.cpp

 1 #include "t.h"
 2 #include <iostream>
 3 
 4 using std::cout;
 5 using std::endl;
 6 
 7 void test();
 8 
 9 int main() {
10     test();
11     cout << "\nmain: \n";
12     cout << "T objects'current count: " << T::get_cnt() << endl;
13 }
14 
15 void test() {
16     cout << "test class T: \n";
17     cout << "T info: " << T::doc << endl;
18     cout << "T objects'max count: " << T::max_cnt << endl;
19     cout << "T objects'current count: " << T::get_cnt() << endl << endl;
20 
21 
22     T t1;
23     cout << "t1 = "; t1.display(); cout << endl;
24 
25     T t2(3, 4);
26     cout << "t2 = "; t2.display(); cout << endl;
27 
28     T t3(t2);
29     t3.adjust(2);
30     cout << "t3 = "; t3.display(); cout << endl;
31 
32     T t4(std::move(t2));
33     cout << "t3 = "; t4.display(); cout << endl;
34 
35     cout << "T objects'current count: " << T::get_cnt() << endl;
36 
37     func();
38 }

运行结果截图:

回答问题:

问题1:

答:func()函数需要在类外声明,不然就会显示func()函数没有定义。

问题2:

答:

普通构造函数T(int x = 0, int y = 0),传入两个参数x,y来创建一个实例,并且x,y在默认情况下都是0。

复制构造函数T(const T& t),传入参数是一个已经存在的对象,会复制这个已有对象的x,y来创建一个新的对象。

移动构造函数T(T &&t),右值引用,将已经存在对象的x,y移动过来创建一个新的对象,并且原对象不再拥有原来的x,y。

构造函数在每次对象的创建时会调用,根据不同答参数传入和预期功能,分别调用合适的构造函数,特别的,复制构造函数在对象作为函数参数传递时也会调用。析构函数在每个对象生命周期结束时会被调用。

问题3:

答:不能正确编译运行

 

 

 

task2

代码:

Complex.h

 

 1 #pragma once
 2 #include <string>
 3 
 4 using std::string;
 5 
 6 class Complex {
 7 public:
 8     Complex();
 9     Complex(double r, double i = 0);
10     Complex(const Complex& c);
11 
12     double get_real() const;
13     double get_imag() const;
14     void add(const Complex& c);
15 
16     friend Complex add(const Complex& c1, const Complex& c2);
17     friend bool is_equal(const Complex& c1, const Complex& c2);
18     friend bool is_not_equal(const Complex& c1, const Complex& c2);
19     friend void output(const Complex& c);
20     friend double abs(const Complex& c);
21 
22 private:
23     double real, imag;
24 
25 public:
26     static const string doc;
27 };

Complex.cpp

 

 1 #include <iostream>
 2 #include "Complex.h"
 3 
 4 using std::string;
 5 using std::cout;
 6 using std::endl;
 7 
 8 const string Complex::doc{ "a simplified complex class" };
 9 
10 Complex::Complex():real{0}, imag{0}{}
11 Complex::Complex(double r, double i): real{r}, imag{i}{}
12 Complex::Complex(const Complex& c):real{c.real}, imag{c.imag}{}
13 
14 double Complex::get_real() const { return real; }
15 double Complex::get_imag() const { return imag; }
16 void Complex::add(const Complex& c) {
17     real += c.real;
18     imag += c.imag;
19 }
20 
21 Complex add(const Complex& c1, const Complex& c2) {
22     Complex c3;
23     c3.real = c1.real + c2.real;
24     c3.imag = c1.imag + c2.imag;
25     return c3;
26 }
27 
28 bool is_equal(const Complex& c1, const Complex& c2) {
29     if (c1.real == c2.real && c1.imag == c2.imag)
30         return true;
31     else 
32         return false;
33 }
34 
35 bool is_not_equal(const Complex& c1, const Complex& c2) {
36     if (c1.real == c2.real && c1.imag == c2.real)
37         return false;
38     else
39         return true;
40 }
41 
42 void output(const Complex& c) {
43     cout << c.real;
44     if (c.imag >= 0)
45         cout << " + " << c.imag << "i" << endl;
46     else
47         cout << " - " << abs(c.imag) << "i" << endl;
48     
49     
50 }
51 
52 double abs(const Complex& c) {
53     return sqrt(c.real * c.real + c.imag * c.imag);
54 }

task2.cpp

 

 1 #include "Complex.h"
 2 #include <iostream>
 3 
 4 using std::cout;
 5 using std::endl;
 6 using std::boolalpha;
 7 
 8 void test() {
 9     cout << "类成员测试: " << endl;
10     cout << Complex::doc << endl;
11 
12     cout << endl;
13 
14     cout << "Complex对象测试: " << endl;
15     Complex c1;
16     Complex c2(3, -4);
17     const Complex c3(3.5);
18     Complex c4(c3);
19 
20     cout << "c1 = "; output(c1); cout << endl;
21     cout << "c2 = "; output(c2); cout << endl;
22     cout << "c3 = "; output(c3); cout << endl;
23     cout << "c4 = "; output(c4); cout << endl;
24     cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl;
25 
26     cout << endl;
27 
28     cout << "复数运算测试: " << endl;
29     cout << "abs(c2) = " << abs(c2) << endl;
30     c1.add(c2);
31     cout << "c1 += c2, c1 = "; output(c1); cout << endl;
32     cout << boolalpha;
33     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
34     cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
35     c4 = add(c2, c3);
36     cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
37 }
38 
39 int main() {
40     test();
41 }

运行结果截图:

 

 

 

 

task3

代码:

 1 #include <iostream>
 2 #include <complex>
 3 
 4 using std::cout;
 5 using std::endl;
 6 using std::boolalpha;
 7 using std::complex;
 8 
 9 void test() {
10     cout << "标准库模板类comple测试: " << endl;
11     complex<double> c1;
12     complex<double> c2(3, -4);
13     const complex<double> c3(3.5);
14     complex<double> c4(c3);
15 
16     cout << "c1 = " << c1 << endl;
17     cout << "c2 = " << c2 << endl;
18     cout << "c3 = " << c3 << endl;
19     cout << "c4 = " << c4 << endl;
20     cout << "c4.real = " << c4.real() << ", c4.imag = " << c4.imag() << endl;
21     cout << endl;
22 
23     cout << "复数运算测试: " << endl;
24     cout << "abs(c2) = " << abs(c2) << endl;
25     c1 += c2;
26     cout << "c1 += c2, c1 = " << c1 << endl;
27     cout << boolalpha;
28     cout << "c1 == c2 : " << (c1 == c2) << endl;
29     cout << "c1 != c3 : " << (c1 != c3) << endl;
30     c4 = c2 + c3;
31     cout << "c4 = c2 + c3, c4 = " << c4 << endl;
32 }
33 
34 int main() {
35     test();
36 }

测试结果截图:

回答问题:

问题1:

答:在输出复数时能直接cout,而不用调用output()函数;在进行+=、+等运算时,能够像普通数字运算那样直接进行;在判断是否相等时,也能够用==、!=来判断。

问题2:

答:标准库complex模板类大大方便了使用复数时的操作。不过同样的功能,我们也可以在自定义类里通过操作函数来实现。

 

 

 

task4

代码:

Fraction.h

 1 #pragma
 2 #include<string>
 3 
 4 using namespace std;
 5 
 6 class Fraction {
 7 public:
 8     Fraction(int u, int d = 1);
 9     Fraction(const Fraction & f);
10 
11     int get_up() const;
12     int get_down() const;
13     Fraction negative() const;
14 
15 private:
16     int up, down;
17 
18 public:
19     static const string doc;
20 
21     friend void output(const Fraction f);
22     friend Fraction add(const Fraction f1, const Fraction f2);
23     friend Fraction sub(const Fraction f1, const Fraction f2);
24     friend Fraction mul(const Fraction f1, const Fraction f2);
25     friend Fraction div(const Fraction f1, const Fraction f2);
26 
27 };
28 
29 void output(const Fraction f);
30 Fraction add(const Fraction f1, const Fraction f2);
31 Fraction sub(const Fraction f1, const Fraction f2);
32 Fraction mul(const Fraction f1, const Fraction f2);
33 Fraction div(const Fraction f1, const Fraction f2);

Fraction.cpp

 

  1 #include<iostream>
  2 #include<string>
  3 #include"Fraction.h"
  4 
  5 using namespace std;
  6 
  7 const string Fraction::doc{ "Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算.\n" };
  8 
  9 Fraction::Fraction(int u, int d) {
 10     int i = 1;
 11     if (u < 0)
 12         i = -i;
 13     if (d < 0)
 14         i = -i;
 15 
 16     int p = abs(u);
 17     int q = abs(d);
 18     if (u != 0 && d != 0) {
 19         int a = max(p, q);
 20         int b = min(p, q);
 21         int r = a % b;
 22         while (r != 0) {
 23             a = b;
 24             b = r;
 25             r = a % b;
 26         }
 27 
 28         up = i * p / b;
 29         down = q / b;
 30 
 31     }
 32     else {
 33         up = u;
 34         down = d;
 35     }
 36 }
 37 Fraction::Fraction(const Fraction & f) : up{ f.up }, down{ f.down } {}
 38 
 39 int Fraction::get_up() const { 
 40     return up; 
 41 }
 42 int Fraction::get_down() const { 
 43     return down; 
 44 }
 45 Fraction Fraction::negative() const {
 46     Fraction f(1);
 47     f.up = -up;
 48     f.down = down;
 49     return f;
 50 }
 51 
 52 //最大公因数
 53 int func(int x, int y) {
 54     if (x == 0 || y == 0)
 55         return 1;
 56     int a = max(x, y);
 57     int b = min(x, y);
 58     int r = a % b;
 59     while(r != 0){
 60         a = b;
 61         b = r;
 62         r = a % b;
 63     }
 64 
 65     return b;
 66 }
 67 
 68 void output(Fraction f) {
 69     int t = func(abs(f.up), abs(f.down));
 70     int up = abs(f.up) / t;
 71     int down = abs(f.down) / t;
 72 
 73     if (f.down == 0)
 74         cout << "分母不能为0" << endl;
 75     else if (f.up == 0)
 76         cout << "0" << endl;
 77     else if (f.down == 1)
 78         cout << f.up << endl;
 79     else {
 80         if ((f.up < 0 && f.down > 0) || (f.up > 0 && f.down < 0))
 81             cout << "-";
 82         cout << up << "/" << down << endl;
 83     }
 84     
 85 }
 86 
 87 Fraction add(const Fraction f1, const Fraction f2) {
 88     Fraction f(1);
 89     f.down = f1.down * f2.down / func(f1.down, f2.down);
 90     f.up = f1.up * f.down / f1.down + f2.up * f.down / f2.down;
 91     return f;
 92 }
 93 
 94 Fraction sub(const Fraction f1, const Fraction f2) {
 95     Fraction f(1);
 96     f.down = f1.down * f2.down / func(f1.down, f2.down);
 97     f.up = f1.up * f.down / f1.down - f2.up * f.down / f2.down;
 98     return f;
 99 }
100 
101 Fraction mul(const Fraction f1, const Fraction f2) {
102     Fraction f(1);
103     f.up = f1.up * f2.up;
104     f.down = f1.down * f2.down;
105     return f;
106 }
107 
108 Fraction div(const Fraction f1, const Fraction f2) {
109     Fraction f(1);
110     if (f2.up == 0 || f2.down == 0)
111         f.up = f.down = 0;
112     else {
113         f.up = f1.up * f2.down;
114         f.down = f1.down * f2.up;
115     }
116     return f;
117 }

task4.cpp

 

 1 #include "Fraction.h"
 2 #include <iostream>
 3 
 4 using std::cout;
 5 using std::endl;
 6 
 7 
 8 void test1() {
 9     cout << "Fraction类测试: " << endl;
10     cout << Fraction::doc << endl << endl;
11 
12     Fraction f1(5);
13     Fraction f2(3, -4), f3(-18, 12);
14     Fraction f4(f3);
15     cout << "f1 = "; output(f1); cout << endl;
16     cout << "f2 = "; output(f2); cout << endl;
17     cout << "f3 = "; output(f3); cout << endl;
18     cout << "f4 = "; output(f4); cout << endl;
19 
20     Fraction f5(f4.negative());
21     cout << "f5 = "; output(f5); cout << endl;
22     cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << f5.get_down() << endl;
23 
24     cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl;
25     cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl;
26     cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl;
27     cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl;
28     cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl;
29 }
30 
31 void test2() {
32     Fraction f6(42, 55), f7(0, 3);
33     cout << "f6 = "; output(f6); cout << endl;
34     cout << "f7 = "; output(f7); cout << endl;
35     cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl;
36 }
37 
38 int main() {
39     cout << "测试1: Fraction类基础功能测试\n";
40     test1();
41 
42     cout << "\n测试2: 分母为0测试: \n";
43     test2();
44 }

运行结果截图:

 

 

 

 

task5

代码:

account.h

 1 #pragma once
 2 
 3 class SavingAccount {
 4 private:
 5     int id;
 6     double balance;
 7     double rate;
 8     int lastDate;
 9     double accumulation;
10 
11     static double total;
12 
13     void record(int date, double amount);
14 
15     double accumulate(int date) const {
16         return accumulation + balance * (date - lastDate);
17     }
18 
19 public:
20     SavingAccount(int date, int id, double rate);
21 
22     int getId() const { return id; }
23     double getBalance() const { return balance; }
24     double getRate() const { return rate; }
25 
26     static double getTotal() { return total; }
27 
28     void deposit(int date, double amount);
29     void withdraw(int date, double amount);
30     void settle(int date);
31     void show() const;
32 
33 };

account.cpp

 1 #include"account.h"
 2 #include<iostream>
 3 #include<cmath>
 4 
 5 using namespace std;
 6 
 7 double SavingAccount::total = 0;
 8 
 9 SavingAccount::SavingAccount(int date, int id, double rate)
10     : id(id), balance(0), rate(rate), lastDate(date), accumulation(0) {
11     cout << date << "\t#" << id << "is created" << endl;
12 }
13 
14 void SavingAccount::record(int date, double amount) {
15     accumulation = accumulate(date);
16     lastDate = date;
17     amount = floor(amount * 100 + 0.5) / 100;
18     balance += amount;
19     total += amount;
20     cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl;
21 }
22 
23 void SavingAccount::deposit(int date, double amount) {
24     record(date, amount);
25 }
26 
27 void SavingAccount::withdraw(int date, double amount) {
28     if (amount > getBalance())
29         cout << "Error: not enough money" << endl;
30     else
31         record(date, -amount);
32 }
33 
34 void SavingAccount::settle(int date) {
35     double interest = accumulate(date) * rate / 365;
36     if (interest != 0)
37         record(date, interest);
38     accumulation = 0;
39 }
40 
41 void SavingAccount::show() const {
42     cout << "#" << id << "\tBalance: " << balance;
43 }

5_11.cpp

 1 #include "account.h"
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 int main() {
 7     SavingAccount sa0(1, 21325302, 0.015);
 8     SavingAccount sa1(1, 58320212, 0.015);
 9 
10     sa0.deposit(5, 5000);
11     sa1.deposit(25, 10000);
12     sa0.deposit(45, 5500);
13     sa1.withdraw(60, 4000);
14 
15     sa0.settle(90);
16     sa1.settle(90);
17 
18     sa0.show(); cout << endl;
19     sa1.show(); cout << endl;
20     cout << "Total: " << SavingAccount::getTotal() << endl;
21 
22 }

运行结果截图:

回答问题:

合理,有疑问:计算完年息之后accumulation为什么要重置为0?

posted on 2024-10-27 12:45  Forgiver  阅读(28)  评论(0编辑  收藏  举报