实验四

  • 实验任务5

代码:

vectorInt.hpp:

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

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 
19     output(x2);
20     output(x3);
21 
22     x2.at(0) = 77;
23 
24     output(x2);
25     output(x3);
26 }
27 
28 int main() {
29     test();
30 }

运行截图:

 

  • 实验任务六

Matrix.hpp:

 

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

 

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);    // 创建一个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(1, 2, 555);  // 将矩阵m3第0行第0列元素值设为999
25     m3.print();
26 }
27 
28 int main() {
29     test();
30 }

运行截图:

 

posted @ 2022-11-07 19:42  RICHENGG  阅读(20)  评论(0)    收藏  举报