首页 > 其他分享 >实验四

实验四

时间:2022-11-07 23:57:23浏览次数:26  
标签:const Matrix int lines cols 实验 vectorInt

实验任务5

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

task5.cpp

 1 #include<iostream>
 2 #include"vectorInt.hpp"
 3 
 4 void test() {
 5     using namespace std;
 6 
 7     int n;
 8     cin >> n;
 9 
10     vectorInt x1(n);
11     for (auto i = 0; i < n; ++i)
12         x1.at(i) = i * i;
13 
14     output(x1);
15 
16     vectorInt x2(n, 42);
17     vectorInt x3(x2);
18     output(x2);
19     output(x3);
20 
21     x2.at(0) = 77;
22     output(x2);
23     output(x3);
24 }
25 int main() {
26     test();
27 }

实验截图

实验任务6

Matrix.hpp

 1 #pragma once
 2 #include<iostream>
 3 
 4 using std::cout;
 5 using std::endl;
 6 
 7 class Matrix {
 8 public:
 9     Matrix(int n);    // 构造函数,构造一个n*n的矩阵
10     Matrix(int n, int m);// 构造函数,构造一个n*m的矩阵
11     Matrix(const Matrix& x);// 复制构造函数,使用已有的矩阵X构造
12     ~Matrix();//析构函数
13 
14     void set(const double* pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值
15     void set(int i, int j, int value);//设置矩阵第i行第j列元素值为value
16     double at(int i, int j)const;// 返回矩阵第i行第j列元素的值
17     int get_lines()const {return lines; }//返回矩阵行数
18     int get_cols()const { return cols; }//返回矩列数
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     p = new double[n * n];
28 }
29 Matrix::Matrix(int n, int m) :lines{ n }, cols{ m } {
30     p = new double[n * m];
31 }
32 Matrix::Matrix(const Matrix& x) :lines{ x.lines }, cols{x.cols} {
33 
34     p = new double[lines * cols];
35     for (auto i = 0; i < lines * cols; i++) {
36         p[i] = x.p[i];
37     }    
38 }
39 Matrix::~Matrix() {
40     delete p;
41 }
42 void Matrix::set(const double* pvalue) {
43     for (auto i = 0; i < lines * cols; i++) {
44         p[i] = pvalue[i];
45     }
46 }
47 void Matrix::print()const {
48     int k = 0;
49     for (auto j = 0; j < lines; j++) {
50         for (auto i = 0; i < cols; i++) {
51             cout << p[k++] << ", ";
52         }
53         cout << "\b\b \n";
54     }
55 }
56 void Matrix::set(int i, int j, int value) {
57     p[i*cols+j] = value;
58 }
59 double Matrix::at(int i, int j)const{
60     return p[i*cols+j];
61 }

task6.cpp

 1 #include<iostream>
 2 #include"Matrix.hpp"
 3 
 4 void test() {
 5     using namespace std;
 6 
 7     double x[] = { 1,2,3,4,5,6 };
 8 
 9     Matrix m1(3, 2);
10     m1.set(x);
11     m1.print();
12     cout << "the first line is: " << endl;
13     cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl;
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); 
24     m3.set(0, 0, 999); 
25     m3.print();
26     cout << endl;
27     
28 }
29 int main()
30 {
31     test();
32 }

实验截图

 

 

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

相关文章

  • 实验四
    task5vectorInt.hpp#pragmaonce#include<iostream>usingnamespacestd;classvectorInt{public:vectorInt(intn){cout<<"constructor2ca......
  • 软件工程实验一
    (1) 回顾你过去将近3年的学习经历问:当初你报考的时候,是真正喜欢软件工程这个专业吗?答:是。问:你现在后悔选择了这个专业吗?答:不后悔。问:你认为你现在最喜欢的领域是什么......
  • 实验四
    #include<iostream>#include<cassert>usingstd::cout;usingstd::endl;classvectorInt{public:vectorInt(intn,intm);vectorInt(intn);vecto......
  • 第四次实验
    1#pragmaonce2#include<iostream>3usingnamespacestd;4classvectorInt5{6public:7vectorInt(intn);8vectorInt(intn,intnum);9......
  • 软件工程实验一
    (1)回顾你过去将近3年的学习经历当初你报考的时候,是真正喜欢计算机这个专业吗?你现在后悔选择了这个专业吗?你认为你现在最喜欢的领域是什么(可以是计算机的也可以是其它......
  • 软件工程实验一MXY
    (1)回顾你过去将近3年的学习经历当初你报考的时候,是真正喜欢计算机这个专业吗?    报考时不了解所学究竟是什么,迷茫焦虑没有方向。你现在后悔选择了这个专业吗?......
  • 实验4:开源控制器实践——OpenDaylight
    (一)基本要求1.利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;2.通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s。......
  • 0001-华为ENSP WLAN配置实验-郑锦程
    0001-华为ENSPWLAN配置实验-郑锦程创建管理vlan和业务vlan​​[AC6605]vlanbatch100101​​​​Info:Thisoperationmaytakeafewseconds.Pleasewaitforamome......
  • 二叉树相关上机实验
    #include<stdio.h>#include<malloc.h>#defineOK1#defineERROR0#defineMAXNUM20typedefintStatus;typedefstructbnode{intdata;structbnode......
  • 软件工程基础Y-实验一王瑜
    (1)回顾你过去将近3年的学习经历当初你报考的时候,是真正喜欢软件工程这个专业吗?有一些喜欢至少比其它专业喜欢你现在后悔选择了这个专业吗?不后悔你认为你现在最喜欢......