Can some one please help me with this ?
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.
*/
double paren(){
double ans;
if( s.charAt( 0 ) == '(' ){
s = s.substring( 1 );
ans = add();
s = s.substring( 1 ); // we assume this is a ')'
} else {
ans = term();
}
return ans;
}
/*
* Exponentiation solver.
*/
double exp(){
boolean neg = false;
if( s.charAt( 0 ) == '-' ){
neg = true;
s = s.substring( 1 );
}
double ans = term();
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 = term();
if( ans < 0 ){ // if it's negative
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;
} else {
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.
*/
double trig(){
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( "cos" ) == 0 ){
s = s.substring( 3 );
ans = Math.cos( trig() );
found = true;
} else if( s.indexOf( "tan" ) == 0 ){
s = s.substring( 3 );
ans = Math.tan( trig() );
found = true;
}
if( !found ){
ans = term();
}
return ans;
}
/*
* Multiplication, division expression solver.
*/
double mul(){
double ans = term();
while( s.length() > 0 ){
if( s.charAt( 0 ) == '*' ){
s = s.substring( 1 );
ans *= term();
} else if( s.charAt( 0 ) == '/' ){
s = s.substring( 1 );
ans /= term();
} else break;
}
return ans;
}
/*
* Addition, subtraction expression solver.
*/
double add(){
double ans = term();
while( s.length() > 0 ){
if( s.charAt( 0 ) == '+' ){
s = s.substring( 1 );
ans += term();
} else if( s.charAt( 0 ) == '-' ){
s = s.substring( 1 );
ans -= term();
} else{
break;
}
}
return ans;
}
/*
* Public access method to evaluate this expression.
*/
public double evaluate(){
s = x.intern();
return term();
}
/*
* Creates new Expression.
*/
public Expression( String s ){
// 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.
*/
public String toString(){
return x.intern();
}
/*
* Test our Expression class by evaluating the command-line
* argument and then returning.
*/
public static void main( String[] args ){
Expression e = new Expression( (args[0]) );
//error on the above line ?
System.out.println( e + " = " + e.evaluate() );
}
}