1 Replies - 293 Views - Last Post: 07 June 2019 - 06:29 PM Rate Topic: -----

#1 ran13ia   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-June 19

Simplify a 2nd degree equation in Prolog

Posted 06 June 2019 - 05:18 PM

Hi, I have a homework in Prolog to simplify a 2nd degree equation for example: a=1, b=-1, c=0, we should have: x²-x and not: x²-x+0 So I have to write this expert system in Prolog. I already did it but my code doesn't work :no:/> can anyone help ?
Code :
go:-    write('Donnez la valeur de A ?'),
        read(A),nl,
        write('Donnez la valeur de B ?'),
        read(B)/>/>,nl,
        write('Donnez la valeur de C ?'),
        read(C),nl,
        write('Le trinome s écrit : '),
        action(A),action(B,A),action(C,B,A).
        
action(A):- A=0,Sg2=' ',Coef2=' ',Var2=' ',Expo2=' ';
            A=1,Sg2=' ',Coef2=' ',Var2='x',Expo2=2;
            A>1,Sg2=' ',Coef2=A,Var2='x',Expo2=2;
            A=(-1),Sg2='-',Coef2='',Var2='x',Expo2=2;
            A<(-1),Sg2='-',Coef2=abs(A),Var2='x',Expo2=2.
action(B,A):- B=0,Sg1=' ',Coef1=' ',Var1=' ';
              B=1,A=0->Sg1=' ',Coef1=' ',Var1='x';
              B=1,A\=0->Sg1='+',Coef1=' ',Var1='x';
              B>1,A=0->Sg1=' ',Coef1=B,Var1='x';
              B>1,A\=0->Sg1='+',Coef1=B,Var1='x';
              B=(-1),Sg1='-',Coef1=' ',Var1='x';
              B<(-1),Sg1='-',Coef1=abs(B)/>/>,Var1='x'.
action(C,B,A):- C=0,abs(A)+abs(B)/>/>=0->Sg0='',Coef=0;
                C=0,abs(A)+abs(B)/>/>\=0->Sg0='',Coef='';
                C>0,abs(A)+abs(B)/>/>=0->Sg0='',Coef0=C;
                C>0,abs(A)+abs(B)/>/>\=0->Sg0='+',Coef0=C;
                C<0,Sg0='-',Coef0=abs(C).


Is This A Good Question/Topic? 0
  • +

Replies To: Simplify a 2nd degree equation in Prolog

#2 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1860
  • View blog
  • Posts: 6,698
  • Joined: 19-February 09

Re: Simplify a 2nd degree equation in Prolog

Posted 07 June 2019 - 06:29 PM

Are the action rules written correctly?

You could have a rule for each part of the equation - the signs, the coefficients and the variables.

A rule for the sign could be passed a coefficient. If the coefficient is zero nothing is printed, and if the coefficient is positive a plus sign is printed, if the coefficient is negative a minus sign is printed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1