数字转化成大写汉字有着很广泛的应用,比如银行系统,比如语音导航系统等。

处理数字转换成汉字的过程中,主要是完成权值和数字的转换,在我国使用的权值一般是按照4位一段进行读取的,也就是万、亿、万亿等,但是每一段中还有更小的权值:千、百、十。所有数字转换成汉字的程序需要处理好段的权值和每段中的每个数字之间的权值问题。

具体的算法过程:
1.首先根据数字截取4位一段的数字段;
2.判断每段数字位所在的数字和权值;
3.处理好0的读取:
a.以每段作为判断标准小节,小节的结尾即使是0,也不使用零,即也不读出零
b.小节内两个非0数字之间要使用零
c.当小节的千位是0时,若本小节的前一小节无其他数字,则不用零,否则实用零。

以下是自己写的一个实例,仅供参考:

#pragma once
#include <QString>
#include <QVector>
//数字转换成汉字#define NUMBER_COUNT 10//数字数量
#define SECTION_COUNT 4//权值数量
#define BIT_COUNT 4//位数量typedef struct tagSection {QString sectionNameStr;//权位int num;//权位值
}NumSection;class DigitalToChinese {
public:DigitalToChinese();~DigitalToChinese();//数字转换成对应的汉字QString digitalToChinese(long long num);private:QString bitSection(int num,bool bHead);//把每一段转换成汉字QString IntelSec(QVector<NumSection>&sectionList, int readNum);QString IntelSecZero(QVector<NumSection>&sectionList);
private:const char* _chineseNumber[NUMBER_COUNT] = { "零","一","二" ,"三" ,"四" ,"五" ,"六" ,"七" ,"八" ,"九" };const char* _section[SECTION_COUNT] = { "","万","亿","万亿" };const char* _bit[BIT_COUNT] = { "","十","百","千" };long long _converNum = 0;QVector<NumSection> _sectionList;//
};
#include "DigitalToChinese.h"DigitalToChinese::DigitalToChinese() {
}DigitalToChinese::~DigitalToChinese() {
}QString DigitalToChinese::digitalToChinese(long long num) {QString numStr;if (num < 0){//处理负值numStr = QStringLiteral("负");}_converNum = abs(num);int sectionPos = -1;//记录数字段数 - 万为一段int sectionNumber = 0;while (_converNum > 0){sectionPos++;sectionNumber = _converNum % 10000;//保存每个权位和对应的数值NumSection numSec;numSec.num = sectionNumber;numSec.sectionNameStr = QString::fromLocal8Bit(_section[sectionPos]);_sectionList.push_back(numSec);_converNum = _converNum / 10000;}int numSec = _sectionList.size();for (int i = numSec - 1; i >= 0;i--) {NumSection numSec = _sectionList.at(i);numStr += bitSection(_sectionList.at(i).num,i+1 == _sectionList.size());numStr += _sectionList.at(i).sectionNameStr;}return numStr;
}QString DigitalToChinese::bitSection(int num, bool bHead) {QVector<NumSection>bitList;int bitPos = 0;int tempNum = num;while (num > 0) {int bitNum = num % 10;NumSection bM;bM.num = bitNum;bM.sectionNameStr = QString::fromLocal8Bit(_bit[bitPos]);bitList.push_back(bM);num = num / 10;bitPos++;}QString numStr;//1.后面一个或者多个零if (tempNum / 1000 > 0 && tempNum % 1000 == 0) {//整千return IntelSec(bitList, 1);} else if (tempNum / 100 > 0 && tempNum % 100 == 0) {//整百if (tempNum/1000 > 0){return IntelSec(bitList, 2);} else {if (!bHead) {//如果是第一段numStr += QString::fromLocal8Bit(_chineseNumber[0]);}return numStr += IntelSec(bitList, 1);}} else if (tempNum / 10 > 0 && tempNum % 10 == 0) {//整十if (tempNum / 1000 > 0) {return IntelSec(bitList, 3);} else if (tempNum / 100 > 0){if (!bHead) {//如果是第一段numStr += QString::fromLocal8Bit(_chineseNumber[0]);}return numStr += IntelSec(bitList, 2);}else {if (!bHead) {//如果是第一段numStr += QString::fromLocal8Bit(_chineseNumber[0]);}return numStr += IntelSec(bitList, 1);}return IntelSec(bitList, 3);}int n = bitList.size();//2.前面有一个或者多个零if ( n < 4){if (!bHead){//如果是第一段numStr += QString::fromLocal8Bit(_chineseNumber[0]);}return numStr += IntelSecZero(bitList);}//3.中间有一个或者多个零return IntelSecZero(bitList);
}QString DigitalToChinese::IntelSec(QVector<NumSection>&sectionList, int readNum) {QString numStr;int n = sectionList.size();for (int i = n - 1; i >= n - readNum; i--) {NumSection bM = sectionList.at(i);numStr += QString::fromLocal8Bit(_chineseNumber[bM.num]);numStr += bM.sectionNameStr;}return numStr;
}QString DigitalToChinese::IntelSecZero(QVector<NumSection>&sectionList) {QString numStr;int n = sectionList.size();for (int i = n - 1; i >= 0; i--) {NumSection bM = sectionList.at(i);if (bM.num == 0 && sectionList.at(i + 1).num != 0){numStr += QString::fromLocal8Bit(_chineseNumber[0]);continue;} else if(bM.num == 0 && sectionList.at(i+1).num == 0){numStr += QString::fromLocal8Bit(_chineseNumber[0]);i--;continue;}numStr += QString::fromLocal8Bit(_chineseNumber[bM.num]);numStr += bM.sectionNameStr;}return numStr;
}

使用:

int main(int argc, char *argv[]) {DigitalToChinese dtc;QString strNum = dtc.digitalToChinese(1230950043);return 0;
}


aaa

阿拉伯数字转换成中文数字 C++相关推荐

  1. python练习题--阿拉伯数字转换成中文数字

    python练习题–阿拉伯数字转换成中文数字 用户输入任意阿拉伯数字,如[123456],把它转换成中文数字[壹拾贰万叁仟肆佰伍拾陆] 以前面试时面试官问的问题,当时没能答出来,今天初步解决,把它记录 ...

  2. 【工具封装】Python 实现将阿拉伯数字 === 转换成中文大写数字

    一.序言:   工具封装第四弹,阿拉伯数字 ===> 转换成 ===> 中文大写数字,喜欢就赶紧收藏+点赞+关注吧 !!! ---- Nick.Peng 二.实现代码如下: #!/usr/ ...

  3. 将数字转换成中文数字

    将阿拉伯数字转换成中文数字,大家可以看我的测试数据,应该没有BUG. /** * 将数字转换成中文数字 * @author Prosper * */ public class IntToCN { pu ...

  4. js将阿拉伯数字转换成中文的大写数字

    js将阿拉伯数字转换成中文的大写数字 export const numberToChinese = (num) => {var AA = new Array("零", &qu ...

  5. 将一组阿拉伯数字转换成中文大写数字

    题目大概:  将一组阿拉伯数字转换成中文大写数字  52306 ==> 伍万贰千叁百零陆 我实现了将文件中的一组数字(每行为一个数)  形如: Java代码   25364 466932300 ...

  6. 阿拉伯数字转换成中文算法--计数单位

    今天继续看<算法的乐趣>,学习了阿拉伯数字与中文数字的转化. 汉字用零一二三四五六七八九作为基本计数,与阿拉伯数字靠数字偏移位置的权位不一样,中文数字是才有"数字+权位" ...

  7. java练习:金额转换,阿拉伯数字转换成中文传统形式

    需求:金额转换,阿拉伯数字转换成中文传统形式   ,例如 101000001010   转为     壹仟零壹拾亿零壹仟零壹拾圆整 最终版: import java.util.Scanner; pub ...

  8. win7计算机名改成大写,处置win7系统将word中的阿拉伯数字转换成大写数字的还原方案...

    随着电脑的使用率越来越高,我们有时候可能会遇到对win7系统将word中的阿拉伯数字转换成大写数字进行设置,如果我们需要对win7系统将word中的阿拉伯数字转换成大写数字进行设置时,要怎么处理win ...

  9. 《读九章算术学Python》如何用Python编程实现阿拉伯数字转换成汉字数字?

    第6章 数量转换 Python编程基础 字典 字符串操作 if-elif-else语句 递归 前面的输入和输出都是阿拉伯数字,这一章我们来看一下如何实现阿拉伯数字和汉字数字之间的相互转换. 6.1 阿 ...

  10. python数字转换成中文大写_python初学者笔记(2):阿拉伯数字转换成中文大写

    题:输入一个数字,转换成中文大写的写法 可运行的程序(Python 2.7.9): 1 #-*- coding: utf-8 -*- #在python2的py文件里面写中文,必须要添加一行声明文件编码 ...

最新文章

  1. JavaScript 编程精解 中文第三版 零、前言
  2. delphi 解析json java_Delphi处理JSON格式数据
  3. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter错误解决
  4. Elasticsearch和MongoDB对比
  5. 2015年第六届蓝桥杯 - 省赛 - C/C++大学A组 - C.奇妙的数字
  6. java终结器_Java的终结器仍然存在
  7. abp的权限与导航菜单的关系
  8. Delphi 对象的创建(create)与释放(free/destory)
  9. (转)让页面自动滚动到刷新页面之前的控件处,减少页面刷新带来的不便。
  10. MySQL导出记录到Excel表格下载打开后乱码问题
  11. 转载-常用邮箱SMTP服务器地址大全
  12. 惠普暗影精灵8 Pro酷睿版和锐龙版的区别 哪个更值得入手
  13. [HPM] Error occurred while trying to proxy request /login/account from localhost:8000 to localhost:8
  14. 程序员创业必读的几本书
  15. 国外人经常上的网站,即全球各个领域最大的互联网网站
  16. 5G NR学习理解系列——时频结构及相关概念
  17. Python+Tushare,自制A股筛选器
  18. 自制AutoCAD实用工具
  19. Offset is outside the bounds of the DataView;at api notifyBLECharacteristicValueChanged success call
  20. 如何编写一个功能完善的HTTP服务器

热门文章

  1. 腾讯服务器解封微信号6,微信号封了怎么解封?微信号如何解封方法终于来了...
  2. 判断一个数字是不是素数
  3. IT互联网行业猎头的年终总结:结束后开始
  4. 免费PBootCMS采集支持聚合文章采集插件
  5. 大师速写作品及理论,有你喜欢的知识
  6. OSPF的DR和BDR【eNSP实现】
  7. 剑指offer 数组中出现次数超过一半的数字
  8. android境外支付
  9. UVM中starting_phase
  10. 在线微信编辑器(构思编辑器)——如何让微信公众号文章图文设计更完美