ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

实验三

2021-11-03 18:02:37  阅读:123  来源: 互联网

标签:const Matrix int lines cols Vector 实验


实验结论

实验五:实现Vector<int>

vector_int.hpp

 

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

 

main.cpp

 

#include "vector_int.hpp"

int main() {
    using namespace std;
    Vector_int array_1(5);
    Vector_int array_2(array_1);

    array_1.at(1) = 100;
    cout << "array1: ";
    array_1.show();
    cout << "array2: ";
    array_2.show();

    Vector_int array_3(3, 9);
    cout << "array3: ";
    array_3.show();
}
View Code

 

运行效果图

 

 

 

实验五:实现动态矩阵

 

Matrix.hpp

 

#pragma once
#include<iostream>

class Matrix {
private:
    int lines;
    int cols;
    double* p;

public:
    Matrix(int n);
    Matrix(int n, int m);
    Matrix(const Matrix& x);
    ~Matrix();
    void set(const double* pvalue);
    void set(int i, int j, int value);
    double& at(int i, int j);
    double at(int i, int j) const;
    int get_lines() const;
    int get_cols() const;
    void print() const;

};

Matrix::Matrix(int n) : Matrix(n, n) {

}

Matrix::Matrix(int n, int m) {
    p = new double[n * m];
    lines = n;
    cols = m;
}

Matrix::Matrix(const Matrix& x) {
    lines = x.lines;
    cols = x.cols;
    p = new double[lines * cols];
    for (int i = 0; i < cols * lines; i++) {
        p[i] = x.p[i];
    }
}

Matrix::~Matrix() {
    delete[] p;
}

void Matrix::set(const double* pvalue) {
    for (int i = 0; i < lines * cols; i++) {
        p[i] = pvalue[i];
    }
}
void Matrix::set(int i, int j, int value)
{
    p[i * cols + j] = value;
}

double& Matrix::at(int i, int j)
{
    return p[i * cols + j];
}
double Matrix::at(int i, int j) const {
    return p[i * cols + j];
}
int  Matrix::get_lines() const {
    return lines;
}
int  Matrix::get_cols() const {
    return cols;
}
void  Matrix::print() const {
    for (int i = 0; i < lines; i++) {
        for (int j = 0; j < cols; j++) {
            std::cout << p[i * cols + j] << ", ";
        }
        std::cout << "\b\b \n";
    }
}
View Code

 

main.cpp

 

 1 #include "matrix.hpp"
 2 int main()
 3 {
 4     using namespace std;
 5     double x[] = {6, 5, 4, 3, 2, 1};
 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(1, 0) << " " << m1.at(1, 1) << endl; // 输出矩阵m1第2行两个元素的值
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(1, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl;
17     cout << endl;
18     Matrix m3(m2); // 用矩阵m2构造新的矩阵m3
19     m3.set(0, 0, 999); // 将矩阵m3第0行第0列元素值设为999
20     m3.print();
21 }
View Code

 

运行效果图:

 

 

实验总结

实验三心得:

1.在拼接color字符串时,“color " 一定要留有空格,这是为了拼接字符串,并使用c_str做准备,

c_str() 函数调用时有他自己的format

 

2. ball.hpp函数中的up等函数有点冗余, 可以使用三元运算符来精炼代码,提高可读性

eg. y = (y - left < 0) ? 0 : y - left;

 

实验四心得:

总的来说没有什么难度吧

1. 可以利用assert和之前学习的show()的书写方法来提高程序的质感

assert(): 断言,如果不满足括号中的条件,就抛出异常

2. at() 的返回值应该是引用类型,否则不能达到修改数组中对应元素的效果

 

实验五心得:

1.Matrix::Matrix(int n)可以委托Matrix(n, n)来实现,减少代码的重复书写

2.总的来说安全性比较差,并没有对pvalue的长度进行限制

  因此可能会发生下标越界的情况,使用时应该小心

3. 总的来说就是二维数组的一维存放,难度不大

 

标签:const,Matrix,int,lines,cols,Vector,实验
来源: https://www.cnblogs.com/sobfu/p/15504758.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有