10.21日报

上午进行了软件设计的实验课,以下为实验内容:

实验6:原型模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解原型模式的动机,掌握该模式的结构;

2、能够利用原型模式解决实际问题。

 
   

 

 

[实验任务一]:向量的原型

用C++完成数学中向量的封装,其中,用指针和动态申请支持向量长度的改变,使用浅克隆和深克隆复制向量类,比较这两种克隆方式的异同。

实验要求:

1.  画出对应的类图;

 

2.提交源代码(用C++完成);

#include <iostream>

#include <cstring>

 

class Vector {

private:

    double* data; // 动态数组

    int size;    // 当前大小

    int capacity; // 容量

 

public:

    // 构造函数

    Vector() : data(nullptr), size(0), capacity(0) {}

 

    // 带初始大小的构造函数

    Vector(int initialSize) {

        size = initialSize;

        capacity = initialSize;

        data = new double[capacity];

    }

 

    // 析构函数

    ~Vector() {

        delete[] data;

    }

 

    // 改变大小

    void resize(int newSize) {

        if (newSize > capacity) {

            double* newData = new double[newSize];

            std::memcpy(newData, data, size * sizeof(double));

            delete[] data;

            data = newData;

            capacity = newSize;

        }

        size = newSize;

    }

 

    // 深克隆

    Vector deepClone() const {

        Vector clonedVector(size);

        std::memcpy(clonedVector.data, data, size * sizeof(double));

        return clonedVector;

    }

 

    // 浅克隆

    Vector(const Vector& other) : data(nullptr), size(other.size), capacity(other.capacity) {

        if (other.data) {

            data = new double[capacity];

            std::memcpy(data, other.data, size * sizeof(double));

        }

    }

 

    // 赋值运算符

    Vector& operator=(const Vector& other) {

        if (this != &other) {

            if (capacity < other.size) {

                delete[] data;

                capacity = other.capacity;

                data = new double[capacity];

            }

            size = other.size;

            std::memcpy(data, other.data, size * sizeof(double));

        }

        return *this;

    }

 

    // 下标运算符

    double& operator[](int index) {

        return data[index];

    }

 

    // 常量下标运算符

    const double& operator[](int index) const {

        return data[index];

    }

};

 

int main() {

    Vector vec1(5);

    for (int i = 0; i < 5; ++i) {

        vec1[i] = i;

    }

 

    // 浅克隆

    Vector vec2 = vec1;

    std::cout << "Shallow Clone: ";

    for (int i = 0; i < 5; ++i) {

        std::cout << vec2[i] << " ";

    }

    std::cout << std::endl;

 

    // 深克隆

    Vector vec3 = vec1.deepClone();

    std::cout << "Deep Clone: ";

    for (int i = 0; i < 5; ++i) {

        std::cout << vec3[i] << " ";

    }

    std::cout << std::endl;

 

    return 0;

}#include <iostream>

#include <cstring>

 

class Vector {

private:

    double* data; // 动态数组

    int size;    // 当前大小

    int capacity; // 容量

 

public:

    // 构造函数

    Vector() : data(nullptr), size(0), capacity(0) {}

 

    // 带初始大小的构造函数

    Vector(int initialSize) {

        size = initialSize;

        capacity = initialSize;

        data = new double[capacity];

    }

 

    // 析构函数

    ~Vector() {

        delete[] data;

    }

 

    // 改变大小

    void resize(int newSize) {

        if (newSize > capacity) {

            double* newData = new double[newSize];

            std::memcpy(newData, data, size * sizeof(double));

            delete[] data;

            data = newData;

            capacity = newSize;

        }

        size = newSize;

    }

 

    // 深克隆

    Vector deepClone() const {

        Vector clonedVector(size);

        std::memcpy(clonedVector.data, data, size * sizeof(double));

        return clonedVector;

    }

 

    // 浅克隆

    Vector(const Vector& other) : data(nullptr), size(other.size), capacity(other.capacity) {

        if (other.data) {

            data = new double[capacity];

            std::memcpy(data, other.data, size * sizeof(double));

        }

    }

 

    // 赋值运算符

    Vector& operator=(const Vector& other) {

        if (this != &other) {

            if (capacity < other.size) {

                delete[] data;

                capacity = other.capacity;

                data = new double[capacity];

            }

            size = other.size;

            std::memcpy(data, other.data, size * sizeof(double));

        }

        return *this;

    }

 

    // 下标运算符

    double& operator[](int index) {

        return data[index];

    }

 

    // 常量下标运算符

    const double& operator[](int index) const {

        return data[index];

    }

};

 

int main() {

    Vector vec1(5);

    for (int i = 0; i < 5; ++i) {

        vec1[i] = i;

    }

 

    // 浅克隆

    Vector vec2 = vec1;

    std::cout << "Shallow Clone: ";

    for (int i = 0; i < 5; ++i) {

        std::cout << vec2[i] << " ";

    }

    std::cout << std::endl;

 

    // 深克隆

    Vector vec3 = vec1.deepClone();

    std::cout << "Deep Clone: ";

    for (int i = 0; i < 5; ++i) {

        std::cout << vec3[i] << " ";

    }

    std::cout << std::endl;

 

    return 0;

}

3.注意编程规范。

posted @ 2024-11-27 10:17  Code13  阅读(11)  评论(0)    收藏  举报