Program 2, Restricted Structures 21. Write a program to evaluate an arithmetic e
ID: 3807596 • Letter: P
Question
Program 2, Restricted Structures
21. Write a program to evaluate an arithmetic expression written in postfixed notation. The arithmetic expression will be input as a String (by the user) and will contain only integer operands. Use the following code sequence to parse (i.e., remove) the integers and the ioerators from the input string, mathExpression:
import java.util;
String thisToken;
StringTokenizer tokens = new StringTokenizer(mathExpression);
while(tokens.hasMoreTokens())
{ this token = tokens.nextToken();
//proccessing for thisToken goes here
}
You may assume the user will enter a one line properly formatted post fixed mathexpression, with the tokens (operators and operands) separated by white space. You should use the API Stack class and the StringTokenizer class. Declare your Stack object to be a stack that can only store objects in the class Double.
Below is an outline of the program.
import java.util.*; // needed for the StringTokenizer class
import javax.swing.*;
// Declare a Java API Stack object that can store Double objects (See page 168)
// Accept the user’s input of the math String into the variable mathExpression
// Evaluate the math expression //
// Pop the stack and output the returned value
(Be sure to parse the numeric operands to doubles before pushing them onto the stack)
Explanation / Answer
public class Test {
public static void main(String[] args) {
Evaluater evltr = new Evaluater();
System.out.println("Enter Postfix Expression :");
Scanner sc = new Scanner(System.in);
String mathExpression = sc.nextLine();
double result = evltr.evaluate(mathExpression);
System.out.println("Result :"+result);
}
}
class Evaluater{
public double evaluate(String mathExpression){
StringTokenizer tokens = new StringTokenizer(mathExpression);
java.util.Stack<String> stk = new java.util.Stack<String>();
String thisToken;
while(tokens.hasMoreTokens()){
thisToken = tokens.nextToken();
if(isOperater(thisToken)){
String oprnd1 = stk.pop();
String oprnd2 = stk.pop();
double n1 = Double.parseDouble(oprnd1);
double n2 = Double.parseDouble(oprnd2);
double result = performOperation(thisToken, n2, n1) ;
String resultOprnd = Double.toString(result);
stk.push(resultOprnd);
}
else{
stk.push(thisToken);
}
}
double finalResult = Double.parseDouble(stk.pop());
return finalResult;
}
boolean isOperater(String str){
if(str.equalsIgnoreCase("+")||str.equalsIgnoreCase("*")||str.equalsIgnoreCase("-")||str.equalsIgnoreCase("/"))
return true;
return false;
}
double performOperation(String opr,double oprnd1,double oprnd2){
if(opr.equalsIgnoreCase("+")) return (oprnd1+oprnd2);
if(opr.equalsIgnoreCase("-")) return (oprnd1-oprnd2);
if(opr.equalsIgnoreCase("*")) return (oprnd1*oprnd2);
if(opr.equalsIgnoreCase("/")) return (oprnd1/oprnd2);
return 0.0d;
}
}
===================OUTPUT===========================
Enter Postfix Expression :
3 4 + 3 /
Result :2.3333333333333335
Enter Postfix Expression :
100 50 / 8 + 2 /
Result :5.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.