# visualgo **Repository Path**: yueluozifeng/visualgo ## Basic Information - **Project Name**: visualgo - **Description**: 数据结构与算法可视化demo - **Primary Language**: C++ - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2022-03-05 - **Last Updated**: 2022-04-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: Cpp, EasyX ## README # visualgo #### 介绍 数据结构与算法可视化demo,前端使用easyx库显示,后端使用工厂 + 观察者模式进行具体实例的组装。 #### 软件架构 ```c++ class ProcessControl { // 程序运行管理 public: ProcessControl() : factory(nullptr), instance(nullptr) { } void init(); // 程序运行初始化 int chooseSecondaryInterface(); // 选择次级分支 void enterSecondaryInterface(int choose); // 进入次级分支 int chooseInstance(); // 选择示例项目 void enterInstance(int); // 进入实例项目s void back(); // 返回上级界面 void exit(); // 退出 ~ProcessControl() { if (factory != nullptr) { factory.reset(nullptr); } if (instance != nullptr) { instance.reset(nullptr); } } /* public: Factory* getFactory() { return factory; } void setFactory(Factory* _factory) { factory = _factory; } void setFactory(Factory&& _factory) { factory = &_factory; } AbstractClass* getInstance() { return instance; } void setInstance(AbstractClass* _instance) { instance = _instance; } void setInstance(AbstractClass&& _instance) { instance = &_instance; } */ private: HWND hWnd; std::unique_ptr factory; std::unique_ptr instance; }; class Shape { // 数据节点 public: Shape(int data) : m_data(data), m_position(), m_color(BLACK), m_ifFill(false), m_fillColor(BLACK) { } Shape(int data, int x, int y, COLORREF color, bool ifFill, COLORREF fillcolor) : m_data(data), m_position(x, y), m_color(color), m_ifFill(ifFill), m_fillColor(fillcolor) { } virtual void draw() = 0; virtual ~Shape() { } private: // 形状、位置 int m_data; // 数据 typedef struct location { int m_x, m_y; location() : m_x(0), m_y(0) { } location(int x, int y) : m_x(x), m_y(y) { } } Position; Position m_position; // 起始位置 COLORREF m_color; // 线条颜色 bool m_ifFill; // 是否填充 COLORREF m_fillColor; // 填充颜色 }; class AbstractClass { // 抽象基类 public: }; class AbstractSort : public AbstractClass{ // 抽象排序 public: AbstractSort(double speed) : shape(), _speed(speed), text(nullptr), continuousPlayback(false) { } virtual void sort() = 0; // 调用排序 - 主函数 virtual void description() = 0; // 显示文字描述 void generation(); // 随机生成数据 void generation(int n); void generation(int n, int left, int right); void clearData() { // 清除数据 for (auto& s : shape) { s.reset(nullptr); } shape.resize(0); } virtual ~AbstractSort() { } protected: virtual void swap2Shape(std::unique_ptr shape1, std::unique_ptr shape2); // 互换两个节点的位置 void pause(); // 暂停演示动画 protected: std::vector> shape; double _speed; // 动画速度 std::unique_ptr text; // 文字描述 bool continuousPlayback; // 是否连续播放演示 }; class AbstractDataStructure :public AbstractClass{ // 抽象数据结构 public: AbstractDataStructure() : shape(), text(nullptr) { } int size() { return shape.size(); } bool isempty() { return size() == 0; } virtual void push() = 0; virtual void pop() = 0; virtual int front() = 0; // 一些数据结构通用的函数 void clearData() { for (auto& s : shape) { s.reset(nullptr); } shape.resize(0); } virtual void draw() = 0; virtual void description() = 0; // 显示文字描述 virtual ~AbstractDataStructure() { } private: std::vector> shape; std::unique_ptr text; // 文字描述 }; class Factory { // 生产工厂 public: virtual std::unique_ptr createInstance() = 0; virtual ~Factory() { } }; ```