Expression Class
2015-01-13azim58 - Expression Class
import java.util.*;
public class Expression
/*
* Strings used for storing expression.
*/
String s, x;
/*
* Term evaluator for number literals.
*/
double term(){
double ans = 0;
StringBuffer temp = new StringBuffer();
while( s.length() > 0 && Character.isDigit( s.charAt( 0 ) ) ){
temp.append(Integer.parseInt( "" + s.charAt( 0 ) ));
s = s.substring( 1 );
if( s.length() > 0 && s.charAt( 0 ) == '.' )
temp.append( '.' );
s = s.substring( 1 );
while( s.length() > 0 && Character.isDigit( s.charAt( 0 ) ) ){
temp.append(Integer.parseInt( "" + s.charAt( 0 ) ));
s = s.substring( 1 );
}
if( s.length() > 0 && (s.charAt(0) == 'e' || s.charAt(0) == 'E') )
temp.append( 'e' );
s = s.substring( 1 );
temp.append( s.charAt( 0 ) );
s = s.substring( 1 );
while( s.length() > 0 && Character.isDigit( s.charAt( 0 ) ) ){
temp.append(Integer.parseInt( "" + s.charAt( 0 ) ));
s = s.substring( 1 );
}
ans = Double.valueOf( temp.toString() ).doubleValue();
return ans;
}
/*
- Parentheses solver.
- /
else
double ans;
if( s.charAt( 0 ) == '(' ){
s = s.substring( 1 );
ans = add();
s = s.substring( 1 );
ans = term();
return ans;
}
/*
- Exponentiation solver.
- /
boolean neg = false;
if( s.charAt( 0 ) == '-' ){
neg = true;
s = s.substring( 1 );
double ans = paren();
while( s.length() > 0 )
if( s.charAt( 0 ) == '^' ){
s = s.substring( 1 );
boolean expNeg = false;
if( s.charAt( 0 ) == '-' ){
expNeg = true;
s = s.substring( 1 );
double e = paren();
if( ans < 0 )
if it's negativeelse
double x = 1;
if( Math.ceil(e) == e ){ only raise to an integer
if( expNeg )
e *= -1;
if( e == 0 )
ans = 1;
else if( e > 0 )
for( int i = 0; i < e; i++ )
x *= ans;
else
for( int i = 0; i < -e; i++ )
x /= ans;
ans = x;
ans = Math.log(-1); // otherwise make it NaN
}
else if( expNeg )
ans = Math.exp( -e*Math.log( ans ) );
else
ans = Math.exp( e*Math.log( ans ) );
} else
break;
}
if( neg )
ans *= -1;
return ans;
}
/*
- Trigonometric function solver.
- /
else if( s.indexOf( "cos" ) == 0 )
double ans = 0;
boolean found = false;
if( s.indexOf( "sin" ) == 0 ){
s = s.substring( 3 );
ans = Math.sin( trig() );
found = true;
else if( s.indexOf( "tan" ) == 0 )
s = s.substring( 3 );
ans = Math.cos( trig() );
found = true;
s = s.substring( 3 );
ans = Math.tan( trig() );
found = true;
if( !found )
ans = exp();
return ans;
}
/*
- Multiplication, division expression solver.
- /
else if( s.charAt( 0 ) == '/' )
double ans = trig();
if( s.length() > 0 ){
while( s.length() > 0 ){
if( s.charAt( 0 ) == '*' ){
s = s.substring( 1 );
ans *= trig();
else break;
s = s.substring( 1 );
ans /= trig();
}
}
return ans;
}
/*
- Addition, subtraction expression solver.
- /
else if( s.charAt( 0 ) == '-' )
double ans = mul();
while( s.length() > 0 ){
if( s.charAt( 0 ) == '+' ){
s = s.substring( 1 );
ans += mul();
else
s = s.substring( 1 );
ans -= mul();
break;
}
return ans;
}
/*
- Public access method to evaluate this expression.
- /
s = x.intern();
double last = add();
return last;
/*
- Creates new Expression.
- /
// remove white space, assume only spaces or tabs
StringBuffer b = new StringBuffer();
StringTokenizer t = new StringTokenizer( s, " " );
while( t.hasMoreElements() )
b.append( t.nextToken() );
t = new StringTokenizer( b.toString(), "\t" );
b = new StringBuffer();
while( t.hasMoreElements() )
b.append( t.nextToken() );
x = b.toString();
/*
- The String value of this Expression.
- /
return x.intern();
/*
- Test our Expression class by evaluating the command-line
- argument and then returning.
- /
Expression e = new Expression( args[0] );
System.out.println( e + " = " + e.evaluate() );
}