Question: Section 5. Programming Exercise 1 We commonly write arithmetic expressions in the so-called infix form. That is, with each operator placed between its operand,

Section 5. Programming Exercise 1 We commonly write arithmetic expressions in theso-called infix form. That is, with each operator placed between its operand,

Section 5. Programming Exercise 1 We commonly write arithmetic expressions in the so-called infix form. That is, with each operator placed between its operand, as below (5+6)*(4/3) Although we are comfortable writing expressions in this form, infix form has the disadvantage that parentheses must be used to indicate the order in which operators are to be evaluated. These parentheses, in turn, complicate the evaluation process. N Evaluation is much easier if we can simply evaluate operators from left to right. Unfortunately, this evaluation will not work with the infix form of arithmetic expressions. However, it will work if the expression is in postfix form. In the postfix form of an arithmetic expression, each operator is placed immediately after its operands. The expression above is written in postfix form as 56+43/* Note that both forms place the numbers in the same order (reading from left to right). The order of the operators is different, however, because the operators in the postfix form are positioned in the order that they are evaluated. The resulting postfix expression is hard to read at first, but it is easy to evaluate programmatically. We will do so with stacks. Suppose you have an arithmetic expression in postfix form that consists of a sequence of single digit, nonnegative integers and the four basic arithmetic operators (+..."./). This expression can be evaluated using the following algorithm in conjuction with a stack of floating-point numbers. Part One: Create a program that reads the postfix form of an arithmetic expression, evaluates it, and outputs the result. Assume that the expression consists of single-digit, nonnegative integers ('0' to '9') and the 4 basic arithmetic operators (+,-, *, /'). Further assume that the arithmetic expression is input from the keyboards with all the characters separated by white space on one line. Save your program in a file called postfix.java. In order to complete this step, you will need to use the following: o Scanner sc = new Scanner (System.in); o Char[] c = sc.next.toCharArray(); This will return an array of characters with the postfix operation. You must change the numeric numbers from their char representation to Float objects to push them on stack. To be able to do this, use the following: In addition, use the unboxing functions of Float to change from object Float to primitive data type float: o Float floatOperand= operandObj.floatValue() Part Two: Using only a single stack object, solve the postfix arithmetic expression. Part Three: Test you program by running the following expressions and verifying the output. Test Case One operator o int operand = Character.getNumeric Value(c[0]); o Float operandObj = Float.valueOf(operand); Nested operators Uneven nesting All operators at end Zero dividend Single-digit number Arithmetic Expression 34+ 34+52/* 93*2+1- 4675-+* 02/ 7

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!