# Simplify a 2nd degree equation in Prolog

Page 1 of 1

## 1 Replies - 417 Views - Last Post: 07 June 2019 - 06:29 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=416394&amp;s=79e81dc414b886e7e4942f123a24e026&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ran13ia

Reputation: 0
• 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 /> can anyone help ?
Code :
```go:-    write('Donnez la valeur de A ?'),
write('Donnez la valeur de B ?'),
write('Donnez la valeur de C ?'),
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

• Duke of Err

Reputation: 1862
• Posts: 6,711
• 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.