c - 在 C 中将数字打印为英语单词

标签 c numbers words

<分区>

我有这段代码可以用英文单词打印从 0 到 9 的数字(比如一个代表 1,两个代表 2,等等)。如果我想打印 374 怎么办?或者更大的东西,比如 7549846451?

#include <stdio.h>
    int main() 
    {
        double sum;
        if(scanf("%1f",&num)!=0)
        {
            if(num=(int)num)
            {
                switch((int)sum)
                {
                    case 0:printf("zero\n");break;
                    case 1:printf("one\n");break;
                    case 2:printf("two\n");break;
                    case 3:printf("three\n");break;
                    case 4:printf("four\n");break;
                    case 5:printf("five\n");break;
                    case 6:printf("six\n");break;
                    case 7:printf("seven\n");break;
                    case 8:printf("eight\n");break;
                    case 9:printf("nine\n");break;
                    default:printf("not a digit"); break;
                }
            }else
            {
                printf("Invalid")
                    return 0;
            }
        }
        return 0;
    }

最佳答案

这是一个好的开始,但要完成您的程序还需要更多的时间:

  • 首先将您的代码扩展为打印数字 10..99。 11..19 会有一个特殊情况,但之后就很正常了。较低的 20 个可以使用查找表进行寻址。事实上,为整个范围制作一个查找表也不会太糟糕。
  • 通过手头写出数字 0..99 的例程,您可以通过查看右边的第三位数字、写出它、添加 “hundred”,然后继续编写,将其扩展为数百输出数字 0..99
  • 现在您已经有了一个写出三位数的例程,您需要做的就是将您的数字分成几组树,为非零组调用此例程,然后添加 "billion" , "million", "thousand"对应组的排名。

关于c - 在 C 中将数字打印为英语单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37993572/

相关文章:

c - Visual Studio 似乎将 header 编译为 .c 文件

c - C 语言的维吉尼亚密码 (CS50)

perl - 在perl中四舍五入小数,结果错误

javascript 输入字段到数字

c++ - 可以将 std::numeric_limits<T> 专门用于用户定义的类数类吗?

用于生成具有非重复字符的单词列表的 Shell 脚本

c - 将内部变量序列化到 i2c

c++ - C 或 C++ 中的链表

javascript - 无法让我的单词随机生成器脚本正确循环

c - 寻找独特的角色