Please help me implement this simple method? I have trouble implementing EvalEp(
ID: 3667403 • Letter: P
Question
Please help me implement this simple method? I have trouble implementing EvalEp() from the following pseudo code
public int EvalExp(String z)
{
while( z != null)
if
isNumber(z);
valStk.push(z);
else
repeatOps('$');
return valStk.top();
}
how do i fix it to make it work in java?
Algorithm for Evaluating Expressions Slide by Matt Stallmann included with permission. Two stacks: Algorithm EvalExpO Input: a stream of tokens representing opStk holds operators valStk holds values an arithmetic expression (with numbers) a Use $as special "end of input" utput: the value of the expression token with lowest precedence Algorithm doop) while there's another token z x vaistk.pop(); y vaistk. pop(); op opStk.pop0: valStk.push( y op x) if isNumber(z) then valStk.push(z) else Algorithm repeatOps(refop) while (valStk.size) > 1 A opStk.push(z) prec(refOp) s prec(opStk.top)) return valStk.top) doOp0 © 2014 Goodrich, Tamassia, Goldwasser StacksExplanation / Answer
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import jiava.io.*;
class opStk{
// private variables declaration as per your requirement
public int pop();// pop the element from the stack opStk
}
class valStk{
// private variables declaration as per your requirement
public int pop();// pop the element from the stack valStks
public int push(int y,class op,int y){
//push operation here
}
}
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{ public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
//valStk class intilizationi
valStk valstack = new valStk();
//opStk class initileization
opStk opstack = new opstack();
private int x;
private int y;
static public void doOp(){
x=valstack.pop();
y =valstack.pop();
op = opstack.pop();
valstack.push(y,op, x);
}
public int EvalExp(String z){
while(z!=null){
do{
valstack.push(z)
}while(isNumber(z));
}
return valstack.pop();
}
}
}
}