1

So the code is rather complicated so ill try to do some neat pseudo code that covers the most important issues. I'm trying to parse a math expression. For example: 1-5*(-2)+3 = 14

The syntax that im using is:

expression = term   OR term+expression OR term-expression
term       = factor OR factor*term     OR factor/term
factor     = number OR -factor         OR (expression)

I have written a piece of code which checks if an expression follows this syntax and it works well for checking the expressions but not for calculating it.

The pseudo code goes something like:

double readExpression()
    number = readTerm()
    if token == +
        number2 = readExpression()
        return number + number2
    else if token == -
        number2 = readExpression()
        return number - number2
    else
        return number
...
(The code for readTerm() is identical to readExpression() in structure)
...
double readFactor()
    if token == number
        return number
    else if token == -
        number = readFactor()
        return (-1)*number
    else if token == (
        number = readExpression()
        return number
    else raise exception

If I do the above calculation with this code it will give me a tree that looks like this:

So anyway, as you matematicians have figured out byt now, the expression should give 14 and not 8 as the tree suggests. I have noticed the that the problem arises when there are minus-signs in front of expressions since affect the whole right term i this problem whilst they should only affect the middle-term.

Ive been thinking like crazy for weeks and thought about solutions for this and looked at other codes and so on. Please dont toss a bunch of links on me if they are not really really simple and good since ive been browsing alot myself on tree traversals and other relevant topics.

What could i do at this stage? As I said, my program can tell if its right or wrong. So now I only need to parse a correct expression. Should I write another class for the parsing of the correct expression? Is it easier? Anyway I dont see how that code would look different than this.

1 Answers1

0

Yes I would parse the equation, it just looks like you miss a key part of the order of operations/parsing. You need to include an additional check for double negatives.

The key factor here is that: In a situation with two identical operators then the left most operation is always carried out first.

First lets narrow down the issue.

This 1-5*(-2)+3 is equal to 1--10+3.

Now for our purposes lets assign a positive to the first operator because it helps illustrate a point:

1--10+3 is the same as +1--10+3

Now if we where to run +1--10+3 through a correct parser we would know that this -- is equal to + but only when used in the following situation:

+X--Y = X+Y

So now our parser has turned the original expression of 1--10+3 into 1+10+3 and we know that is equal to 14.

So all up: Yes you need a parser, but pay special attention to how +X--Y and X+Y work.

Also take a look at this answer: https://stackoverflow.com/a/26227947/1270000

Community
  • 1
  • 1
sorifiend
  • 5,927
  • 1
  • 28
  • 45
  • 1
    Its actually not about double negatives. It can handle those. Also 2-3*2+3 gets calculated like 3*2=6 then 6+3=9 then 2-9=-7 which is wrong. Its something with the syntax....it cant see that it should add from left to right or something like that. – Peter Kanerva Jun 16 '16 at 01:46
  • I looked at the link you provided, and I cant see any logical differences in that code compared to my pseudo code atleast ; / so i dont know – Peter Kanerva Jun 16 '16 at 01:53
  • Also, that code only evaluates, mine does that too. I also want to calculate the expression. – Peter Kanerva Jun 16 '16 at 01:54
  • Well if that's not the issue then there is only one thing that it can be, and that is the order of your operations: For example, this should happen first `+1--10` but your code is clearly ignoring the left to right rule and is doing `-10 + 3` instead. It may just be a simple matter of your code not backing up and doing the left to right operation that we expect. It may be worth pasting some more code, because I think you will find that its something simple that you will kick yourself for in hindsight. – sorifiend Jun 16 '16 at 01:57
  • To add to my last comment: how and where do you determine the order of each operation? It should always be done before each individual calculation, rather than a single assessment at the beginning. – sorifiend Jun 16 '16 at 01:59
  • I think I just solved this piece of shit exercies : DDD !! I had to put in a little tweaker function in between if the term is negative! – Peter Kanerva Jun 16 '16 at 03:05
  • I think Ill upload my code after Ive cleaned it a bit, Id be happy if some of you could testrun it maybe for some expressions to see if its totally water proof : D ? – Peter Kanerva Jun 16 '16 at 03:06
  • I cant believe it that the code got so complicated, they shouldnt give those kinds of assignments with so weak instructions I think xo – Peter Kanerva Jun 16 '16 at 03:07
  • Haha, well looking back in hindsight you can now see that its actually quite simple isn't it? Its a seriously good exercise to learn the inns and outs of math operators and ordering in code. – sorifiend Jun 16 '16 at 03:09