2

I have a program that takes in a simple linear equation and transforms it into its equivalent in postfix.

For example:

 3x+7=4(2x-1) 

would be transformed into

3 x * 7 + = 4 2 x * 1 - *

How can i get the value of x in this example using its postfix form. Any help will be greatly appreciated thank you

EDIT - I need help with the logic not the code (I'm not asking for people to do the code for me)

Airspeed Velocity
  • 40,491
  • 8
  • 113
  • 118

2 Answers2

0

If your linear equation is always in the form of a right hand side (RHS) in terms of x and a left hand side (LHS) in terms of x, then the following would work.

  1. Subtract the LHS from both the LHS and the RHS. Then you have 0 on the LHS and an expression in terms of x on the RHS.

  2. Begin to simplify the postfix expression. Every time you encounter an addition or subtraction operation with a numerical operand, add or subtract that value from the LHS as appropriate, and replace the operand in the calculation with 0.

  3. At the end you should be left with an equation in the form of b = a * x. The solution (if one exists and is unique) is then b / a.

Justin Kaufman
  • 251
  • 1
  • 5
0

Same as algebra, first let's get it into a simplified form:

3 x * 7 + = 4 2 x * 1 - *

We see a = 2 x *, then b = a 1 -, leaving 4 b *. Multiply each term in b:

3 x * 7 + = 2 4 * x * 1 4 * -
3 x * 7 + = 8 x * 4 -

Do the same on the left:

3 7 / x * 1 + = 8 x * 4 -

Now subtract 1 from each side by removing a top-level 1 + or otherwise altering some top-level addition:

3 7 / x * = 8 x * 5 -

And subtract 8 x *:

3 7 / x * 8 x * - = 0 5 -

Move things around and multiply by -1:

8 x * 3 7 / x * - = 5

Note: multiplying by -1 is easy. Algebraic notation:

(a - b) * -1 = (0 + (a - b)) * -1
(a - b) * -1 = -1*0 + (-1*a - -1*b)
(a - b) * -1 = 0 + (-a - -b)
(a - b) * -1 = (-a + b)
(a - b) * -1 = b - a

I tried using this once to fix a mistake way down the line in linear algebra and lost several points on the test because the instructor said I can't just claim -(a-b) = (b-a) so I had to prove 0-x = -x I guess.

In reverse polish, a b - 0 - = b a - 0 +. Because x is common, reorder the multiplication:

8 3 7 / - x * = 5
53 7 / x * = 5

Divide both sides by 53 / 7:

x = 5 53 7 / *
x = 5 53 * 7 /
x = 265 7 /
x = 37 6 7 / +

Solve for x.

John Moser
  • 465
  • 5
  • 11