实验任务五代码截图:
vectorInt.hpp:
1 #pragma once 2 3 #include<iostream> 4 #include<cassert> 5 using namespace std; 6 7 class vectorInt 8 { 9 public: 10 vectorInt(int n); 11 vectorInt(int n, int value); 12 vectorInt(const vectorInt& v); 13 ~vectorInt(); 14 int& at(int index) const; 15 int get_size() const; 16 friend void output(const vectorInt& v); 17 18 private: 19 int* p; 20 int size; 21 }; 22 23 vectorInt::vectorInt(int n) :size{ n } 24 { 25 p = new int[size]; 26 cout << "constructor 1 called." << endl; 27 } 28 vectorInt::vectorInt(int n, int value) : size{ n } 29 { 30 p = new int[size]; 31 for (auto i = 0; i < size; i++) 32 p[i] = value; 33 cout << "constructor 2 called." << endl; 34 } 35 vectorInt::vectorInt(const vectorInt& v) 36 { 37 size = v.size; 38 p = new int[size]; 39 for (auto i = 0; i < size; i++) 40 p[i] = v.p[i]; 41 cout << "copy constructor called." << endl; 42 } 43 vectorInt::~vectorInt() 44 { 45 free(p); 46 cout << "destructor called." << endl; 47 } 48 int& vectorInt::at(int index) const 49 { 50 return p[index]; 51 } 52 int vectorInt::get_size() const 53 { 54 return size; 55 } 56 void output(const vectorInt& v) 57 { 58 for (auto i = 0; i < v.size; i++) 59 cout << v.p[i] << ","; 60 cout << "\b " << endl; 61 }
task5.cpp:
1 #include<iostream> 2 #include"vectorInt.hpp" 3 4 void test() 5 { 6 using namespace std; 7 8 int n; 9 cin >> n; 10 11 vectorInt x1(n); 12 for (auto i = 0; i < n; i++) 13 x1.at(i) = i * i; 14 output(x1); 15 vectorInt x2(n, 42); 16 vectorInt x3(x2); 17 output(x2); 18 output(x3); 19 x2.at(0) = 77; 20 output(x2); 21 output(x3); 22 } 23 int main() 24 { 25 test(); 26 }
运行截图:
实验任务六代码:
Matrix.hpp:
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() = default; //析构函数 12 void set(const double* pvalue); // 用pvalue指向的连续内存块数据按行为矩阵赋值 13 void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value 14 double& at(int i, int j); //返回矩阵第i行第j列元素的引用 15 double at(int i, int j) const; // 返回矩阵第i行第j列元素的值 16 int get_lines() const; //返回矩阵行数 17 int get_cols() const; //返回矩列数 18 void print() const; // 按行打印输出矩阵 19 private: 20 int lines; // 矩阵行数 21 int cols; // 矩阵列数 22 double* p; // 指向存放矩阵数据的内存块的首地址 23 }; 24 Matrix::Matrix(int n) 25 { 26 p = new double[n * n]; 27 lines = n; 28 cols = n; 29 } 30 Matrix::Matrix(int n, int m) 31 { 32 p = new double[n * m]; 33 lines = n; 34 cols = m; 35 } 36 Matrix::Matrix(const Matrix& X) 37 { 38 lines = X.lines; 39 cols = X.cols; 40 p = new double[lines * cols]; 41 for (auto i = 0; i < lines * cols; i++) 42 p[i] = X.p[i]; 43 } 44 void Matrix::set(const double* pvalue) 45 { 46 for (auto 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 (auto i = 0; i < lines; i++) 72 { 73 for (auto j = 0; j < cols; j++) 74 cout << p[i * cols + j] << ","; 75 cout << "\b " << endl; 76 } 77 }
task6.cpp:
1 #include <iostream> 2 #include "matrix.hpp" 3 void test() { 4 using namespace std; 5 double x[] = { 2, 4, 6, 8, 10, 12 }; 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, 666); // 将矩阵m3第0行第0列元素值设为666 20 m3.print(); 21 } 22 int main() { 23 test(); 24 }
代码运行截图:
标签:const,Matrix,int,lines,cols,实验,数组,vectorInt,指针 From: https://www.cnblogs.com/jane-d/p/16862482.html