首页 > 其他分享 >实验四

实验四

时间:2022-11-02 21:03:33浏览次数:37  
标签:const Matrix int lines cols 实验 vectorInt

 1 #pragma once
 2 #include<iostream>
 3 using namespace std;
 4 class vectorInt
 5 {
 6 public:
 7     friend void output(vectorInt& x);
 8     vectorInt(int n);
 9     vectorInt(int n, int value);
10     vectorInt(const vectorInt& r);
11     //vectorInt(vectorInt&& r) = delete;
12     ~vectorInt();
13     int& at(int i);
14     int get_size();
15 private:
16     int size;
17     int* p;
18 };
19 vectorInt::vectorInt(int n) :size(n)
20 {
21     cout << "constructor 1 called." << endl;
22     p = new int[n];
23 }
24 vectorInt::vectorInt(int n, int value) :size(n)
25 {
26     cout << "constructor 2 called." << endl;
27     p = new int[n];
28     for (int i = 0; i < size; i++)
29     {
30         p[i] = value;
31     }
32 }
33 vectorInt::vectorInt(const vectorInt& r) :size{ r.size }
34 {
35     cout << "copy constructor called." << endl;
36     p = new int[size];
37     for (int i = 0; i < size; i++)
38         p[i] = r.p[i];
39 }
40 vectorInt::~vectorInt()
41 {
42     cout << "destructor called." << endl;
43     delete[] p;
44 }
45 int& vectorInt::at(int i)
46 {
47     return p[i];
48 }
49 int vectorInt::get_size()
50 {
51     return size;
52 }
53 void output(vectorInt& x)
54 {
55     /*for (int i = 0; i < x.size; i++)
56     {
57         cout <<*(x.p) << " ";
58         (x.p)++;
59     }*/
60     for (int i = 0; i < x.size; i++)
61         cout << x.p[i] << " ";
62     cout << endl;
63 }
#include <iostream>
#include "vectorInt.h"
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();
}

 

 

 1 #pragma once
 2 #include <iostream>
 3 using std::cout;
 4 using std::endl;
 5 class Matrix
 6 {
 7 public:
 8     Matrix(int n);                     // 构造函数,构造一个n*n的矩阵
 9     Matrix(int n, int m);              // 构造函数,构造一个n*m的矩阵
10     Matrix(const Matrix& X);           // 复制构造函数,使用已有的矩阵X构造
11     ~Matrix();                         //析构函数
12 
13     void set(const double* pvalue);     // 用pvalue指向的连续内存块数据按行为矩阵赋值
14     void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value
15     double& at(int i, int j);          //返回矩阵第i行第j列元素的引用
16     double at(int i, int j) const;     // 返回矩阵第i行第j列元素的值
17     int get_lines() const;             //返回矩阵行数
18     int get_cols() const;              //返回矩列数
19     void print() const;                // 按行打印输出矩阵
20 
21 private:
22     int lines; // 矩阵行数
23     int cols;  // 矩阵列数
24     double* p; // 指向存放矩阵数据的内存块的首地址
25 };
26 Matrix::Matrix(int n) :lines(n), cols(n)
27 {
28     p = new double[n * n];
29 }
30 Matrix::Matrix(int n, int m) :lines(n), cols(m)
31 {
32     p = new double[n * m];
33 }
34 Matrix::Matrix(const Matrix& X) :lines(X.lines), cols(X.cols)
35 {
36     p = new double[X.lines * X.cols];
37     for (int i = 0; i < X.lines * X.cols; i++)
38         p[i] = X.p[i];
39 }
40 Matrix::~Matrix()
41 {
42     delete[]p;
43 }
44 void Matrix::set(const double* pvalue)
45 {
46     for (int i = 0; i < lines * cols; i++)
47         p[i] = pvalue[i];
48 }
49 void Matrix::set(int i, int j, int value)
50 {
51     p[i * cols + j] = value;
52 }
53 double& Matrix::at(int i, int j)
54 {
55     return p[i * cols + j];
56 }
57 double Matrix::at(int i, int j) const
58 {
59     return p[i * cols + j];
60 }
61 int Matrix::get_lines() const
62 {
63     return lines;
64 }
65 int Matrix::get_cols() const
66 {
67     return cols;
68 }
69 void Matrix::print() const
70 {
71     for (int i = 0; i < lines * cols; i++)
72     {
73         cout << p[i] << " ";
74         if ((i + 1) % cols == 0)
75             cout << endl;
76     }
77 }
 1 #include <iostream>
 2 #include "matrix.h"
 3 
 4 void test() {
 5     using namespace std;
 6 
 7     double x[] = { 2, 3, 4, 5, 6,7 };
 8 
 9     Matrix m1(3, 2);    // 创建一个3×2的矩阵
10     m1.set(x);          // 用一维数组x的值按行为矩阵m1赋值
11     m1.print();         // 打印矩阵m1的值
12     cout << "the first line is: " << endl;
13     cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl;  // 输出矩阵m1第1行两个元素的值
14     cout << endl;
15 
16     Matrix m2(2, 3);
17     m2.set(x);
18     m2.print();
19     cout << "the first line is: " << endl;
20     cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
21     cout << endl;
22 
23     Matrix m3(m2);      // 用矩阵m2构造新的矩阵m3
24     m3.set(0, 0, 888);  // 将矩阵m3第0行第0列元素值设为888
25     m3.print();
26 }
27 
28 int main() {
29     test();
30 }

 

标签:const,Matrix,int,lines,cols,实验,vectorInt
From: https://www.cnblogs.com/220011wan/p/16852422.html

相关文章

  • 实验7:基于REST API的SDN北向应用实践
    #delete.pyimportrequestsfromrequests.authimportHTTPBasicAuthif__name__=="__main__":url='http://127.0.0.1:8181/restconf/config/opendaylight-in......
  • 实验7:基于REST API的SDN北向应用实践
    一、实验目的1.能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;2.能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验环境1.下载虚拟机软件OracleVisua......
  • 实验六
    1.基础要求a)回答L2Switch和POX的Hub模块有何不同通过实验结果可知,相比于POX的Hub模块,L2Switch的相同之处在于二者实现的都是洪泛发送ICMP报文,所以在h1去pingh2时,h2和h3......
  • 实验7:基于REST API的SDN北向应用实践
    实验要求(一)基本要求编写Python程序,调用OpenDaylight的北向接口实现以下功能(1)利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;(2)下发指令删除s1上的流表数据......
  • 实验7:基于REST API的SDN北向应用实践
    实验7:基于RESTAPI的SDN北向应用实践一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验......
  • 实验四
    vectorInt.hpp#include<iostream>#include<cassert>usingnamespacestd;classvectorInt{public:vectorInt(intn);vectorInt(intn,intva......
  • 实验2:Open vSwitch虚拟交换机实践
    实验2:OpenvSwitch虚拟交换机实践一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的Python......
  • 实验7
    (一)基本要求编写Python程序,调用OpenDaylight的北向接口实现以下功能利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;下发指令删除s1上的流表数据importrequ......
  • 实验6:开源控制器实践——RYU
    一、实验目的1、能够独立部署RYU控制器;2、能够理解RYU控制器实现软件定义的集线器原理;3、能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Des......
  • 实验四:类与数组、指针
    实验任务五vectorInt.hpp#pragma#include<iostream>usingstd::cout;usingstd::endl;classvectorInt{public:vectorInt(intn);vectorInt......