220011wan

导航

实验四

 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 }

 

posted on 2022-11-02 20:50  或者明天  阅读(12)  评论(0编辑  收藏  举报