获取数字并输出其英文单词的算法
我想用 C 语言编写一个程序,要求用户输入一个数字,然后用英文打印该数字。
例如:
if(INPUT == 1) then print ONE
if(INPUT == 2) then print TWO
等等。它可以使用 switch-case 和 if else 来制作,但它会使代码变得冗长。对于少数数字来说还可以,但如果我们必须写到 100 个数字,那就会很长了。
有没有一个简短的算法或想法?
I want to make a program in C which will ask the user to input a number and then it will print that number in English.
For example:
if(INPUT == 1) then print ONE
if(INPUT == 2) then print TWO
and so on. It can be made using switch-case and if else but it makes the code lengthy. For few numbers it's fine but if we have to write up to 100 then it will be lengthy.
Is there a short algorithm or idea for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以使用下面的内容,但这只能打印数千个。我这样做是为了解决一些特定的编程问题。这就是为什么我没有扩展到数千以上。但扩展到更大的数量并不难。另外,这个程序还可以优化或者变得更清晰。
You can use the below, but this prints only upto thousands. I did this to solve some particular programming problem. Thats why i did not extend beyond thousands. But its not hard to extend for bigger number. Also, this program can be still optimized or made more clearer.
您可以使用它,它可用于将最多前 99 个整数转换为单词。它有点简单。看看:
希望这有帮助。
You can use this it can be used to convert upto first 99 integers to words. and its a bit simple. have a look:
Hope this helps.
只需使用递归即可。我没有足够的时间来测试它,因此这段代码可能有错误,但您可以轻松扩展它。
Just use recursion . I dont have enough time to test it, so this code might be buggy, but you can easily extend it.
你需要的是一个递归函数,它在个位、十位、百位和千位之后调用自身。
例如。
What you need is a recursive function which calls itself after ones,tens,hundreth and thousand digits.
For eg.
如果人类还没有实施数千次(抱歉:1000 次),我会感到惊讶。
所以我检查了 Github,发现了至少一些。
我没有贡献任何一个。
I would be surprised if the humanity didn't implement it a thousands (sorry: 1000) already.
So I checked on Github and a found at least a few.
I haven't contributed any of them.
我很难想出一个好的方法来自动化这个过程并且仍然使它简短。如果你知道终点(即你想要从 1 到 100),那么你可以这样做:
然后当你收到输入时,只需使用该数字来访问该数组索引,它就会吐出你的答案:
如果我的语法错误,我深表歉意,我已经使用 PHP 和 javaScript 这么久了,现在我不太记得 C 语法了......
I'm having trouble thinking of a good way to automate this and still make it short. If you know the end point (i.e you want to go 1-100), then you could do something like this:
And then when you receive input, simply use the number to access that array index, and it will spit out your answer:
I apologize if my syntax is wrong, I've been doing PHP and javaScript for so long now I don't remember C syntax all that well...
这是我写的,很容易扩展到任何大小。我还没有清理一些我可以清理的东西,但逻辑工作得很好
import java.util.Arrays;
导入java.util.Scanner;
公共类 NumReader {
}
This is what I wrote, this is very easily extensible to any size. I've not cleaned up some things which I could but the logic works very fine
import java.util.Arrays;
import java.util.Scanner;
public class NumReader {
}
我想改进 kayan 的答案。有两个错误:
像这样修改代码将解决这些错误:
I want to improve kalyan answers. There are 2 errors:
Modify code like this will resolve those errors:
这是另一种方法。不确定效率与内存与速度的优点,但很容易添加代码来处理更多数字。
示例:
Here's another way. Not sure of the merits efficiency vs. memory vs. speed, but It is easy to add code to handle more digits.
Examples: