生成普通话编号系统

时间:2015-03-16 04:27:50

标签: prolog

我正构建一个从0到10,000的Prolog程序。我的工作时间是0到99.我需要帮助100到1,000。编号系统可以在http://mandarin.about.com/od/vocabulary/a/numbers.htm找到

/* líng */
mandarin(A, 0,[number,zero,nonfirst]):- name(A,[108,236,110,103]).

/* yī */
mandarin(A, 1, [number,unit,first]):- name(A,[121,299]).

/* èr */
mandarin(A, 2, [number,unit,nonfirst]):- name(A,[232,114]).

/* sān */
mandarin(A, 3, [number,unit,nonfirst]):- name(A,[115,257,110]).

/* sì */
mandarin(A, 4, [number,unit,nonfirst]):- name(A,[115,236]).

/* wǔ */
mandarin(A, 5, [number,unit,nonfirst]):- name(A,[119,468]).

/* liù */
mandarin(A, 6, [number,unit,nonfirst]):- name(A,[108,105,249]).

/* qī */
mandarin(A, 7, [number,unit,nonfirst]):- name(A,[113,299]).

/* bā */
mandarin(A, 8, [number,unit,nonfirst]):- name(A,[98,257]).

/* jiǔ */
mandarin(A, 9, [number,unit,nonfirst]):- name(A,[106,105,468]).

/* shí */
mandarin(A, 10, [number,ten,first]):- name(A,[115,104,237]).

/* bǎi */
mandarin(A, 100, [number,hundred,first]):- name(A,[121,236,32,98,462,237]).

/* yì qiān */

mandarin(A, 1000, [number,thousand,first]):- name(A,[121,236,32,113,105,257,110]).

/* yì wàn */

mandarin(A, 10000, [number,thousand,first]):- name(A,[121,236,32,119,224,110]).

下面的函数以A = lìng, B = 0 A = yī, B = 1的形式成功输出0到9之间的普通话数字:

mandarin(A, B, []):- mandarin(A,B,[number, zero, _]); mandarin(A, B, [number,unit,_]).

以下功能以A = [shí] B = 10 A = [shí, yī], B = 11的形式成功输出10 - 99的普通话数字:

mandarin(A, B, [number]):-
    mandarin(C, D, [_,ten,_]),
    write('A = ['), write(C), write(']'), nl, 
    write('B = '), write(D),
    mandarin(E, F, [_,unit,_]),
    append([C], [E], J),
    B is D + F,
    B < 100,
    flatten(J,A).

我需要帮助定义一个从100 - 1,000开始的函数。

到目前为止,我有:

mandarin(A, B, [number]):-
    mandarin(X, Y, [_,hundred,_]),
    write('A = ['), write(X), write(']'),
    nl, write('B = '), write(Y),
    mandarin(C, D, [_,zero,_]),
    mandarin(E, F, [_,unit,_]),
    append([X], [C], U), append([U], [E], J),
    B is Y + D + F,
    B >= 100,
    flatten(J,A).

哪个错误输出:

A = [yì bǎí]
B = 100
A = ['yì bǎí', lìng, yī],
B = 101 
A = ['yì bǎí', lìng, èr],
B = 102 
A = ['yì bǎí', lìng, sān],
B = 103 
A = ['yì bǎí', lìng, sì],
B = 104 
A = ['yì bǎí', lìng, wǔ],
B = 105 
A = ['yì bǎí', lìng, liù],
B = 106 
A = ['yì bǎí', lìng, qī],
B = 107 
A = ['yì bǎí', lìng, bā],
B = 108 
A = ['yì bǎí', lìng, jiǔ],
B = 109 
A = [[èr,yì bǎí]]
B = 200
A = [èr, 'yì bǎí', lìng, yī],
B = 201 
...

错误,因为它会跳过数字110 - 199,210 - 299等。

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

我不明白为什么你会使用如此复杂的架构,所以我已经修改了事实&#39;

mandarin(líng,  0, [number,unit,nonfirst]).
mandarin(yī,    1, [number,unit,first]).
mandarin(èr,    2, [number,unit,nonfirst]).
mandarin(sān,   3, [number,unit,nonfirst]).
mandarin(sì,    4, [number,unit,nonfirst]).
mandarin(wǔ,    5, [number,unit,nonfirst]).
mandarin(liù,   6, [number,unit,nonfirst]).
mandarin(qī,    7, [number,unit,nonfirst]).
mandarin(bā ,   8, [number,unit,nonfirst]).
mandarin(jiǔ,   9, [number,unit,nonfirst]).
mandarin(shí,   10, [number,ten,first]).
mandarin('yì bǎí',  100, [number,hundred,first]).

mandarin(B, [number]):-
    mandarin(_, H, [_,hundred,_]),
    mandarin(_, D, [_,ten,_]),
    mandarin(_, H1, [_,unit,_]),
    mandarin(_, D1, [_,unit,_]),
    mandarin(_, F1, [_,unit,_]),
    B is H*H1 + D*D1 + F1, B>100.

相关更改为0,我已将其标记为单位。现在,B在十进制系统中照常计算。