首页 > 代码库 > 把数字数值转换成单词形式
把数字数值转换成单词形式
原文:把数字数值转换成单词形式
《C和指针》第7章第6道编程题:
编写函数
void written_amount( unsigned int amount, char *buffer );
它把 amount 表示的值转换为单词形式,并存储于 buffer 中。这个函数可以在一个打印支票的程序中使用。例如,如果 amount 的值是 16 312,那么 buffer 中存储的字符串应该是
SIXTEEN THOUSAND THREE HUNDRED TWELVE
调用程序保证 buffer 缓冲区的空间足够大。例如,1 200 可以是 ONE THOUSAND TWO HUNDRED 或 TWELVE HUNDRED。你可以选择一种你喜欢的形式。
1 /* 2 ** 把非负整数数值转化为单词形式 3 */ 4 5 #include <stdio.h> 6 #include <string.h> 7 8 void written_amount( unsigned int amount, char *buffer ); 9 10 int 11 main() 12 { 13 unsigned int num; 14 scanf( "%u", &num ); 15 char b[100] = { 0 }; 16 17 written_amount( num, b ); 18 19 puts( b ); 20 21 return 0; 22 } 23 24 /* 25 ** 函数功能:把amount的值转化为单词形式,结果存储在buffer中 26 */ 27 void 28 written_amount( unsigned int amount, char *buffer ) 29 { 30 /* 31 ** 数字对应的单词,分为4组 32 */ 33 char word_1_9[10][6] = { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", 34 "SIX", "SEVEN", "EIGHT", "NINE" }; 35 char word_10_19[10][10] = {"TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", 36 "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN" }; 37 char word_ty[10][8] = { "", "", "TWENTY", "THIRTY", "FORTY", "FIFTY", 38 "SIXTY", "SEVENTY", "EIGHTY", "NINETY" }; 39 char word_div[4][9] = { "HUNDRED", "THOUSAND", "MILLION", "BILLION" }; 40 41 static int count = 0; // 记录千分位的个数 42 unsigned int tmp = amount / 1000; 43 44 /* 45 ** 用递归函数,直到数值< 1000 46 */ 47 if( tmp ) 48 { 49 count ++; // 千分位的个数增加 50 written_amount( tmp, buffer ); // 递归,处理下一个千分位 51 } 52 53 int val = amount % 1000; // 3位3位地进行转化单词 54 55 /* 56 ** 值大于100,先把百位上的数的单词存到buffer中 57 ** 后面加上 HUNDRED 58 ** 把值%100,变成两位数,后续继续处理 59 */ 60 if( val >= 100 ) 61 { 62 strcat( buffer, word_1_9[ val / 100 ] ); 63 strcat( buffer, " " ); 64 strcat( buffer, word_div[ 0 ] ); 65 val %= 100; 66 } 67 68 /* 69 ** 值在20至99之间,先把几十对应的单词加到buffer中 70 ** 再%10,后续处理个位上的数 71 */ 72 if( val >= 20 && val <= 99 ) 73 { 74 strcat( buffer, " " ); 75 strcat( buffer, word_ty[ val / 10 ] ); 76 val %= 10; 77 } 78 79 /* 80 ** 值在10到19之间,直接把对应的单词加到buffer中 81 */ 82 if( val >= 10 && val <= 19 ) 83 { 84 strcat( buffer, " " ); 85 strcat( buffer, word_10_19[ val % 10 ] ); 86 } 87 88 /* 89 ** 值在0到9之间,把对应的单词加到buffer中 90 */ 91 if( val >= 0 && val <= 9 ) 92 { 93 strcat( buffer, " " ); 94 strcat( buffer, word_1_9[ val ] ); 95 } 96 97 /* 98 ** 一个千位处理完后,后面加上对应的分位单词"THOUSAND"、"MILLION"等 99 */100 if( count > 0 )101 {102 strcat( buffer, " " );103 strcat( buffer, word_div[ count-- ] );104 strcat( buffer, " " );105 }106 }
以上版本处理个位是0的数字会多显示ZERO,下面这个版本进行改进:
1 /* 2 ** 把非负整数数值转化为单词形式 3 ** 2.0版本,改进: 4 ** 个位为 0 的数字多输出 ZERO 的问题 5 */ 6 7 #include <stdio.h> 8 #include <string.h> 9 10 void written_amount( unsigned int amount, char *buffer ); 11 12 int 13 main() 14 { 15 unsigned int num1, num2; 16 scanf( "%u%u", &num1, &num2 ); 17 char b1[100] = { 0 }; 18 char b2[100] = { 0 }; 19 20 written_amount( num1, b1 ); 21 22 puts( b1 ); 23 24 written_amount( num2, b2 ); 25 26 puts( b2 ); 27 28 return 0; 29 } 30 31 /* 32 ** 函数功能:把amount的值转化为单词形式,结果存储在buffer中 33 */ 34 void 35 written_amount( unsigned int amount, char *buffer ) 36 { 37 /* 38 ** 数字对应的单词,分为4组 39 */ 40 char word_1_9[10][6] = { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", 41 "SIX", "SEVEN", "EIGHT", "NINE" }; 42 char word_10_19[10][10] = {"TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", 43 "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN" }; 44 char word_ty[10][8] = { "", "", "TWENTY", "THIRTY", "FORTY", "FIFTY", 45 "SIXTY", "SEVENTY", "EIGHTY", "NINETY" }; 46 char word_div[4][9] = { "HUNDRED", "THOUSAND", "MILLION", "BILLION" }; 47 48 /* 49 ** 如果amount=0,则直接输出ZERO 50 */ 51 if( amount == 0 ) 52 strcat( buffer, word_1_9[ 0 ] ); 53 54 /* 55 ** amount不等于0,用下面的方法处理 56 */ 57 static int count = 0; // 记录千分位的个数 58 unsigned int tmp = amount / 1000; 59 60 /* 61 ** 用递归函数,直到数值< 1000 62 */ 63 if( tmp ) 64 { 65 count ++; // 千分位的个数增加 66 written_amount( tmp, buffer ); // 递归,处理下一个千分位 67 } 68 69 int val = amount % 1000; // 3位3位地进行转化单词 70 71 /* 72 ** 值大于100,先把百位上的数的单词存到buffer中 73 ** 后面加上 HUNDRED 74 ** 把值%100,变成两位数,后续继续处理 75 */ 76 if( val >= 100 ) 77 { 78 strcat( buffer, word_1_9[ val / 100 ] ); 79 strcat( buffer, " " ); 80 strcat( buffer, word_div[ 0 ] ); 81 val %= 100; 82 } 83 84 /* 85 ** 值在20至99之间,先把几十对应的单词加到buffer中 86 ** 再%10,后续处理个位上的数 87 */ 88 if( val >= 20 && val <= 99 ) 89 { 90 strcat( buffer, " " ); 91 strcat( buffer, word_ty[ val / 10 ] ); 92 val %= 10; 93 } 94 95 /* 96 ** 值在10到19之间,直接把对应的单词加到buffer中 97 */ 98 if( val >= 10 && val <= 19 ) 99 {100 strcat( buffer, " " );101 strcat( buffer, word_10_19[ val % 10 ] );102 }103 104 /*105 ** 值在0到9之间,把对应的单词加到buffer中106 */107 if( val > 0 && val <= 9 )108 {109 strcat( buffer, " " );110 strcat( buffer, word_1_9[ val ] );111 }112 113 /*114 ** 一个千位处理完后,后面加上对应的分位单词"THOUSAND"、"MILLION"等115 */116 if( count > 0 )117 {118 strcat( buffer, " " );119 strcat( buffer, word_div[ count-- ] );120 strcat( buffer, " " );121 }122 }
以上2.0版本,1000000 、1000000000 等数字多输出分位单词,以下3.0版本对此进行修正:
1 /* 2 ** 把非负整数数值转化为单词形式 3 ** 3.0版本,在2.0的基础上改进: 4 ** 1000000 多输出 THOUSAND 的问题 5 */ 6 7 #include <stdio.h> 8 #include <string.h> 9 10 void written_amount( unsigned int amount, char *buffer ); 11 12 int 13 main() 14 { 15 unsigned int num1, num2; 16 scanf( "%u%u", &num1, &num2 ); 17 char b1[100] = { 0 }; 18 char b2[100] = { 0 }; 19 20 written_amount( num1, b1 ); 21 22 puts( b1 ); 23 24 written_amount( num2, b2 ); 25 26 puts( b2 ); 27 28 return 0; 29 } 30 31 /* 32 ** 函数功能:把amount的值转化为单词形式,结果存储在buffer中 33 */ 34 void 35 written_amount( unsigned int amount, char *buffer ) 36 { 37 /* 38 ** 数字对应的单词,分为4组 39 */ 40 char word_1_9[10][6] = { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", 41 "SIX", "SEVEN", "EIGHT", "NINE" }; 42 char word_10_19[10][10] = {"TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN", 43 "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN" }; 44 char word_ty[10][8] = { "", "", "TWENTY", "THIRTY", "FORTY", "FIFTY", 45 "SIXTY", "SEVENTY", "EIGHTY", "NINETY" }; 46 char word_div[4][9] = { "HUNDRED", "THOUSAND", "MILLION", "BILLION" }; 47 48 /* 49 ** 如果amount=0,则直接输出ZERO 50 */ 51 if( amount == 0 ) 52 { 53 strcat( buffer, word_1_9[ 0 ] ); 54 return ; 55 } 56 57 /* 58 ** amount不等于0,用下面的方法处理 59 */ 60 static int count = 0; // 记录千分位的个数 61 unsigned int tmp = amount / 1000; 62 63 /* 64 ** 用递归函数,直到数值< 1000 65 */ 66 if( tmp ) 67 { 68 count ++; // 千分位的个数增加 69 written_amount( tmp, buffer ); // 递归,处理下一个千分位 70 } 71 72 int val = amount % 1000; // 3位3位地进行转化单词 73 74 if( val == 0 ) 75 { 76 count = 0; 77 return ; 78 } 79 /* 80 ** 值大于100,先把百位上的数的单词存到buffer中 81 ** 后面加上 HUNDRED 82 ** 把值%100,变成两位数,后续继续处理 83 */ 84 if( val >= 100 ) 85 { 86 strcat( buffer, word_1_9[ val / 100 ] ); 87 strcat( buffer, " " ); 88 strcat( buffer, word_div[ 0 ] ); 89 val %= 100; 90 } 91 92 /* 93 ** 值在20至99之间,先把几十对应的单词加到buffer中 94 ** 再%10,后续处理个位上的数 95 */ 96 if( val >= 20 && val <= 99 ) 97 { 98 strcat( buffer, " " ); 99 strcat( buffer, word_ty[ val / 10 ] );100 val %= 10;101 }102 103 /*104 ** 值在10到19之间,直接把对应的单词加到buffer中105 */106 if( val >= 10 && val <= 19 )107 {108 strcat( buffer, " " );109 strcat( buffer, word_10_19[ val % 10 ] );110 }111 112 /*113 ** 值在0到9之间,把对应的单词加到buffer中114 */115 if( val > 0 && val <= 9 )116 {117 strcat( buffer, " " );118 strcat( buffer, word_1_9[ val ] );119 }120 121 /*122 ** 一个千位处理完后,后面加上对应的分位单词"THOUSAND"、"MILLION"等123 */124 if( count > 0 )125 {126 strcat( buffer, " " );127 strcat( buffer, word_div[ count-- ] );128 strcat( buffer, " " );129 }130 }
把数字数值转换成单词形式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。