Created
April 21, 2022 13:46
-
-
Save zombie110year/81442f63274fa00ad3f7bf17d16dcf16 to your computer and use it in GitHub Desktop.
convert number to its english presentation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| typedef unsigned long number_t; | |
| const char *SMALL_NUMBERS[] = {"zero", "one", "two", "three", | |
| "four", "five", "six", "seven", | |
| "eight", "nine", "ten", "eleven", | |
| "twelve", "thirteen", "fourteen", "fifteen", | |
| "sixteen", "seventeen", "eighteen", "nineteen"}; | |
| const char *TEN_POWERS[] = {"", "", "twenty", "thirty", "forty", | |
| "fifty", "sixty", "seventy", "eighty", "ninety"}; | |
| // 可以往上加 trillion 等,同时 SCALE 也要再乘 1000 | |
| const char *THOUSAND_POWERS[] = {"billion", "million", "thousand", ""}; | |
| // 默认十亿 (1 billion) | |
| const number_t SCALE = 1000 * 1000 * 1000; | |
| /// 将正整数转换成其英文文本表示 | |
| /// | |
| /// @param str 用于存放结果的字符串,需要提前准备好足够的空间 | |
| /// @param n $n \in N_+$ | |
| void numtext(char *str, number_t n); | |
| /// @param n 0 < n <= 1000 | |
| void spell_hundreds(char *str, number_t n); | |
| void spell_hundreds(char *str, number_t n) { | |
| if (n == 1000) { | |
| strcat(str, "one thousand"); | |
| return; | |
| } | |
| if (n >= 100) { | |
| strcat(str, SMALL_NUMBERS[n / 100]); | |
| strcat(str, " hundred"); | |
| n %= 100; | |
| if (n > 0) { | |
| strcat(str, " and "); | |
| } | |
| } | |
| if (n >= 20) { | |
| strcat(str, TEN_POWERS[n / 10]); | |
| n %= 10; | |
| if (n > 0) { | |
| strcat(str, " "); | |
| } | |
| } | |
| if (0 < n && n < 20) { | |
| strcat(str, SMALL_NUMBERS[n]); | |
| } | |
| } | |
| void numtext(char *str, number_t n) { | |
| if (n < 20) { | |
| strcat(str, SMALL_NUMBERS[n]); | |
| return; | |
| } | |
| const char **scale_level = THOUSAND_POWERS; | |
| number_t scale = SCALE; | |
| while (scale > 0) { | |
| if (n >= scale) { | |
| number_t h = n / scale; | |
| spell_hundreds(str, h); | |
| strcat(str, " "); | |
| strcat(str, *scale_level); | |
| n %= scale; | |
| if (n > 0) { | |
| strcat(str, " and "); | |
| } | |
| } | |
| scale /= 1000; | |
| scale_level += 1; | |
| } | |
| } | |
| void display(number_t n) { | |
| char *str = (char *)calloc(4096, sizeof(char)); | |
| numtext(str, n); | |
| printf("%s\n", str); | |
| } | |
| int main() { | |
| display(0); | |
| display(1); | |
| display(10); | |
| display(19); | |
| display(20); | |
| display(99); | |
| display(109); | |
| display(999); | |
| display(1000); | |
| display(100032); | |
| display(999999); | |
| display(1000001); | |
| display(9999999); | |
| display(1000000000); | |
| display(1000000001); | |
| display(9999999999); | |
| display(1000000000000); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment