實驗二 c++

笔墨书稠發表於2024-10-27

實驗任務一

原始碼

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 }

task.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.報錯截圖,因為t.h中僅僅只宣告瞭func是類T的友元函式,並沒有給出定義,所以會報錯

2.普通建構函式構造一個新的物件,複製建構函式將一個物件複製到另一個新物件中,移動建構函式將一個物件移動到一個新物件中。在主函式結束時,依次呼叫解構函式

3.會編譯報錯,static的資料在類中宣告,需要在類外(也就是t.cpp中)定義

實驗任務二

原始碼

x.h

 1 #ifndef X_H
 2 #define X_H
 3 #include<string> 
 4 using std::string;
 5 
 6 class Complex {
 7 
 8 public:
 9 
10     Complex(double real0 = 0, double imag0 = 0);
11     Complex(const Complex& obj);
12     ~Complex();
13 
14     double get_real() const;
15     double get_imag() const;
16     void add(Complex c);
17     friend Complex add(const Complex a, const Complex b);
18     friend bool is_equal(Complex a, Complex b);
19     friend bool is_not_equal(Complex a, Complex b);
20     friend void output(Complex c);
21     friend double abs(Complex c);
22 
23 private:
24 
25     double real;
26     double imag;
27 
28 public:
29     static const string doc;
30 
31 };
32 
33 #endif

x.cpp

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

main.cpp

 1 #include <iostream>
 2 #include "x.h"
 3 
 4 using std::cout;
 5 using std::endl;
 6 using std::boolalpha;
 7 
 8 void test() {
 9 
10     cout << "類成員測試: " << endl;
11     cout << Complex::doc << endl;
12 
13     cout << endl;
14 
15     cout << "Complex物件測試: " << endl;
16     Complex c1;
17     Complex c2(3, -4);
18     const Complex c3(3.5);
19 
20     Complex c4(c3);
21 
22 
23 
24     cout << "c1 = "; output(c1); cout << endl;
25     cout << "c2 = "; output(c2); cout << endl;
26     cout << "c3 = "; output(c3); cout << endl;
27     cout << "c4 = "; output(c4); cout << endl;
28     cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl;
29 
30     cout << endl;
31 
32     cout << "複數運算測試: " << endl;
33     cout << "abs(c2) = " << abs(c2) << endl;
34     c1.add(c2);
35     cout << "c1 += c2, c1 = "; output(c1); cout << endl;
36     cout << boolalpha;
37     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
38     cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl;
39     c4 = add(c2, c3);
40     cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
41 
42 }
43 
44 
45 
46 int main() {
47 
48     test();
49 
50 }

執行結果截圖

實驗任務三

原始碼

 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 }

執行結果截圖

實驗任務四

原始碼

Fraction.h

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

Fraction.cpp

 1 #include "Fraction.h"
 2 #include<iostream>
 3 using std::string;
 4 using std::cout;
 5 using std::endl;
 6 
 7 const string Fraction::doc{ "Fraction類 v 0.01版\n目前僅支援分數物件的構造,輸出,加//減//乘//除運算" };
 8 
 9 int gcd(int a, int b) {
10     int t;
11     if (a == 0 || b == 0)
12         return 0;
13     while (b != 0) {
14         t = a % b;
15         a = b;
16         b = t;
17     }
18     return a;
19 }
20 int gcs(int a, int b) {
21     if (gcd(a, b) == 0)
22         return 0;
23     return a * b / gcd(a, b);
24 }
25 
26 Fraction::Fraction(int up0 , int down0) :up{ up0 }, down{ down0 } {};
27 Fraction::Fraction(const Fraction& obj) {
28     up = obj.up;
29     down = obj.down;
30 }
31 
32 int Fraction::get_up ()const { return up; }
33 int Fraction::get_down ()const { return down; }
34 Fraction Fraction::negative() {
35     Fraction f;
36     if ( down > 0) {
37         f.up = -up;
38         f.down = down;
39     }
40         
41 
42     else if ( down < 0)
43     {
44         f.up = up;
45         f.down = -down;
46     }
47     return f;
48 }
49 
50 
51 Fraction add(Fraction f1, Fraction f2) {
52     Fraction f;
53     int r= gcs(f1.down, f2.down);
54     f.up = f1.up * (r / f1.down) + f2.up * (r / f2.down);
55     f.down = r;
56     return f;
57 }
58 Fraction sub(Fraction f1, Fraction f2) {
59     Fraction f;
60     f.up = f1.up * f2.up;
61     f.down = f1.down * f2.down;
62     return f;
63 }
64 Fraction mul(Fraction f1, Fraction f2) {
65     Fraction f;
66     int r = gcs(f1.down, f2.down);
67     f.up = f1.up * (r / f1.down) - f2.up * (r / f2.down);
68     f.down = r;
69     return f;
70 }
71 Fraction div(Fraction f1, Fraction f2) {
72     int t;
73     Fraction f;
74     t = f2.up;
75     f2.up = f2.down;
76     f2.down = t;
77     f=sub(f1, f2);
78     return f;
79 }
80 void output(Fraction f) {
81     int r = abs(gcd(f.up, f.down));
82     if (f.down == 0)
83         cout << "分母不能為0" << endl;
84     else if(f.up==0)
85         cout << f.up << endl;
86     else if (f.down > 0)
87         cout << f.up /r<< "/" << f.down/r << endl;
88     else if (f.down < 0)
89         cout << -(f.up/r) << "/" << -(f.down/r) << endl;
90    
91 
92 }

main.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 }

執行結果截圖

實驗五

原始碼

account.h

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

account.cpp

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

5_11.cpp

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

執行結果截圖

相關文章