正整数的中文读法(C++ Python)
来源:互联网 发布:阿里云t4机房 编辑:程序博客网 时间:2024/05/17 08:46
基本思路
(1)数字到中文的映射
(2)每四位作为一个单元处理
(3)从简入手,逐步细化
映射
中文读法中会出现的汉字如下:
零;一、二、… 、九;十、百、千;万、亿零比较特殊,单独存储
- 一、二、…、九是计数的基本数字,存为一个数组
- 十、百、千是每个万组里面的单位,存为一个数组
- 万、亿是更大单位,存为一个数组
基本处理
通过循环得到正整数各个位的数字,数据保存到数组data[]中
通过取模运算和除法运算,得到该数字所在万组的信息
每个万组结束,加上万组的单位
此时并未处理任何特殊情况
特殊情况“0”
一个万组内,末尾的所有“0”不读,如200、2001234
一个万组内,非零数字之间有“0”,只需要读一个“零”,如:201、3002、1050
一个万组内,高位数字为“0”,如果还有更高的万组,那么“0”读出来,否则不读,如:0030、100300、
特殊情况“1”
一个万组内,千位和百位都是“0”,十位为“1”时,而且没有更高的万组,“一”不读,如:14、123562
如果有更高的万组,“一”需要读出来,如:20012
//c++#include<iostream>#define MAX 10#define MID 4#define MIN 3using namespace std;const string CHINESE_ZERO = "零";;const string CHINESE_DIGITS[MAX] = { "", "一", "二", "三", "四", "五","六","七","八","九" };const string CHINESE_UNITES[MID] = { "","十","百","千" };const string CHINESE_LARGE_UNITES[MIN] = { "", "万","亿" };class NumToCh{public: NumToCh(int num); ~NumToCh(); void Translate();private: int number; int data[MAX]; int len; string word;};NumToCh::NumToCh(int num){ this->number = num; int digit; int position=0; while (this->number>0) { digit = this->number % MAX; this->number = this->number / MAX; data[position] = digit; position += 1; } this->len = position;}void NumToCh::Translate(){ bool groupIsZero = true; bool needZero = false; int unite = 0; int grou = 0; for (int i = len-1; i >=0; i--) { unite = i % MID; grou = i / MID; if (this->data[i] != 0) { if (needZero) this->word.append(CHINESE_ZERO); if (this->data[i] != 1 || unite != 1 || (!groupIsZero) || (grou == 0 && needZero)) this->word.append( CHINESE_DIGITS[this->data[i]]); this->word.append( CHINESE_UNITES[unite]); } groupIsZero = groupIsZero && this->data[i] == 0; if (unite == 0 && (!groupIsZero)) this->word.append( CHINESE_LARGE_UNITES[grou]); needZero = (data[i] == 0 && (unite != 0 || groupIsZero)); if (unite == 0) groupIsZero = true; } for (int j = 0; j < this->word.length(); j++) cout << this->word[j]; cout << endl;}NumToCh::~NumToCh(){}int main(){ while (true) { int number=0; cout << "请输入一个10亿以内的正整数:"; cin >> number; if (number == 0) { cout << "零" << endl; continue; } NumToCh num(number); num.Translate(); } return 0;}
#Python#coding:utf-8CHINESE_NEGATIVE = u'负'CHINESE_ZERO = u'零'CHINESE_DIGITS = [u'', u'一', u'二', u'三', u'四', u'五', u'六', u'七', u'八', u'九']CHINESE_UNITS = [u'', u'十', u'百', u'千']CHINESE_GROUP_UNITS = [u'', u'万', u'亿', u'兆']def _enumerate_digits(number): """ :type number: int|long :rtype: collections.Iterable[int, int] """ position = 0 while number > 0: digit = number % 10 number //= 10 yield position, digit position += 1def translate_number_to_chinese(): number = int(raw_input(u"请输入一个整数:")) group_is_zero = True need_zero = False words = [] for position, digit in reversed(list(_enumerate_digits(number))): unit = position % len(CHINESE_UNITS) group = position // len(CHINESE_UNITS) if digit != 0: if need_zero: words.append(CHINESE_ZERO) if digit != 1 or unit != 1 or not group_is_zero or (group == 0 and need_zero): words.append(CHINESE_DIGITS[digit]) words.append(CHINESE_UNITS[unit]) group_is_zero = group_is_zero and digit == 0 if unit == 0 and not group_is_zero: words.append(CHINESE_GROUP_UNITS[group]) need_zero = (digit == 0 and (unit != 0 or group_is_zero)) if unit == 0: group_is_zero = True for i in range(len(words)): print (words[i]), printdef main(): while(True): translate_number_to_chinese()if __name__ == '__main__': main()
0 0
- 正整数的中文读法(C++ Python)
- 正整数的汉语读法
- C语言基础--输出数值的中文读法
- 数字的人民币中文读法
- 输出以个小于1万亿的正整数的拼音读法
- 阿拉伯数字转为中文读法
- 阿拉伯数字表示的数字转换成中文习惯读法
- 输入数字把数字变成中文的读法
- 华为机试:中文整数读法的拼音转数字
- 每日一题 No.6 数字的中文读法
- C语言里面的嵌套声明的读法
- C语言里面的嵌套声明的读法
- 数字转汉语读法的方法设计(C#)
- C语言复杂定义的读法(右左法则)
- 根据阿拉伯数字输出中文读法
- 阿拉伯数字转换成中文读法
- 使用python实现正整数的阶乘
- 希腊字母的读法
- 二维码扫描库的使用
- 字符串匹配的KMP算法(不建议大家看,这里是我的暂时笔记)
- 如何根据v$parameter查询Oracle隐含参数
- 树莓派(Raspberry Pi)瞎捣鼓
- 240. Search a 2D Matrix II
- 正整数的中文读法(C++ Python)
- 理解进程调度时机跟踪分析进程调度与进程切换的过程(Linux)
- LeetCode 298. Binary Tree Longest Consecutive Sequence(二叉树最长连续序列)
- Ubuntu校园网不能正常使用ipv6上网
- C++初始化列表
- mysql5.7免安装版报错处理You must reset your password using ALTER USER statement before executing
- Linux~文件/目录操作
- 《大规模分布式存储系统:原理解析与架构实战》读书笔记(1)
- VS2015下编译安装wxWidgets环境