Class Expression-Tính biểu thức


/**
* Author: Lai The Anh
* Class: K52CC
* Student code: 07020009
* Purpose: Cacutate Expression
*/
import java.util.*;
import java.lang.*;
public class Expression {
private String as_Expression = "";
public Expression(int _value){
as_Expression += Integer.toString(_value);
}
public Expression(char _value){
as_Expression += Character.toString(_value);
}
public Expression append(char _sign, int _value){
as_Expression += Character.toString(_sign);
as_Expression += Integer.toString(_value);
return this;
}
public Expression append(char _sign, char _value){
as_Expression += Character.toString(_sign);
as_Expression += Character.toString(_value);
return this;
}//

<span id="more-593"></span>
public boolean setValue(char _var, int _value){
String var = Character.toString(_var);
String pattern = "(.*)"+var+"(.*)";
if(as_Expression.matches(pattern)){
as_Expression = as_Expression.replaceAll(var,Integer.toString(_value));
return true;
}
return false;
}
public int eval(){
return caculate(toRpn(as_Expression));
}
public String toString(){
return as_Expression;
}
private String correctEx (String _ex){
char ch;
String var,pattern;
Scanner in = new Scanner(System.in);
int value;
for(ch='a';ch< ='z';ch++){
var=Character.toString(ch);
pattern="(.*)"+var+"(.*)";
if(_ex.matches(pattern)){
System.out.print("You must enter value for \'"+var+"\' to continue\n"+var+"?=");
value = in.nextInt();
_ex = _ex.replaceAll(var,Integer.toString(value));
}
}
return _ex;
}
private String standardizeEx (String _ex){
if(_ex.matches("^(.*)[a-z](.*)$")) _ex=correctEx (_ex);
_ex = _ex.replaceAll(" ","");
if(_ex.charAt(0)!='('){
_ex = "("+_ex;
_ex = _ex+")";
}
while(_ex.matches("(.*)(\\+\\-|\\-\\-|\\+\\+)(.*)")){
_ex=_ex.replaceAll("\\+\\-","-");
_ex=_ex.replaceAll("\\-\\-","+");
_ex=_ex.replaceAll("\\+\\+","+");
}
_ex=_ex.replaceAll("(\\d*)(\\+|\\-|\\*|\\/|\\(|\\))(\\d*)","$1 $2 $3");
_ex=_ex.replaceAll("(\\+|\\-|\\*|\\/|\\()( +)-( +)(\\d+)","$1 -$4");
_ex=_ex.replaceAll(" "," ");
_ex=_ex.trim();
return _ex;
}
private int priority(String sign){
if(sign.matches("\\+|\\-")) return 1;
if(sign.matches("\\*|\\/")) return 2;
return 0;
}
private String toRpn(String _ex){
_ex = standardizeEx (_ex);
String [] partsOfEx = _ex.split(" ");
Stack sign = new Stack();
String rpnString = "";
for(String iter <img src='http://as247.vui360.com/blog/wp-includes/images/smilies/10.gif' alt=':-p' class='wp-smiley' /> artsOfEx){
if(iter.matches("-\\d+|\\d+"))
rpnString += iter+" ";
else{
switch(iter.charAt(0)){
case '(': sign.push(iter); break;
case '+': case '-': case '*': case '/':
while(!sign.isEmpty() && priority(iter) < = priority(sign.peek())) rpnString += sign.pop()+" ";
sign.push(iter);
break;
case ')':
while(sign.peek().charAt(0)!= '(') rpnString += sign.pop() + " ";
if(!sign.isEmpty()) sign.pop();
break;
}
}
}
while(!sign.isEmpty())rpnString += sign.pop() + " ";
return rpnString;
}
private String caculate(String _value2, String _value1, String sign){
int _return=0;
if(sign.equals("+")) _return = Integer.parseInt(_value1) + Integer.parseInt(_value2);
if(sign.equals("-")) _return = Integer.parseInt(_value1) - Integer.parseInt(_value2);
if(sign.equals("*")) _return = Integer.parseInt(_value1) * Integer.parseInt(_value2);
if(sign.equals("/")) _return = Integer.parseInt(_value1) / Integer.parseInt(_value2);
return Integer.toString(_return);
}
public int caculate(String rpn){
String [] partsOfEx = rpn.split(" ");
Stack stack = new Stack();
for(String iter <img src='http://as247.vui360.com/blog/wp-includes/images/smilies/10.gif' alt=':-p' class='wp-smiley' /> artsOfEx){
if(iter.matches("-\\d+|\\d+")) stack.push(iter);
if(iter.matches("\\+|\\-|\\*|\\/")){
stack.push(caculate(stack.pop(),stack.pop(),iter));
}
}
return Integer.parseInt(stack.peek());
}
}

Trả lời

© 2010-2013 As247 Blog. All Rights Reserved. 0,764