如果要形成一个可视化的柱和盘呢?
时间: 2024-03-14 17:45:23 浏览: 130
实现柱面全景图象
3星 · 编辑精心推荐
如果要形成可视化的柱和盘,可以使用图形界面库来实现,例如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的图形界面库,实现了汉诺塔的可视化效果,用户可以选择盘子数量和移动方式,并通过点击移动按钮来移动盘子,程序会判断移动是否合法,如果不合法则会提示错误信息。
阅读全文