首页 > 其他分享 >实验1_OOP_22物联网1班_张文瑞

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

时间:2023-10-19 22:48:00浏览次数:31  
标签:const cout 22 张文瑞 double imag int Complex OOP

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();
}

 

 

   运行测试截图:

 

标签:const,cout,22,张文瑞,double,imag,int,Complex,OOP
From: https://www.cnblogs.com/zhangwenrui/p/17761603.html

相关文章

  • 2023-10-19 第22本书《Jenkins 2.x 实践指南》
    囫囵吞枣读完的,也算听有收获的,他这里面介绍了ansible集成进jenkins感觉有点搞头。收益是:方便部署到多台服务器。明天继续研究一下把。 总体讲的还行,继续看看把。 主要是,我最近有点疲惫,没啥状态。心里乱乱的。 ......
  • oop 实验1类和对象基础编程
    #include<iostream>#include<string>#include<vector>#include<array>//通用函数(此处是模板函数)用于输出容器中的元素,支持范围for(范围for循环,是一种用于遍历容器、数组和其他序列容器的现代C++迭代循环结构。它提供了一种更简洁和易读的方法来遍历容器的元素,而无需手动......
  • Win10_22H2_2023年10月累积更新
    大版本号:22H2内部版本号:19045.3570本系统镜像纯粹日常工作中自用并共享,基于微软官方原版镜像制作,目前只集成自应答文件和常用VC库,若你有好的建议或意见可发我邮箱;下载完记得验证hash值,以防翻车!文件1.Win_10_business_22H2_19045.3570_x64_update2023.10.iso★微软官方商业版64位原......
  • (华为欧拉操作系统)openEuler 22.03 LTS SP2 安装使用记录
    本来是准备在虚拟机中安装rockylinux,,结果安装失败,你可以从第4步开始看。1.到 https://www.virtualbox.org/ 下载VirtualBox-7.0.12-159484-Win.exe  并安装 2.到 https://rockylinux.org/zh_CN/download/下载   Rocky-9.2-x86_64-dvd.iso 由于这个iso有8.8G,正......
  • hadoop集群 大数据项目实战_电信用户行为分析_day03
    配置系统环境  Reis1.先把之前的dump.rdb删除掉rm-rfdump.rdb 2.把原始项目给的dump.rdb放进来,它里面包含了需要的数据,比如端口;在这部之前必须要进行关闭端口,随后传送文件,最后重启端口相关指令:   bin/redis-server conf/redis.conf   bin/redis-cli  bin......
  • 22.函数eval和ast.literal_eval
    函数eval和ast.literal_eval目录函数eval和ast.literal_eval将str转list将str转dict将str转tupleeval和字典eval执行str解析eval的安全问题literal_eval()参考资料python中将字符串型的list,tuple,dict转变成原有的类型eval函数在python中做数据类型的转换还是很有用的。它的作......
  • InCopy(Ic)2022软件下载 IC中文直装版下载 软件激活版
    AdobeInCopyCC2018官方中文版是一款优秀的创意写作编辑软件,由大名鼎鼎的Adobe公司所出品。该InCopyCC与AdobeInDesignCC紧密集合为作家,编辑,设计师等创意人员协同工作流。AdobeInCopyCC2018版本采用了更加现代的UI设计,增加了对HIDPI和Retina显示屏的支持;增强了字体搜索功......
  • 2022 CCPC 女生赛 补题 ACEGHI
    2022女生赛补题ACEGHIhttps://codeforces.com/gym/104081属于是考前抱佛脚了,希望能有个好成绩球球了一些写过的题题解在此:如何评价2022CCPC女生赛?-知乎用户的回答-知乎A.减肥计划模拟直到最大的那个人到前面(最开始用queue模拟的,样例居然过了)WA了之后直接改成变量记......
  • CSP 2022 游记
    updon23/10/18一年了。CSP还剩3days感慨。初赛啥也没干。就随便刷刷洛谷有题。考完普及感觉很稳。考完提高感觉蒙蒙的。听说有很多人过tg不过pj?所以就感觉tg能过(updon2023.9:。。。然后tg只有48。pj81.5。光速打脸。去不了S了。/ng复赛开T1:不就是快速幂吗,水水就......
  • 【专题】2022年中国跨境电商行业研究报告PDF合集分享(附原数据表)
    报告链接:http://tecdat.cn/?p=32044近年来,我国的跨境电子商务发展迅速,在过去五年中,其贸易额增长率达到了16.2%,已经成为稳定对外贸易的一支重要力量。阅读原文,获取专题报告合集全文,解锁文末52份跨境电商行业相关报告。一方面,随着跨境电子商务的发展,跨境电子商务的监管政策得到了......