首页 > 其他分享 >实验四 类与数组、指针

实验四 类与数组、指针

时间:2022-11-06 15:24:55浏览次数:47  
标签:const Matrix int lines cols 实验 数组 vectorInt 指针

实验任务五代码截图:

   vectorInt.hpp:

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

   task5.cpp:

 1 #include<iostream>
 2 #include"vectorInt.hpp"
 3 
 4 void test()
 5 {
 6     using namespace std;
 7 
 8     int n;
 9     cin >> n;
10 
11     vectorInt x1(n);
12     for (auto i = 0; i < n; i++)
13         x1.at(i) = i * i;
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 }
23 int main()
24 {
25     test();
26 }

运行截图:

 

 实验任务六代码:

 Matrix.hpp:

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

 task6.cpp:

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

代码运行截图:

 

 

       

标签:const,Matrix,int,lines,cols,实验,数组,vectorInt,指针
From: https://www.cnblogs.com/jane-d/p/16862482.html

相关文章

  • 实验二:逻辑回归算法实验
    【实验目的】理解逻辑回归算法原理,掌握逻辑回归算法框架;理解逻辑回归的sigmoid函数;理解逻辑回归的损失函数;针对特定应用场景及数据,能应用逻辑回归算法解决实际分类问题。......
  • 实验三
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(int......
  • 实验3
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces......
  • 软件工程实验一
    (1) 回顾你过去将近3年的学习经历问:当初你报考的时候,是真正喜欢计算机这个专业吗?答:是。问:你现在后悔选择了这个专业吗?答:不后悔。问:你认为你现在最喜欢的领域是什么(可......
  • 实验二:逻辑回归算法实验
    【实验目的】理解逻辑回归算法原理,掌握逻辑回归算法框架;理解逻辑回归的sigmoid函数;理解逻辑回归的损失函数;针对特定应用场景及数据,能应用逻辑回归算法解决实际分类问题。......
  • 实验3
    实验任务1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数......
  • 实验三
    #define_CRT_SECURE_NO_WARNINGS1#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,......
  • 实验二:逻辑回归算法实验
    实验二:逻辑回归算法实验 【实验目的】1.理解逻辑回归算法原理,掌握逻辑回归算法框架;2.理解逻辑回归的sigmoid函数;3.理解逻辑回归的损失函数;4.针对特定应用场景及数据,......
  • 实验二:逻辑回归算法实验
    【实验目的】理解逻辑回归算法原理,掌握逻辑回归算法框架;理解逻辑回归的sigmoid函数;理解逻辑回归的损失函数;针对特定应用场景及数据,能应用逻辑回归算法解决实际分类问题。......
  • [哈希]leetcode349. 两个数组的交集
    题目给定两个数组 nums1 和 nums2,返回它们的交集 。输出结果中的每个元素一定是唯一的。我们可以不考虑输出结果的顺序。示例1:输入:nums1=[1,2,2,1],nums2......