首页 > 其他分享 >第四次实验

第四次实验

时间:2022-11-07 21:37:43浏览次数:51  
标签:const Matrix int double 矩阵 实验 vectorInt 第四次

 1 #pragma once
 2 #include<iostream>
 3 using namespace std;
 4 class vectorInt
 5 {
 6 public:
 7     vectorInt(int n);
 8     vectorInt(int n, int num);
 9     vectorInt(vectorInt& other);
10     int get_size();
11     int &at(int i) const;
12     ~vectorInt();
13     void friend  output(vectorInt& s);
14 private:
15     int size;
16     int* p;
17 };
18 
19 static int x = 0;
20 vectorInt::vectorInt(int n)
21 {
22     ++x;
23     size = n;
24     p = new int[size];
25     cout << "constructor " << x << " called" << endl;
26 }
27 vectorInt::vectorInt(int n, int num)
28 {
29     ++x;
30     size = n;
31     p = new int[size];
32     for (int i = 0;i < size;i++)
33     {
34         p[i] = num;
35     }
36     cout << "constructor " << x << " called" << endl;
37 
38 }
39 vectorInt::vectorInt(vectorInt& other)
40 {
41     size = other.size;
42     p = new int[size];
43     for (int i = 0;i < size;i++)
44     {
45         p[i] = other.p[i];
46     }
47     cout << "copy constructor called." << endl;
48 }
49 int &vectorInt::at(int i) const
50 {
51     return p[i];
52     
53 }
54 int vectorInt::get_size()
55 {
56     return size;
57 
58 }
59 vectorInt::~vectorInt()
60 {
61     cout << "destructor called." << endl;
62     delete[] p;
63 }
64 void output(vectorInt& s)
65 {
66     for (int i = 0;i < s.size;i++)
67     {
68         cout << s.p[i] << " ";
69     }
70     cout << endl;
71 }

hpp

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

cpp  

 

 

 

实验6

  1 #pragma once
  2 
  3 #include <iostream>
  4 
  5 
  6 using std::cout;
  7 using std::endl;
  8 
  9 class Matrix {
 10 public:
 11     Matrix(int n);                     // 构造函数,构造一个n*n的矩阵
 12     Matrix(int n, int m);              // 构造函数,构造一个n*m的矩阵
 13     Matrix(const Matrix& X);           // 复制构造函数,使用已有的矩阵X构造
 14     ~Matrix();                         //析构函数
 15 
 16     void set(const double* pvalue);     // 用pvalue指向的连续内存块数据按行为矩阵赋值
 17     void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value
 18     double& at(int i, int j);          //返回矩阵第i行第j列元素的引用
 19     double at(int i, int j) const;     // 返回矩阵第i行第j列元素的值
 20     int get_lines() const;             //返回矩阵行数
 21     int get_cols() const;              //返回矩列数
 22     void print() const;                // 按行打印输出矩阵
 23 
 24 private:
 25     int lines; // 矩阵行数
 26     int cols;  // 矩阵列数
 27     double* p; // 指向存放矩阵数据的内存块的首地址
 28 };
 29 
 30 // 类Matrix的实现:待补足
 31 // ×××
 32 Matrix::Matrix(int n)
 33 {
 34     p = new double[n * n];
 35     lines = n;
 36     cols = n;
 37 }
 38 Matrix::Matrix(int n, int m)
 39 {
 40     p = new double[n * m];
 41     lines = n;
 42     cols = m;
 43 }
 44 Matrix::Matrix(const Matrix& X)
 45 {
 46     lines = X.lines;
 47     cols = X.cols;
 48     p = new double[lines * cols];
 49     for (int i = 0;i < lines*cols;i++)
 50     {
 51         p[i] = X.p[i];
 52     }
 53 
 54 }
 55 Matrix::~Matrix()
 56 
 57 {
 58     delete[] p;
 59 }
 60 void Matrix::set(const double* pvalue)
 61 {
 62     int i = 0;
 63     while (i<cols*lines)
 64     {
 65         p[i++] = *(pvalue++);
 66     }
 67 }
 68 void Matrix::set(int i, int j, int value)
 69 {
 70     p[i  * cols + j ] = value;
 71 }
 72 double& Matrix::at(int i, int j)
 73 {
 74     return p[i * cols + j];
 75 }
 76 double Matrix::at(int i, int j) const
 77 {
 78     return p[i  * cols + j ];
 79 }
 80 int Matrix::get_lines() const
 81 {
 82     return lines;
 83 }
 84 int Matrix::get_cols() const
 85 {
 86     return cols;
 87 }//返回矩列数
 88 void Matrix::print() const
 89 {
 90     int k = 0;
 91     for (int i = 0;i < lines;i++)
 92     {
 93         cout << p[k++];
 94         for (int j = 1;j < cols;j++)
 95         {
 96             cout<<"," << p[k++];
 97         }
 98         cout << endl;
 99     }
100 }

hpp

 1 #include <iostream>
 2 #include "matrix.hpp"
 3 
 4 void test() {
 5     using namespace std;
 6 
 7     double x[] = { 1, 2, 3, 4, 5, 6 };
 8 
 9     Matrix m1(3, 2);    // 创建一个3×2的矩阵
10     m1.set(x);          // 用一维数组x的值按行为矩阵m1赋值
11     m1.print();         // 打印矩阵m1的值
12     cout << "the first line is: " << endl;
13     cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl;  // 输出矩阵m1第1行两个元素的值
14     cout << endl;
15 
16     Matrix m2(2, 3);
17     m2.set(x);
18     m2.print();
19     cout << "the first line is: " << endl;
20     cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
21     cout << endl;
22 
23     Matrix m3(m2);      // 用矩阵m2构造新的矩阵m3
24     m3.set(0, 0, 999);  // 将矩阵m3第0行第0列元素值设为999
25     m3.print();
26 }
27 
28 int main() {
29     test();
30 }

cpp

 

标签:const,Matrix,int,double,矩阵,实验,vectorInt,第四次
From: https://www.cnblogs.com/pdywsf/p/16867526.html

相关文章

  • 软件工程实验一
    (1)回顾你过去将近3年的学习经历当初你报考的时候,是真正喜欢计算机这个专业吗?你现在后悔选择了这个专业吗?你认为你现在最喜欢的领域是什么(可以是计算机的也可以是其它......
  • 软件工程实验一MXY
    (1)回顾你过去将近3年的学习经历当初你报考的时候,是真正喜欢计算机这个专业吗?    报考时不了解所学究竟是什么,迷茫焦虑没有方向。你现在后悔选择了这个专业吗?......
  • 实验4:开源控制器实践——OpenDaylight
    (一)基本要求1.利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器;2.通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s。......
  • 0001-华为ENSP WLAN配置实验-郑锦程
    0001-华为ENSPWLAN配置实验-郑锦程创建管理vlan和业务vlan​​[AC6605]vlanbatch100101​​​​Info:Thisoperationmaytakeafewseconds.Pleasewaitforamome......
  • 二叉树相关上机实验
    #include<stdio.h>#include<malloc.h>#defineOK1#defineERROR0#defineMAXNUM20typedefintStatus;typedefstructbnode{intdata;structbnode......
  • 软件工程基础Y-实验一王瑜
    (1)回顾你过去将近3年的学习经历当初你报考的时候,是真正喜欢软件工程这个专业吗?有一些喜欢至少比其它专业喜欢你现在后悔选择了这个专业吗?不后悔你认为你现在最喜欢......
  • 实验3 函数应用编程
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidpri......
  • 实验2:Open vSwitch虚拟交换机实践
    实验2:OpenvSwitch虚拟交换机实践一、实验目的1.能够对OpenvSwitch进行基本操作;2.能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;3.能够通过Mininet的Pyt......
  • 实验4 类与数组、指针
    task5.cpp#include<iostream>#include"vectorInt.hpp"voidtest(){usingnamespacestd;intn;cin>>n;vectorIntx1(n);for(autoi......
  • 实验二:逻辑回归算法实验
    实验二:逻辑回归算法实验【实验目的】1.理解逻辑回归算法原理,掌握逻辑回归算法框架;2.理解逻辑回归的sigmoid函数;3.理解逻辑回归的损失函数;4.针对特定应用场景及数据,能......