Skip to content

Instantly share code, notes, and snippets.

@Rexagon
Created September 7, 2017 12:55
Show Gist options
  • Save Rexagon/793c78ac17a88879ce35035c9edb4b44 to your computer and use it in GitHub Desktop.
Save Rexagon/793c78ac17a88879ce35035c9edb4b44 to your computer and use it in GitHub Desktop.
Prolog first lab
predicates
nondeterm solve(real, real, real)
nondeterm reply(real, real, real)
nondeterm print_complex(real, real)
nondeterm run
nondeterm do(char)
clauses
solve(A,B,C):-
A=0 AND B=0 AND C<>0,
write("NO solution"), nl.
solve(A,B,C):-
A=0 AND B=0 AND C=0,
write("x is any real number"), nl.
solve(A,B,C):-
A=0,
X=-C/B,write("x=",X),nl.
solve(A,B,C):-
D=B*B-4*A*C,
reply(A,B,D).
reply(A,B,D):-
D<0,
R=-B/(2*A),
I1=sqrt(abs(D))/(2*A),
I2=-sqrt(abs(D))/(2*A),
write("x1="),print_complex(R,I1),nl,
write("x2="),print_complex(R,I2),nl.
reply(A,B,D):-
D=0,
X=-B/(2*A), write("x=", X).
reply(A,B,D):-
X1=(-B+sqrt(D))/(2*A),
X2=(-B-sqrt(D))/(2*A),
write ("x1=",X1,",x2=", X2), nl.
print_complex(R,I):-
I<0,
write(R,I,"*i").
print_complex(R,I):-
I>=0,
write(R,"+",I,"*i").
run:-
write("Enter\n"),
write("-1 to solve quadratic equation\n"),
write(" ax^2+bx+c=0\n"),
write("-0 to EXIT\n"),
write("*************\n"),
write(">>"), readchar(X), write(X), nl,
do(X),
run.
do('1'):-
write("a="), readreal(A), A<>0, nl,
write("b="), readreal(B), nl,
write("c="), readreal(C), nl,
solve(A,B,C).
do('0'):-
write("That's all"), exit.
do(_):-
write("bad value"), nl.
goal run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment