Question

tack Assignment Objective: to be familiar with sta

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Java Program :

import java.util.*;

public class PostfixConverter {
private String infixExpression;

public PostfixConverter(String infixExpression) {
this.infixExpression = infixExpression;
}

private String convertInfixExpression() {
StringTokenizer tokens = new StringTokenizer(infixExpression);
Stack operatorStack = new ArrayStack();
String converted = "";

while(tokens.hasMoreTokens()) {
String token = tokens.nextToken();
if(token.equals("(")) {
operatorStack.push(new String(token));
}
else if(token.equals(")")) {
while(operatorStack.top().equals("(") != true) {
converted = converted + " " + operatorStack.pop();
}
if(operatorStack.top().equals("(")) {
operatorStack.pop();
}
}
else if (isOperator(token)){
if(operatorStack.isEmpty() == true) {
operatorStack.push(new String(token));
}
else {
if(ICP(token) < ISP((String) operatorStack.top())) {
converted = converted + " " + operatorStack.pop();
operatorStack.push(token);
}
else {
operatorStack.push(new String(token));
}
}
}
else {
converted = converted + " " + new String(token);
}
}
while(operatorStack.isEmpty() != true) {
converted = converted + " " + operatorStack.pop();
}

return converted;
}

public int ISP(String token) {
int precedence = 0;
if(token.equals("+")|| token.equals("-")) {
precedence = 2;
}
else if(token.equals("*") || token.equals("/")) {
precedence = 4;
}
else if(token.equals("^")) {
precedence = 5;
}
else if(token.equals("(")) {
precedence = 0;
}
return precedence;
}

public int ICP(String token) {
int precedence = 0;
if(token.equals("+")|| token.equals("-")) {
precedence = 1;
}
else if(token.equals("*") || token.equals("/")) {
precedence = 3;
}
else if(token.equals("^")) {
precedence = 6;
}
return precedence;
}

private boolean isOperator(String token) {
return (token.equals("+") ||
token.equals("-") ||
token.equals("*") ||
token.equals("/") ||
token.equals("^") );
}

public static void main(String[] args) {
System.out.println("Input the infix expression "
+ "(operands, operators are separated by spaces):");
Scanner input = new Scanner(System.in);
String infixExpression = input.nextLine();
PostfixConverter converter = new PostfixConverter(infixExpression);
System.out.println("The converted expression is " +
converter.convertInfixExpression());
}

}

Add a comment
Know the answer?
Add Answer to:
tack Assignment Objective: to be familiar with stack operations and applications Requirements: (a) Implement a Java...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming...

    This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...

  • Write a Java program that will implement a stack object to convert from either infix notation...

    Write a Java program that will implement a stack object to convert from either infix notation to postfix notation or postfix notation to infix notation.  The program will also implement a link list data structure to track all the conversions done. The Program should have a menu like the following as its output: "Please select what type of conversion you would like to do: Infix to postfix Postfix to infix Print Equations Exit"

  • Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expressi...

    Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expression , convert to a postfix expression and finally evaluate the postfix expression… Follow the examples done during class lectures… We are used to infix notation - ”3 + 4” - where the operator is between the operands. There is also prefix notation, where the operand comes before...

  • Write a java program for the following: Your program reads an infix expression represented by a...

    Write a java program for the following: Your program reads an infix expression represented by a string S from the standard input (the keyboard). Then your program converts the infix expression into a postfix expression P using the algorithm. Next, your program evaluates the postfix expression P to produce a single result R. At last, your program displays the original infix expression S, the corresponding postfix expression P and the final result R on the standard output ( the screen...

  • Objective To acquire expertise in stack manipulation and management, subroutine linkage and retur...

    Objective To acquire expertise in stack manipulation and management, subroutine linkage and return conventions, and recursive procedures. Description You are to create a MIPS programming assignment that returns postfix representation of the input and create an expression tree. Your MIPS program should make use of the expression tree to store the input and provide, by means of an adequate traversal, the value for the expression. Your solution should be structured according to the following steps: Step 1: Convert expression from...

  • Total point: 15 Introduction: For this assignment you have to write a c program that will...

    Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...

  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

  • The objectives of this assignment are to: Further enhance your knowledge and skill in Java. Gain...

    The objectives of this assignment are to: Further enhance your knowledge and skill in Java. Gain an understanding and experience with stacks. Gain further experience using generics. Continue to practice good programming techniques. Create a stack class named MyStack that stores generics with the methods shown below. Write a test program that thoroughly tests your stack implementation. void push(E item) push item onto top of stack E peek() return item on top of stack E pop() return item on top...

  • Stacks and Java 1. Using Java design and implement a stack on an array. Implement the...

    Stacks and Java 1. Using Java design and implement a stack on an array. Implement the following operations: push, pop, top, size, isEmpty. Make sure that your program checks whether the stack is full in the push operation, and whether the stack is empty in the pop operation. None of the built-in classes/methods/functions of Java can be used and must be user implemented. Practical application 1: Arithmetic operations. (a) Design an algorithm that takes a string, which represents an arithmetic...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT