首页 > 其他分享 >实验1

实验1

时间:2023-10-17 21:44:55浏览次数:23  
标签:const cout int double void 实验 Rect

试验任务1

task1.cpp源码

// 标准库string, vector, array基础用法

#include <iostream>
#include <string>
#include <vector>
#include <array>

// 函数模板
// 对满足特定条件的序列类型T对象,使用范围for输出
template<typename T>
void output1(const T &obj) {
    for(auto i: obj)
        std::cout << i << ", ";
    std::cout << "\b\b \n";
}

// 函数模板
// 对满足特定条件的序列类型T对象,使用迭代器输出
template<typename T>
void output2(const T &obj) {
    for(auto p = obj.begin(); p != obj.end(); ++p)
        std::cout << *p << ", ";
    std::cout << "\b\b \n";
}

// array模板类基础用法
void test_array() {
    using namespace std;

    array<int, 5> x1;   // 创建一个array对象,包含5个int元素,未初始化
    cout << "x1.size() = " << x1.size() << endl;  // 输出元素个数
    x1.fill(42);        // 把x1的所有元素都用42填充
    x1.at(0) = 999;     // 把下标为0的元素值修改为999
    x1[4] = -999;       // 把下表为4的元素值修改为-999
    cout << "x1: ";
    output1(x1);        // 调用模板函数output1输出x1
    cout << "x1: ";
    output2(x1);        // 调用模板函数output1输出x1

    array<int, 5> x2(x1);
    cout << boolalpha << (x1 == x2) << endl;
    x2.fill(22);
    cout << "x2: ";
    output1(x2);

    swap(x1, x2);   // 交换array对象x1, x2
    cout << "x1: ";
    output1(x1);
    cout << "x2: ";
    output1(x2);
}

// vector模板类基础用法
void test_vector() {
    using namespace std;

    vector<int> v1;
    cout << v1.size() << endl;      // 输出目前元素个数
    cout << v1.max_size() << endl;  // 输出元素个数之最大可能个数
    v1.push_back(55);               // 在v1末尾插入元素
    cout << "v1: ";
    output1(v1);

    vector<int> v2 {1, 0, 5, 2};
    v2.pop_back();                  // 从v2末尾弹出一个元素
    v2.erase(v2.begin());         // 删除v2.begin()位置的数据项
    v2.insert(v2.begin(), 999);     // 在v1.begin()之前的位置插入
    v2.insert(v2.end(), -999);      // 在v1.end()之前的位置插入
    cout << v2.size() << endl;
    cout << "v2: ";
    output2(v2);

    vector<int> v3(5, 42);         //创建vector对象,包含5个元素,每个元素值都是42
    cout << "v3: ";
    output1(v3);

    vector<int> v4(v3.begin(), v3.end()-2); // 创建vector对象,以v3对象的[v3.begin(), v3.end()-2)区间作为元素值
    cout << "v4: ";
    output1(v4);
}

// string类基础用法
void test_string() {
    using namespace std;

    string s1{"oop"};
    cout << s1.size() << endl;
    for(auto &i: s1)
        i -= 32;
    s1 += "2023";
    s1.append(", hello");
    cout << s1 << endl;
}

int main() {
    using namespace std;

    cout << "===========测试1: array模板类基础用法===========" << endl;
    test_array();

    cout << "\n===========测试2: vector模板类基础用法===========" << endl;
    test_vector();

    cout << "\n===========测试3: string类基础用法===========" << endl;
    test_string();
}
View Code

运行测试结果:

实验任务2

task2.cpp源码:

 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 }
View Code

 

运行测试结果:

 实验任务3

task3.cpp源码:

  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 display_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 T::T(int x, int y): m1{x}, m2{y} {
 44     ++count;
 45     cout << "constructor called.\n";
 46 } 
 47 
 48 T::T(const T &t): m1{t.m1}, m2{t.m2}{
 49     ++count;
 50     cout << "copy constructor called.\n";
 51 }
 52 
 53 T::T(T &&t): m1{t.m1}, m2{t.m2}{
 54     ++count;
 55     cout << "move constructor called.\n";
 56 }
 57 
 58 T::~T(){
 59     --count;
 60     cout << "destructor called.\n";
 61 }
 62 
 63 void T::set_m1(int x) {
 64     m1 = x;
 65 }
 66 
 67 int T::get_m1() const {
 68     return m1; 
 69 } 
 70 
 71 int T::get_m2() const {
 72     return m2; 
 73 } 
 74 
 75 void T::display() const {
 76     cout << m1 << "," << m2 << endl; 
 77 }
 78 
 79 // 类方法
 80 void T::display_count() {
 81     cout << "T objects:" << count << endl; 
 82 }
 83 
 84 // 友元函数func(): 实现
 85 void func(){
 86     T t1;
 87     t1.set_m1(55);
 88     t1.m2 = 77;    // 虽然m2是私有成员,依然可以直接访问 
 89     t1.display();
 90 } 
 91 
 92 // 测试
 93 void test(){
 94     cout << "T class info: " << T::doc << endl;
 95     cout << "T objects max_count: " << T::max_count << endl;
 96     T::display_count();
 97 
 98     T t1;
 99     t1.display();
100     t1.set_m1(42);
101     
102     T t2{t1};
103     t2.display();
104     
105     T t3{std::move(t1)};
106     t3.display();
107     t1.display();
108     
109     T::display_count();
110 }
111 
112 // 主函数
113 int main(){
114     cout << "============测试类T============"  << endl; 
115     test();
116     cout << endl;
117     
118     cout << "============测试友元函数func()============"  << endl;  
119     func();
120 } 
View Code

运行测试结果:

 实验任务4

task4.cpp源码:

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

 

运行测试结果:

 

实验任务5

task5.cpp源码:

  1 #include <iostream>
  2 #include <cmath>
  3 
  4 class Complex{
  5 public:
  6     Complex(double r = 0, double i = 0){
  7         real = r;
  8         imag = i;
  9     }
 10     Complex(const Complex &c){
 11         real = c.real;
 12         imag = c.imag;
 13     }
 14     double get_real() const{
 15         return real;
 16     }
 17     double get_imag() const{
 18         return imag;
 19     }
 20     void add(const Complex &c){
 21         real += c.get_real();
 22         imag += c.get_imag();
 23     }
 24     void show(){
 25         std::cout << real;
 26         if (imag>0) std::cout << "+" << abs(imag) << "i";
 27         if (imag<0) std::cout << "-" << abs(imag) << "i";
 28     }
 29     void show() const{
 30         std::cout << real;
 31         if (imag>0) std::cout << "+" << abs(imag) << "i";
 32         if (imag<0) std::cout << "-" << abs(imag) << "i";
 33     }
 34     friend Complex add(const Complex &c1, const Complex &c2);
 35     friend bool is_equal(const Complex &c1, const Complex &c2); 
 36     friend double abs(const Complex &c); 
 37     
 38 private:
 39     double real;
 40     double imag;
 41 };
 42 
 43 Complex add(const Complex &c1, const Complex &c2){
 44     double r, i;
 45     r = c1.get_real() + c2.get_real();
 46     i = c1.get_imag() + c2.get_imag();
 47     Complex c(r, i);
 48     return c;
 49 }
 50 bool is_equal(const Complex &c1, const Complex &c2){
 51     bool r, i;
 52     r = c1.get_real() == c2.get_real();
 53     i = c1.get_imag() == c2.get_imag();
 54     return r && i;
 55 }
 56 double abs(const Complex &c){
 57     return sqrt(pow(c.get_real(), 2) + pow(c.get_imag(), 2));
 58 }
 59 
 60 // 复数类Complex: 测试
 61 void test() {
 62     using namespace std;
 63     
 64     Complex c1(3, -4);
 65     const Complex c2(4.5);
 66     Complex c3(c1);
 67     
 68     cout << "c1 = ";
 69     c1.show();
 70     cout << endl;
 71     
 72     cout << "c2 = ";
 73     c2.show();
 74     cout << endl;
 75     cout << "c2.imag = " << c2.get_imag() << endl;
 76     
 77     cout << "c3 = ";
 78     c3.show();
 79     cout << endl;
 80     
 81     cout << "abs(c1) = ";
 82     cout << abs(c1) << endl;
 83     
 84     cout << boolalpha;
 85     cout << "c1 == c3 : " << is_equal(c1, c3) << endl;
 86     cout << "c1 == c2 : " << is_equal(c1, c2) << endl;
 87     
 88     Complex c4;
 89     c4 = add(c1, c2);
 90     cout << "c4 = c1 + c2 = ";
 91     c4.show();
 92     cout <<endl;
 93     
 94     c1.add(c2);
 95     cout << " c1 += c2, " << " c1 = ";
 96     c1.show();
 97     cout << endl;
 98 } 
 99 
100 int main(){
101     test();
102 }
View Code

运行测试结果:

 

标签:const,cout,int,double,void,实验,Rect
From: https://www.cnblogs.com/xiaochengli/p/17761772.html

相关文章

  • 实验2
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;srand(time(0));for(i=0;i<N;++i){number=rand()%(N2-N1+1)+N1;printf("202......
  • 实验2
     问题1:确保输出的数值在374-465之间问题2:输出4个“学号+0+三位随机数”形式的随机数值#define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){intnumber;inti;......
  • 【产品有效期验证之加速实验方法】
    使用期限:大于等于5年评价路径:整机测试临床使用模式:贮存、待机、运行加速环境实验:是一种激发实验,通过强化应力环境来进行可靠性实验。加速水平通常用加速因子来体现。加速因子:设备正常工作应力下寿命与加速环境下的寿命之比。使用工具:环境实验箱1、温度加速因子(TAF)Arrhenius模型计......
  • 实验一
    #include<iostream>#include<string>#include<vector>#include<array>template<typenameT>voidoutput1(constT&obj){for(autoi:obj)std::cout<<i<<",";std::cout<<"\b\b\......
  • 实验五 队列的基本操作及应用
    实验五队列的基本操作及应用作业要求:实验时间:第7、8周实验目的:掌握队列的初始化、判空、取队头元素、出队、入队、输出队列元素等基本操作实验要求:1、认真阅读和掌握教材上和本实验相关的算法。2、上机将链队列或循环队列的相关算法实现。3、实现下面实验内容要求的功能,并......
  • 实验一 类与对象_基础编程1
    task1.cpp1#include<iostream>2#include<string>3#include<vector>4#include<array>56template<typenameT>7voidoutput1(constT&obj){8for(autoi:obj)9std::cout<<i<<","......
  • 实验2
    任务一源代码 任务一运行结果 任务一问题回答 任务二源代码  任务二运行结果 任务三源代码  任务三运行结果 任务四源代码  任务四运行结果  任务五源代码  任务五运行结果  任务六源代码  任务六运行结果 ......
  • 实验一
    #include<iostream>#include<string>#include<vector>#include<array>//函数模板//对满足特定条件的序列类型T对象,使用范围for输出template<typenameT>voidoutput1(constT&obj){for(autoi:obj)std::cout<<i<<",";......
  • 实验2
    #include<stdio.h>#include<stdlib.h>#include<time.h>#defineN5#defineN1374#defineN2465intmain(){ intnumber; inti; srand(time(0)); for(i=0;i<N;++i){ number=rand()%(N2-N1+1)+N1; printf("2......
  • 实验二
    task1.c1#include<stdio.h>2#include<stdlib.h>3#include<time.h>4#defineN55#defineN13746#defineN24657intmain()8{9intnumber;10inti;11srand(time(0));//以当前系统时间作为随机种子12for(i=0;i<N;++i......