如何检查序言中每个数字的位置值?
toword(0,'zero').
toword(1,'one').
toword(10,'ten').
toword(15,'fifteen').
toword(30,'thirty').
toword(100,'one hundred').
toword(500,'five hundred').
toword(2000,'two thousand).
pv(X,[]).
//pv(X , [H|T) :- toword(X/10, T), pv(??? I know this is wrong.
我试图获取每个位置的值并将其放入列表中。例如; X = 2531 列表将是 [2000,500,30,1]。我没有列出所有的 toword() 事实,但还会有更多。我不知道如何获得该数字的每个位值。
toword(0,'zero').
toword(1,'one').
toword(10,'ten').
toword(15,'fifteen').
toword(30,'thirty').
toword(100,'one hundred').
toword(500,'five hundred').
toword(2000,'two thousand).
pv(X,[]).
//pv(X , [H|T) :- toword(X/10, T), pv(??? I know this is wrong.
I am trying to get each place value and put it into a list. For example; X = 2531 the list would be [two thousand, five hundred, thirty, one]. I didn't put all of the toword() facts, but there would be more. I don't know how I am supposed to get each place value of the number.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将整数转换为其英语口语形式是一个比您想象的更困难的问题。
您需要做的第一件事是将您的值划分为 3 位数字组的列表(其正确名称是句点):
这将变成一个整数,例如
1234567890
像这样的东西会根据组相对于列表右侧的位置为您提供正确的后缀:
唯一真正棘手的部分是弄清楚如何处理第一个/最右边的这样的句点 (数百)。 0-99 之间的数字是有问题的:
二十< /code>、
三十
等),附加一个-
,然后在个位附加数字名称。这些是特殊数字:
以下是如何构造 0 到 999 之间的任何数字的名称:
一旦有了这个名称,其他一切就很容易了。这只是跟踪您当前正在处理的时间段并附加正确的后缀的问题
您可以在 https://swish.swi-prolog.org/p/int-words.pl
Converting an integer into its English language spoken form is rather a more difficult problem than you might think.
You first thing you need to do is partition your value into a list of 3-digit groups (the proper name for which is period):
That will turn an integer like
1234567890
intoSomething like this will give you the correct suffix based on the groups position with respect to the right of the list:
The only real tricky bit is figuring out how to handle the 1st/rightmost such period (hundreds). The numbers from 0-99 are problematic:
twenty
,thirty
, etc.), appending a-
and then appending the name of the digit in the ones place.These are the special numbers:
Here's how to construct the names of any number from 0-999:
Once you have that, everything else is easy. It's just a matter of tracking which period you're currently processing and appending the correct suffix
You can fiddle with it at https://swish.swi-prolog.org/p/int-words.pl
swi-prolog 中的结果:
Result in swi-prolog: