实验1_OOP_22物联网1班_张文瑞

发布时间 2023-10-19 22:36:15作者: 张文瑞

1.实验任务1:

   实验源代码:

 1 // 标准库string, vector, array基础用法
 2 #include <iostream>
 3 #include <string>
 4 #include <vector>
 5 #include <array>
 6 // 函数模板
 7 // 对满足特定条件的序列类型T对象,使用范围for输出
 8 template<typename T>
 9 void output1(const T& obj) {
10     for (auto i : obj)
11         std::cout << i << ", ";
12     std::cout << "\b\b \n";
13 }
14 // 函数模板
15 // 对满足特定条件的序列类型T对象,使用迭代器输出
16 template<typename T>
17 void output2(const T& obj) {
18     for (auto p = obj.begin(); p != obj.end(); ++p)
19         std::cout << *p << ", ";
20     std::cout << "\b\b \n";
21 }
22 // array模板类基础用法
23 void test_array() {
24     using namespace std;
25     array<int, 5> x1; // 创建一个array对象,包含5个int元素,未初始化
26     cout << "x1.size() = " << x1.size() << endl; // 输出元素个数
27     x1.fill(42); // 把x1的所有元素都用42填充
28     x1.at(0) = 999; // 把下标为0的元素值修改为999
29     x1[4] = -999; // 把下表为4的元素值修改为-999
30     cout << "x1: ";
31     output1(x1); // 调用模板函数output1输出x1
32     cout << "x1: ";
33     output2(x1); // 调用模板函数output1输出x1
34     array<int, 5> x2{x1};
35     cout << boolalpha << (x1 == x2) << endl;
36     x2.fill(22);
37     cout << "x2: ";
38     output1(x2);
39     swap(x1, x2); // 交换array对象x1, x2
40     cout << "x1: ";
41     output1(x1);
42     cout << "x2: ";
43     output1(x2);
44 }
45 // vector模板类基础用法
46 void test_vector() {
47     using namespace std;
48     vector<int> v1;
49     cout << v1.size() << endl; // 输出目前元素个数
50     cout << v1.max_size() << endl; // 输出元素个数之最大可能个数
51     v1.push_back(55); // 在v1末尾插入元素
52     cout << "v1: ";
53     output1(v1);
54     vector<int> v2 {1, 0, 5, 2};
55     v2.pop_back(); // 从v2末尾弹出一个元素
56     v2.erase(v2.begin()); // 删除v2.begin()位置的数据项
57     v2.insert(v2.begin(), 999); // 在v1.begin()之前的位置插入
58     v2.insert(v2.end(), -999); // 在v1.end()之前的位置插入
59     cout << v2.size() << endl;
60     cout << "v2: ";
61     output2(v2);
62     vector<int> v3(5, 42); //创建vector对象,包含5个元素,每个元素值都是42
63         cout << "v3: ";
64     output1(v3);
65     vector<int> v4(v3.begin(), v3.end() - 2); // 创建vector对象,以v3对象的[v3.begin(), v3.end() - 2)区间作为元素值
66     cout << "v4: ";
67     output1(v4);
68 }
69 // string类基础用法
70 void test_string() {
71     using namespace std;
72     string s1{ "oop" };
73     cout << s1.size() << endl;
74     for (auto& i : s1)
75         i -= 32;
76     s1 += "2023";
77     s1.append(", hello");
78     cout << s1 << endl;
79 }
80 int main() {
81     using namespace std;
82     cout << "===========测试1: array模板类基础用法===========" << endl;
83     test_array();
84     cout << "\n===========测试2: vector模板类基础用法===========" << endl;
85     test_vector();
86     cout << "\n===========测试3: string类基础用法===========" << endl;
87     test_string();
88 }

 

   运行测试截图:

 

 

2.实验任务2:

  实验源代码:

 1 #include <iostream>
 2 #include <complex>
 3 // 测试标准库提供的复数类模板complex
 4 void test_std_complex() {
 5     using namespace std;
 6     complex<double> c1{3, 4}, c2{ 4.5 };
 7     const complex<double> c3{c2};
 8     cout << "c1 = " << c1 << endl;
 9     cout << "c2 = " << c2 << endl;
10     cout << "c3 = " << c3 << endl;
11     cout << "c3.real = " << c3.real() << ", " << "c3.imag = " << c3.imag()
12         << endl;
13     cout << "c1 + c2 = " << c1 + c2 << endl;
14     cout << "c1 - c2 = " << c1 - c2 << endl;
15     cout << "abs(c1) = " << abs(c1) << endl; // abs()是标准库数学函数,对复数取模
16         cout << boolalpha; // 设置bool型值以true/false方式输出
17     cout << "c1 == c2: " << (c1 == c2) << endl;
18     cout << "c3 == c2: " << (c3 == c2) << endl;
19     complex<double> c4 = 2;
20     cout << "c4 = " << c4 << endl;
21     c4 += c1;
22     cout << "c4 = " << c4 << endl;
23 }
24 int main() {
25     test_std_complex();
26 }

 

   运行测试截图:

 

 

3.实验任务3:

  实验源代码:

 

 1 // 一个简单的类T:定义、使用
 2 #include <iostream>
 3 #include <string>
 4 using namespace std;
 5 // 类T的声明
 6 class T {
 7 public:
 8     T(int x = 0, int y = 0); // 带有默认形值的构造函数
 9     T(const T& t); // 复制构造函数
10     T(T&& t); // 移动构造函数
11     ~T(); // 析构函数
12     void set_m1(int x); // 设置T类对象的数据成员m1
13     int get_m1() const; // 获取T类对象的数据成员m1
14     int get_m2() const; // 获取T类对象的数据成员m2
15     void display() const; // 显示T类对象的信息
16     friend void func(); // 声明func()为T类友元函数
17 private:
18     int m1, m2;
19 public:
20     static void disply_count(); // 类方法,显示当前T类对象数目
21 public:
22     static const string doc; // 类属性,用于描述T类
23     static const int max_count; // 类属性,用于描述T类对象的上限
24 private:
25     static int count; // 类属性,用于描述当前T类对象数目
26 };
27 // 类的static数据成员:类外初始化
28 const string T::doc{"a simple class"};
29 const int T::max_count = 99;
30 int T::count = 0;
31 // 类T的实现
32 T::T(int x, int y) : m1{ x }, m2{ y } {
33     ++count;
34     cout << "constructor called.\n";
35 }
36 T::T(const T& t) : m1{ t.m1 }, m2{ t.m2 } {
37     ++count;
38     cout << "copy constructor called.\n";
39 }
40 T::T(T&& t) : m1{ t.m1 }, m2{ t.m2 } {
41     ++count;
42     cout << "move constructor called.\n";
43 }
44 T::~T() {
45     --count;
46     cout << "destructor called.\n";
47 }
48 void T::set_m1(int x) {
49     m1 = x;
50 }
51 int T::get_m1() const {
52     return m1;
53 }
54 int T::get_m2() const {
55     return m2;
56 }
57 void T::display() const {
58     cout << m1 << ", " << m2 << endl;
59 }
60 // 类方法
61 void T::disply_count() {
62     cout << "T objects: " << count << endl;
63 }
64 // 友元函数func():实现
65 void func() {
66     T t1;
67     t1.set_m1(55);
68     t1.m2 = 77; // 虽然m2是私有成员,依然可以直接访问
69     t1.display();
70 }
71 // 测试
72 void test() {
73     cout << "T class info: " << T::doc << endl;
74     cout << "T objects max_count: " << T::max_count << endl;
75     T::disply_count();
76     T t1;
77     t1.display();
78     t1.set_m1(42);
79     T t2{ t1 };
80     t2.display();
81     T t3{ std::move(t1) };
82     t3.display();
83     t1.display();
84     T::disply_count();
85 }
86 // 主函数
87 int main() {
88     cout << "============测试类T============" << endl;
89     test();
90     cout << endl;
91     cout << "============测试友元函数func()============" << endl;
92     func();
93 }

 

   运行测试截图:

 

4实验任务4:

  实验源代码:

 

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class Rect {
private:
    static int size;
public:
    static const string doc;
    static int size_info() {
        return size;
    }
    friend void test();
private:
    double length, width;
public:
    Rect(double l = 2.0, double w = 1.0) : length{ l }, width{ w } {
        size++;
    }
    Rect(const Rect& obj) : length{ obj.length }, width{ obj.width } {
        size++;
    }
    ~Rect() {
        size--;
    }
    double len() const {
        return length;
    }
    double wide() const {
        return width;
    }
    double area() const {
        return length * width;
    }
    double circumference() const {
        return 2 * (length + width);
    }
    void resize(double times) {
        length *= times;
        width *= times;
    }
    void resize(double l_times, double w_times) {
        length *= l_times;
        width *= w_times;
    }
};
int Rect::size = 0;
const string Rect::doc{"a simple Rect class"};

void output(const Rect& r) {
    cout << "矩形信息: " << endl;
    cout << fixed << setprecision(2); 
    cout << "长: \t" << r.len() << endl;
    cout << "宽: \t" << r.wide() << endl;
    cout << "面积: \t" << r.area() << endl;
    cout << "周长: \t" << r.circumference() << endl;
}

void test() {
    cout << "矩形类信息: " << Rect::doc << endl;
    cout << "当前矩形对象数目: " << Rect::size_info() << endl;
    Rect r1;
    output(r1);
    Rect r2(4, 3);
    output(r2);
    Rect r3(r2);
    r3.resize(2);
    output(r3);
    r3.resize(5, 2);
    output(r3);
    cout << "当前矩形对象数目: " << Rect::size_info() << endl;
}

int main() {
    test();
    cout << "当前矩形对象数目: " << Rect::size_info() << endl;
}

   运行测试截图:

 

 

5.实验任务5:

  实验源代码:


#include <iostream>
#include <cmath>


class Complex {
private:
double real, imag;
public:
Complex(double r = 0, double i = 0) : real{ r }, imag{ i } {};
Complex(const Complex& c) :real{ c.real }, imag{ c.imag } {};
double get_real() const {
return real;
}
double get_imag() const {
return imag;
}
void add(const Complex& c) {
real += c.get_real();
imag += c.get_imag();
}
void show() {
std::cout << real;
if (imag > 0) std::cout << " + " << abs(imag) << 'i';
if (imag < 0) std::cout << " - " << fabs(imag) << 'i';
}
void show() const {
std::cout << real;
if (imag > 0) std::cout << " + " << abs(imag) << 'i';
if (imag < 0) std::cout << " - " << fabs(imag) << 'i';
}
public:
friend Complex add(const Complex&, const Complex&);
friend bool is_equal(const Complex&, const Complex&);
friend double abs(const Complex&);
};
Complex add(const Complex& c1, const Complex& c2) {
double r, i;
r = c1.get_real() + c2.get_real();
i = c1.get_imag() + c2.get_imag();
Complex c(r, i);
return c;
}
bool is_equal(const Complex& c1, const Complex& c2) {
bool r, i;
r = c1.get_real() == c2.get_real();
i = c1.get_imag() == c2.get_imag();
return r && i;
}
double abs(const Complex& c) {
return sqrt(pow(c.get_real(), 2) + pow(c.get_imag(), 2));
}


void test() {
using namespace std;
Complex c1(3, -4);
const Complex c2(4.5);
Complex c3(c1);
cout << "c1 = ";
c1.show();
cout << endl;
cout << "c2 = ";
c2.show();
cout << endl;
cout << "c2.imag = " << c2.get_imag() << endl;
cout << "c3 = ";
c3.show();
cout << endl;
cout << "abs(c1) = ";
cout << abs(c1) << endl;
cout << boolalpha;
cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
Complex c4;
c4 = add(c1, c2);
cout << "c4 = c1 + c2 = ";
c4.show();
cout << endl;
c1.add(c2);
cout << "c1 += c2, " << "c1 = ";
c1.show();
cout << endl;
}


int main() {
test();
}

 

 

   运行测试截图: