实验2

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

 ###task1.cpp

#include "T.h"
#include <iostream>

void test_T();

int main() {
    std::cout << "test Class T: \n";
    test_T();

    std::cout << "\ntest friend func: \n";
    func();
}

void test_T() {
    using std::cout;
    using std::endl;

    cout << "T info: " << T::doc << endl;
    cout << "T objects'max count: " << T::max_cnt << endl;
    cout << "T objects'current count: " << T::get_cnt() << endl << endl;

    T t1;
    cout << "t1 = "; t1.display(); cout << endl;

    T t2(3, 4);
    cout << "t2 = "; t2.display(); cout << endl;

    T t3(t2);
    t3.adjust(2);
    cout << "t3 = "; t3.display(); cout << endl;

    T t4(std::move(t2));
    cout << "t4 = "; t4.display(); cout << endl;

    cout << "test: T objects'current count: " << T::get_cnt() << endl;
}

##运行结果

屏幕截图 2025-10-27 174535

##回答

###问题1

YES

###问题2

创建新对象时自动调用,用一个对象初始化另一个对象,将临时对象的值转移给新对象,对象销毁时自动调用

###问题3

YES

 

 

 

 

 

#任务2

##代码

###Complex.h

 1 #pragma once
 2 
 3 #include<string>
 4 #include<iostream>
 5 
 6 class Complex
 7 {
 8 public:
 9     Complex(double x=0,double y=0);
10     Complex(const Complex &c);
11     //Complex(Complex &&c);
12     ~Complex();
13     
14 
15     double get_real() const;
16     double get_imag() const;
17     void add(const Complex &c);
18     
19     
20 private:
21     double m1,m2;
22     
23 public:
24     static const std::string doc;    
25     
26 private:
27     friend void output(const Complex &c);
28     friend bool is_equal(const Complex &c1,const Complex &c2);
29     friend bool is_not_equal(const Complex &c1,const Complex &c2);
30     friend double abs(const Complex &c);
31     friend Complex add(Complex c1,Complex c2);
32 };
33 void output(const Complex &c);
34 bool is_equal(const Complex &c1,const Complex &c2);
35 bool is_not_equal(const Complex &c1,const Complex &c2);
36 double abs(const Complex &c);
37 Complex add(Complex c1,Complex c2);
38     

###Complex.cpp

 1 #include "Complex.h"
 2 #include<iostream>
 3 #include<string>
 4 #include<cmath>
 5 
 6 const std::string Complex::doc{"a simplified complex class"};
 7 
 8 
 9 Complex::Complex(double x,double y):m1{x},m2{y}
10 {    
11 }
12 Complex::Complex(const Complex &c):m1{c.m1},m2{c.m2}
13 {    
14 }
15 Complex::~Complex()
16 {
17 }
18 
19 
20 
21 
22 double Complex::get_real() const
23 {
24     return m1;
25 }
26 double Complex::get_imag() const
27 {
28     return m2;
29 }
30 void Complex::add(const Complex &c)
31 {
32     m1+=c.m1;
33     m2+=c.m2;
34     
35 }
36 
37 void output(const Complex &c)
38 {
39     if(c.m2>=0)
40         std::cout<<c.m1<<' '<<'+'<<' '<<c.m2<<'i';
41     else
42         std::cout<<c.m1<<' '<<'-'<<' '<<0-c.m2<<'i';
43 }
44 double abs(const Complex &c)
45 {
46     double abs=std::sqrt(c.m1*c.m1+c.m2*c.m2);
47     return abs;
48 }
49 Complex add(Complex c1,Complex c2)
50 {
51     Complex c3(c1.m1+c2.m1,c1.m2+c2.m2);
52     return c3;
53 }
54 bool is_equal(const Complex &c1,const Complex &c2)
55 {
56     if(c1.m1==c2.m1&&c1.m2==c2.m2)
57         return true;
58     else
59         return false;
60 }
61 bool is_not_equal(const Complex &c1,const Complex &c2)
62 {
63     if(c1.m1!=c2.m1||c1.m2!=c2.m2)
64         return true;
65     else
66         return false;
67 }

###task2.cpp

 1 #include "Complex.h"
 2 #include <iostream>
 3 #include <iomanip>
 4 #include <complex>
 5 
 6 void test_Complex();
 7 void test_std_complex();
 8 
 9 int main() {
10     std::cout << "*******测试1: 自定义类Complex*******\n";
11     test_Complex();
12 
13     std::cout << "\n*******测试2: 标准库模板类complex*******\n";
14     test_std_complex();
15 }
16 
17 void test_Complex() {
18     using std::cout;
19     using std::endl;
20     using std::boolalpha;
21 
22     cout << "类成员测试: " << endl;
23     cout << Complex::doc << endl << endl;
24 
25     cout << "Complex对象测试: " << endl;
26     Complex c1;
27     Complex c2(3, -4);
28     Complex c3(c2);
29     Complex c4 = c2;
30     const Complex c5(3.5);
31 
32     cout << "c1 = "; output(c1); cout << endl;
33     cout << "c2 = "; output(c2); cout << endl;
34     cout << "c3 = "; output(c3); cout << endl;
35     cout << "c4 = "; output(c4); cout << endl;
36     cout << "c5.real = " << c5.get_real() 
37          << ", c5.imag = " << c5.get_imag() << endl << endl;
38 
39     cout << "复数运算测试: " << endl;
40     cout << "abs(c2) = " << abs(c2) << endl;
41     c1.add(c2);
42     cout << "c1 += c2, c1 = "; output(c1); cout << endl;
43     cout << boolalpha;
44     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
45     cout << "c1 != c2 : " << is_not_equal(c1, c2) << endl;
46     c4 = add(c2, c3);
47     cout << "c4 = c2 + c3, c4 = "; output(c4); cout << endl;
48 }
49 
50 void test_std_complex() {
51     using std::cout;
52     using std::endl;
53     using std::boolalpha;
54 
55     cout << "std::complex<double>对象测试: " << endl;
56     std::complex<double> c1;
57     std::complex<double> c2(3, -4);
58     std::complex<double> c3(c2);
59     std::complex<double> c4 = c2;
60     const std::complex<double> c5(3.5);
61 
62     cout << "c1 = " << c1 << endl;
63     cout << "c2 = " << c2 << endl;
64     cout << "c3 = " << c3 << endl;
65     cout << "c4 = " << c4 << endl;
66 
67     cout << "c5.real = " << c5.real() 
68          << ", c5.imag = " << c5.imag() << endl << endl;
69 
70     cout << "复数运算测试: " << endl;
71     cout << "abs(c2) = " << abs(c2) << endl;
72     c1 += c2;
73     cout << "c1 += c2, c1 = " << c1 << endl;
74     cout << boolalpha;
75     cout << "c1 == c2 : " << (c1 == c2)<< endl;
76     cout << "c1 != c2 : " << (c1 != c2) << endl;
77     c4 = c2 + c3;
78     cout << "c4 = c2 + c3, c4 = " << c4 << endl;
79 }

##运行结果

屏幕截图 2025-10-27 205319

屏幕截图 2025-10-27 205404

##回答

###问题1

标准库模板类complex更简洁,有

###问题2

####2.1

需要,m1,m2均为私有成员

####2.2

####2.3

需要访问私有成员时

###问题3

将拷贝赋值运算符声明为private

 

 

 

 

 

#任务3

##代码

###PlayerControl.h

 1 #pragma once
 2 #include <string>
 3 
 4 enum class ControlType {Play, Pause, Next, Prev, Stop, Unknown};
 5 
 6 class PlayerControl {
 7 public:
 8     PlayerControl();
 9 
10     ControlType parse(const std::string& control_str);   // 实现std::string --> ControlType转换
11     void execute(ControlType cmd) const;   // 执行控制操作(以打印输出模拟)       
12 
13     static int get_cnt();
14 
15 private:
16     static int total_cnt;   
17 };

###PlayerControl.cpp

 1 #include "PlayerControl.h"
 2 #include <iostream>
 3 #include <algorithm>   
 4 
 5 int PlayerControl::total_cnt = 0;
 6 
 7 PlayerControl::PlayerControl() {}
 8 
 9 // 待补足
10 // 1. 将输入字符串转为小写,实现大小写不敏感
11 // 2. 匹配"play"/"pause"/"next"/"prev"/"stop"并返回对应枚举
12 // 3. 未匹配的字符串返回ControlType::Unknown
13 // 4. 每次成功调用parse时递增total_cnt
14 
15 ControlType PlayerControl::parse(const std::string &control_str)
16 {
17     std::string lower_str=control_str;
18     std::transform(lower_str.begin(),lower_str.end(),lower_str.begin(),::tolower);
19 
20     if(lower_str=="play")
21     {
22         total_cnt++;
23         return ControlType::Play;
24     } 
25     else if(lower_str=="pause")
26     {
27         total_cnt++;
28         return ControlType::Pause;
29     }
30     else if(lower_str=="next")
31     {
32         total_cnt++;
33         return ControlType::Next;
34     }
35     else if(lower_str=="prev")
36     {
37         total_cnt++;
38         return ControlType::Prev;
39     }
40     else if(lower_str=="stop")
41     {
42         total_cnt++;
43         return ControlType::Stop;
44     }
45     else 
46         return ControlType::Unknown;
47 }
48 
49 void PlayerControl::execute(ControlType cmd) const {
50     switch (cmd) {
51     case ControlType::Play:  std::cout << "[play] Playing music...\n"; break;
52     case ControlType::Pause: std::cout << "[Pause] Music paused\n";    break;
53     case ControlType::Next:  std::cout << "[Next] Skipping to next track\n"; break;
54     case ControlType::Prev:  std::cout << "[Prev] Back to previous track\n"; break;
55     case ControlType::Stop:  std::cout << "[Stop] Music stopped\n"; break;
56     default:                 std::cout << "[Error] unknown control\n"; break;
57     }
58 }
59 
60 int PlayerControl::get_cnt() {
61     return total_cnt;
62 }

###task3.cpp

 1 #include "PlayerControl.h"
 2 #include <iostream>
 3 
 4 void test() {
 5     PlayerControl controller;
 6     std::string control_str;
 7     std::cout << "Enter Control: (play/pause/next/prev/stop/quit):\n";
 8 
 9     while(std::cin >> control_str) {
10         if(control_str == "quit")
11             break;
12         
13         ControlType cmd = controller.parse(control_str);
14         controller.execute(cmd);
15         std::cout << "Current Player control: " << PlayerControl::get_cnt() << "\n\n";
16     }
17 }
18 
19 int main() {
20     test();
21 }

##运行结果

屏幕截图 2025-10-28 182921

 

 

 

 

 

#任务4

##代码

###Fraction.h

 1 #pragma once
 2 #include<string>
 3 
 4 class Fraction
 5 {
 6     public:
 7         Fraction(int x=0,int y=1);
 8         Fraction(const Fraction &f);
 9         ~Fraction();
10         
11         int get_up() const;
12         int get_down() const;
13         Fraction negative();                
14     private:
15         int up,down;
16         
17     public:
18         static const std::string doc;
19         
20     private:
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 int gcd(int a,int b);

###Fraction.cpp

 1 #include "Fraction.h"
 2 #include<iostream>
 3 #include<string>
 4 
 5 const std::string Fraction::doc{"Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算."};
 6 
 7 
 8 Fraction::Fraction(int x,int y):up{x},down{y}
 9 {
10     if(down<0)
11     {
12            up=-up;
13            down=-down;
14     }
15     int g=gcd(abs(up),down);
16     up/=g;
17     down/=g;
18 }
19 Fraction::Fraction(const Fraction &f):up{f.up},down{f.down}
20 {
21 }
22 Fraction::~Fraction()
23 {
24 }
25 
26 int Fraction::get_up() const
27 {
28     return up;
29 }
30 int Fraction::get_down() const
31 {
32     return down;
33 }
34 Fraction Fraction::negative()
35 {
36     return Fraction(-up,down);
37 }
38 
39 
40 void output(const Fraction &f)
41 {
42     
43     if(f.down==0)
44         std::cout<<"分母不能为0";
45     else if(f.up==0||f.down==1)
46         std::cout<<f.up;
47     else
48         std::cout<<f.up<<'/'<<f.down;
49         
50 }
51 
52 Fraction add(const Fraction &f1, const Fraction &f2)
53 {
54     return Fraction(f1.up*f2.down+f1.down*f2.up,f1.down*f2.down);
55 }
56 Fraction sub(const Fraction &f1, const Fraction &f2)
57 {
58     return Fraction(f1.up*f2.down-f1.down*f2.up,f1.down*f2.down);
59 }
60 Fraction mul(const Fraction &f1, const Fraction &f2)
61 {
62     return Fraction(f1.up*f2.up,f1.down*f2.down);
63 }
64 Fraction div(const Fraction &f1, const Fraction &f2)
65 {
66     return Fraction(f1.up*f2.down,f1.down*f2.up);
67 }
68 
69 int gcd(int a,int b)
70 {
71     a=(a<0)?-a:a;
72     b=(b<0)?-b:b;
73     while(b!=0)
74     {
75         int temp=b;
76         b=a%b;
77         a=temp;
78     }
79     return a;
80 }

###task4.cpp

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

##运行结果

屏幕截图 2025-10-28 202628

##回答

###问题1

选择友元函数

友元函数可以访问类的私有和保护成员,便于实现复杂的运算逻辑。对于运算符重载(如加、减、乘、除)等操作,友元函数是一种常用且高效的设计方案。

 

posted @ 2025-10-28 20:36  五树  阅读(2)  评论(0)    收藏  举报