Need help with both the eval and apply methods. Please don\'t just post a \"simi
ID: 3536024 • Letter: N
Question
Need help with both the eval and
apply methods. Please don't just
post
a "similar" solution. I can't figure out
how to start these 2
methods.
public static int eval(String s)
A method to completely evaluate a single expression
in
the form of a String.
public static void
apply(GenStack val,
GenStack op)
A method to apply a single binary operation.
This
methods takes an operator from op along with two values from
val
and performs the operation. The result is pushed back onto
val.
public static int prec(char op)
A method to determine operator precedence.
My project code so far:
import java.util.*;
import java.io.*;
public class Project {
public static void main(String[]args) throws
FileNotFoundException{
GenStack
op = new GenStack();
GenStack val
= new
GenStack();
}
public static void apply (GenStack
val, GenStack op){
}public static int eval(String s){
}public static int eval(String s){
Scanner infile = new Scanner((new
FileReader("eval.txt")));
String
=
infile.next();
for (int i=0;
i.length;i++)>
{
char ch = input.charAt(i);
if (ch == '(') op.push(ch);
//needs
work
if (character.isdigit(s[x])){
int sum = 0;
while (character.isdigit(s[x])){
sum - sum * 10 + s[x] - 0;
x++;
}
}
}
public static int prec (char c) {
switch (c) {
case '+':
case '-':
return(5);
case '*':
case '/':
case '%':
return(10);
case '(':
return(0);
} /* switch */
return (0);
} /* prec */
}
}
My GenStack code: (I think this is complete)
import java.util.*;
public class GenStack {
private Node top;
private class Node {
private T
data;
private Node
next;
private Node(T item)
{
data = item;
next = null;
}
}
public GenStack () {
top = null;
}
public void dumplist() {
Node p =
top;
while ( p != null )
{
System.out.print(p.data + " ");
p
= p .next; }
System.out.println();
}
public T pop() throws EmptyStackException
{
if ( top == null
)
throw new EmptyStackException();
T val =
top.data;
top =
top.next;
return val;
}
public T top() throws EmptyStackException
{
if ( top == null
)
throw new EmptyStackException();
return
top.data;
}
public void push (T item) {
Node n = new
Node(item);
n.next = top;
top= n;
}
public boolean isEmpty()
{
return top == null;
}
}
Input expression (( 50 + 40) / 2 ) + ( 2* ( 3 +
1))
Should output like (( 50 + 40) / 2 ) + ( 2* ( 3
+
1)) = 53
All expression evaluation will occur in the eval() method
where
you will have two stacks: an operator stack and a value
stack.
Expressions will have the following components:
Integers
Binary Operators {*, /, %, +, -}.
Parentheses for altering precedence.
Whitespace which can be ignored.
Precedence rules are the same as for Java. Parentheses {(,
)}first, then {*, /, %} and lastly {+, -}. Operators of like
precedence are evaluated from left to right.
You should pass prec() the operator and have it return an
integer.
This is so you can compare the precedence of the operator on
the
stack with the one from the input stream.
Be certain to apply whatever is left on the value and
operator
stacks once the expression in its string form has been
exhausted.
The result will be the only value left on the value stack.
Consider the following evaluation rules:
The rules for evaluation of expressions are as follows:
(
push onto operator stack
value
push value onto value stack
)
while (top operator stack != '(')
apply
pop '(' from operator stack
operator
while (! empty operator stack && (prec(currop) <= p="">
p="">
prec(top operator stack)))
apply
push currop
end of string
while (! empty operator stack)
apply
result remains on value stack
Explanation / Answer
I shall be answering this later.. just dont rate this answer..