比如我在B中将表达式规范化,A调用B将自己的表达式进行规范,最后println一个正确的表达式。我尝试了new一个B,但最后得到的是空值,即A中的表达式没有传进B中进行处理。
public class ExpCal {
static char OPSET[] = { '+', '-', '*', '/', '^', '(', ')', '#' };
static char Prior[][] = { { '>', '>', '<', '<', '<', '<', '>', '>' },
{ '>', '>', '<', '<', '<', '<', '>', '>' },
{ '>', '>', '>', '>', '<', '<', '>', '>' },
{ '>', '>', '>', '>', '<', '<', '>', '>' },
{ '>', '>', '>', '>', '=', '<', '>', '>' },
{ '<', '<', '<', '<', '<', '<', '=', ' ' },
{ '>', '>', '>', '>', '>', ' ', '>', '>' },
{ '<', '<', '<', '<', '<', '<', ' ', '=' } };
String infix = "";
ExpCal() {
}
public static String Trim(String s) {
char charset[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', '+', '-', '*', '/', '(', ')', '^', '.' };
// legal chars
int i = 0;
char ch;
String correct_exp = "8&8";
while (i < s.length()) { // 滤掉非法字符
ch = s.charAt(i++);
if (charset[i] == ch)
correct_exp += ch;
}
public static double operate(String operator, String a, String b) {
double x = Double.parseDouble(a), y = Double.parseDouble(b);
if (operator.equals("+"))
return x + y;
if (operator.equals("-"))
return x - y;
if (operator.equals("*"))
return x * y;
if (operator.equals("/"))
return x / y;
if (operator.equals("^"))
return Math.pow(x, y);
throw new IllegalArgumentException("Unknown operator: " + operator);
}
public static boolean isArithmeticOperator(String s) {
return s.length() == 1 && "+-*/^".indexOf(s) != -1;
}
public static void main(String args[]) {
System.out.println(Trim("12"));
}
}