首页 > 其他分享 >实验一

实验一

时间:2023-10-19 23:11:05浏览次数:31  
标签:count const cout int double 实验 include

任务一

点击查看代码
#include<iostream>
#include<string>
#include<vector>
#include<array>

template<typename T>
void output1(const T &obj){
    for(auto i:obj)
    std::cout<<i<<", ";
    std::cout<<"\b\b \n";
}

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";
}

void test_array(){
    using namespace std;

    array<int,5> x1;
    cout<<"x1.size()="<<x1.size()<< endl;
    x1.fill(42);
    x1.at(0)=999;
    x1[4]=-999;
    cout<<"x1:";
    output1(x1);
    cout<<"x1:";
    output2(x1);

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

}

void test_vector(){
    using namespace std;

    vector<int> v1;
    cout<<v1.size()<< endl;
    cout<<v1.max_size()<<endl;
    v1.push_back(55);
    cout<<"v1: ";
    output1(v1);

    vector<int> v2{1,0,5,2};
    v2.pop_back();
    v2.erase(v2.begin());
    v2.insert(v2.begin(), 999);
    v2.insert(v2.end(), -999);
    cout << v2.size() << endl;
    cout << "v2: ";
    output2(v2);

    vector<int>v3(5,42);
    cout<<"v3:";
    output1(v3);

    vector<int> v4(v3.begin(),v3.end()-2);
    cout<<"v4:";
    output1(v4);
}

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

任务二

点击查看代码
#include <iostream>
#include <complex>

// 测试标准库提供的复数类模板complex
void test_std_complex() {
    using namespace std;
    complex<double> c1{3, 4}, c2{4.5};
    const complex<double> c3{c2};

    cout << "c1 = " << c1 << endl;
    cout << "c2 = " << c2 << endl;
    cout << "c3 = " << c3 << endl;
    cout << "c3.real = " << c3.real() << ", " << "c3.imag = " << c3.imag()<< endl;

    cout << "c1 + c2 = " << c1 + c2 << endl;
    cout << "c1 - c2 = " << c1 - c2 << endl;
    cout << "abs(c1) = " << abs(c1) << endl; // abs()是标准库数学函数,对复数取模

    cout << boolalpha; // 设置bool型值以true/false方式输出
    cout << "c1 == c2: " << (c1 == c2) << endl;
    cout << "c3 == c2: " << (c3 == c2) << endl;

    complex<double> c4 = 2;
    cout << "c4 = " << c4 << endl;
    c4 += c1;
    cout << "c4 = " << c4 << endl;

}


int main(){
    test_std_complex();
}

任务三

点击查看代码
#include <iostream>
#include <string>
using namespace std;

class T {
public:
T(int x = 0, int y = 0);
T(const T &t);
T(T &&t);
~T();
void set_m1(int x);
int get_m1() const;
int get_m2() const;
void display() const;
friend void func();
private:
int m1, m2;
public:
static void disply_count();
public:
static const string doc;
static const int max_count;
private:
static int count;
};

const string T::doc{"a simple class"};
const int T::max_count = 99;
int T::count = 0;

T::T(int x, int y): m1{x}, m2{y} {
++count;
cout << "constructor called.\n";
}
T::T(const T &t): m1{t.m1}, m2{t.m2} {
++count;
cout << "copy constructor called.\n";
}

T::T(T &&t): m1{t.m1}, m2{t.m2} {
++count;
cout << "move constructor called.\n";
}
T::~T() {
--count;
cout << "destructor called.\n";
}
void T::set_m1(int x) {
m1 = x;
}
int T::get_m1() const {
return m1;
}
int T::get_m2() const {
return m2;
}
void T::display() const {
cout << m1 << ", " << m2 << endl;
}

void T::disply_count() {
cout << "T objects: " << count << endl;
}

void func() {
T t1;
t1.set_m1(55);
t1.m2 = 77;
t1.display();
}

void test() {
cout << "T class info: " << T::doc << endl;
cout << "T objects max_count: " << T::max_count << endl;
T::disply_count();
T t1;
t1.display();
t1.set_m1(42);
T t2{t1};
t2.display();
T t3{std::move(t1)};
t3.display();
t1.display();
T::disply_count();
}

int main() {
cout << "============测试类T============" << endl;
test();
cout << endl;
cout << "============测试友元函数func()============" << endl;
func();
}

任务四

点击查看代码
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class Rect {
private:
double length;
double width;
static int count;

public:
static string doc;
Rect() : length(0), width(0) {
     ++count;
 }
Rect(double l, double w) : length(l), width(w) { count++; }
Rect(const Rect& r) : length(r.length), width(r.width) { count++; }
~Rect() {
    --count;
}
double area() const { return length * width; }
double perimeter() const { return 2 * (length + width); }
void resize(double l, double w) {
   length = l;
   width = w;
}
void resize(double s) {
length = s;
 width = s;
 }
friend void output(const Rect& r);
static int size_info() {
 return count;
 }
};

int Rect::count = 0;
 string Rect::doc { "a simple rect class"};


void output(const Rect &r) {
    cout << "矩形信息: " << endl;
cout << fixed << setprecision(2);
cout << "长: " << r.length << endl;
cout << "宽: " << r.width << endl;
cout << "面积: " << r.area() << endl;
cout << "周长: " << r.perimeter() << 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;
}

任务五

点击查看代码
#include <iostream>
#include <cmath>

using namespace std;

class Complex {
private:
    double real;
    double imag;

public:
    Complex(double r = 0, double i = 0) : real(r), imag(i) {}

    Complex(const Complex& other) : real(other.real), imag(other.imag) {}

    double get_real() const {
        return real;
    }

    double get_imag() const {
        return imag;
    }

    void show() const {
        cout << real;
        if(imag != 0){
        if (imag > 0)
            cout << " + " << imag << "i";
        else
            cout << " - " << -imag << "i";
    }
    }

    Complex& add(const Complex& other) {
        real += other.real;
        imag += other.imag;
        return *this;
    }

    friend Complex add(const Complex& c1, const Complex& c2) {
        Complex result(c1);
        return result.add(c2);
    }

    friend bool is_equal(const Complex& c1, const Complex& c2) {
        return (c1.real == c2.real) && (c1.imag == c2.imag);
    }

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

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

标签:count,const,cout,int,double,实验,include
From: https://www.cnblogs.com/liujiandong66da/p/17775945.html

相关文章

  • 实验一 类与对象
    Task1:#include<iostream>#include<string>#include<vector>#include<array>template<typenameT>voidoutput1(constT&obj){for(autoi:obj)std::cout<<"i"<<",";std::cout<<......
  • 实验1_OOP_22物联网1班_张文瑞
    1.实验任务1:  实验源代码:1//标准库string,vector,array基础用法2#include<iostream>3#include<string>4#include<vector>5#include<array>6//函数模板7//对满足特定条件的序列类型T对象,使用范围for输出8template<typenameT>9voidoutpu......
  • 实验1 类与对象
    实验任务1源代码:1#include<iostream>2#include<string>3#include<vector>4#include<array>567template<typenameT>8voidoutput1(constT&obj){9for(autoi:obj)10std::cout<<......
  • 实验二.
     test1#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.实验任务一task1源代码: 1//标准库string,vector,array基础用法23#include<iostream>4#include<string>5#include<vector>6#include<array>78//函数模板9//对满足特定条件的序列类型T对象,使用范围for输出10template<typename......
  • 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\)的值,那么我们就说这个子序列是"贪心上升"的。定义一个子序列的权......