實驗結論
實驗五:實現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 }
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(); }
運行效果圖
實驗五:實現動態矩陣
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"; } }
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 }
運行效果圖:
實驗總結
實驗三心得:
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. 總的來說就是二維數組的一維存放,難度不大