c语言编程:输入一个整形数,然后按汉语的习惯,将其读出来并输出.如1052,读作:一千零五十二.
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言编程:输入一个整形数,然后按汉语的习惯,将其读出来并输出.如1052,读作:一千零五十二.相关的知识,希望对你有一定的参考价值。
#include <stdio.h>#include <string.h>
const char * sCommonCnNumbers = "零一二三四五六七八九十百千万亿负";
// 万以内的读法
char * I2cnSub1(char * o, unsigned int num, int ys = 0)
if(num > 9999) return NULL;
unsigned int q = num / 1000;
num -= q * 1000;
unsigned int b = num / 100;
num -= b * 100;
unsigned int s = num / 10;
unsigned int g = num - s * 10;
int l = 0;
if(q) *o++ = q + '0'; *o++ = 'q'; ys = 1;
else if(ys == 1) l = 1;
if(b)
if(l == 1) *o++ = '0'; l = 0; ys = 0;
*o++ = b + '0'; *o++ = 'b'; ys = 1;
else if(ys == 1) l = 1;
if(s)
if(l == 1) *o++ = '0'; l = 0; ys = 0;
if(s == 1 && ys == 0) ;
else *o++ = s + '0';
*o++ = 's'; ys = 1;
else if(ys == 1) l = 1;
if(g)
if(l == 1) *o++ = '0';
*o++ = g + '0';
*o = 0;
return o;
// 数字串转换为中文数字串
char * I2cnSub2(char * o, const char * i, const char * kCN)
char * or = o;
size_t len = strlen(i);
char * buf = new char[len], *pbl, *pbh;
memcpy(buf, i, len);
pbl = buf;
pbh = pbl + len;
while(pbl < pbh)
switch(*pbl++)
case '0': memcpy(o, kCN, 2); o+=2; break;
case '1': memcpy(o, kCN + 2, 2); o+=2; break;
case '2': memcpy(o, kCN + 4, 2); o+=2; break;
case '3': memcpy(o, kCN + 6, 2); o+=2; break;
case '4': memcpy(o, kCN + 8, 2); o+=2; break;
case '5': memcpy(o, kCN + 10, 2); o+=2; break;
case '6': memcpy(o, kCN + 12, 2); o+=2; break;
case '7': memcpy(o, kCN + 14, 2); o+=2; break;
case '8': memcpy(o, kCN + 16, 2); o+=2; break;
case '9': memcpy(o, kCN + 18, 2); o+=2; break;
case 's': memcpy(o, kCN + 20, 2); o+=2; break;
case 'b': memcpy(o, kCN + 22, 2); o+=2; break;
case 'q': memcpy(o, kCN + 24, 2); o+=2; break;
case 'w': memcpy(o, kCN + 26, 2); o+=2; break;
case 'y': memcpy(o, kCN + 28, 2); o+=2; break;
case '-': memcpy(o, kCN + 30, 2); o+=2; break;
default:
;
*o = 0;
delete [] buf;
return or;
;
// 数字转换为中文数字,结果符合中文数字读法
char * I2cn(char * o, __int64 num, const char * kCN = sCommonCnNumbers)
char * or = o;
const unsigned __int64 kMaxInt32 = 4294967296ui64;
const unsigned __int64 kYY = 10000000000000000ui64;
const unsigned __int64 kWY = 1000000000000ui64;
const unsigned __int64 kY = 100000000ui64;
const unsigned __int64 kW = 10000ui64;
if(num < 0) *o++ = '-'; num = -num;
int ys = 0, l = 0;
if(num > kYY)
unsigned int yy = (unsigned int)(num / kYY);
num -= yy * kYY;
o = I2cnSub1(o, yy);
*o++ = 'y';
if(num < kY) *o++ = 'y';
ys = 1;
else if(ys == 1) l = 1;
if(num > kWY)
if(l == 1) *o++ = '0'; l = 0; ys = 0;
unsigned int wy = (unsigned int)(num / kWY);
num -= wy * kWY;
o = I2cnSub1(o, wy, ys);
*o++ = 'w';
if(num < kY) *o++ = 'y';
ys = 1;
else if(ys == 1) l = 1;
if(num > kY)
if(l == 1) *o++ = '0'; l = 0; ys = 0;
unsigned int y = (unsigned int)(num / kY);
num -= y * kY;
o = I2cnSub1(o, y, ys);
*o++ = 'y';
ys = 1;
else if(ys == 1) l = 1;
if(num > kW)
if(l == 1) *o++ = '0'; l = 0; ys = 0;
unsigned int w = (unsigned int)(num / kW);
num -= w * kW;
o = I2cnSub1(o, w, ys);
*o++ = 'w';
if(num)
if(l == 1) *o++ = '0'; ys = 0;
o = I2cnSub1(o, (unsigned int)num, ys);
*o = 0;
return I2cnSub2(or, or, kCN);
int main()
__int64 x;
char buf[128];
printf("请输入一个数字:");
scanf("%I64d", &x);
printf("此数字的中文读法为:%s\\n", I2cn(buf, x));
return 0;
参考技术A #include <stdio.h>
void func(long n,int i);
int main(void)
long n;
scanf("%ld",&n);
func(n,0);
return 0;
void func(long n,int i)
char dgt[][5]="零","一","二","三","四","五","六","七","八","九";
char plc[][5]="","十","百","千","万","十万","百万","千万","亿";
if(n)
if(n<0)
printf("负");
n=-n;
if(n%10==0)
for(;n%100/10==0;n/=10,++i);
func(n/10,i+1);
printf("%s%s",dgt[n%10],n%10?plc[i]:"");
if(!n&&!i)
printf("%s",dgt[i]);
本回答被提问者采纳
以上是关于c语言编程:输入一个整形数,然后按汉语的习惯,将其读出来并输出.如1052,读作:一千零五十二.的主要内容,如果未能解决你的问题,请参考以下文章