北京邮电大学 面向对象程序设计与实践 C++基础题目

WATCH OUT

以第一个实验为例,$${PWD}/src 中的每个文件夹包含一个实验所需要用到的代码
需要进行测试时在main.cpp中调用各模块对应Test文件内的函数即可

下载地址

提取码:1919

项目结构

目录结构

CmakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(oop_course_lab)

set(CMAKE_CXX_STANDARD 20)

include_directories(include)

set(MATRIX_SOURCE
        src/matrix/Matrix.cpp)

set(SHAPE_SOURCE
        src/shape/Shape.cpp
        src/shape/Circle.cpp
        src/shape/Rectangle.cpp
        src/shape/Square.cpp)

set(POINT_CIRCLE_SOURCE
        src/point_circle/Point.cpp
        src/point_circle/Circle.cpp)

set(SOURCE src/main.cpp)

add_library(libshapes       STATIC ${SHAPE_SOURCE})
add_library(libpoint_circle STATIC ${POINT_CIRCLE_SOURCE})
add_library(libmatrix       STATIC ${MATRIX_SOURCE})

add_executable(lab ${SOURCE})
target_link_libraries(lab libpoint_circle libshapes libmatrix)

内容例:

实验一、C++基础知识实验(编写简单矩阵类)

$${PWD}/src/matrix/

Matrix.h
//
// Created by LCBHSStudent on 2020/7/23.
//

#ifndef OOP_COURSE_LAB_MATRIX_H
#define OOP_COURSE_LAB_MATRIX_H

#include <lcbhss.h>

// template <typename T>
class Matrix {
public:
    using dataType = double;
        Matrix() = delete;
        Matrix(size_t rowCount, size_t columnCount);
        Matrix(Matrix&&) noexcept;
        Matrix(const Matrix&) noexcept;

    virtual
        ~Matrix();

public FUNCTION:
    Matrix
        operator+ (const Matrix &matrix) const;
    Matrix
        operator- (const Matrix &matrix) const;
    Matrix&
        operator= (Matrix&& matrix);
    Matrix&
        operator= (const Matrix& matrix);

    friend std::istream&
        operator>> (std::istream& istream, Matrix& matrix);
    friend std::ostream&
        operator<< (std::ostream& ostream, Matrix& matrix);

private FUNCTION:
    static void checkFormat(const Matrix&, const Matrix&);

private RESOURCE:
    dataType**
        m_ppData{};
    size_t
        m_rowCount{};
    size_t
        m_columnCount{};
};
#endif //OOP_COURSE_LAB_MATRIX_H
Matrix.cpp
//
// Created by LCBHSStudent on 2020/7/23.
//

#include "Matrix.h"

Matrix::Matrix(size_t rowCount, size_t columnCount):
    m_rowCount(rowCount),
    m_columnCount(columnCount)
{
    m_ppData = new dataType* [rowCount];
    for(auto i = 0; i < rowCount; i++) {
        m_ppData[i] = new dataType [columnCount];
        memset(m_ppData[i], 0, sizeof(dataType)*columnCount);
    }
}


Matrix::~Matrix() {
    if(m_ppData != nullptr) {
        for(auto i = 0; i < m_rowCount; i++) {
            delete[] m_ppData[i];
        }
        delete[] m_ppData;
        std::cout << "matrix destroyed\n";
    }
}

Matrix Matrix::operator+ (const Matrix &matrix) const {
    checkFormat(*this, matrix);
    Matrix result(this->m_rowCount, this->m_columnCount);

    for(auto i = 0; i < m_rowCount; i++) {
        for(auto j = 0; j < m_columnCount; j++) {
            result.m_ppData[i][j] =
                this->m_ppData[i][j] +
                matrix.m_ppData[i][j];
        }
    }

    return result;
}

Matrix Matrix::operator-(const Matrix &matrix) const {
    checkFormat(*this, matrix);
    Matrix result(this->m_rowCount, this->m_columnCount);

    for(auto i = 0; i < m_rowCount; i++) {
        for(auto j = 0; j < m_columnCount; j++) {
            result.m_ppData[i][j] =
                this->m_ppData[i][j] -
                matrix.m_ppData[i][j];
        }
    }

    return result;
}

Matrix::Matrix(Matrix&& matrix) noexcept:
    m_ppData(matrix.m_ppData),
    m_rowCount(matrix.m_rowCount),
    m_columnCount(matrix.m_columnCount)
{
    matrix.m_ppData = nullptr;
}

Matrix::Matrix(const Matrix& matrix) noexcept:
    m_rowCount(matrix.m_rowCount),
    m_columnCount(matrix.m_columnCount)
{
    m_ppData = new dataType* [m_rowCount];
    for(auto i = 0; i < m_rowCount; i++) {
        m_ppData[i] = new dataType [m_columnCount];
    }
    for(auto i = 0; i < m_rowCount; i++) {
        memcpy(
            m_ppData[i],
            matrix.m_ppData[i],
            sizeof(dataType) * m_columnCount
        );
    }
}


Matrix& Matrix::operator= (Matrix &&matrix) {
    checkFormat(*this, matrix);
	
	if (&matrix == this)
		return *this;
	
    this->m_ppData  = matrix.m_ppData;
    matrix.m_ppData = nullptr;
    return *this;
}

Matrix& Matrix::operator= (const Matrix& matrix) {
    checkFormat(*this, matrix);

	if (&matrix == this)
		return *this;

    for(auto i = 0; i < m_rowCount; i++) {
        memcpy(
            m_ppData[i],
            matrix.m_ppData[i],
            sizeof(dataType) * m_columnCount
        );
    }
    return *this;
}

std::istream& operator>>(
        std::istream    &istream,
        Matrix          &matrix
) {
    for(auto i = 0; i < matrix.m_rowCount; i++) {
        for(auto j = 0; j < matrix.m_columnCount; j++) {
            istream >> matrix.m_ppData[i][j];
        }
    }
    return istream;
}

std::ostream &operator<<(std::ostream &ostream, Matrix &matrix) {
    ostream << "[matrix_data] pointer is: "
            << std::hex << matrix.m_ppData << '\n';

    auto fill       = ostream.fill();
    auto width      = ostream.width();
    auto precision  = ostream.precision();

    for(auto i = 0; i < matrix.m_rowCount; i++) {
        for(auto j = 0; j < matrix.m_columnCount; j++) {
            ostream.fill(' ');
            ostream.width(10);
            ostream.precision(3);
            ostream << matrix.m_ppData[i][j];
        }
        ostream << std::endl;
    }
    ostream << std::endl;

    ostream.fill(fill);
    ostream.width(width);
    ostream.precision(precision);

    return ostream;
}

void Matrix::checkFormat(const Matrix &A, const Matrix& B) {
    if (A.m_rowCount != B.m_rowCount ||
        A.m_columnCount != B.m_columnCount
    ) {
        throw std::runtime_error(
                "Please check whether the two matrix forms are the same");
    }
}
TestMatrix.hpp
//
// Created by LCBHSStudent on 2020/7/23.
//

#ifndef OOP_COURSE_LAB_TESTMATRIX_H
#define OOP_COURSE_LAB_TESTMATRIX_H

#include "Matrix.h"

void TestMatrix() {
    static constexpr int ROW_COUNT = 5;
    static constexpr int COLUMN_COUNT = 4;

    Matrix M1(ROW_COUNT, COLUMN_COUNT);
    std::cout << M1;
    Matrix M2(ROW_COUNT, COLUMN_COUNT);
    std::cout << M2;

    std::cin >> M1;
    std::cout << M1;

    std::cin >> M2;
    std::cout << M2;

    Matrix M3 = M1 + M2;
    std::cout << M3;

    Matrix M4 = M1 - M2;
    std::cout << M4;

    Matrix M5 = std::move(M4);
    Matrix M6 = M5;
}

void TestMatrix2() {
    // 不会浪费临时对象,且对一般引用使用深拷贝
    auto M1 =  new Matrix(4, 4);
    auto M2 =  new Matrix(4, 4);
    auto M3 =  new Matrix(4, 4);

    std::cin >> *M1 >> *M2;
    std::cout << *M1 << *M2;

    *M3 = *M1 + *M2;
    std::cout << *M3;

    delete M1;
    delete M2;
    delete M3;
}

#endif //OOP_COURSE_LAB_TESTMATRIX_H
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值