类似的程序我写过一个,但并不完善(我忘记什么地方不完善了),但用来参考应该足够了。
程序代码:
#include <stdio.h>
#include <string.h>
void
written_amount( unsigned int amount, char *buffer );
int
main( void )
{
char buffer[1000];
unsigned int a;
while( 1 == scanf("%u",&a) )
{
written_amount( a, buffer );
printf("%d\n",a);
printf("%s\n",buffer);
}
return 0;
}
static char *nums[] = { "ling ", "yi ", "er ", "san ", "si ", "wu ", "liu ", "qi ", "ba ", "jiu " };
static char *magnitudes[] ={ "","shi ","bai ","qian ","wan ","shiwan ","baiwan ","qianwan ","yi " };
void
written_amount( unsigned int amount, char *buffer )
{
unsigned int value;
unsigned int count;
unsigned int num, prev;
unsigned int magnitude;
value = amount / 10;
magnitude = 1;
buffer[0] = '\0';
if( 0 == amount )
strcat( buffer, nums[ amount ] );
for( count = 0; 0 < value; count++, value /= 10, magnitude *= 10 )
;
for( value = amount, prev = 1; 0 < value; count--, magnitude /= 10 )
{
num = value / magnitude;
value %= magnitude;
if( 0 != num || 0 != prev )
strcat( buffer, nums[ num ] );
if( 0 != num )
strcat( buffer, magnitudes[ count ] );
prev = num;
}
}