如何编写一个程序,生成一个具有给定数字的算术意义的操作符号列表?

时间:2016-12-17 13:52:57

标签: prolog

我需要编写一个获取参数的程序:

  • list_of_numbers
  • result_number
  • Result_list(用于生成标志列表)。

并且正在生成一个操作单+-的列表,这些操作在算术意义上的结果为result_number。此外,它会对数字进行连接以形成新数字,因此算术意义是正确的。

因此,例如,如果我们有一个方法arrange_signs(list_of_numbers, result_number, Result_List),那么它是如何工作的:

?- arrange_signs([1, 2, 3, 4, 5, 6, 7, 8, 9], 4, Result_List).
12-3+45-67+8-9 = 4

?- arrange_signs([1, 2, 3, 4, 5, 6, 7, 8, 9], 15, Result_List).
1+2-34+56+7-8-9 = 15

如何编写正在执行此操作的程序?

我写了一个这样做的程序:

?- arrange_signs([12, 3, 45, 67, 8, 9], 4, Result_List).
12-3+45-67+8-9 = 4

?- arrange_signs([1, 2, 34, 56, 7, 8, 9], 15, Result_List).
1+2-34+56+7-8-9 = 15

但我不确定如何编写与[1, 2, 3, 4, 5, 6, 7, 8, 9]一起使用的程序。

这是我的代码:

arrange_signs([Number|Number_List],Result,Result_List) :- 
    generateOperationList(Number_List, [], OperationList),
    find_result(Number,Number_List,OperationList,Result), 
    getResult([Number|Number_List], OperationList, Result_List), 
    show_result(Result_List, Result). 

generateOperationList([_Head|Tail], Temp_List, [First_Operation|OperationList]) :- 
    getSign(First_Operation),
    generateOperationList(Tail, Temp_List, OperationList). 

generateOperationList([],Temp_List,Temp_List).

getSign('-').
getSign('+').

getOperation(Number1, '-', Number2, Answer) :- 
    Answer is Number1 - Number2.

getOperation(Number1, '+', Number2, Answer) :- 
    Answer is Number1 + Number2.

getResult([Number|Number_List], [Operation|OperationList], [Number,Operation|Result_List]) :- 
    getResult(Number_List, OperationList, Result_List).

getResult(Number_List, [], Number_List).

find_result(Temp_Answer,[Number|Number_List],[Operation|OperationList],Result) :- 
    getOperation(Temp_Answer, Operation, Number, New_Temp_Result),
    find_result(New_Temp_Result,Number_List,OperationList,Result).

find_result(New_Temp_Result,[],[],New_Temp_Result). 

print_result(Ready_Result) :- 
    write(Ready_Result).

show_result([First_Element|Result_List],Result) :- 
    generate_result([First_Element|Result_List],Result,'',Ready_Result),
    print_result(Ready_Result).

generate_result([First_Element|Result_List],Result,Formatting_Result,Ready_Result) :- 
    atom_concat(Formatting_Result, First_Element, New_Formatting_Result), 
    generate_result(Result_List,Result,New_Formatting_Result,Ready_Result).

generate_result([],Result,Formatting_Result,Ready_Result) :- 
    atom_concat(Formatting_Result, '=', Temp_Variable),
    atom_concat(Temp_Variable, Result, New_Formatting_Result), 
    generate_result(New_Formatting_Result,Ready_Result).

generate_result(New_Formatting_Result,New_Formatting_Result).

1 个答案:

答案 0 :(得分:1)

很抱歉,但我发现您的代码很难理解。

我提出以下解决方案

atomL_concat([], '').

atomL_concat([A | T], C1) :-
  atomL_concat(T, C0),
  atom_concat(A, C0, C1).

arrangeS([], Target, Target, [' = ', ATarget]) :-
  number_atom(Target, ATarget).

arrangeS([NH | NT], Target, Sum0, ['+', ANH | ST]) :-
  Sum1 is Sum0 + NH,
  arrangeS(NT, Target, Sum1, ST),
  number_atom(NH, ANH).

arrangeS([NH | NT], Target, Sum0, ['-', ANH | ST]) :-
  Sum1 is Sum0 - NH,
  arrangeS(NT, Target, Sum1, ST),
  number_atom(NH, ANH).

arrangeS([NH1, NH2 | NT], Target, Sum, ResList) :-
  NH is NH1 * 10 + NH2,
  arrangeS([NH | NT], Target, Sum, ResList).

arrange_signs(NumList, Target, ResList) :- 
  arrangeS(NumList, Target, 0, ['+'|ResList]),
  atomL_concat(ResList, PrintList),
  write(PrintList), nl.

如果你想接受以负数开头的解决方案(例如:" -12-3-4 + 5-6 + 7 + 8 + 9 = 4")你可以删除第一个{{ 1}}删除并将+写为

arrange_signs/2

但是,在这种情况下,以正数开头的解以arrange_signs(NumList, Target, ResList) :- arrangeS(NumList, Target, 0, ResList), atomL_concat(ResList, PrintList), write(PrintList), nl. 符号开头(所以" + 1 + 2-34 + 5 + 6 + 7 + 8 + 9 = 4& #34;而不是" 1 + 2-34 + 5 + 6 + 7 + 8 + 9 = 4")。