如果要形成一个可视化的柱和盘呢?

时间: 2024-03-14 15:45:23 浏览: 14
如果要形成可视化的柱和盘,可以使用图形界面库来实现,例如Windows下的Win32 API,或者跨平台的Qt等。这里给出一个使用Qt实现的汉诺塔可视化程序的示例代码: ```c++ #include <QApplication> #include <QWidget> #include <QHBoxLayout> #include <QVBoxLayout> #include <QPushButton> #include <QLabel> #include <QLineEdit> #include <QComboBox> #include <vector> using namespace std; class Disk : public QLabel { public: Disk(int size, QWidget *parent = nullptr) : QLabel(parent) { setText(QString(size, '#')); setAlignment(Qt::AlignCenter); setStyleSheet("background-color: #FFC0CB; border-radius: 5px;"); } }; class Tower : public QWidget { public: Tower(QWidget *parent = nullptr) : QWidget(parent) { QVBoxLayout *layout = new QVBoxLayout(this); layout->setMargin(0); layout->addStretch(); } void addDisk(int size) { Disk *disk = new Disk(size, this); QVBoxLayout *layout = static_cast<QVBoxLayout *>(this->layout()); layout->insertWidget(0, disk); disks.push_back(disk); } void removeDisk() { QVBoxLayout *layout = static_cast<QVBoxLayout *>(this->layout()); delete layout->itemAt(0)->widget(); disks.pop_back(); } int topSize() const { if (disks.empty()) { return -1; } return static_cast<int>(disks.back()->text().size()); } private: vector<Disk *> disks; }; class MainWindow : public QWidget { public: MainWindow(QWidget *parent = nullptr) : QWidget(parent) { setWindowTitle("汉诺塔"); setFixedSize(800, 600); QHBoxLayout *layout = new QHBoxLayout(this); layout->setMargin(30); for (int i = 0; i < 3; i++) { Tower *tower = new Tower(this); layout->addWidget(tower); towers.push_back(tower); } QVBoxLayout *buttonLayout = new QVBoxLayout(); buttonLayout->setAlignment(Qt::AlignTop); QLabel *label1 = new QLabel("盘子数量:"); buttonLayout->addWidget(label1); QLineEdit *lineEdit = new QLineEdit(); buttonLayout->addWidget(lineEdit); QLabel *label2 = new QLabel("移动方式:"); buttonLayout->addWidget(label2); QComboBox *comboBox = new QComboBox(); comboBox->addItem("A -> B"); comboBox->addItem("A -> C"); comboBox->addItem("B -> A"); comboBox->addItem("B -> C"); comboBox->addItem("C -> A"); comboBox->addItem("C -> B"); buttonLayout->addWidget(comboBox); QPushButton *button = new QPushButton("移动"); connect(button, &QPushButton::clicked, this, [=](){ int from = comboBox->currentIndex() / 2; int to = comboBox->currentIndex() % 2; moveDisk(from, to); }); buttonLayout->addWidget(button); QLabel *label3 = new QLabel("操作说明:"); buttonLayout->addWidget(label3); QLabel *label4 = new QLabel("1. 输入盘子数量后按回车;"); buttonLayout->addWidget(label4); QLabel *label5 = new QLabel("2. 选择移动方式后点击移动按钮;"); buttonLayout->addWidget(label5); QLabel *label6 = new QLabel("3. 操作错误时会有提示信息。"); buttonLayout->addWidget(label6); layout->addLayout(buttonLayout); } void moveDisk(int from, int to) { if (from == to) { return; } Tower *pFrom = towers[from]; Tower *pTo = towers[to]; if (pFrom->topSize() == -1) { QMessageBox::warning(this, "错误", "移动的起始塔没有盘子!", QMessageBox::Ok); return; } if (pTo->topSize() != -1 && pFrom->topSize() > pTo->topSize()) { QMessageBox::warning(this, "错误", "不能把大盘子放在小盘子上面!", QMessageBox::Ok); return; } pTo->addDisk(pFrom->topSize()); pFrom->removeDisk(); if (towers[2]->topSize() == n) { QMessageBox::information(this, "提示", "恭喜你完成了汉诺塔的移动!", QMessageBox::Ok); } } private: vector<Tower *> towers; int n; }; int main(int argc, char *argv[]) { QApplication app(argc, argv); MainWindow window; window.show(); return app.exec(); } ``` 这个程序使用Qt的图形界面库,实现了汉诺塔的可视化效果,用户可以选择盘子数量和移动方式,并通过点击移动按钮来移动盘子,程序会判断移动是否合法,如果不合法则会提示错误信息。

相关推荐

最新推荐

recommend-type

用Pandas和Folium做一个新冠数据可视化

目录数据集介绍可视化步骤1. 导入pandas等需要的库2. 读取数据,查看基本情况3. 读取城市坐标数据4. 按省份统计人数5. 合并城市坐标数据 与 省份数据6. 绘制地图写入HTML文件,并用浏览器打开完整代码 数据集介绍 1...
recommend-type

CODESYS之可视化控件属性(一).docx

前面已经讲解了控件的移动以及控件颜色的...X表示距离可视化界面左侧的距离,Y表示距离可视化界面上侧距离 Width表示矩形框宽度,Height表示矩形框高度,Angle表示矩形框的旋转角度 Center中心指的是矩形框黑点的中心
recommend-type

Python的地形三维可视化Matplotlib和gdal使用实例

主要介绍了Python的地形三维可视化Matplotlib和gdal使用实例,具有一定借鉴价值,需要的朋友可以了解下。
recommend-type

数据可视化课程练习题.docx

几百道数据可视化课程的习题, 部分试题: 什么是平稳时间序列?...对于一个时间序列来说,如果它的均值没有系统的变化(无趋势),方差没有系统变化,并且严格消除 了周期性的变化,就称为是平稳的。
recommend-type

使用pytorch实现可视化中间层的结果

今天小编就为大家分享一篇使用pytorch实现可视化中间层的结果,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SQL怎么实现 数据透视表

SQL可以通过使用聚合函数和GROUP BY子句来实现数据透视表。 例如,假设有一个销售记录表,其中包含产品名称、销售日期、销售数量和销售额等信息。要创建一个按照产品名称、销售日期和销售额进行汇总的数据透视表,可以使用以下SQL语句: ``` SELECT ProductName, SaleDate, SUM(SaleQuantity) AS TotalQuantity, SUM(SaleAmount) AS TotalAmount FROM Sales GROUP BY ProductName, SaleDate; ``` 该语句将Sales表按照ProductName和SaleDat
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。