gpt4 book ai didi

c++ - 如何获得C++或Qt中所有货币符号和货币缩写的数组?

转载 作者:行者123 更新时间:2023-12-02 10:19:25 24 4
gpt4 key购买 nike

我需要在程序中以数组的形式获取所有可能的货币符号和货币缩写。如何使用C++或Qt来实现?

最佳答案

解析现有最新货币列表

我找到了货币代码JSON列表https://gist.github.com/Fluidbyte/2973986
此列表会定期更新。

如何解析列表。从链接下载原始JSON文本。将其保存到UTF-8编码的文本文件中。解析后写在下面。

#include <QtWidgets/QApplication>    
#include <QVariantMap>
#include <QStringList>
#include <QJsonDocument>
#include <QJsonObject>
#include <QFile>
#include <QDebug>
#include <QPlainTextEdit>

using CurrencyMap = QMap<QString, QVariantMap>; // Use three-letter code as a key
using CurrencyMapIterator = QMapIterator<QString, QVariantMap>;

CurrencyMap parseCurrencyDataFromJson(QByteArray utf8Json)
{
CurrencyMap ret;

QJsonParseError parseError;
QJsonDocument document = QJsonDocument::fromJson(utf8Json, &parseError);

if (parseError.error != QJsonParseError::NoError)
{
qWarning() << parseError.errorString();
}

if (!document.isNull())
{
QJsonObject jobject = document.object();

// Iterate over all the currencies
for (QJsonValue val : jobject)
{
// Object of a given currency
QJsonObject obj = val.toObject();

// Three-letter code of the currency
QString name = obj.value("code").toString();

// All the data available for the given currency
QVariantMap fields = obj.toVariantMap();

ret.insert(name, fields);
}
}

return ret;
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QPlainTextEdit plainTextEdit;
plainTextEdit.show();

// File saved from the GitHub repo
QFile file("curr.json");
if (file.open(QIODevice::ReadOnly))
{
QByteArray jsonData = file.readAll();

CurrencyMap currencyMap = parseCurrencyDataFromJson(jsonData);

// Output all the available currency symbols to the plainTextEdit
CurrencyMapIterator currency(currencyMap);

while (currency.hasNext())
{
currency.next();

QString code = currency.key();
QVariantMap fileds = currency.value();

QStringList currencyInfo
{
code,
fileds.value("symbol").toString(),
fileds.value("symbol_native").toString()
};

plainTextEdit.appendPlainText(currencyInfo.join('\t'));
}

QString total = QString("\nTotal Available Currencies Count = %1")
.arg(currencyMap.count());

plainTextEdit.appendPlainText(total);
}

return a.exec();
}

QChar受限解决方案
QChar和基于Unicode的解决方案。但请注意, QChar本身仅支持最大0xFFFF的代码。因此,只能以这种方式检索有限数量的符号。
// In Qt, Unicode characters are 16-bit entities.
// QChar itself supports only codes with 0xFFFF maximum
QList<QChar> getAllUnicodeSymbolsForCategory(QChar::Category cat)
{
QList<QChar> ret;

// QChar actually stores 16-bit values
const static quint16 QCharMaximum = 0xFFFF;

for (quint16 val = 0; val < QCharMaximum; val++)
{
QChar ch(val);

if (ch.category() == cat)
{
ret.append(ch);
}
}

return ret;
}

用法
QList<QChar> currencies = getAllUnicodeSymbolsForCategory(QChar::Symbol_Currency);

关于c++ - 如何获得C++或Qt中所有货币符号和货币缩写的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60899299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com