Mar-22-2021, 09:52 AM
Use python tags for code.
Yes, that code looks better.
You are using Polish notation, developed by the Polish mathematician Jan Łukasiewicz. Most calculators of the past used Reverse Polish notation (RPN), so you would type in the two values and then provide the operator. Note that PN and RPN are both parenthesis-free. So instead of prompting for the operator, and then the two operands, you could do like old mechanical calculators did: ask for the values, and then the operator. However, you could also use a single line of input, e.g.
Yes, that code looks better.
You are using Polish notation, developed by the Polish mathematician Jan Łukasiewicz. Most calculators of the past used Reverse Polish notation (RPN), so you would type in the two values and then provide the operator. Note that PN and RPN are both parenthesis-free. So instead of prompting for the operator, and then the two operands, you could do like old mechanical calculators did: ask for the values, and then the operator. However, you could also use a single line of input, e.g.
1 2 +but, even better
1 2 + 3 * 4 /This involves having a stack; when you encounter an integer, you push it onto the stack, when you encounter an operator, you pop off the top two stack elements, do the computation, and push them back on. The above expression computes
(1 + 2)*3 / 4whereas
1 2 + 3 * 3 1 + /computes
(1 + 2) * 3 / (3 + 1)and works like this
1 2 + 3 * 3 1 + / ^ | you are here stack: 1 1 2 + 3 * 3 1 + / ^ stack: 1 2 1 2 + 3 * 3 1 + / ^ stack: 3 1 2 + 3 * 3 1 + / ^ stack: 3 3 1 2 + 3 * 3 1 + / ^ stack: 9 1 2 + 3 * 3 1 + / ^ stack: 9 3 1 2 + 3 * 3 1 + / ^ stack: 9 3 1 1 2 + 3 * 3 1 + / ^ Stack: 9 4 1 2 + 3 * 3 1 + / ^ Stack: 2Now you have invented the HP calculator. However, there are a couple very simple algorithms for converting infix (with parentheses or not) using standard operator hierarchy, but I don't want to go into that until you have chosen to implement RPN or not.