Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA please. Do not copy any other posts. Give copyable code with OUTPUT snippets. GIVE ALL THE CODE CORRECTLY Problem 1 (30 points) In this

JAVA please. Do not copy any other posts. Give copyable code with OUTPUT snippets. GIVE ALL THE CODE CORRECTLY

Problem 1 (30 points) In this problem, you are asked to develop an Infix-to-Postfix Converter. Stacks are used by compilers to help in the process of evaluating expressions and generating machine-language code. In this and the next exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4 and 7 / 9 in which the operator (+ or / here) is written between its operandsthis is called infix notation. Computers prefer postfix notation, in which the operator is written to the right of its two operands. The preceding infix expressions would appear in postfix notation as 3 4 + and 7 9 /, respectively. To evaluate a complex infix expression, a compiler would first convert the expression to postfix notation and evaluate the postfix version. Each of these algorithms requires only a single left-toright pass of the expression. Each algorithm uses a stack object in support of its operation, but each uses the stack for a different purpose.

In this exercise, youll write a Java/C++/Python (of your selected choice) version of the infix-to-postfix conversion algorithm. In the next exercise, youll write a Java/C++/Python version of the postfix expression evaluation algorithm. Write a class InfixToPostfixConverter to convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers such as (6 + 2) * 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of the this infix expression is 6 2 + 5 * 8 4 / - The program should read the expression into StringBuffer (Python StringIO, C++ std::stringbuf) infix and use one of the stack classes implemented in this chapter to help create the postfix expression in StringBuffer postfix. The algorithm for creating a postfix expression is as follows: a) Push a left parenthesis '(' onto the stack. b) Append a right parenthesis ')' to the end of infix. c) While the stack is not empty, read infix from left to right and do the following: If the current character in infix is a digit, append it to postfix. If the current character in infix is a left parenthesis, push it onto the stack. If the current character in infix is an operator: Pop operators (if there are any) at the top of the stack while they have equal or higher precedence than the current operator, and append the popped operators to postfix. Push the current character in infix onto the stack. If the current character in infix is a right parenthesis: Pop operators from the top of the stack and append them to postfix until a left parenthesis is at the top of the stack. Pop (and discard) the left parenthesis from the stack. The following arithmetic operations are allowed in an expression: + addition - subtraction

* multiplication / division ^ exponentiation % remainder Some methods of class, InfixToPostfixConverter, you will need are as follows: a) Method convertToPostfix, which converts the infix expression to postfix notation. b) Method isOperator, which determines whether c is an operator (provided). c) Method precedence, which determines whether the precedence of operator1 (from the infix expression) is less than, equal to or greater than that of operator2 (from the stack). The method returns true if operator1 has lower precedence than operator2. Otherwise, false is returned. The methods isOperator, precedence are provided below. You are to implement convertToPostfix. Method top (returns the top value of the stack without popping the stack (provided in Stack implementation). A simple Array-based implementation is provided in your textbook chapter on Stacks and Queues. // check if c is an operator private static boolean isOperator( char c ) { return c == '+' || c == '-' || c == '*' || c == '/' || c == '^' || c == '%'; } // end method isOperator // true if operator1 has precedence less than operator2 private static boolean precedence( char operator1, char operator2 ) { if ( operator1 == '^' ) return false; else if ( operator2 == '^' ) return true;

else if ( operator1 == '*' || operator1 == '/' || operator1 == '%' ) return false; else if ( operator2 == '*' || operator2 == '/' || operator2 == '%' ) return true; else return false; } // end method precedence Implement the code for InfixToPostfixConverter class Solution should contain the following a) Implement it in your chosen language and show a sample run and show its output in PDF and include the code files in zip file

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions