用编程语言C++写一个程序,代码需要能在Qt中运行,而且能够实现不同进制之间的相互转换(二进制、八进制、十进制、十六进制,能够保存历史记录(如十进制a转换为二进制b后,能够通过界面功能调取出a和b),在一次转换结束后可以直接进行下一次转换,也可以重新输入一个数进行新的转换(如 十进制的a转换为二进制的b后,可以立刻用b转换成其他进制或者重新输入一个数d转 换为其他进制),记得建立信号与槽
时间: 2023-12-10 22:42:13 浏览: 65
进制间的转换C++,包括二进制,八进制,十进制,十六进制
以下是一个简单的实现,可以在Qt中运行,实现不同进制之间的相互转换,并且能够保存历史记录,支持多次转换和重新输入:
```cpp
#include <QApplication>
#include <QMainWindow>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include <QDebug>
class Converter : public QObject {
Q_OBJECT
public:
Converter() {
// 初始化界面元素
window = new QMainWindow();
window->setWindowTitle("进制转换器");
inputLineEdit = new QLineEdit();
outputLineEdit = new QLineEdit();
outputLineEdit->setReadOnly(true);
binaryButton = new QPushButton("二进制");
octalButton = new QPushButton("八进制");
decimalButton = new QPushButton("十进制");
hexadecimalButton = new QPushButton("十六进制");
convertButton = new QPushButton("转换");
clearButton = new QPushButton("清空");
saveButton = new QPushButton("保存记录");
loadButton = new QPushButton("加载记录");
QHBoxLayout* inputLayout = new QHBoxLayout();
inputLayout->addWidget(new QLabel("输入:"));
inputLayout->addWidget(inputLineEdit);
QHBoxLayout* outputLayout = new QHBoxLayout();
outputLayout->addWidget(new QLabel("输出:"));
outputLayout->addWidget(outputLineEdit);
QVBoxLayout* buttonLayout = new QVBoxLayout();
buttonLayout->addWidget(binaryButton);
buttonLayout->addWidget(octalButton);
buttonLayout->addWidget(decimalButton);
buttonLayout->addWidget(hexadecimalButton);
buttonLayout->addStretch();
buttonLayout->addWidget(convertButton);
buttonLayout->addWidget(clearButton);
buttonLayout->addWidget(saveButton);
buttonLayout->addWidget(loadButton);
QVBoxLayout* mainLayout = new QVBoxLayout();
mainLayout->addLayout(inputLayout);
mainLayout->addLayout(outputLayout);
mainLayout->addLayout(buttonLayout);
QWidget* centralWidget = new QWidget();
centralWidget->setLayout(mainLayout);
window->setCentralWidget(centralWidget);
// 连接信号与槽
connect(binaryButton, &QPushButton::clicked, this, &Converter::binaryButtonClicked);
connect(octalButton, &QPushButton::clicked, this, &Converter::octalButtonClicked);
connect(decimalButton, &QPushButton::clicked, this, &Converter::decimalButtonClicked);
connect(hexadecimalButton, &QPushButton::clicked, this, &Converter::hexadecimalButtonClicked);
connect(convertButton, &QPushButton::clicked, this, &Converter::convertButtonClicked);
connect(clearButton, &QPushButton::clicked, this, &Converter::clearButtonClicked);
connect(saveButton, &QPushButton::clicked, this, &Converter::saveButtonClicked);
connect(loadButton, &QPushButton::clicked, this, &Converter::loadButtonClicked);
}
void show() {
window->show();
}
private:
QMainWindow* window;
QLineEdit* inputLineEdit;
QLineEdit* outputLineEdit;
QPushButton* binaryButton;
QPushButton* octalButton;
QPushButton* decimalButton;
QPushButton* hexadecimalButton;
QPushButton* convertButton;
QPushButton* clearButton;
QPushButton* saveButton;
QPushButton* loadButton;
QStringList history;
enum class Base {
Binary,
Octal,
Decimal,
Hexadecimal
};
void convert(Base from, Base to) {
QString input = inputLineEdit->text();
// 判断输入是否合法
bool ok;
int inputInt;
switch (from) {
case Base::Binary:
inputInt = input.toInt(&ok, 2);
break;
case Base::Octal:
inputInt = input.toInt(&ok, 8);
break;
case Base::Decimal:
inputInt = input.toInt(&ok, 10);
break;
case Base::Hexadecimal:
inputInt = input.toInt(&ok, 16);
break;
}
if (!ok) {
QMessageBox::warning(window, "错误", "输入不合法!");
return;
}
// 进行转换
QString output;
switch (to) {
case Base::Binary:
output = QString::number(inputInt, 2);
break;
case Base::Octal:
output = QString::number(inputInt, 8);
break;
case Base::Decimal:
output = QString::number(inputInt, 10);
break;
case Base::Hexadecimal:
output = QString::number(inputInt, 16).toUpper();
break;
}
// 更新输出框
outputLineEdit->setText(output);
outputLineEdit->selectAll();
// 添加历史记录
QString item = QString("%1 %2 %3").arg(input).arg(getBaseName(from)).arg(getBaseName(to));
history.append(item);
}
QString getBaseName(Base base) {
switch (base) {
case Base::Binary:
return "二进制";
case Base::Octal:
return "八进制";
case Base::Decimal:
return "十进制";
case Base::Hexadecimal:
return "十六进制";
}
return "";
}
private slots:
void binaryButtonClicked() {
convert(Base::Decimal, Base::Binary);
}
void octalButtonClicked() {
convert(Base::Decimal, Base::Octal);
}
void decimalButtonClicked() {
convert(Base::Hexadecimal, Base::Decimal);
}
void hexadecimalButtonClicked() {
convert(Base::Decimal, Base::Hexadecimal);
}
void convertButtonClicked() {
bool ok;
int inputInt = inputLineEdit->text().toInt(&ok);
if (!ok) {
QMessageBox::warning(window, "错误", "输入不合法!");
return;
}
// 判断原进制
Base from;
if (inputLineEdit->text().startsWith("0b")) {
from = Base::Binary;
} else if (inputLineEdit->text().startsWith("0")) {
from = Base::Octal;
} else if (inputLineEdit->text().startsWith("0x")) {
from = Base::Hexadecimal;
} else {
from = Base::Decimal;
}
// 判断目标进制
Base to;
if (binaryButton->isChecked()) {
to = Base::Binary;
} else if (octalButton->isChecked()) {
to = Base::Octal;
} else if (decimalButton->isChecked()) {
to = Base::Decimal;
} else if (hexadecimalButton->isChecked()) {
to = Base::Hexadecimal;
} else {
QMessageBox::warning(window, "错误", "请选择目标进制!");
return;
}
convert(from, to);
}
void clearButtonClicked() {
inputLineEdit->clear();
outputLineEdit->clear();
}
void saveButtonClicked() {
QString fileName = QFileDialog::getSaveFileName(window, "保存记录", "", "Text files (*.txt)");
if (fileName.isEmpty()) {
return;
}
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::warning(window, "错误", QString("无法保存文件 %1!").arg(fileName));
return;
}
QTextStream out(&file);
for (const QString& item : history) {
out << item << "\n";
}
QMessageBox::information(window, "保存记录", QString("记录已保存到文件 %1!").arg(fileName));
}
void loadButtonClicked() {
QString fileName = QFileDialog::getOpenFileName(window, "加载记录", "", "Text files (*.txt)");
if (fileName.isEmpty()) {
return;
}
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::warning(window, "错误", QString("无法加载文件 %1!").arg(fileName));
return;
}
QStringList newHistory;
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine();
newHistory.append(line);
}
history = newHistory;
QMessageBox::information(window, "加载记录", QString("已从文件 %1 加载 %2 条记录!").arg(fileName).arg(history.size()));
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Converter converter;
converter.show();
return app.exec();
}
#include "main.moc"
```
编译运行后,会弹出一个界面,可以输入数字进行转换,也可以选择不同的进制进行转换,还可以保存和加载历史记录:
注意,代码中使用了Qt的信号与槽机制,需要添加 `#include "main.moc"` 才能正确编译。
阅读全文