任务4 源代码:

Vector_int.hpp

#pragma once
#include<iostream>
#include<cassert>

class Vector_int
{
public:
    Vector_int(int n);
    Vector_int(int n, int initial);
    Vector_int(const Vector_int& vector0);
    ~Vector_int();
    int& at(int pos);
private:
    int* vec;
    int length;
};

Vector_int::Vector_int(int n)
{
    std::cout << "constructor called" << std::endl;
    vec = new int[n];
}


Vector_int::Vector_int(int n, int initial):length(n)
{
    std::cout << "constructor called" << std::endl;
    vec = new int[n];
    for (int i = 0; i < n; i++)
        this->vec[i] = initial;
}

Vector_int::Vector_int(const Vector_int& vector0)
{
    std::cout << "copy constructor called" << std::endl;
    this->length = vector0.length;
    this->vec = new int[this->length];
    for (int i = 0; i < this->length; i++)
        this->vec[i] = vector0.vec[i];
}

Vector_int::~Vector_int()
{
    std::cout << "destructor called" << std::endl;
    delete[]vec;
    this->vec = nullptr;
    this->length = 0;
}

int& Vector_int::at(int pos)
{
    int& data = this->vec[pos];
    return data;
}

task4.cpp

#include"Vector_int.hpp"
#include<iostream>
#include<iomanip>

int main()
{
    int n = 5;

    Vector_int x1(n);

    for (int i = 0; i < n; i++)
    {
        std::cout.width(5);
        std::cout <<std:: setiosflags( std::ios::left);
        std::cout << x1.at(i) << " ";
        
    }
    std::cout << std::endl;

    Vector_int x2(n, 6);

    for (int i = 0; i < n; i++)
    {
        std::cout.width(5);
        std::cout << std::setiosflags(std::ios::left);
        std::cout << x2.at(i) << " ";
    }
    std::cout << std::endl;

    Vector_int y(x2);
    y.at(0) = 999;

    for (int i = 0; i < n; i++)
    {
        std::cout.width(5);
        std::cout << std::setiosflags(std::ios::left);
        std::cout << y.at(i) << " ";
    }
    std::cout << std::endl;

    return 0;
}

 

 任务5 源代码:

Matrix.hpp

#pragma once
#include<iostream>
#include<iomanip>

class Matrix
{
public:
    Matrix(int n);                     // 构造函数,构造一个n*n的矩阵
    Matrix(int n, int m);              // 构造函数,构造一个n*m的矩阵
    Matrix(const Matrix& X);           // 复制构造函数,使用已有的矩阵X构造
    ~Matrix();                         //析构函数
    void set(const double* pvalue);    // 用pvalue指向的连续内存块数据按行为矩阵赋值
    void set(int i, int j, int value); //设置矩阵第i行第j列元素值为value 
    double& at(int i, int j);          //返回矩阵第i行第j列元素的引用
    double at(int i, int j) const;     // 返回矩阵第i行第j列元素的值 
    int get_lines() const;             //返回矩阵行数
    int get_cols() const;              //返回矩列数
    void print() const;                 // 按行打印输出矩阵
private:
    int lines; // 矩阵行数 
    int cols; // 矩阵列数 
    double* p; // 指向存放矩阵数据的内存块的首地址
};

// 类Matrix的实现:待补足
// ×××
Matrix::Matrix(int n) :lines(n), cols(n)
{
    p = new double[this->lines * this->cols];
}

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

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

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

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

void Matrix::set(int i, int j, int value)
{
    p[i * this->cols + j] = value;
}

double& Matrix::at(int i, int j)
{
    return p[i * this->cols + j];
}

double Matrix::at(int i, int j) const
{
    return p[i * this->cols + j];
}

int Matrix::get_lines() const
{
    return this->lines;
}

int Matrix::get_cols() const
{
    return this->cols;
}

void Matrix::print() const
{
    using namespace std;
    for (int i = 0; i < this->lines; i++)
    {
        for (int j = 0; j < this->cols; j++)
        {
            cout.width(5);
            cout << setiosflags(ios::right) << this->p[i * this->cols + j] << ",";
        }
            
        std::cout <<"\b "<< std::endl;
    }
}

 

task5.cpp

#include <iostream>
#include "matrix.hpp"

int main() 
{
    using namespace std; 

    double x[] = { 1, 2, 3, 4, 5, 6 }; 

    Matrix m1(3, 2);// 创建一个3×2的矩阵
    m1.set(x);      // 用一维数组x的值按行为矩阵m1赋值
    m1.print();     // 打印矩阵m1的值
    cout << "the first line is: " << endl; 
    cout << m1.at(0, 0) << " " << m1.at(0, 1) << endl; // 输出矩阵m1第1行两个元素的值 
    cout << endl;

    Matrix m2(2, 3); 
    m2.set(x); 
    m2.print(); 
    cout << "the first line is: " << endl;
    cout << m2.at(0, 0) << " " << m2.at(0, 1) << " " << m2.at(0, 2) << endl; 
    cout << endl;

    Matrix m3(m2); // 用矩阵m2构造新的矩阵m3
    m3.set(0, 0, 999); // 将矩阵m3第0行第0列元素值设为999 
    m3.print();
}

 

 

 

 posted on 2021-11-07 09:07  Popcorn_ZSJ  阅读(13)  评论(3编辑  收藏  举报