package ;
import ;
public class ArithExpression {
private String expression;
private String errorMessage;
private String result = "0";
public ArithExpression() {
expression = null;
errorMessage = null;
}
public ArithExpression(String str) {
expression = str;
}
public boolean isDigit(char ch) {
String digits = "0123456789";
if((ch) == -1) {
return false;
} else {
return true;
}
}
public boolean isOperator(char ch) {
String operators = "+-*/";
if((ch) == -1) {
return false;
} else {
return true;
}
}
public boolean isParenthese(char ch) {
String parentheses = "()";
if((ch) == -1) {
return false;
} else {
return true;
}
}
private int charCount(char ch) {
int count = 0;
for(int i=0; i<(); i++) {
if((i) == ch) {
count++;
}
}
return count;
}
public boolean isLegal() {
if(() == 0) {
errorMessage = "表达式为空……";
return false;
}
for(int i=0; i<(); i++) {
if("0123456789.+-*/()".indexOf((i))== -1) {
errorMessage = "表达式中第"+(i+1)+"个字符不合表达式的要求!请仔细检查……";
return false;
}
}
if(charCount('(') != charCount(')')) {
errorMessage = "请检查括号个数是否匹配!";
return false;
}
if("-0123456789(".indexOf((0))==-1 ||
"0123456789)".indexOf((()-1))==-1){
errorMessage = "请检查第一个字符或者最后一个字符是否合法!";
return false;
}
for(int j=1; j<()-1; j++) {
if((j) == '(') {
if("+-*/(".indexOf((j-1))==-1 ||
"0123456789(-".indexOf((j+1))==-1) {
errorMessage = "表达式中第"+(j+1)+"个字符'('处有错误,请仔细检查……";
return false;
}
}
if((j) == ')') {
if("+-*/)".indexOf((j+1))==-1 ||
"0123456789)".indexOf((j-1))==-1) {
errorMessage = "表达式中第"+(j+1)+"个字符')'处有错误,请仔细检查……";
return false;
}
}
if(isOperator((j))) {
if((j) == '-'
&& ("0123456789)(".indexOf(((j-1)))!=-1 || "".indexOf(((j-1)))!=-1)
&& "0123456789(".indexOf((j+1))!=-1) {
continue;
} else if((j) == '/' && (j+1) == '0') {
errorMessage = "被除数为0是不符合算术运算的……";
return false;
} else {
if("0123456789)".indexOf(((j-1)))==-1
|| "0123456789(".indexOf((j+1))==-1) {
errorMessage = "表达式中第"+(j+1)+"个字符处有错误,请仔细检查……";
return false;
}
}
}
if((j) == '.') {
if(!isDigit((j-1)) || !isDigit((j+1))) {
errorMessage = "表达式中第"+(j+1)+"个字符'.'处有错误,请仔细检查……";
return false;
}
}
}
return true;
}
public String getErrorMessage() {
return errorMessage;
}
private String value(String str) {
Stack<Double> staOperand = new Stack<Double>();
Stack<String> staOperator = new Stack<String>();
Double res = new Double(0);
int j = 0;
if((0) == '-') {
str = "0"+str;
}
for(int i=0; i<(); i++) {
if(i == ()-1) {
res = new Double(((j, i+1)));
(res);
while(!()) {
double a = ();
double b = ();
switch(().charAt(0)) {
case '+': res = new Double(a+b); break;
case '-': res = new Double(b-a); break;
case '*': res = new Double(a*b); break;
case '/': res = new Double(b/a); break;
}
(res);
}
break;
}
if("+-*/".indexOf((i)) != -1) {
if((i) == '-') {
if("+-*/".indexOf((i-1)) != -1) {
j = i;
continue;
}
}
(new Double(((j, i))));
j = i+1;
if(()) {
((i)+"");
} else {
if("*/".indexOf((i))!=-1 && "+-".indexOf(())!=-1) {
((i)+"");
}
if("*/".indexOf((i))!=-1 && "*/".indexOf(())!=-1) {
double a = ();
double b = ();
if(().equals("*")) {
res = new Double(a*b);
} else {
res = new Double(b/a);
}
((i)+"");
(res);
}
if("+-".indexOf((i))!=-1){
double a = ();
double b = ();
switch(().charAt(0)) {
case '+': res = new Double(a+b); break;
case '-': res = new Double(b-a); break;
case '*': res = new Double(a*b); break;
case '/': res = new Double(b/a); break;
}
((i)+"");
(res);
}
}
}
}
return res+"";
}
public String getResult() {
String strTemp = expression;
String resTemp = "0";
int left = 0;
int right = 0;
while((')') != -1) {
right = (')');
left = (0, right).lastIndexOf('(');
resTemp = value((left+1, right));
strTemp = (0, left) + resTemp +(right+1);
}
result = value(strTemp);
return result;
}
public String toString() {
return expression;
}
}