首页 > 其他分享 >实验4

实验4

时间:2022-11-05 21:12:49浏览次数:45  
标签:Matrix int cols 实验 x2 vectorInt include

//vectorInt.hpp
#include<iostream> using namespace std; class vectorInt{ private: int size; int *data; public: vectorInt(int n) { size=n; cout<<"constructor 1 called"<<endl; data=new int[n]; for(int i=0;i<size;i++) data[i]=0; } vectorInt(int n,int value) { size=n; cout<<"constructor 2 called"<<endl; data=new int[n]; for(int i=0;i<size;i++) data[i]=value; } ~vectorInt(){ cout<<"destructor called"<<endl; }; vectorInt(const vectorInt &p) { cout<<"copy constructor called"<<endl; size=p.size; data=new int[p.size]; for(int i=0;i<size;i++) data[i]=p.data[i]; } int &at(int i) { return data[i]; } int get_size() { return size; } friend void output(vectorInt &p); }; void output(vectorInt &p) { for(int i=0;i<p.size;i++) cout<<p.data[i]<<" "; cout<<endl; }

task.cpp:

#include <iostream>
#include "vectorInt.hpp"
  void test() {
using namespace std;
     int n;
     cin >> n;
     vectorInt x1(n);
     for(auto i = 0; i < n; ++i)
     x1.at(i) = i*i;
     output(x1);
     vectorInt x2(n, 42);
     vectorInt x3(x2);
     output(x2);
     output(x3);
     x2.at(0) = 77;
     output(x2);
     output(x3);
}
int main() {
     test();
}

运行结果:

 

 

 

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

task.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     Matrix m1(3, 2); 
 7     m1.set(x); 
 8     m1.print();
 9     cout << "the first line is: " << endl;
10     cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; 
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); 
19     m3.set(0, 0, 999); 
20     m3.print();
21 }
22 int main() {
23    test();
24 }

运行结果

 

 

   

标签:Matrix,int,cols,实验,x2,vectorInt,include
From: https://www.cnblogs.com/lc114514/p/16855507.html

相关文章

  • 实验二:逻辑回归算法实验
    【实验目的】1.理解逻辑回归算法原理,掌握逻辑回归算法框架;2.理解逻辑回归的sigmoid函数;3.理解逻辑回归的损失函数;4.针对特定应用场景及数据,能应用逻辑回归算法解决实际分......
  • 实验二:逻辑回归算法实验
    |20大数据三班|实验二:逻辑回归算法实验-作业-20级大数据3班机器学习-班级博客-博客园(cnblogs.com)||学号|201613328|实验二:逻辑回归算法实验-胡辛原-博客......
  • 实验四
    #include<iostream>usingnamespacestd;classvectorint{public:vectorint(intl=0){size=l;cout<<"constructor1called\n"<<endl;......
  • 实验6:开源控制器实践——RYU
    一、实验目的能够独立部署RYU控制器;能够理解RYU控制器实现软件定义的集线器原理;能够理解RYU控制器实现软件定义的交换机原理。二、实验环境Ubuntu20.04Desktopam......
  • 实验3 C语言控制语句应用编程
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces......
  • 实验3
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#define N 80void print_text(int line, int col, char text[]);void print_spaces(in......
  • 实验4 类与数组、指针
    实验任务5:vectorInt.hpp:#include<iostream>#include<cassert>usingnamespacestd;classvectorInt{public:vectorInt(intn);vectorI......
  • 实验二:逻辑回归算法实验
    1导包和读取数据#导包importnumpyasnpimportpandasaspdimportmatplotlib.pyplotasplt#读取数据data=pd.read_csv("D:/1/ex2data1.txt",delimiter=',',header=Non......
  • 实验4
    实验4.5:vectorInt.hpp1#include<iostream>2usingnamespacestd;3classvectorInt{4public:5vectorInt(intn);6vectorInt(intn,intv......
  • 实验3
    task1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_s......