华为面试题数字大小写转换
时间:2021-06-19 08:16:16|栏目:C代码|点击: 次
这题因为限定了4位数,所以只考虑了4位数的情况,吃点分享一个大小写转换的源码,里面有不限位数的情况,当时调试的很痛苦,思想差不多。
复制代码 代码如下:
void iConvert(int digit)
{
char a[5][10] = {"千","百","十","","零"};
char b[11][10] = {"零","一","二","三","四","五","六","七","八","九","十"};
char result[50] = {'\0'};
int A[4] = {};
for(int i=3;i>=0;i--)
{
A[i] = digit % 10;
digit = int(digit/10);
}
printf("%d,%d,%d,%d\n",A[0],A[1],A[2],A[3]);
int foundZero = 0;
for(int i = 0 ;i<4;i++)
{
if(A[i]>0)
{
strcat(result,b[A[i]]);
strcat(result,a[i]);
}
if(A[i]==0 && foundZero == 0)
{
if(i!=3)//如果不是最后一位,则不追加零
{
strcat(result,a[4]);
foundZero = 1;
}
}
}
puts(result);
}
运行结果: