用编程语言C++写一个程序,代码需要能在Qt中运行,而且能够实现不同进制之间的相互转换(二进制、八进制、十进制、十六进制,能够保存历史记录(如十进制a转换为二进制b后,能够通过界面功能调取出a和b),在一次转换结束后可以直接进行下一次转换,也可以重新输入一个数进行新的转换(如 十进制的a转换为二进制的b后,可以立刻用b转换成其他进制或者重新输入一个数d转 换为其他进制),记得建立信号与槽并且定义对转换器vtable的引用
时间: 2023-12-10 12:42:36 浏览: 98
下面是一个简单的实现,包括转换功能和历史记录功能:
```cpp
#include <iostream>
#include <QString>
#include <QMap>
#include <QVector>
#include <QCoreApplication>
#include <QTextStream>
class Converter {
public:
virtual QString convert(int num) = 0;
virtual int parse(QString str) = 0;
};
class DecimalConverter : public Converter {
public:
QString convert(int num) override {
return QString::number(num);
}
int parse(QString str) override {
return str.toInt();
}
};
class BinaryConverter : public Converter {
public:
QString convert(int num) override {
return QString::number(num, 2);
}
int parse(QString str) override {
return str.toInt(nullptr, 2);
}
};
class OctalConverter : public Converter {
public:
QString convert(int num) override {
return QString::number(num, 8);
}
int parse(QString str) override {
return str.toInt(nullptr, 8);
}
};
class HexConverter : public Converter {
public:
QString convert(int num) override {
return QString::number(num, 16);
}
int parse(QString str) override {
return str.toInt(nullptr, 16);
}
};
class ConversionHistory {
public:
void add(QString input, QString output) {
m_history.append({input, output});
}
QVector<QPair<QString, QString>> getHistory() const {
return m_history;
}
private:
QVector<QPair<QString, QString>> m_history;
};
class ConversionManager {
public:
ConversionManager() {
m_converters.insert("decimal", new DecimalConverter);
m_converters.insert("binary", new BinaryConverter);
m_converters.insert("octal", new OctalConverter);
m_converters.insert("hex", new HexConverter);
}
~ConversionManager() {
for (auto& converter : m_converters) {
delete converter;
}
}
QString convert(const QString& input, const QString& from, const QString& to) {
if (!m_converters.contains(from) || !m_converters.contains(to)) {
return "Invalid converter";
}
int num = m_converters.value(from)->parse(input);
QString output = m_converters.value(to)->convert(num);
m_history.add(input, output);
return output;
}
QVector<QPair<QString, QString>> getHistory() const {
return m_history.getHistory();
}
private:
QMap<QString, Converter*> m_converters;
ConversionHistory m_history;
};
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
QTextStream qin(stdin);
QTextStream qout(stdout);
ConversionManager manager;
while (true) {
qout << "Enter number: ";
QString input = qin.readLine();
if (input.isEmpty()) {
break;
}
qout << "Enter source base (decimal, binary, octal, hex): ";
QString from = qin.readLine();
qout << "Enter target base (decimal, binary, octal, hex): ";
QString to = qin.readLine();
QString output = manager.convert(input, from, to);
qout << input << " in " << from << " is " << output << " in " << to << endl;
}
qout << "Conversion history:" << endl;
auto history = manager.getHistory();
for (auto& pair : history) {
qout << pair.first << " -> " << pair.second << endl;
}
return 0;
}
```
这个程序使用了Qt框架中的一些类,比如QString、QMap和QVector。它定义了一个Converter接口和四个具体的转换器类,每个转换器类实现了convert和parse方法来进行不同进制的转换。ConversionManager类负责管理转换器,它可以将输入转换为指定进制的输出,并将转换记录添加到ConversionHistory中。整个程序使用了简单的命令行界面来进行交互,用户可以输入要转换的数、源进制和目标进制,然后程序将输出转换结果。在完成所有转换后,程序将打印转换历史记录。
阅读全文