Proper handling of multiplication
This commit is contained in:
parent
1232fa0b3c
commit
9a181a076a
39
polymani.pl
39
polymani.pl
@ -82,8 +82,8 @@ polyplay :-
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
;
|
;
|
||||||
%% Parsing failed
|
%% Parsing failed
|
||||||
writeln("I didn't understand what you want.")
|
writeln("Could not parse input, so I didn't understand what you want.")
|
||||||
),
|
),
|
||||||
%% Go back to the beginning
|
%% Go back to the beginning
|
||||||
polyplay
|
polyplay
|
||||||
@ -247,13 +247,8 @@ do_process_input(simplify(PT)) :-
|
|||||||
do_process_input(store_multiplication(TN, PT, V)) :-
|
do_process_input(store_multiplication(TN, PT, V)) :-
|
||||||
polynomial_tree_to_polynomial(TN, N),
|
polynomial_tree_to_polynomial(TN, N),
|
||||||
polynomial_tree_to_polynomial(PT, P),
|
polynomial_tree_to_polynomial(PT, P),
|
||||||
(
|
%% Left is always number. Enforced by parser
|
||||||
not(number(N)),
|
scalepoly(P, N, P2),
|
||||||
scalepoly(N, P, P2)
|
|
||||||
;
|
|
||||||
number(N),
|
|
||||||
scalepoly(P, N, P2)
|
|
||||||
),
|
|
||||||
simpoly(P2, SP),
|
simpoly(P2, SP),
|
||||||
assertz(polynomial_store(V, SP)),
|
assertz(polynomial_store(V, SP)),
|
||||||
write(V),
|
write(V),
|
||||||
@ -265,13 +260,8 @@ do_process_input(multiply(TN, PT)) :-
|
|||||||
polynomial_tree_to_polynomial(TN, N),
|
polynomial_tree_to_polynomial(TN, N),
|
||||||
polynomial_tree_to_polynomial(PT, P),
|
polynomial_tree_to_polynomial(PT, P),
|
||||||
%% Use function from UI layer
|
%% Use function from UI layer
|
||||||
(
|
%% Left is always the number. Enforced by parser
|
||||||
not(number(N)),
|
scalepoly(N, P, P2),
|
||||||
scalepoly(N, P, P2)
|
|
||||||
;
|
|
||||||
number(N),
|
|
||||||
scalepoly(P, N, P2)
|
|
||||||
),
|
|
||||||
simpoly(P2, SP),
|
simpoly(P2, SP),
|
||||||
writeln(SP).
|
writeln(SP).
|
||||||
do_process_input(op(+, TN, PT)) :-
|
do_process_input(op(+, TN, PT)) :-
|
||||||
@ -671,7 +661,6 @@ parse_polynomial(T) -->
|
|||||||
%% ?- parse_polynomial(T, [two, plus, x, times, four, plus, y, raised, to, five], _).
|
%% ?- parse_polynomial(T, [two, plus, x, times, four, plus, y, raised, to, five], _).
|
||||||
%@ T = op(+, 2, op(+, op(*, x, 4), op(^, y, 5))).
|
%@ T = op(+, 2, op(+, op(*, x, 4), op(^, y, 5))).
|
||||||
%% ?- parse_polynomial(T, [two, plus, two, plus, one, times, y], _).
|
%% ?- parse_polynomial(T, [two, plus, two, plus, one, times, y], _).
|
||||||
%@ true.
|
|
||||||
%@ T = op(+, op(+, 2, 2), op(*, 1, y)).
|
%@ T = op(+, op(+, 2, 2), op(*, 1, y)).
|
||||||
%% ?- parse_polynomial(T, [polynomial, 2, plus, 3, plus, 4, y], NC).
|
%% ?- parse_polynomial(T, [polynomial, 2, plus, 3, plus, 4, y], NC).
|
||||||
%@ T = op(+, op(+, 2, 3), op(*, 4, y)),
|
%@ T = op(+, op(+, 2, 3), op(*, 4, y)),
|
||||||
@ -782,16 +771,28 @@ parse_command(simplify(T)) -->
|
|||||||
parse_polynomial(T).
|
parse_polynomial(T).
|
||||||
parse_command(store_multiplication(TN, TP, V)) -->
|
parse_command(store_multiplication(TN, TP, V)) -->
|
||||||
[multiply],
|
[multiply],
|
||||||
parse_polynomial(TN),
|
parse_floating_number(TN),
|
||||||
[by],
|
[by],
|
||||||
parse_polynomial(TP),
|
parse_polynomial(TP),
|
||||||
[as],
|
[as],
|
||||||
[V].
|
[V].
|
||||||
|
parse_command(store_multiplication(TN, TP, V)) -->
|
||||||
|
[multiply],
|
||||||
|
parse_polynomial(TP),
|
||||||
|
[by],
|
||||||
|
parse_floating_number(TN),
|
||||||
|
[as],
|
||||||
|
[V].
|
||||||
parse_command(multiply(TN, TP)) -->
|
parse_command(multiply(TN, TP)) -->
|
||||||
[multiply],
|
[multiply],
|
||||||
parse_polynomial(TN),
|
parse_floating_number(TN),
|
||||||
[by],
|
[by],
|
||||||
parse_polynomial(TP).
|
parse_polynomial(TP).
|
||||||
|
parse_command(multiply(TN, TP)) -->
|
||||||
|
[multiply],
|
||||||
|
parse_polynomial(TP),
|
||||||
|
[by],
|
||||||
|
parse_floating_number(TN).
|
||||||
parse_command(op(-, TN, TP)) -->
|
parse_command(op(-, TN, TP)) -->
|
||||||
[sub],
|
[sub],
|
||||||
parse_polynomial(TN),
|
parse_polynomial(TN),
|
||||||
|
Reference in New Issue
Block a user