Solved Why is this way of adding not working?

40 Views Asked by At

I'm making a calculator as a school project.
It works by solving each 2 members of an operation ;
Concretely, if I type "2+3-5+4-2+6-7+1", it calculates "(2+3)-(5+4)-(2+6)-(7+1)"
Why is it wrong ?

1

There are 1 best solutions below

2
On BEST ANSWER

The error is the distributive law, for example consider $-7+1$: your program will display $-(7+1)=-8$, but this is incorrect. Instead, if you use the distributive law, you have: $$-7+1=-(7-1)=-6$$