Hi, I need to build this program to read from a file sentences like "three plus zero." (all of them 3 words and just a simple operation) and return the result of the arithmetic operation. The solution I thought of was to use read_atom to store each word in X,Y,Z atoms. then make a list of them; so [X,Y,Z] is [three,plus,two], reverse order to [plus,three,two] and then using =.. convert that list to plus(three,two). I have defined (for example) three as number(three):- X = 3. and plus function as plus(number(X),number(Y)):- Z is X + Y. So now i have a file filled with functions such as plus(three,two). My question is how do I actually make the arithmetic process now? I'm totally stuck. any help would be much appreciated.
here is the code:
num(four):- X = 4.
num(five):- X = 5.
num(six):- X = 6.
num(seven):- X = 7.
num(eight):- X = 8.
num(nine):- X = 9.
num(ten):- X = 10.
plus(num(X),num(Y)):-
Z is X + Y.
minus(num(X),num(Y)):-
Z is X - Y.
times(num(X),num(Y)):-
Z is X * Y.
div(num(X),num(Y)):-
Z is X / Y.
my_word_calculator(In,Out):-
see(In),
tell(Out),
calculate,
told,
seen.
calculate:-
read_atom(X),
read_atom(Y),
read_atom(Z),
L =.. [Y,X,Z],
write(L).
here is the code:
num(four):- X = 4.
num(five):- X = 5.
num(six):- X = 6.
num(seven):- X = 7.
num(eight):- X = 8.
num(nine):- X = 9.
num(ten):- X = 10.
plus(num(X),num(Y)):-
Z is X + Y.
minus(num(X),num(Y)):-
Z is X - Y.
times(num(X),num(Y)):-
Z is X * Y.
div(num(X),num(Y)):-
Z is X / Y.
my_word_calculator(In,Out):-
see(In),
tell(Out),
calculate,
told,
seen.
calculate:-
read_atom(X),
read_atom(Y),
read_atom(Z),
L =.. [Y,X,Z],
write(L).