实验三

task4 Vector_int hpp

 1 #ifndef Vector_hpp
 2 #define Vector_hpp
 3 #include<iostream>
 4 #include<cassert>
 5 using namespace std;
 6 class Vector_int
 7 {
 8     private:
 9         int size;
10         int *p;
11     public:
12         Vector_int(const Vector_int &y)
13         {
14             size=y.size;
15             p=new int[size];
16             for(int i=0;i<size;i++){
17                 p[i]=y.p[i];    
18             }
19         }
20         
21         
22         Vector_int(int n,int value=0):size(n){
23             p=new int[size];
24             for(int i=0;i<size;i++)
25             {
26                 p[i]=value;
27             }
28             cout<<"constructor called..."<<endl;
29         }
30         
31     int &at(int b)
32     {
33         assert(b>=0&&b<size);
34         return p[b];
35      } 
36      
37     ~Vector_int()
38     {
39         cout<<"Destructor called..."<<endl;
40         delete[] p;
41     }
42     
43     void show() const
44     {
45         for(int i=0;i<size;i++)
46         cout<<p[i]<<" ";
47         cout<<endl;
48     }
49 };
50 #endif

task 4.cpp

 1 #include<iostream>
 2 #include"Vector_int.hpp"
 3 using namespace std;
 4 
 5 int main(){
 6      int n;
 7      cin>>n;
 8      cout<<endl;
 9      Vector_int x1(n);
10      x1.show();
11      Vector_int x2(n,6);
12      x2.show();
13      Vector_int y(x1);
14      y.show();
15      y.at(0)=999;
16      y.show();
17      
18 }

运行结果

 

task 5 Matrix.hpp

 1 #ifndef MATIX_H
 2 #define MATRIX_H
 3 
 4 #include<iostream>
 5 
 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::Matrix(int n):lines(n),cols(n)
26 {
27     p=new double[n*n];
28 }
29 
30 Matrix::Matrix(int n,int m):lines(n),cols(m)
31 {
32     p=new double[n*m];
33     lines=n;
34     cols=m;
35 }
36 
37 Matrix::Matrix(const Matrix &X):lines(X.lines),cols(X.cols)
38 {
39     p=new double[lines*cols];
40     for(int i=0;i<lines*cols;i++)
41     {
42         p[i]=X.p[i];
43     }
44 }
45 
46 Matrix::~Matrix()
47 {
48     delete[] p;
49 }
50 
51 void Matrix::set(const double *pvalue)
52 {
53     for(int i=0;i<lines*cols;i++)
54     p[i]=pvalue[i];
55 }
56 
57 void Matrix::set(int i,int j,int value)
58 {
59     p[i*cols+j]=value;
60 }
61 
62 double &Matrix::at(int i,int j)
63 {
64     return p[i*cols+j];
65 }
66 
67 double Matrix::at(int i,int j) const
68 {
69     return p[i*cols+j];
70 }
71 
72 int Matrix::get_lines() const
73 {
74     return lines;
75 }
76 
77 int Matrix::get_cols() const
78 {
79     return cols;
80 }
81 
82 void Matrix::print() const
83 {
84     for(int i=0;i<lines;i++){
85         for(int j=0;j<cols;j++){
86             std::cout<<p[i*cols+j]<<", ";
87         }
88            std::cout<<"\b\b \n";
89     }
90 }
91 
92 #endif

task5.cpp

 1 #include <iostream>
 2 #include "matrix.hpp"
 3 int main()
 4 {
 5 using namespace std;
 6 double x[] = {1, 2, 3, 4, 5, 6};
 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, 999); // 将矩阵m3第0行第0列元素值设为999
21 m3.print();
22 }

运行结果

 

 

posted @ 2021-11-09 21:28  zoracoding  阅读(27)  评论(3)    收藏  举报