Write a class Calc that will evaluate simple arithmetic expressions. However, in
ID: 673842 • Letter: W
Question
Write a class Calc that will evaluate simple arithmetic expressions. However, instead of the usual arithmetic functions (+, *, -, /), your code must evaluate expressions with the functions ('f', 'g', 'h', '%'), where f, g, and h are defined below (and '%' is the usual modulo operator)
Your class should have the following (static) methods:
convert - Converts an infix expression to an expression in reverse polish notation< >params< >expr An infix arithmetic expression, as a Stringreturn - The equivalent expression in RPN, as a Stringevaluate - Evaluates an expression in RPN notation.< >params< >expr An expression in RPN notation, as a Stringreturn - The result of evaluating the expression, as an integer
xfy=xy/x+y
xgy=xy
xhy=x+y
Hints
To convert an infix expression to RPN, you may use the following algorithm:
ForEach token in expr
If the token is an integer
append token to the output
Else If
pop and add to output all operators (if any on the stack)
push token
EndIf
EndFor
While the stack is not empty
item stack.pop()
append item to output
EndWhile
Example Dialog
Explanation / Answer
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;
/**
* Note: Static functions convert() and evaluate() should be called in strict order. It means first convert
* should be called, then evaluate should be called. If this order is violated, then results will not be proper.
*/
public class Calc {
/*
* Operator functions
*/
//function for operator f
private static int f(int x, int y)
{
return ((x*y)/(x+y));
}
//function for operator g
private static int g(int x, int y)
{
return (-x-y);
}
//function for operator h
private static int h(int x, int y)
{
return (x+y);
}
//utility function to evaluate an operator
private static int evaluate_operator(int x, int y,char optr)
{
int value=0;
System.out.println(optr);
switch(optr)
{
case 'f':
value=f(x,y);
break;
case 'g':
value=g(x,y);
break;
case 'h':
value=h(x,y);
break;
case '%':
System.out.println("x:"+x+" ,y:"+y);
value=y%x;
System.out.println("value:"+value);
break;
default:
System.out.println("Error in evaluation! Exiting...");
System.exit(0);
}
return value;
}
/**
* Function to convert infix expression to reverse polish notion
* @param expr expression in infix notation
* @return expression in reverse polish notation as an string
* @throws Exception
*/
static String convert(String expr) throws Exception
{
String rpn=""; //Expression in reverse polish notation
if(expr!=null)
{
//System.out.println(expr);
int len=expr.length();
Stack<Character> st=new Stack<Character>();
//System.out.println(1%2);
for(int i=0;i<len;i++)
{
char c=expr.charAt(i);
if(c>='0' && c<='9')
{
//System.out.println(c);
rpn+=c;
//System.out.println(rpn);
}
else
{
while(!st.isEmpty())
{
char o=st.peek().toString().charAt(0);
if(o=='f'||o=='g'||o=='h'||o=='%')
{
o=st.pop().toString().charAt(0);
rpn+=o;
}
else
break;
}
st.push(c);
}
}
while(!st.isEmpty())
{
char c=st.pop().toString().charAt(0);
rpn+=c;
}
}
return rpn;
}
/**
* Function to evaluate expression in reverse polish notation
* @param rpn - expression in reverse polish notation
* @return - result of evaluation as in integer
* @throws Exception
*/
static int evaluate(String rpn) throws Exception
{
int result=0;
if(rpn!=null)
{
System.out.println(rpn);
int len=rpn.length();
//System.out.println(len);
Stack<Integer> st=new Stack<Integer>();
for(int i=0;i<len;i++)
{
char c=rpn.charAt(i);
if(c>='0' && c<='9')
{
//System.out.println("c:"+c);
st.push(c-'0');
}
else
{
int x=st.pop();
int y=st.pop();
System.out.println("x:"+x+", y:"+y);
result=evaluate_operator(x,y,c);
System.out.println(result);
st.push(result);
}
}
result=st.pop();
}
return result;
}
/**
* Main function
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader bufferedReader=new BufferedReader(reader);
System.out.println("Welcome to the simple expression evaluator");
System.out.println("Type `exit' to quit.");
//while loop to execute calculator
while(true)
{
String prompt="->";
System.out.println(prompt);
String str=bufferedReader.readLine();
if(str.equals("exit"))
{
System.exit(0);
}
else
{
String expr=str; //string in infix notation
String rpn=convert(expr); //result of conversion in reverse polish notation
int result=evaluate(rpn);
System.out.println("result:"+result);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.