package Shallwe.calculator;
class Without {
public static void main(String args[]) {
String s = new String("3-(1+2*3)+(3*3)");
String ss = new String("3-7+(3*3)");
String sss = new String("2+5*(4+2)-2+1+(2+2)");
s = dismiss(sss);
int result = calculator(s);
System.out.println(result);
}
private static String dismiss(String k) {
String temps = k;
char[] one = new char[temps.length()];
one = temps.toCharArray();
int[] kk = new int[k.length()];
int kk_index = 0;
for (int i = 0; i < one.length; i++) {
if (one[i] == 40) {
kk[kk_index] = i;
kk_index++;
} else if (one[i] == 41) {
kk[kk_index] = i;
kk_index++;
}
}
if (kk[1] == 0) {
return temps;
} else {
String temp = temps.substring(kk[0] + 1, kk[1]);
int xx = calculator(temp);
String xxx = String.valueOf(xx);
String xxxx = temps.substring(kk[0], kk[1] + 1);
String eh = temps.replace(xxxx, xxx);
kk = new int[eh.length()];
return dismiss(eh);
}
}
private static int calculator(String s) {
String words = s;
char[] temp = null;
words = words.trim();
temp = words.toCharArray();
int n = 0;
int c = 0;
int char_c = 0;
int result = 0;
int[] m = new int[words.length()];
char CHAR[] = new char[words.length()];
int[] f = new int[words.length()];
for (int i = 0; i < words.length(); i++) {
if (temp[i] < 58 && temp[i] > 47) {
switch (temp[i]) {
case 48:
m[i] = 0;
break;
case 49:
m[i] = 1;
break;
case 50:
m[i] = 2;
break;
case 51:
m[i] = 3;
break;
case 52:
m[i] = 4;
break;
case 53:
m[i] = 5;
break;
case 54:
m[i] = 6;
break;
case 55:
m[i] = 7;
break;
case 56:
m[i] = 8;
break;
case 57:
m[i] = 9;
break;
}
n = 10 * n + m[i];
} else if (temp[i] == 43) {
f[c] = n;
c = c + 1;
n = 0;
CHAR[char_c] = temp[i];
char_c = char_c + 1;
} else if (temp[i] == 42) {
f[c] = n;
c = c + 1;
n = 0;
CHAR[char_c] = temp[i];
char_c = char_c + 1;
} else if (temp[i] == 45) {
f[c] = n;
c = c + 1;
n = 0;
CHAR[char_c] = temp[i];
char_c = char_c + 1;
} else if (temp[i] == 47) {
f[c] = n;
c = c + 1;
n = 0;
CHAR[char_c] = temp[i];
char_c = char_c + 1;
} else {
System.out.println("貌似算不了额");
break;
}
if (i == words.length() - 1) {
f[c] = n;
n = 0;
}
}
for (int i = 0; CHAR.length > i; i++) {
switch (CHAR[i]) {
case 42:
f[i + 1] = f[i] * f[i + 1];
f[i] = 0;
CHAR[i] = 43;
break;
case 47:
f[i + 1] = f[i] / f[i + 1];
f[i] = 0;
CHAR[i] = 43;
break;
}
}
for (int i = 0; CHAR.length > i; i++) {
if (i == 0) {
result = f[0];
}
switch (CHAR[i]) {
case 43:
result = f[i + 1] + result;
break;
case 45:
result = result - f[i + 1];
break;
}
}
return result;
}
}