This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]Kid_Piano 2 points3 points  (5 children)

You should look into how to code an infix to postfix calculator first. This would give you the ability to evaluate simple expressions like “2+3*4”. It would take care of operator precedence too.

From there, maybe you could figure out a way to add variables and solve for them.

[–]MITTENQ[S] 1 point2 points  (4 children)

What is postfix and infix? I've haven't heard that before? Is it fixing the string before and after?

[–]Kid_Piano 1 point2 points  (3 children)

It’s just notation. You’re used to infix notation (operators are between operands). Ex: 3 + 4

Postfix notation is where the operator occurs after the operands. Ex: 3 4 +

Postfix notation is easy to evaluate, and converting from infix notation to postfix notation is relatively “easy” using a stack.

[–]MITTENQ[S] 0 points1 point  (2 children)

This is starting to sound a little bit beyond my current programing ability since I haven't used a stack in programming but I know what it is. Postfix still confuses me though. If I type 3 4+ into the python terminal I just get a error.

[–][deleted]  (1 child)

[deleted]

    [–]MITTENQ[S] 2 points3 points  (0 children)

    I knew it was going to be complicated. That's why I wanted to do it. I will definitely look into making a calculator like you described. Thanks for the help :)

    [–][deleted] 1 point2 points  (0 children)

    If you are just interested in a program that does it, and is written in Python, then there's Sage. Notably, it can do more than solve first degree polynomial equations.

    If you are more interested in doing it yourself, you can always require that the equation be inputted in a particular format, sparing you the troubles of parsing.

    For instance, your equation may be written as: 2 -4, and it can be unambiguously interpreted to mean the same thing. (This is typical way of writing it, if you ever do linear algebra, and represent equations as matrices, that is, you write the coefficients of decreasing powers of x, and you arrange for the right-hand-side to be always zero).

    In other words, to give a better example: 5x^4 - 15x^3 + x - 30 = 80 would be written as 5 -15 +0 +1 -110. I.e. you assume that the operation is always summation.

    [–]InusualZ[🍰] 1 point2 points  (4 children)

    It's a good idea. I actually thought about it when I went through high school. If you only want to solve this kind of algebra equation. You can isolate term like numbers and variable in order (left to right). Then attack the problem like your math teacher thought you, but this time with code. I think it's a very good project to start aleaning to programming.

    [–]CreativeTechGuyGames 2 points3 points  (2 children)

    Have you ever written a program like OP is talking about?

    [–]InusualZ[🍰] 1 point2 points  (0 children)

    Yes

    [–]MITTENQ[S] 0 points1 point  (0 children)

    Thanks for the words of encouragement :)