实验任务4
- vector_int.hpp
1 #ifndef VECTOR_INT_HPP 2 #define VECTOR_INT_HPP 3 #include <iostream> 4 #include <cassert> 5 using namespace std; 6 class Vector_int 7 { 8 public: 9 Vector_int(int n); 10 Vector_int(int n,int v); 11 Vector_int(const Vector_int &x); 12 ~Vector_int() 13 { 14 cout<<"deleting..."<<endl; 15 delete[] p; 16 }; 17 int &at(int index); 18 private: 19 int size; 20 int value; 21 int index; 22 int *p; 23 }; 24 Vector_int::Vector_int(int n):size(n) 25 { 26 cout<<"dynamic create array..."<<endl; 27 p=new int[n]; 28 for(int x=0;x<size;x++) 29 { 30 p[x]=0; 31 } 32 } 33 Vector_int::Vector_int(int n,int v):size(n),value(v) 34 { 35 cout<<"dynamic create array with value..."<<endl; 36 p=new int[n]; 37 for(int x=0;x<size;x++) 38 { 39 p[x]=value; 40 } 41 } 42 Vector_int::Vector_int(const Vector_int &x):size(x.size) 43 { 44 cout<<"deep-copy create array..."<<endl; 45 p=new int[size]; 46 for(int i=0;i<size;i++) 47 { 48 p[i]=x.p[i]; 49 } 50 } 51 int &Vector_int::at(int index) 52 { 53 assert(index>=0&&index<size); 54 return p[index]; 55 } 56 #endif
- task4.cpp
1 #include <iostream> 2 #include "vector_int.hpp" 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */ 4 using namespace std; 5 6 int main() { 7 int n; 8 cout<<"请输入n的值:"<<endl; 9 cin>>n; 10 Vector_int x(n); 11 cout<<"x.at(0)="<<x.at(0)<<endl; 12 Vector_int z(n,6); 13 cout<<"z.at(0)="<<z.at(0)<<endl; 14 Vector_int y(x); 15 y.at(0)=999; 16 cout<<"y.at(0)="<<y.at(0)<<endl; 17 }
- 运行测试结果:
实验任务5
- matrix.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(){delete[] p;}; //析构函数 14 void set(const double *pvalue); // 用pvalue指向的连续内存块数据为矩阵赋值 15 void set(int i, int j, int 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 Matrix::Matrix(int n):lines(n),cols(n) 28 { 29 p=new double[lines*cols]; 30 } 31 32 Matrix::Matrix(int n,int m):lines(n),cols(m) 33 { 34 p=new double[lines*cols]; 35 } 36 37 Matrix::Matrix(const Matrix &X):lines(X.lines),cols(X.cols) 38 { 39 p=new double[lines*cols]; 40 for (auto i=0;i<lines*cols;i++) 41 { 42 p[i]=X.p[i]; 43 } 44 } 45 46 void Matrix::set(const double *pvalue) 47 { 48 int x=0; 49 for (auto i=0;i<lines*cols;i++) 50 { 51 p[i]=pvalue[x++]; 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 using namespace std; 83 for(int i=0;i<lines*cols;i++) 84 { 85 cout<<p[i]; 86 if((i+1)%cols==0&&i!=0) 87 { 88 cout<<endl; 89 } 90 else 91 { 92 cout<<","; 93 } 94 } 95 } 96 #endif
- task5.cpp
1 #include <iostream> 2 #include "matrix.hpp" 3 4 int main() 5 { 6 using namespace std; 7 8 double x[] = {2,4,6,8,10,12}; 9 10 Matrix m1(3, 2); // 创建一个3×2的矩阵 11 m1.set(x); // 用一维数组x的值按行为矩阵m1赋值 12 m1.print(); // 打印矩阵m1的值 13 cout << "the first line is: " << endl; 14 cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; 15 cout << endl; 16 17 Matrix m2(2, 3); 18 m2.set(x); 19 m2.print(); 20 cout << "the first line is: " << endl; 21 cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl; 22 cout << endl; 23 24 Matrix m3(m2); 25 m3.set(0, 0, 888); 26 m3.print(); 27 }
- 运行测试结果: