in java with comment An expression is in prefix form when operators are written
ID: 3867050 • Letter: I
Question
in java with comment
An expression is in prefix form when operators are written before their operands. Here are some examples of prefix expressions and the values they evaluate to: An expression (such as 12) that begins with an integer is a prefix expression that evaluates to itself. Otherwise, an expression is a prefix expression if it begins with an operator and is followed by two prefix expressions. In this latter case, the value of the expression is recursively computed from the values of its constituent prefix sub-expressions. Write a program that allows the user to enter prefix expressions in a text field. The program reads the expression, evaluates it, and displays the value in a suitable GUI component. Assume that the user enters expressions that use only positive integers and the two operators + and *. Your program should use a stack to store values of sub-expressions as they are computed, and another stack to store operators that have not yet been applied.Explanation / Answer
import java.util.Stack;
public class Evalution
{
//This method will calculating value for the
//given prefix expression
public static int Calculating(String expr)
{
//all the characters are stored in the array called tokens[]
char[] tokens=expr.toCharArray();
//stack to store integers
Stack<Integer> values = new Stack<Integer>();
//stack to store operators
Stack<Character>ops = new Stack<Character>();
//checking with every character in the token
for(int i=0;i<tokens.length;i++)
{
//if white space occurs, then skip the iteration, and
//go to next iteration
if(tokens[i]==' ')
continue;
//if integer, then push into values stack
if(tokens[i]>='0' && tokens[i]<='9')
{
StringBuffer buf=new StringBuffer();
while(i<tokens.length && tokens[i] >= '0' && tokens[i] <= '9')
buf.append(tokens[i++]);
values.push(Integer.parseInt(buf.toString()));
}
//if operator, push into ops stack according to precedence
else if(tokens[i] == '+' || tokens[i] == '*')
{
while(!ops.empty() && check_precedence(tokens[i],ops.peek()))
{
values.push(Operation(ops.pop(),values.pop(),values.pop()));
}
ops.push(tokens[i]);
}
}
while(!ops.empty())
{
values.push(Operation(ops.pop(),values.pop(),values.pop()));
}
return values.pop();
}
//this method will check, if the given two operators
//have precedence or not
public static boolean check_precedence(char op1, char op2)
{
if(op1=='*' && op2=='+')
return false;
else
return true;
}
//this method calculate the actuall values
public static int Operation(char op,int y, int x)
{
switch(op)
{
case '+':
return x+y;
case '*':
return x*y;
}
return 0;
}
//main method
public static void main(String args[])
{
Evalution obj = new Evalution();
//passing expresions to the method called Calculating
System.out.println(Evalution.Calculating("12"));
System.out.println(Evalution.Calculating("+2 51"));
System.out.println(Evalution.Calculating("* 5 7"));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.