首页 > 其他分享 >实验4

实验4

时间:2022-11-05 18:12:32浏览次数:57  
标签:const Matrix int lines cols 实验 vectorInt

实验4.5:

vectorInt.hpp

 1 #include <iostream>
 2 using namespace std; 
 3 class vectorInt{
 4     public:
 5     vectorInt(int n);
 6     vectorInt(int n,int value);
 7     vectorInt( vectorInt& T);
 8     ~vectorInt();
 9     int &at(int i);
10     int get_size();
11     friend void output(vectorInt &T);
12     private:
13     int* p;    int size;
14 }; 
15 vectorInt::vectorInt(int n):size{n}{
16     p=new int[n];
17     for(auto i=0:n){
18         p[i]=0;
19     }
20     cout<<"constructor 1 called.\n";
21 }
22 vectorInt::vectorInt(int n,int value):size{n}{
23         p=new int[n];
24         
25     for(int i=0;i<size;++i){
26         p[i]=value;
27     }
28     cout<<"constructor 2 called.\n";
29 }
30 vectorInt::vectorInt( vectorInt& T){
31     this->p=new int[T.size];
32     this->size=T.size;
33     for(int i=0;i<size;++i){
34         this->p[i]=T.at(i);
35     }
36 cout<<"copy constructor called.\n";
37 }
38 int &vectorInt::at(int i){
39     return p[i];
40 }
41 int vectorInt::get_size(){
42     return size;
43 }
44 void output(vectorInt &T){
45     for(int i=0;i<T.get_size();++i){
46     cout<<T.at(i)<<", ";
47     }
48     cout<<"\b\b \n";
49 }
50 vectorInt::~vectorInt(){
51     delete [] p;
52     cout<<"destructor called.\n";
53 }

task5.cpp

 1 #include <iostream>
 2 #include "vectorInt.hpp"
 3 void test(){
 4     using namespace std;
 5     int n;
 6     cin>>n;
 7     vectorInt x1(n);
 8     for(auto i=0;i<n;i++){
 9         x1.at(i)=i*i;
10     }
11     output(x1);
12     vectorInt x2(n,42);
13     vectorInt x3(x2);
14     
15     output(x2);
16     output(x3);
17     
18     x2.at(0)=77;
19     output(x2);
20     output(x3);
21 }
22 int main(){
23     test();}

 

 

实验4.6

matrix.hpp:

 1 #pragma once
 2 #include <iostream>
 3 using std::cout;
 4 using std::endl;
 5 class Matrix{
 6     public:
 7         Matrix(int n);
 8         Matrix(int n,int m);
 9         Matrix(const Matrix &X);
10         ~Matrix();
11         
12         void set(const double* pvalue);
13         void set(int i,int j,int value);
14         double &at(int i,int j)const;
15         int get_lines()const;
16         int get_cols()const;
17         void print()const;
18     private:
19         int lines;
20         int cols;
21         double* p;
22 };
23 int Matrix::get_lines()const{
24     return lines;
25 }
26 int Matrix::get_cols()const{
27     return cols;
28 }
29 Matrix::Matrix(int n):lines{n},cols{n}{
30     p=new double[n*n];
31 }
32 Matrix::Matrix(int n,int m):lines{n},cols{m}{
33     p=new double[n*m];
34 }
35 Matrix::Matrix(const Matrix& X){
36     this->lines=X.get_lines();
37     this->cols=X.get_cols();
38     this->p=X.p;
39 }
40 void Matrix::set(const double*pvalue){
41     int k=0;
42     for(k=0;k<lines*cols;k++){
43     p[k]=pvalue[k];
44     }
45 }
46 void Matrix::set(int i,int j,int value){
47     int k=i*cols+j;
48     p[k]=value;
49 }
50 double &Matrix::at(int i,int j)const{
51         return p[i*cols+j];
52 }
53 void Matrix:: print()const{
54     for(int i=0;i<lines;++i){
55         for(int j=0;j<cols;++j){
56             if(j==cols-1){
57                 cout<<p[i*cols+j]<<endl;
58             }
59             else{
60                 cout<<p[i*cols+j]<<", ";
61             }
62         }
63     }
64 }
65 Matrix::~Matrix(){
66     delete [] p;
67 }

task6.cpp:

 1 #include <iostream>
 2 #include "matrix.hpp"
 3 void test(){
 4     using namespace std;
 5     double x[]={1,2,3,4,5,6};
 6     
 7     Matrix m1(3,2);
 8     m1.set(x);
 9     m1.print();
10     cout<<"the first line is: "<<endl;
11     cout<<m1.at(0,0)<<" "<<m1.at(0,1)<<endl;
12     cout<<endl;
13     
14     Matrix m2(2,3);
15     m2.set(x);
16     m2.print();
17     cout<<"the first line is: "<<endl;
18     cout<<m2.at(0,0)<<" "<<m2.at(0,1)<<" "<<m2.at(0,2)<<endl;
19     cout<<endl;
20     
21     Matrix m3(m2);
22     m3.set(0,0,999);
23     m3.print();
24     
25 }
26 int main(){
27     test();
28 }

 

标签:const,Matrix,int,lines,cols,实验,vectorInt
From: https://www.cnblogs.com/gzj035/p/16860757.html

相关文章

  • 实验3
    task1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_s......
  • 实验7:基于REST API的SDN北向应用实践
    (一)基本要求编写Python程序,调用OpenDaylight的北向接口实现以下功能(1)利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;.生成拓扑sudomn--topo=single,3--......
  • Java雷电游戏 毕业实训实验
    Java雷电游戏实训实验能满足学习和二次开发可以作为熟悉Java的学习,作为老师阶段性学习的一个成功检验不再是单调的理解老师空泛的知识,导入就能运行。满足下述条件:1.熟练掌......
  • 实验4 类与数组 指针
    实验五:vectorInt.hpp1#pragmaonce2#include<cassert>3usingnamespacestd;45classvectorInt{6public:7vectorInt(intn);8......
  • 实验三
    task1.c#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spa......
  • 实验四
    task5://point.hpp1#pragmaonce2classpoint{3public:4point(){}5point(intxx){x=xx;}6intreturn_x(){returnx;}7private:8i......
  • 【数据库系统概论】实验五 SQL数据库安全控制
    一、实验目的1.掌握SQLServer数据库用户基本操作2.掌握SQLServer数据库授权及回收权限的方法二、实验内容创建登录用户st1,st2使st1,st2成为stu_db的合法用户EXECsp_grant......
  • 搭建华为VRP实验平台WinPcap-Virtualbox-wireshark-eNSP
    WinPcap-Virtualbox-wireshark-eNSPWireshark的安装顺序可以放在eNSP前,也可以放在eNSP之后(如果安装的是最新版的eNSP,Wireshark的安装必须放在eNSP之前)注意事项:①装wireshar......
  • 实验3 函数应用编程
     实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprin......
  • 实验四 类与数组、指针
    实验任务五vectorInt.hpp1#include<iostream>2#include<cassert>3usingnamespacestd;45classvectorInt{67public:8vectorInt(int......