Iz E-študij, proste zakladnice študentskega znanja
//Drevo za izracun aritmeticnega izraza
import java.io.*;
public class ArithmeticExprTree {
static final int PLUS = '+' ;
static final int MINUS = '-' ;
static final int TIMES = '*' ;
static final int DIVIDE = '/' ;
static final int L_BRACKET = '(' ;
static final int R_BRACKET = ')' ;
static final int NUM_VALUE = '\0' ;
ArithmeticExprNode rootNode ;
public ArithmeticExprTree() {
rootNode = null ;
}
public double evaluate() {
return evaluate(rootNode) ;
}
private double evaluate(ArithmeticExprNode node) {
if (node == null)
return 0.0 ; // alternatively throw exception ;
if (node.operator == NUM_VALUE)
return node.value ;
double lValue = evaluate(node.left) ;
double rValue = evaluate(node.right) ;
switch (node.operator) {
case PLUS: return lValue + rValue ;
case MINUS: return lValue - rValue ;
case TIMES: return lValue * rValue ;
case DIVIDE: return lValue / rValue ;
default: return 0.0 ; // alternatively throw exception ;
}
}
public void exprToTree(StreamTokenizer st) throws IOException {
st.nextToken() ;
rootNode = expression(st) ;
if (st.ttype != st.TT_EOF)
System.out.println("Expession error");
}
private ArithmeticExprNode expression(StreamTokenizer st) throws IOException {
ArithmeticExprNode root = product(st) ;
if (st.ttype == PLUS || st.ttype == MINUS)
return addSubtract(st, root) ;
else return root ;
}
private ArithmeticExprNode addSubtract(StreamTokenizer st, ArithmeticExprNode left) throws IOException{
ArithmeticExprNode root = new ArithmeticExprNode(st.ttype) ;
root.left = left ;
st.nextToken() ;
root.right = product(st) ;
if (st.ttype == PLUS || st.ttype == MINUS)
return addSubtract(st, root) ;
else return root ;
}
private ArithmeticExprNode product(StreamTokenizer st) throws IOException {
ArithmeticExprNode root = value(st) ;
if (st.ttype == TIMES || st.ttype ==DIVIDE)
return multiplyDivide(st, root) ;
else return root ;
}
private ArithmeticExprNode multiplyDivide(StreamTokenizer st, ArithmeticExprNode left) throws IOException{
ArithmeticExprNode root = new ArithmeticExprNode(st.ttype) ;
root.left = left ;
st.nextToken() ;
root.right = value(st) ;
if (st.ttype == TIMES || st.ttype == DIVIDE)
return multiplyDivide(st, root) ;
else return root ;
}
private ArithmeticExprNode value(StreamTokenizer st) throws IOException{
if (st.ttype == st.TT_NUMBER) {
ArithmeticExprNode node = new ArithmeticExprNode(NUM_VALUE, st.nval);
st.nextToken() ;
return node ;
}
else if (st.ttype != L_BRACKET) // should be left bracket
return null ; // error, alternatively throw exception
else {
st.nextToken() ;
ArithmeticExprNode node = expression(st) ;
if (st.ttype != R_BRACKET) // should be right bracket
return null ; // error, alternatively throw exception
else {
st.nextToken();
return node ;
}
}
}
public static void main(String[] args) {
ArithmeticExprTree exprTree = new ArithmeticExprTree();
String exprString = "3.5 + 5.0 * 6.1/(3.4 - 6.2)" ;
Reader r = new StringReader(exprString) ;
StreamTokenizer st = new StreamTokenizer(r) ;
st.ordinaryChar('/');
try {
exprTree.exprToTree(st) ;
System.out.println(exprString + " = " + exprTree.evaluate()) ;
}
catch (IOException e) {
System.out.println("Unexpected IO error"+e.getMessage()) ;
}
}
}