Create a method to add the missing stars

** If you are attempting any of the extra credit opportunities, you must submit your code in the HW3 – Extra Credit Gradescope assignment before the due date
The given examples just represent the expected output, not the syntax for using the function
EXTRA CREDIT #1: 30 pts, added regardless of the maximum 100
In the regular assignment, two consecutive operators return an error message

postfix(‘ 2 * 5.4 + 3 ^ -2+1 +4 ‘) ‘error’
calculate(‘ 2 * 5 + 3 ^ -2+1 +4 ‘) ‘error’
Modify your code to support negation
postfix(‘ 2 * 5 + 3 ^ -2+1 +4 ‘) ‘2.0 5.0 * 3.0 -2.0 ^ + 1.0 + 4.0 +’
postfix(‘-2 * 5 + 3 ^ 2+1 + 4’) ‘-2.0 5.0 * 3.0 2.0 ^ + 1.0 + 4.0 +’
postfix(‘-2 * -5 + 3 ^ 2+1 + 4’) ‘-2.0 -5.0 * 3.0 2.0 ^ + 1.0 + 4.0 +’
postfix(‘2 * + 5 + 3 ^ 2 +1 +4’) ‘error’
calculate(‘ 2 * 5 + 3 ^ -2+1 +4 ‘) 15.11111111111111
calculate(‘-2 * 5 + 3 ^ 2+1 + 4’) 4.0
calculate(‘ -2 / (-4) * (3 – 2*( 4- 2^3)) + 3’) 8.5
calculate(‘2 + 3 * ( -2 +(-3) (5^2 – 23^(-2) ) *(-4) ) * ( 2 /8 + 2( 3 – 1/ 3) ) – 2/ 3^2′) 4948.611111111111 EXTRA CREDIT #2: 50 pts, added regardless of the maximum 100 In our regular assignment, ‘3(5)’ is an invalid expression, since valid multiplication is denoted as ‘35′ or ‘3*(5)’. Modify your code to support the * omission. For example, ‘3(5)’ is treated as ‘3*(5)’. You can create a method to add the missing stars in in self.expr before passing it to postfix. You need to figure out the necessary and sufficient rules to add the missing *, for example, if there is an operand followed by a “(“, then insert a *.
calculate(‘ 3 ( 5 )- 15 + 85 (12) ‘) 1020.0
calculate(‘ (-2/6)+ (5(9.4)) ‘) 46.6666666667

Leave a Reply

Your email address will not be published. Required fields are marked *