1 C++阿拉伯数字字符串转换为中文读法的中文字符串,不支持小数点
最近碰到一个这样的需求,就是将阿拉伯数字字符串转换为中文读法的中文字符串,形如将
"199922"
转换为:
"十九万九千九百二十二"
经过一番折腾,已经可以成功完成转换,并支持个位数到兆的范围转换,我的示例代码如下:
#include <iostream>
#include <string>
std::wstring ConvertNumToChineseNum(const std::wstring& num)
{
std::wstring chinaeseNumResult;
std::wstring num_copy = num;
int size = num_copy.length();
for (int i = 0; i < num_copy.size(); ++i)
{
if (num_copy[i] == L'1')
{
chinaeseNumResult += L"一";
}
else if (num_copy[i] == L'2')
{
chinaeseNumResult += L"二";
}
else if (num_copy[i] == L'3')
{
chinaeseNumResult += L"三";
}
else if (num_copy[i] == L'4')
{
chinaeseNumResult += L"四";
}
else if (num_copy[i] == L'5')
{
chinaeseNumResult += L"五";
}
else if (num_copy[i] == L'6')
{
chinaeseNumResult += L"六";
}
else if (num_copy[i] == L'7')
{
chinaeseNumResult += L"七";
}
else if (num_copy[i] == L'8')
{
chinaeseNumResult += L"八";
}
else if (num_copy[i] == L'9')
{
chinaeseNumResult += L"九";
}
if (num_copy[i] == L'0')
{
if (size % 4 == 3)
{
if (num_copy[i + 1] != L'0' || num_copy[i + 2] != L'0')
{
chinaeseNumResult += L"零";
}
}
else if (size % 4 == 2 && num_copy[i - 1] != L'0')
{
if (num_copy[i + 1] != L'0')
{
chinaeseNumResult += L"零";
}
}
}
if (size % 4 == 1)
{
if (size / 4 == 1)
{
if (num_copy[i] != L'0' || num_copy[i - 1] != L'0' || num_copy[i - 2] != L'0' || num_copy[i - 3] != L'0')
{
chinaeseNumResult += L"万";
}
}
else if (size / 4 == 2)
{
if (num_copy[i] != L'0' || num_copy[i - 1] != L'0' || num_copy[i - 2] != L'0' || num_copy[i - 3] != L'0')
{
chinaeseNumResult += L"亿";
}
}
if (size / 4 == 3)
{
if (num_copy[i] != L'0' || num_copy[i - 1] != L'0' || num_copy[i - 2] != L'0' || num_copy[i - 3] != L'0')
{
chinaeseNumResult += L"兆";
}
}
}
if (num_copy[i] != L'0')
{
if (size % 4 == 2)
{
chinaeseNumResult += L"十";
}
if (size % 4 == 3)
{
chinaeseNumResult += L"百";
}
if (size % 4 == 0)
{
chinaeseNumResult += L"千";
}
}
--size;
}
return chinaeseNumResult;
}
int main()
{
std::wstring result = ConvertNumToChineseNum(L"352717183921");
std::wcout << result << std::endl;
return 0;
}
运行结果如下:
2 C++阿拉伯数字字符串转换为中文读法的中文字符串,支持小数点
形如将
"199922.99"
转换为:
"十九万九千九百二十二点九九"
示例代码如下:
#include <iostream>
#include <string>
// 将阿拉伯字符串的整数部分,即小数点前的部分转换为中文数字字符串
std::wstring ConverArabicNumIntegerPartToChineseNum(const std::wstring& arab_num_integer_part)
{
std::wstring chineseNumResult;
std::wstring arab_num_integer_part_copy = arab_num_integer_part;
int size = arab_num_integer_part_copy.length();
for (int i = 0; i < arab_num_integer_part_copy.size(); ++i)
{
if (arab_num_integer_part_copy[i] == L'1')
{
chineseNumResult += L"一";
}
else if (arab_num_integer_part_copy[i] == L'2')
{
chineseNumResult += L"二";
}
else if (arab_num_integer_part_copy[i] == L'3')
{
chineseNumResult += L"三";
}
else if (arab_num_integer_part_copy[i] == L'4')
{
chineseNumResult += L"四";
}
else if (arab_num_integer_part_copy[i] == L'5')
{
chineseNumResult += L"五";
}
else if (arab_num_integer_part_copy[i] == L'6')
{
chineseNumResult += L"六";
}
else if (arab_num_integer_part_copy[i] == L'7')
{
chineseNumResult += L"七";
}
else if (arab_num_integer_part_copy[i] == L'8')
{
chineseNumResult += L"八";
}
else if (arab_num_integer_part_copy[i] == L'9')
{
chineseNumResult += L"九";
}
if (arab_num_integer_part_copy[i] == L'0')
{
if (size % 4 == 3)
{
if (arab_num_integer_part_copy[i + 1] != L'0' || arab_num_integer_part_copy[i + 2] != L'0')
{
chineseNumResult += L"零";
}
}
else if (size % 4 == 2 && arab_num_integer_part_copy[i - 1] != L'0')
{
if (arab_num_integer_part_copy[i + 1] != L'0')
{
chineseNumResult += L"零";
}
}
}
if (size % 4 == 1)
{
if (size / 4 == 1)
{
if (arab_num_integer_part_copy[i] != L'0' || arab_num_integer_part_copy[i - 1] != L'0' || arab_num_integer_part_copy[i - 2] != L'0' || arab_num_integer_part_copy[i - 3] != L'0')
{
chineseNumResult += L"万";
}
}
else if (size / 4 == 2)
{
if (arab_num_integer_part_copy[i] != L'0' || arab_num_integer_part_copy[i - 1] != L'0' || arab_num_integer_part_copy[i - 2] != L'0' || arab_num_integer_part_copy[i - 3] != L'0')
{
chineseNumResult += L"亿";
}
}
if (size / 4 == 3)
{
if (arab_num_integer_part_copy[i] != L'0' || arab_num_integer_part_copy[i - 1] != L'0' || arab_num_integer_part_copy[i - 2] != L'0' || arab_num_integer_part_copy[i - 3] != L'0')
{
chineseNumResult += L"兆";
}
}
}
if (arab_num_integer_part_copy[i] != L'0')
{
if (size % 4 == 2)
{
chineseNumResult += L"十";
}
if (size % 4 == 3)
{
chineseNumResult += L"百";
}
if (size % 4 == 0)
{
chineseNumResult += L"千";
}
}
--size;
}
return chineseNumResult;
}
// 将阿拉伯字符串的小数部分,即小数点后的部分转换为中文数字字符串
std::wstring ConverArabicNumDecimalPartToChineseNum(const std::wstring& arab_num_decimal_part)
{
std::wstring chineseNumResult;
std::wstring arab_num_decimal_part_copy = arab_num_decimal_part;
for (int i = 0; i < arab_num_decimal_part_copy.size(); ++i)
{
if (arab_num_decimal_part_copy[i] == L'0')
{
chineseNumResult += L"零";
}
else if (arab_num_decimal_part_copy[i] == L'1')
{
chineseNumResult += L"一";
}
else if (arab_num_decimal_part_copy[i] == L'2')
{
chineseNumResult += L"二";
}
else if (arab_num_decimal_part_copy[i] == L'3')
{
chineseNumResult += L"三";
}
else if (arab_num_decimal_part_copy[i] == L'4')
{
chineseNumResult += L"四";
}
else if (arab_num_decimal_part_copy[i] == L'5')
{
chineseNumResult += L"五";
}
else if (arab_num_decimal_part_copy[i] == L'6')
{
chineseNumResult += L"六";
}
else if (arab_num_decimal_part_copy[i] == L'7')
{
chineseNumResult += L"七";
}
else if (arab_num_decimal_part_copy[i] == L'8')
{
chineseNumResult += L"八";
}
else if (arab_num_decimal_part_copy[i] == L'9')
{
chineseNumResult += L"九";
}
}
return chineseNumResult;
}
std::wstring ConvertArabicNumToChineseNum(const std::wstring& arab_num)
{
std::wstring chineseNumResult;
std::wstring arab_num_copy = arab_num;
// 如果包含了小数点
if (arab_num_copy.find(L".") != std::wstring::npos)
{
int dotPosition = arab_num_copy.find(L".");
std::wstring integerPartNum = arab_num_copy.substr(0, dotPosition);
std::wstring decimalPartNum = arab_num_copy.substr(dotPosition + 1);
chineseNumResult += ConverArabicNumIntegerPartToChineseNum(integerPartNum);
chineseNumResult += L"点";
chineseNumResult += ConverArabicNumDecimalPartToChineseNum(decimalPartNum);
}
else
{
chineseNumResult += ConverArabicNumIntegerPartToChineseNum(arab_num_copy);
}
return chineseNumResult;
}
int main()
{
std::wstring result = ConvertArabicNumToChineseNum(L"131346546511.99");
std::wcout << result << std::endl;
return 0;
}
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – 阿拉伯数字字符串转换为中文读法的中文字符串,支持小数点
原文链接:https://www.stubbornhuang.com/1851/
发布于:2021年12月04日 18:40:01
修改于:2023年06月26日 20:59:57
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论
52