实验三
task4.hpp
1 #pragma once 2 #ifndef VECTOR_INT_HPP 3 #define VECTOR_INT_HPP 4 #include<iostream> 5 6 class vector_int 7 { 8 private: 9 int* p; 10 int size; 11 public: 12 vector_int(int numble, int val); 13 vector_int(const vector_int& a1); 14 ~vector_int(); 15 void show(); 16 int& at(int index); 17 }; 18 19 vector_int::vector_int(int numble,int val=0) { 20 21 using namespace std; 22 size = numble; 23 p = new int[size]; 24 for (int i = 0; i < size; i++) { 25 p[i] = val; 26 } 27 cout << "calling the constructor" << endl; 28 } 29 30 vector_int::vector_int(const vector_int& a1) { 31 using namespace std; 32 size = a1.size; 33 p = new int[size]; 34 for (int i = 0; i < size; i++) { 35 p[i] = a1.p[i]; 36 } 37 cout << "calling the copy constructor" << endl; 38 } 39 40 vector_int::~vector_int() { 41 using namespace std; 42 delete[] p; 43 cout << "destroy the constructor" << endl; 44 } 45 46 void vector_int::show() { 47 48 using namespace std; 49 for (int i = 0; i < size; i++) { 50 cout << p[i] << ", "; 51 } 52 cout << endl; 53 54 } 55 56 int& vector_int::at(int index) { 57 if (index > 0 && index < size) 58 return p[index]; 59 60 } 61 62 #endif
task4.cpp
1 #include "vector_int.hpp" 2 3 int main() { 4 using namespace std; 5 int n=10; 6 vector_int x(n); 7 x.show(); 8 vector_int z(n, 6); 9 z.show(); 10 vector_int y(x); 11 y.show(); 12 y.at(0) = 999; 13 y.show(); 14 }
运行截图:
task6.hpp
1 #ifndef MATRIX_H 2 #define MATRIX_H 3 4 #include <iostream> 5 #include <cassert> 6 7 class Matrix 8 { 9 public: 10 Matrix(int n); // 构造函数,构造一个n*n的矩阵 11 Matrix(int n, int m); // 构造函数,构造一个n*m的矩阵 12 Matrix(const Matrix &X); // 复制构造函数,使用已有的矩阵X构造 13 ~Matrix(); //析构函数 14 void set(const double *pvalue); // 用pvalue指向的连续内存块数据为矩阵赋值 15 void set(int i, int j, double value); //设置矩阵第i行第j列元素值为value 16 double &at(int i, int j); //返回矩阵第i行第j列元素的引用 17 double at(int i, int j) const; // 返回矩阵第i行第j列元素的值 18 int get_lines() const; //返回矩阵行数 19 int get_cols() const; //返回矩列数 20 void print() const; // 按行打印输出矩阵 21 private: 22 int lines; // 矩阵行数 23 int cols; // 矩阵列数 24 double *p; // 指向存放矩阵数据的内存块的首地址 25 }; 26 27 28 // 类Matrix的实现:待补足 29 // ××× 30 Matrix::Matrix(int n): lines(n), cols(n) 31 { 32 this->p = new double[this->lines * this->cols]; 33 } 34 Matrix::Matrix(int n, int m): lines(n), cols(m) 35 { 36 this->p = new double[this->lines * this->cols]; 37 } 38 Matrix::Matrix(const Matrix &X): lines(X.lines), cols(X.cols) 39 { 40 this->p = new double[this->lines * this->cols]; 41 for (int i = 0; i < this->lines * this->cols; i++) 42 p[i] = X.p[i]; 43 } 44 Matrix::~Matrix() 45 { 46 delete[] this->p; 47 this->p = nullptr; 48 } 49 void Matrix::set(const double *pvalue) 50 { 51 int tail = 0; 52 for (int i = 0; i < this->lines * this->cols; i++) 53 p[i] = pvalue[tail++]; 54 } 55 void Matrix::set(int i, int j, double value) 56 { 57 p[i * this->cols + j] = value; 58 } 59 double& Matrix::at(int i, int j) 60 { 61 return p[i * this->cols + j]; 62 } 63 double Matrix::at(int i, int j) const 64 { 65 return p[i * this->cols + j]; 66 } 67 int Matrix::get_lines() const 68 { 69 return this->lines; 70 } 71 int Matrix::get_cols() const 72 { 73 return this->cols; 74 } 75 void Matrix::print() const 76 { 77 for (int i = 0; i < this->lines; i++) 78 { 79 for (int j = 0; j < this->cols; j++) 80 std::cout << this->p[i * this->cols + j] << " "; 81 std::cout << std::endl; 82 } 83 } 84 85 #endif
task5.cpp
#include <iostream> #include "matrix.hpp" int main() { using namespace std; double x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; Matrix m1(3, 4); // 创建一个3×2的矩阵 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 m1.print(); // 打印矩阵m1的值 cout << "the first line is: " << endl; for (int i = 0; i < 4; i++) cout << m1.at(0, i) << " "; cout << endl << endl; Matrix m2(4, 3); m2.set(x); m2.print(); cout << "the first line is: " << endl; for (int i = 0; i < 3; i++) cout << m2.at(0, i) << " "; cout << endl << endl; Matrix m3(m2); m3.set(0, 0, 999); m3.print(); }
运行截图: