首页 > 其他分享 >实验一

实验一

时间:2023-10-19 21:37:41浏览次数:31  
标签:const int double void Complex 实验 Rect

1.实验任务一

task1源代码:

 

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

 

task1运行结果:

 

 

2.实验任务二

task2源代码:

 

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

 

task2运行结果:

 

 

3.实验任务三

task3源代码:

 

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

 

task3运行结果:

 

 

4.实验任务四

task4源代码:

 

  1 #include<iostream>
  2 #include<string>
  3 #include<iomanip>
  4 
  5 using namespace std;
  6 
  7 // 矩形类Rect的定义 
  8 class Rect {
  9     public:
 10         Rect(double length = 2.0, double width = 1.0);     //  带默认值的构造函数 
 11         Rect(const Rect &r);
 12         ~Rect();    // 析构函数 
 13                 
 14         double len() const;
 15         double wide() const;
 16         double circumference() const;    // 周长 
 17         double area() const;    // 面积 
 18         void resize(double times);
 19         void resize(double length_times, double width_times);
 20         
 21     public:
 22         static const string doc;    // 类属性,用于描述类基础信息 
 23         void display() const;
 24         static int size_info();    // 类方法,返回当前矩形对象总数 
 25         
 26     private:
 27         double length, width;
 28         static int size;    // 类属性,用于描述当前矩形对象总数 
 29 };
 30 
 31 const string Rect::doc{"a simple Rect class"};
 32 int Rect::size = 0;
 33 
 34 Rect::Rect(double length, double width): length{length}, width(width) {
 35     ++size;
 36 }
 37 
 38 Rect::Rect(const Rect &r): length{r.length}, width{r.width} {
 39     ++size;
 40 }
 41 
 42 Rect::~Rect() {
 43     --size;
 44 }
 45 
 46 double Rect::len() const{
 47     return length;
 48 }
 49 
 50 double Rect::wide() const {
 51     return width;
 52 }
 53 
 54 double Rect::circumference() const {
 55     return 2 * length + 2 * width;
 56 }
 57 
 58 double Rect::area() const {
 59     return length * width;
 60 }
 61 
 62 void Rect::resize(double times) {
 63     length *= times;
 64     width *= times;
 65 }
 66 
 67 void Rect::resize(double length_times, double width_times) {
 68     cout << size;
 69 }
 70 
 71 int Rect::size_info() {
 72     return size;
 73 }
 74 
 75 // 普通函数:输出矩形信息
 76 void output(const Rect &r) {
 77     cout << "矩形信息:" << endl;
 78     cout << fixed << setprecision(2);    // 控制输出格式:以浮点数形式输出,小数部分保留两位
 79     
 80     cout << left << setw(12) << "长:" << r.len() << endl;
 81     cout << left << setw(12) << "宽:" << r.wide() << endl;
 82     cout << left << setw(12) << "面积:" << r.area() << endl;
 83     cout << left << setw(12) << "周长:" << r.circumference() << endl;
 84 } 
 85 
 86 void test() {
 87     cout << "矩形类信息:" << Rect::doc << endl;
 88     cout << "当前矩形对象数目:" << Rect::size_info() << endl;
 89     Rect r1;
 90     output(r1);
 91     
 92     Rect r2(4, 3);
 93     output(r2);
 94     
 95     Rect r3(r2);
 96     r3.resize(2);
 97     output(r3);
 98     r3.resize(5, 2);
 99     output(r3);
100     cout << "当前矩形对象数目:" << Rect::size_info() << endl;
101 }
102 
103 // 主函数
104 int main() {
105     test();
106     cout << "当前矩形对象数目:" << Rect::size_info() << endl; 
107 }

 

 

task4运行结果:

 

5.实验任务五

task5源代码:

 

#include<iostream>
#include<cmath>

class Complex{
    public:
        Complex(double real = 0.0, double imag = 0.0);
        Complex(const Complex &c);
        ~Complex();
        
        double get_real() const;
        double get_imag() const;
        
        void add(const Complex &c);
        void show() const;
        
    friend Complex add(const Complex &c1, const Complex &c2);
    friend bool is_equal(const Complex &c1, const Complex &c2);
    friend double abs(const Complex &c);
        
    private:
        double real, imag;
};

Complex::Complex(double real, double imag): real{real}, imag{imag} {}
Complex::Complex(const Complex &c): real{c.real}, imag{c.imag} {}
Complex::~Complex() {}

double Complex::get_real() const {
    return real;
}

double Complex::get_imag() const {
    return imag;
}

void Complex::show() const {
    using namespace std;
    
    if(imag == 0) {
        cout << real << endl;
    }
    else if(imag > 0) {
        cout << real << "+" << imag << "i" << endl;
    }
    else if(imag < 0) {
        cout << real << imag << "i" << endl;
    }
}

void Complex::add(const Complex &c) {
    real += c.real;
    imag += c.imag;
}

Complex add(const Complex &c1, const Complex &c2) {
    return Complex(c1.real + c2.real, c1.imag + c2.imag);
}

bool is_equal(const Complex &c1, const Complex &c2) {
    if(c1.real == c2.real && c1.imag == c2. imag) {
        return true;
    }
    else {
        return false;
    }
}

double abs(const Complex &c) {
    return sqrt(c.real * c.real + c.imag * c.imag);
}


// 复数类Complex:调试 
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();
}

 

task5运行结果:

 

 

标签:const,int,double,void,Complex,实验,Rect
From: https://www.cnblogs.com/atry/p/17761655.html

相关文章

  • oop 实验1类和对象基础编程
    #include<iostream>#include<string>#include<vector>#include<array>//通用函数(此处是模板函数)用于输出容器中的元素,支持范围for(范围for循环,是一种用于遍历容器、数组和其他序列容器的现代C++迭代循环结构。它提供了一种更简洁和易读的方法来遍历容器的元素,而无需手动......
  • 实验二
    task.2#include<stdio.h>intmain(){charn;while(scanf("%c",&n)!=EOF){getchar();switch(n){case'r':printf("stop!\n");break;case'g':printf(&q......
  • 实验2 代码
    #include<stdio.h>#include<string.h>#include<openssl/evp.h>#include<openssl/err.h>voidtDigest(){   unsignedcharmd_value[EVP_MAX_MD_SIZE];   unsignedintmd_len;   EVP_MD_CTX*mdctx;   mdctx=EVP_MD_CTX_new();   charmsg......
  • 山东省实验中学 2023 秋提高级友好学校赛前联测 3 T4
    子序列(sequence)题目描述给定一个长度为\(N\)的序列\(A\)。对于一个子序列,若任意两个在子序列中相邻的元素\(A_x,A_y(x<y)\),都满足\(A_x<A_y\),且原序列的区间\([x,y)\)中不存在严格大于\(A_x\)的值,那么我们就说这个子序列是"贪心上升"的。定义一个子序列的权......
  • 实验二
    实验一1#include<stdio.h>2#include<stdlib.h>3#include<time.h>45#defineN56#defineN13747#defineN24658intmain()9{10intnumber;11inti;12srand(time(0));13for(i=0;i<N;++i)14{15......
  • 山东省实验中学 2023 秋提高级友好学校赛前联测 3 T2
    琼玉牌(qiongyu)题目描述青雀正在玩「帝垣琼玉」牌。「帝垣琼玉」牌有\(3\)种不同花色的琼玉牌,青雀的桌子上有\(4\)个放牌的位置,最开始青雀的牌桌上没有琼玉牌。青雀会进行\(n\)回合的抽牌。每个回合开始时,青雀会从牌堆里立即随机抽取\(2\)次牌(牌堆里每种牌都有无......
  • 山东省实验中学 2023 秋提高级友好学校赛前联测 3 T3
    零一串(string)题目描述给定一个长度为\(n\)的01串,你需要将它划分成若干段,每一段的长度都不超过\(m\),且满足以下两种条件之一:这个段中全部为\(0\)或全部为\(1\).这个段中\(0,1\)数量之差不超过\(k\).你需要求出该01串合法的划分最少要多少段。输入格式第......
  • 山东省实验中学 2023 秋提高级友好学校赛前联测 3 T1
    生成树(tree)题目描述给定一棵\(n\)个节点的树。定义这棵树的生成完全图为一个\(n\)个节点的完全图,图中两点\(u,v\)的边权为这两点在树上简单路径上的边权和。请你求出这张完全图的最小生成树和最大生成树,分别输出两种生成树的边权之和。输入格式第一行输入一个正整......
  • 实验1 类和对象_基础编程1
    1.实验任务1task.1程序代码:#include<iostream>#include<string>#include<vector>#include<array>template<typenameT>voidoutput1(constT&obj){for(autoi:obj)std::cout<<i<<",";......
  • 电子级PFA试剂瓶50ml 100ml 250ml 500ml高纯实验级PFA取样瓶
    PFA试剂瓶(PFAreagentbottle)一、产品简介PFA试剂瓶又叫PFA样品瓶、PFA取样瓶、PFA广口瓶。我司PFA试剂瓶分为GL45的广口瓶和GL32的细口瓶。PFA塑料的耐化学腐蚀性,对所有化学品都耐腐蚀,摩擦系数在塑料中低,还有很好的电性能,其电绝缘性不受温度影响。因其未添加回料具有低的本底,金......