首页 > 其他分享 >实验4 类与数组、指针

实验4 类与数组、指针

时间:2022-11-02 19:11:47浏览次数:50  
标签:Matrix int double lines cols 实验 数组 vectorInt 指针

task5

//vectorInt.hpp

#pragma once
#include<iostream>

using namespace std;

class vectorInt{
    
public :
    vectorInt(int len) : size{len}{
        cout << "constructor 1 called\n";
        p = new int[size];
    }
    
    vectorInt(int len, int value) : size{len} {
        cout << "constructor 2 called\n";
        p = new int[size];
        for(int i = 0; i < size; i ++)
            *(p + i) = value;
    }
    
    vectorInt(const vectorInt & obj) : size{obj.size} {
        cout << "copy constructor called\n";
        p = new int[size];
        for(int i = 0; i < size; i ++)
            *(p + i) = obj.at(i);
    }
    
    int & at(int i) const{
        return *(p + i);
    }
    
    int get_size() const{
        return size;
    }
    
    friend void output(const vectorInt &x) {
        for(int i = 0; i < x.size; i ++)
            cout << *(x.p + i) << ",";
        cout << "\b \n";
    }
    
    ~vectorInt(){
        cout << "destructor called\n";
        delete []p;    
    }
    
private :
     int *p;
    int size;    
};
//task5.cpp

#include <iostream>
#include "vectorInt.hpp"

void test() {
    using namespace std;
    
    int n;
    cin >> n;
    vectorInt x1(n);
    for(auto i = 0; i < n; ++ i)
        x1.at(i) = i * i;
    
    output(x1);
    vectorInt x2(n, 42);
    vectorInt x3(x2);
    
    output(x2);
    output(x3);
    
    x2.at(0) = 77;
    
    output(x2);
    output(x3);
}


int main() {
    test();
}

测试结果:

 

 

 

 

task6

//Matrix.hpp

#pragma once
#include <iostream>

using std::cout;
using std::endl;

class Matrix {

public:
    Matrix(int n) : lines{n}, cols{n}{
        p = new double[lines * cols];
    }
    
    Matrix(int n, int m) : lines{n}, cols{m}{
        p = new double[lines * cols];
    }
    
    Matrix(const Matrix &X) : lines{X.lines}, cols{X.cols}{
        p = new double[lines * cols];    
        for(int i = 0; i < lines * cols; i ++)
            p[i] = X.p[i];
    }
    
    ~Matrix(){
        delete []p;
    }
    
    
    void set(const double *pvalue){
        for(int i = 0; i < lines * cols; i ++)
                p[i] = pvalue[i];
    }
    
    void set(int i, int j, int value){ p[i * cols + j] = value;    }
    
    double &at(int i, int j){ return p[i * cols + j]; }
    
    double at(int i, int j) const {    return p[i * cols + j];    }
    
    int get_lines() const{ return lines; }
    
    int get_cols() const{ return cols; }
        
    void print() const{
        for(int i = 0; i < lines; i ++){
            for(int j = 0; j < cols; j ++)
                cout << p[i * cols + j] << ",";
            cout << "\b \n";
        }
    }
private:
    int lines;
    int cols; 
    double *p;
};
//task6.cpp

#include <iostream>
#include "matrix.hpp"

void test() {
    
    using namespace std;
    
    double x[] = {1, 2, 3, 4, 5, 6};
    Matrix m1(3, 2);
    m1.set(x);
    m1.print();

    cout << "the first line is: " << endl;
    cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl;
    cout << endl;
    
    Matrix m2(2, 3);
    m2.set(x);
    m2.print();
    
    cout << "the first line is: " << endl;
    cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
    cout << endl;
    
    Matrix m3(m2);
    m3.set(0, 0, 999);
    m3.print();
}

int main() {
    test();
}

测试结果:

更换一组测试数据的结果:

 

标签:Matrix,int,double,lines,cols,实验,数组,vectorInt,指针
From: https://www.cnblogs.com/lyhy/p/16852063.html

相关文章

  • 实验4 类与数组、指针
    实验任务1程序源码point.hpp#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classPoint{public:Point():x{0},y{0}{}Point(i......
  • v-for绑定数组item时深拷贝的问题
     changedef(index){debuggervararr=JSON.parse(JSON.stringify(store.state.gigi.extdata))arr[index].defValue=this.optionList[index].defValue......
  • vue修改对象后加属性数组,该数组变化,dom不发生变化的问题
    当vue中改变对象元素的情况下,不会重新渲染dom元素,这时候可以用vue的$set方法。一般情况下就可以实现功能了,也就是改变对象元素后,会重新渲染dom,如果当你使用挺好this.$set方......
  • 实验2:Open vSwitch虚拟交换机实践
    实验2:OpenvSwitch虚拟交换机实践一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Pytho......
  • 实验四 类与数组、指针
    task5.1#pragmaonce2#include<bits/stdc++.h>3usingnamespacestd;4classvectorInt{5public:6vectorInt(intn);7vectorInt(intn,int......
  • 实验7:基于REST API的SDN北向应用实践
    实验7:基于RESTAPI的SDN北向应用实践一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验......
  • 实验7:基于REST API的SDN北向应用实践
    实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。实验要求(一)基本要求编写Python程序,调用OpenDayl......
  • 实验4
    实验任务5:vectorint: #include<iostream>usingnamespacestd;classvectorInt{public: vectorInt(intn):size{n}{ p=newint[n]; cout<<"co......
  • shell编程之数组
    1什么是数组数组(Array)是有序的元素序列。若将有限个类型相同的变量的集合命名,那么这个名称为数组名。组成数组的各个变量称为数组的分量,也称为数组的元素,有时也称为下......
  • 实验4 类与数组、指针
    实验目的会正确定义和使用简单的类模板能够说明指针、引用的联系和区别,能根据问题场景灵活、正确使用指针作为函数参数、引用作为函数参数知道什么是深复制、浅复制,能......