Question & Answer: For my java Class I have to build a calculator using infix form including parentheses where necessary, and then its value……

For my java Class I have to build a calculator using infix form including parentheses where necessary, and then its value.

For this assignment you may assume that all of the operators are binary, and are represented by the following symbols: P (plus for addition), M (minus for subtraction), D (division symbol for integer division), and T (times for multiplication).

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: For my java Class I have to build a calculator using infix form including parentheses where necessary, and then its value……
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

I can use something like this

Question & Answer: For my java Class I have to build a calculator using infix form including parentheses where necessary, and then its value...... 1

and the answer should be this:

Question & Answer: For my java Class I have to build a calculator using infix form including parentheses where necessary, and then its value...... 2

For this assignment you may assume that all of the operators are binary, and are represented by the following symbols: P (plus for addition), M (minus for subtraction), D (division symbol for integer division), and T (times for multiplication).

Expert Answer

/*Source code is given below; it should be run from command promt:*/

import java.util.Arrays;

import java.util.Scanner;

import java.util.Stack;

public class RNPCalculator {

 

/**

* Checks if the input is operator or not

* @return true if operator

*/

private boolean isOperator(String c){

if(c.equals(“P”) || c.equals(“M”) || c.equals(“D”) || c.equals(“T”))

return true;

return false;

}

 

/**

* Converts any postfix to infix

* postfix String expression to be converted

* @return String infix expression produced

*/

public String convert(String[] token){

Stack<String> s = new Stack<>();

 

String result=””;

for(int i = 0; i < token.length; i++){

if(token[i].equals(“P”))

System.out.print(” “+”+”);

else if(token[i].equals(“M”))

System.out.print(” “+”-“);

else if(token[i].equals(“D”))

System.out.print(” “+”/”);

else if(token[i].equals(“T”))

System.out.print(” “+”*”);

else

System.out.print(” “+token[i]);

}

System.out.println(“n”);

for(int i = 0; i < token.length; i++){

String op=token[i];

if(isOperator(op)){

String b = s.pop();

String a = s.pop();

if(op.equals(“P”))

op=”+”;

if(op.equals(“M”))

op=”-“;

if(op.equals(“D”))

op=”/”;

if(op.equals(“T”))

op=”*”;

 

s.push(“(“+a+op+b+”)”);

}

else

s.push(“”+op);

}

result = s.pop();

return result;

}

public int evalRPN(String[] tokens) {

int returnValue = 0;

String operators = “PMTD”;

Stack<String> stack = new Stack<String>();

for(String t : tokens){

if(!operators.contains(t)){

stack.push(t);

}else{

int a = Integer.valueOf(stack.pop());

int b = Integer.valueOf(stack.pop());

int index = operators.indexOf(t);

switch(index){

case 0:

stack.push(String.valueOf(a+b));

break;

case 1:

stack.push(String.valueOf(b-a));

break;

case 2:

stack.push(String.valueOf(a*b));

break;

case 3:

stack.push(String.valueOf(b/a));

break;

}

}

}

returnValue = Integer.valueOf(stack.pop());

return returnValue;

}

public static void main(String[] args) {

RNPCalculator obj = new RNPCalculator();

//System.out.println(“Enter postfix expression:”);

//Scanner sc =new Scanner(System.in);

//String postfix = sc.nextLine();

//String[] token = postfix.split(“\s”);

//System.out.println(obj.convert(token));

//System.out.println(obj.evalRPN(token));

System.out.println(obj.convert(args));

System.out.println(obj.evalRPN(args));

}

}

/*output screenshot is given here*/

Question & Answer: For my java Class I have to build a calculator using infix form including parentheses where necessary, and then its value...... 3

 

Still stressed from student homework?
Get quality assistance from academic writers!