首页 > 其他分享 >实验四

实验四

时间:2022-11-03 18:03:13浏览次数:45  
标签:const Matrix int lines cols 实验 vectorInt

实验任务五:

vectorInt.hpp:
 1 #include <iostream>
 2 #include <cassert>
 3 using namespace std;
 4 class vectorInt {
 5 public:
 6     vectorInt(int n);
 7     vectorInt(int n, int x0);
 8     vectorInt(const vectorInt& vp);
 9     ~vectorInt();
10     int& at(int index);
11     int get_size()const;
12     friend void output(vectorInt&x);
13 private:
14     int size;
15     int* p;
16 };
17 int vectorInt::get_size()const {
18     return size;
19 }
20 vectorInt::vectorInt(int n, int x0) {
21     cout << "constructor 2 called" << endl;
22     size = n;
23     p = new int[size];
24     for (auto i = 0; i < size; ++i)
25         p[i] = x0;
26 }
27 vectorInt::vectorInt(const vectorInt& vp) : size{ vp.size } {
28     cout << "copy constructor called" << endl;
29     p = new int[size];
30     for (auto i = 0; i < size; ++i)
31         p[i] = vp.p[i];
32 }
33 vectorInt::vectorInt(int n) : size{ n } {
34     cout << "constructor 1 called" << endl;
35     p = new int[n];
36 }
37 vectorInt::~vectorInt() {
38     cout << "destructor called" << endl;
39     delete[] p;
40 }
41 int& vectorInt::at(int index) {
42     assert(index >= 0 && index < size);
43     return p[index];
44 }
45 void output(vectorInt& x) {
46     for (auto i = 0; i < x.size; ++i)
47         cout << x.at(i) << " ";
48     cout << endl;
49 }
task5.cpp:
 1 #include <iostream>
 2 #include "vectorInt.hpp"
 3 void test() {
 4     using namespace std;
 5     int n;
 6     cin >> n;
 7     vectorInt x1(n);
 8     for (auto i = 0; i < n; ++i)
 9         x1.at(i) = i * i;
10     output(x1);
11     vectorInt x2(n, 42);
12     vectorInt x3(x2);
13     output(x2);
14     output(x3);
15     x2.at(0) = 77;
16     output(x2);
17     output(x3);
18 }
19 int main() {
20     test();
21 }

运行截图:

实验任务六:

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

运行截图:

更改数据后运行截图:

 

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

相关文章

  • 实验四
    实验4.5#pragmaonce#include<iostream>usingnamespacestd;classvectorInt{friendvoidoutput(vectorInt&v);public:vectorInt(intn);vectorIn......
  • Python第十章实验报告
    一、实验题目Python第十章实例和实战作业二、实验目的和要求1.熟悉Pycharm的运行环境2.学习并掌握Python文件及目录操作三、主要仪器设备联想小新air15硬件:AMDR75......
  • 实验四
    vectorint.hpp#pragmaonce#include<iostream>usingstd::cout;usingstd::endl;classvectorint{private:intsize;int*p;public:vectorint(int......
  • 实验7:基于REST API的SDN北向应用实践
    实验7:基于RESTAPI的SDN北向应用实践一、实验目的能够编写程序调用OpenDaylightRESTAPI实现特定网络功能;能够编写程序调用RyuRESTAPI实现特定网络功能。二、实验......
  • 实现实验室和寝室两台电脑文件实时同步
    考虑到白天去实验室工作,晚上又要回寝室,文件传输会很麻烦,于是寻求能够方便进行文件远程同步的方案。1.使用工具内网穿透:zerotier(全平台均可)文件同步(备份)工具:FreeFileSync(Win......
  • 实验3
     task1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<Windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_sp......
  • P4 tutorials实验 - basic-tunnel
    P4tutorials实验-basic-tunnel基础知识隧道(tunnel):可以将IP隧道视为一对节点之间的虚拟点对点链路虚拟链路是在隧道入口处的路由器内创建的,当隧道入口处的路由器想通......
  • Openwrt 跨网实现二层实验
    由于公司项目需要跨网实现二层通信,在咨询大量的大神后他们推荐的方案是vxlan方案。于是就有了下面的实验。网络拓扑:网络环境:内网IP公网IP系统软件路由R1192.168.220.254/241......
  • Python实验报告——第10章 文件及目录操作
    实验报告【实验目的】 1.掌握Python自带的函数进行基本文件操作。2.掌握Python内置的os模块及其子模块os.path进行目录相关的操作。【实验条件】1.PC机或者远程编......
  • 实验三
      #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函......