design and implement a class of postfix calculators use the

design and implement a class of postfix calculators. use the algorithm given in this chapter to evaluate postfix expressions , as entered into the calculator . use only the operators +, - ,*, %, and /. assume that the postfix expressions have single digit numbers in the expression and are syntactically correct. Read the input as a string from console. First, you convert the infix expression into postfix expression. When you store each operand or operator into the post-fix expression, you place a space right after it. If it is not a correct infix expression, report the error, otherwise, perform the postfix evaluation. When evaluating the postfix expression, you can use the class StringTokenizer since there are spaces as delimiters. Pick off the characters one at a time using charAt. If they are operands, push them; if operator, pop two operands and apply, pushing the result. When the end of the string is, report the top of the stack as the result

Solution

Code:

Calculator.java


package calculator;
import java.util.Scanner;
public class Calculator {

public static void main(String[] args) {
// read the expression
Scanner sc=new Scanner(System.in);
String postlist;
System.out.print(\"Enter inf: \");
postlist = sc.nextLine();
       // convert infix to postfix
inftopost intopo = new inftopost(postlist);
System.out.print(\"Postfix: \");
intopo.print();
       // evaluvate postfix
posteval eval = new posteval(intopo.postlist());
System.out.println();
System.out.println(\"Result: \" + eval.res());
}
  
}


inftopost.java


package calculator;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
public class inftopost {
   // infix expression to be converted
private String in;
private Deque<Character> st = new ArrayDeque<Character>();
private List<String> post = new ArrayList<String>();
   // parametrized constructor
inftopost(String inf)
{
in = inf;
conv_inf();
}


// If number push to post list otherwise push to stack
private void conv_inf()
{
StringBuilder tem = new StringBuilder();

for(int i = 0; i != in.length(); ++i)
{   
if(Character.isDigit(in.charAt(i)))
{
//If digit is encountered read all digit untill operator is encountered
  
tem.append(in.charAt(i));

while((i+1) != in.length() && (Character.isDigit(in.charAt(i+1))
|| in.charAt(i+1) == \'.\'))
{
tem.append(in.charAt(++i));
}

post.add(tem.toString());
tem.delete(0, tem.length());
}
  
else
pushToStack(in.charAt(i));
}
stack_clear();
}


   // function to push character to stack
private void pushToStack(char inp)
{
if(st.isEmpty() || inp == \'(\')
st.addLast(inp);
else
{
if(inp == \')\')
{
while(!st.getLast().equals(\'(\'))
{
post.add(st.removeLast().toString());
}
st.removeLast();
}
else
{
if(st.getLast().equals(\'(\'))
st.addLast(inp);
else
{
                   // check precedence
while(!st.isEmpty() && !st.getLast().equals(\'(\') &&
precedence(inp) <= precedence(st.getLast()))
{
post.add(st.removeLast().toString());
}
st.addLast(inp);
}
}
}
}

// function to find precedance of operator
private int precedence(char operator)
{
if (operator == \'+\' || operator == \'-\')
return 1;
else if (operator == \'*\' || operator == \'/\')
return 2;
else if (operator == \'^\')
return 3;
else return 0;
}

// function to clear the stack
private void stack_clear()
{
while(!st.isEmpty())
{
post.add(st.removeLast().toString());
}
}

// function to print
public void print()
{
for(String str : post)
{
System.out.print(str + \' \');
}
}


public List<String> postlist()
{
return post;
}
}


posteval.java

package calculator;
import java.math.BigDecimal;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
public class posteval {
private List<String> inf = new ArrayList<String>();
private Deque<Double> st = new ArrayDeque<Double>();

public posteval(List<String> post) {inf = post;}

       // function to evaluate the expression
public BigDecimal res()
{
for(int i = 0; i != inf.size(); ++i)
{
  
if(Character.isDigit(inf.get(i).charAt(0)))
{
st.addLast(Double.parseDouble(inf.get(i)));
}
else
{
double tempr = 0;
double tem;

switch(inf.get(i))
{
case \"+\": tem = st.removeLast();
tempr = st.removeLast() + tem;
break;

case \"-\": tem = st.removeLast();
tempr = st.removeLast() - tem;
break;

case \"*\": tem = st.removeLast();
tempr = st.removeLast() * tem;
break;

case \"/\": tem = st.removeLast();
tempr = st.removeLast() / tem;
break;
}
st.addLast(tempr);
}
}
return new BigDecimal(st.removeLast());
}
}

Output:

run:

Enter infix: 1+2*3

Postfix: 1 2 3 * +

Result: 7

BUILD SUCCESSFUL (total time: 7 seconds)

design and implement a class of postfix calculators. use the algorithm given in this chapter to evaluate postfix expressions , as entered into the calculator .
design and implement a class of postfix calculators. use the algorithm given in this chapter to evaluate postfix expressions , as entered into the calculator .
design and implement a class of postfix calculators. use the algorithm given in this chapter to evaluate postfix expressions , as entered into the calculator .
design and implement a class of postfix calculators. use the algorithm given in this chapter to evaluate postfix expressions , as entered into the calculator .

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site