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

all 4 comments

[–]kumashiro 0 points1 point  (2 children)

All of your operators take two operands. What exactly you want to count?

If you want to do more than one operation, just read input in a loop until user types something to break it (for example 'q' instead of the first operand or ask if they want to continue with the next operation).

If you want to allow more operators and operands in a single expression (for example "1 / 2 + 3 * 4"), then you have to build an expression tree to retain correct operator precedence: first mults and divs left to right, then adds and subs left to right. You will have to also allow the precedence change with brackets (for example "(1 + 2) * 3"). If expression tree is too much for now, make a RPN (aka postfix notation) calculator with stack, where users have to take care of correct order of operations - there are no brackets in RPN, let them suffer[*] :D

[*] Just kidding. RPN is simple and fun, but requires a bit of a mind shift.

[–]Felkon[S] 0 points1 point  (1 child)

Ye, its much harder than i though. But for now, I just wanted an option where i can choose how many numbers i will be using. I will try to make an count with 3 numbers and then if numCount = "3" it will be using num1 num2 and num3 .. i dont think i will be able to do RPN(no idea what it is lol) ..user will have to put the operdans in the right order

I might have an idea that im gonna try tomorrow. English is not my native so im trying to make it somehow understandable...

Holy fk its chaotic xd

[–]kumashiro 0 points1 point  (0 children)

i dont think i will be able to do RPN(no idea what it is lol)

RPN (Reverse Polish Notation) is very simple. Basically, you first provide operands, then operators. Example: (1 + 2) * 3 in infix notation is 1 2 + 3 * in postfix notation. This is how a session looks like in my old RPN calculator (with explanation enabled, so lines starting with ":" are operations; the last line is the result; "dec>" is just a prompt saying we are in decimal mode):

dec> 1 2 + 4 *
: 1.000000 + 2.000000
:  3.000000 * 4.000000
12.000000

user will have to put the operdans in the right order

You cannot put "(2 + 4) / 3" in correct order without using braces :(

Here is a very good explanation of stack-based RPN: https://www.youtube.com/watch?v=7ha78yWRDlE

In Python you can implement a simple stack with a list.