输入一个整形数,然后按汉语的习惯,将其读出来并输出。如1052,读作:一千零五十二。 输入样例: 1052 输出样例: 一千零五十二
代码打了很多补丁,可读性不强;
假设输入最大值为千万,先输出“XX”万,再使用同样思路输出“XX”万“XX”;
输出“XX”时,只需要考虑以下情况:2222、2022、2002、2000、2010
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char num[10],c[10][4] = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"},r[20];
int h = 0,l;
gets(num);
l = strlen(num);
int nh = strlen(num),nl = 0,flag = 0,k;
if(l > 4)
{
int nh = l - 4 - 1,nl = 0,flag = 0,k;
switch(l-4)
{
case 4:
k = num[nl++]-'0';
//r[h++] = c[num[nl++]-'0'];
printf("%s",c[k]);
r[h++] = '千';
printf("%s", "千");
for(int i = nl; i <= nh; i++)
{
if(num[i] != '0') flag = 1;
}
if(!flag)
{
nl += 3;
break;
}
case 3:
k = num[nl++]-'0';
//r[h++] = c[num[nl++]-'0'];
for(int i = nl; i <= nh; i++)
{
if(num[i] != '0') flag = 1;
}
if(!flag)
{
nl += 2;
break;
}
printf("%s",c[k]);
if(num[nl - 1] != '0')
printf("%s","百");
case 2:
if(l > 6 && num[nl] == '0' && num[nl-1] == '0')
{
nl++;
}
else
{
k = num[nl++]-'0';
//r[h++] = c[num[nl++]-'0'];
if(k != 1)
printf("%s",c[k]);
if(num[nl - 1] != '0')
printf("%s","十");
}
case 1:
if(num[nl] != '0')
{
k = num[nl++]-'0';
//r[h++] = c[num[nl++]-'0'];
printf("%s",c[k]);
}else nl++;
}
printf("%s","万");
switch(4)
{
case 4:
nh = strlen(num);
k = num[nl++]-'0';
flag = 0;
//r[h++] = c[num[nl++]-'0'];
printf("%s",c[k]);
r[h++] = '千';
printf("%s", "千");
for(int i = nl; i < nh; i++)
{
if(num[i] != '0') flag = 1;
}
if(!flag)
{
nl += 3;
break;
}
case 3:
k = num[nl++]-'0';
//r[h++] = c[num[nl++]-'0'];
for(int i = nl-1; i < nh; i++)
{
if(num[i] != '0') flag = 1;
}
if(!flag)
{
nl += 2;
break;
}
printf("%s",c[k]);
if(num[nl - 1] != '0')
printf("%s","百");
case 2:
if(num[nl] == '0' && num[nl-1] == '0')
{
nl++;
}
else
{
k = num[nl++]-'0';
//r[h++] = c[num[nl++]-'0'];
if(k != 1)
printf("%s",c[k]);
if(num[nl - 1] != '0')
printf("%s","十");
}
case 1:
if(num[nl] != '0')
{
k = num[nl++]-'0';
//r[h++] = c[num[nl++]-'0'];
printf("%s",c[k]);
}else nl++;
}
}else
{
int nh = strlen(num);
switch(strlen(num))
{
case 4:
nh = strlen(num);
k = num[nl++]-'0';
//r[h++] = c[num[nl++]-'0'];
printf("%s",c[k]);
r[h++] = '千';
printf("%s", "千");
for(int i = nl; i < nh; i++)
{
if(num[i] != '0') flag = 1;
}
if(!flag)
{
nl += 3;
break;
}
case 3:
k = num[nl++]-'0';
//r[h++] = c[num[nl++]-'0'];
for(int i = nl-1; i < nh; i++)
{
if(num[i] != '0') flag = 1;
}
if(!flag)
{
nl += 2;
break;
}
printf("%s",c[k]);
if(num[nl - 1] != '0')
printf("%s","百");
case 2:
if( num[nl] == '0' && num[nl-1] == '0')
{
nl++;
}
else
{
k = num[nl++]-'0';
//r[h++] = c[num[nl++]-'0'];
if(k != 1)
printf("%s",c[k]);
if(num[nl - 1] != '0')
printf("%s","十");
}
case 1:
if(num[nl] != '0')
{
k = num[nl++]-'0';
//r[h++] = c[num[nl++]-'0'];
printf("%s",c[k]);
}
}
}
return 0;
}