实验四

实验任务五:

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 }

运行截图:

更改数据后运行截图:

 

posted @ 2022-11-03 17:51  段彦博  阅读(7)  评论(0)    收藏  举报