实验三

实验任务四

vector_int.hpp

 1 #ifndef VECTOR_INT_HPP
 2 #define VECTOR_INT_HPP
 3 #include<iostream>
 4 #include<cassert>
 5 
 6 using namespace std;
 7 class Vector_int
 8 {
 9 public:
10     Vector_int(int n) : size{n}
11     {
12         cout << "constructor called." << endl;
13         p = new int[size]();
14         for(int i = 0; i < size; i++)
15             p[i] = 0;        
16     } 
17     
18     Vector_int(int n, int m) : size{n}
19     {
20         cout << "constructor called." << endl;
21         p = new int[size];//向系统申请n个int型数据项空间
22         for(int i = 0; i < size; i++)
23             p[i] = m;
24     } 
25     
26     Vector_int(const Vector_int &x) : size{x.size}
27     {
28         cout << "copy constructor called." << endl;
29         p = new int[size];
30         for(int i = 0; i < size; i++)
31         {
32             p[i] = x.p[i];
33         }
34     } 
35     
36     ~Vector_int()
37     {
38         cout << "deleting..." << endl;
39         delete[] p;
40     }
41     //设置数组元素值 
42     void reset_p()
43     {
44         int temp;
45         cout << "请输入数组元素: " << endl; 
46         for(int i = 0; i < size; i++)
47         {
48             cin >> temp;
49             p[i] = temp;
50         }
51     }
52  
53     void show_p()
54     {
55         cout << "数组元素: " << endl; 
56         for(int i = 0; i < size; i++)
57         {
58             cout << p[i] << " ";
59         }
60         cout << endl;
61     }
62     
63     // 通过at()方法访问对象中索引为index的数据项
64     int &at(int index)
65     {
66         assert(index >= 0 && index < size);
67         return p[index];
68     }
69 private:
70     int size;//数组大小 
71     int *p;
72 };
73 
74 #endif

task4.cpp

 1 #include<iostream>
 2 #include"vector_int.hpp"
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int n, m, index; 
 9     cin >> n >> m;
10     Vector_int x(n);
11     x.show_p();
12     x.reset_p();
13     x.show_p();
14     x.at(0) = 999;
15     cout << "索引后"; 
16     x.show_p();
17      
18     Vector_int x2(n, m);
19     x2.show_p();
20     x2.reset_p();
21     x2.show_p();
22     x.at(0) = 999;
23     cout << "索引后";
24     x.show_p();
25     
26     Vector_int y(x2);
27     y.show_p();
28     x.at(0) = 999;
29     cout << "索引后";
30     x.show_p();
31     
32     return 0; 
33 }

实验三

 

 实验任务五

Matrix.hpp

 1 #ifndef MATRIX_H
 2 #define MATRIX_H
 3 #include <iostream>
 4 
 5 using namespace std;
 6 class Matrix
 7 {
 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     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 private:
21     int lines; // 矩阵行数
22     int cols; // 矩阵列数
23     double *p; // 指向存放矩阵数据的内存块的首地址
24 };
25 // 类Matrix的实现:
26 Matrix::Matrix(int n): lines{n}, cols{n} 
27 {
28     p = new double[n*n];
29 }
30 
31 Matrix::Matrix(int n, int m):lines{n}, cols{m} 
32 {
33     p = new double[n*m];
34 }
35 
36 Matrix::Matrix(const Matrix &X):lines{X.lines}, cols{X.cols}
37 {
38     p = new double[lines*cols];
39     for(int i = 0; i < lines*cols; i++)
40     {
41         p[i] = X.p[i];
42     }
43 }
44 
45 Matrix::~Matrix() {}
46 
47 void Matrix::set(const double *pvalue)
48 {
49     for(int i = 0; i < lines*cols; i++)
50     {
51         p[i] = pvalue[i];
52     }
53 }
54 
55 void Matrix::set(int i, int j, int value)
56 {
57     p[i * cols + j] = value;
58 }
59 
60 double &Matrix::at(int i, int j)
61 {
62     return p[i * cols + j];
63 }
64 
65 double Matrix::at(int i, int j) const
66 {
67     return p[i * cols + j];
68 }
69 
70 int Matrix::get_lines() const
71 {
72     return lines;
73 }
74 
75 int Matrix::get_cols() const
76 {
77     return cols;
78 }
79 
80 void Matrix::print() const
81 {
82     
83     for(int i = 0; i < lines; i++)
84     {
85         for(int j = 0; j < cols; j++)
86         {
87             cout << p[i * cols +j] << " ";
88         }
89         cout << endl;
90     }
91 }
92 #endif

task5.cpp

 1 #include <iostream>
 2 #include "matrix.hpp"
 3 int main()
 4 {
 5     using namespace std;
 6     double x[] = {3, 2, 1, 8, 9, 7};
 7     Matrix m1(3, 2); // 创建一个3×2的矩阵
 8     m1.set(x); // 用一维数组x的值按行为矩阵m1赋值
 9     m1.print(); // 打印矩阵m1的值
10     cout << "the first line is: " << endl;
11     cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; // 输出矩阵m1第1行两个元素的值
12     cout << endl;
13     Matrix m2(2, 3);
14     m2.set(x);
15     m2.print();
16     cout << "the first line is: " << endl;
17     cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
18     cout << endl;
19     Matrix m3(m2); // 用矩阵m2构造新的矩阵m3
20     m3.set(0, 0, 10); // 将矩阵m3第0行第0列元素值设为10
21     m3.print();
22 }

实验三

 

上一篇:JavaScript 属性类型(数据属性和访问器属性)


下一篇:05 RDD练习:词频统计