modify code

master
algorithmzuo 2 years ago
parent e933c1fa58
commit 94f49558a6

@ -10,72 +10,48 @@ public class Code04_ExpressionCompute {
} }
// 请从str[i...]往下算,遇到字符串终止位置或者右括号,就停止 // 请从str[i...]往下算,遇到字符串终止位置或者右括号,就停止
// 返回两个值长度为2的数组 int[] ans // 返回两个值长度为2的数组
// ans[0] 负责的这一段的结果是多少 // 0) 负责的这一段的结果是多少
// ans[1] 负责的这一段,算到了哪个位置停止的 // 1) 负责的这一段计算到了哪个位置
public static int[] f(char[] str, int i) { public static int[] f(char[] str, int i) {
// 12 + 345 - 5 * LinkedList<String> queue = new LinkedList<String>();
LinkedList<String> que = new LinkedList<String>();
int cur = 0; int cur = 0;
int[] bra = null; int[] bra = null;
// 从i出发开始撸串 // 从i出发开始撸串
while (i < str.length && str[i] != ')') { while (i < str.length && str[i] != ')') {
if (str[i] >= '0' && str[i] <= '9') { //遇到数字字符 if (str[i] >= '0' && str[i] <= '9') {
cur = cur * 10 + str[i++] - '0'; cur = cur * 10 + str[i++] - '0';
} else if (str[i] != '(') { // 遇到的是运算符号 } else if (str[i] != '(') { // 遇到的是运算符号
// cur = 34 str[i] = '+' addNum(queue, cur, str[i++]);
addNum(que, cur);
que.addLast(String.valueOf(str[i++]));
cur = 0; cur = 0;
} else { // 遇到左括号了 } else { // 遇到左括号了
// (
// i i+1
bra = f(str, i + 1); bra = f(str, i + 1);
cur = bra[0]; cur = bra[0];
i = bra[1] + 1; i = bra[1] + 1;
} }
} }
// 1终止了2遇到右括号了 addNum(queue, cur, '+');
addNum(que, cur); return new int[] { getAns(queue), i };
return new int[] { getNum(que), i };
} }
public static void addNum(LinkedList<String> que, int num) { public static void addNum(LinkedList<String> queue, int num, char op) {
if (!que.isEmpty()) { if (!queue.isEmpty() && (queue.peekLast().equals("*") || queue.peekLast().equals("/"))) {
int cur = 0; String top = queue.pollLast();
String top = que.pollLast(); int pre = Integer.valueOf(queue.pollLast());
if (top.equals("+") || top.equals("-")) { num = top.equals("*") ? (pre * num) : (pre / num);
que.addLast(top);
} else {
cur = Integer.valueOf(que.pollLast());
num = top.equals("*") ? (cur * num) : (cur / num);
}
} }
que.addLast(String.valueOf(num)); queue.addLast(String.valueOf(num));
queue.addLast(String.valueOf(op));
} }
public static int getNum(LinkedList<String> que) { public static int getAns(LinkedList<String> queue) {
int res = 0; int ans = Integer.valueOf(queue.pollFirst());
boolean add = true; while (queue.size() > 1) {
String cur = null; String op = queue.pollFirst();
int num = 0; int num = Integer.valueOf(queue.pollFirst());
while (!que.isEmpty()) { ans += op.equals("+") ? num : -num;
cur = que.pollFirst();
if (cur.equals("+")) {
add = true;
} else if (cur.equals("-")) {
add = false;
} else {
num = Integer.valueOf(cur);
res += add ? num : (-num);
}
} }
return res; return ans;
}
public static void main(String[] args) {
String test = "52+13*(5+2-(5*(23-4)))-40+((3*2)+(5/2))";
System.out.println(calculate(test));
} }
} }

@ -25,7 +25,7 @@ public class Code01_ExpressionCompute {
// 0) 负责的这一段的结果是多少 // 0) 负责的这一段的结果是多少
// 1) 负责的这一段计算到了哪个位置 // 1) 负责的这一段计算到了哪个位置
public static int[] f(char[] str, int i) { public static int[] f(char[] str, int i) {
LinkedList<String> que = new LinkedList<String>(); LinkedList<String> queue = new LinkedList<String>();
int cur = 0; int cur = 0;
int[] bra = null; int[] bra = null;
// 从i出发开始撸串 // 从i出发开始撸串
@ -33,8 +33,7 @@ public class Code01_ExpressionCompute {
if (str[i] >= '0' && str[i] <= '9') { if (str[i] >= '0' && str[i] <= '9') {
cur = cur * 10 + str[i++] - '0'; cur = cur * 10 + str[i++] - '0';
} else if (str[i] != '(') { // 遇到的是运算符号 } else if (str[i] != '(') { // 遇到的是运算符号
addNum(que, cur); addNum(queue, cur, str[i++]);
que.addLast(String.valueOf(str[i++]));
cur = 0; cur = 0;
} else { // 遇到左括号了 } else { // 遇到左括号了
bra = f(str, i + 1); bra = f(str, i + 1);
@ -42,41 +41,28 @@ public class Code01_ExpressionCompute {
i = bra[1] + 1; i = bra[1] + 1;
} }
} }
addNum(que, cur); addNum(queue, cur, '+');
return new int[] { getNum(que), i }; return new int[] { getAns(queue), i };
} }
public static void addNum(LinkedList<String> que, int num) { public static void addNum(LinkedList<String> queue, int num, char op) {
if (!que.isEmpty()) { if (!queue.isEmpty() && (queue.peekLast().equals("*") || queue.peekLast().equals("/"))) {
int cur = 0; String top = queue.pollLast();
String top = que.pollLast(); int pre = Integer.valueOf(queue.pollLast());
if (top.equals("+") || top.equals("-")) { num = top.equals("*") ? (pre * num) : (pre / num);
que.addLast(top);
} else {
cur = Integer.valueOf(que.pollLast());
num = top.equals("*") ? (cur * num) : (cur / num);
}
} }
que.addLast(String.valueOf(num)); queue.addLast(String.valueOf(num));
queue.addLast(String.valueOf(op));
} }
public static int getNum(LinkedList<String> que) { public static int getAns(LinkedList<String> queue) {
int res = 0; int ans = Integer.valueOf(queue.pollFirst());
boolean add = true; while (queue.size() > 1) {
String cur = null; String op = queue.pollFirst();
int num = 0; int num = Integer.valueOf(queue.pollFirst());
while (!que.isEmpty()) { ans += op.equals("+") ? num : -num;
cur = que.pollFirst();
if (cur.equals("+")) {
add = true;
} else if (cur.equals("-")) {
add = false;
} else {
num = Integer.valueOf(cur);
res += add ? num : (-num);
}
} }
return res; return ans;
} }
} }

@ -14,7 +14,7 @@ public class Code01_ExpressionCompute {
// 0) 负责的这一段的结果是多少 // 0) 负责的这一段的结果是多少
// 1) 负责的这一段计算到了哪个位置 // 1) 负责的这一段计算到了哪个位置
public static int[] f(char[] str, int i) { public static int[] f(char[] str, int i) {
LinkedList<String> que = new LinkedList<String>(); LinkedList<String> queue = new LinkedList<String>();
int cur = 0; int cur = 0;
int[] bra = null; int[] bra = null;
// 从i出发开始撸串 // 从i出发开始撸串
@ -22,8 +22,7 @@ public class Code01_ExpressionCompute {
if (str[i] >= '0' && str[i] <= '9') { if (str[i] >= '0' && str[i] <= '9') {
cur = cur * 10 + str[i++] - '0'; cur = cur * 10 + str[i++] - '0';
} else if (str[i] != '(') { // 遇到的是运算符号 } else if (str[i] != '(') { // 遇到的是运算符号
addNum(que, cur); addNum(queue, cur, str[i++]);
que.addLast(String.valueOf(str[i++]));
cur = 0; cur = 0;
} else { // 遇到左括号了 } else { // 遇到左括号了
bra = f(str, i + 1); bra = f(str, i + 1);
@ -31,41 +30,28 @@ public class Code01_ExpressionCompute {
i = bra[1] + 1; i = bra[1] + 1;
} }
} }
addNum(que, cur); addNum(queue, cur, '+');
return new int[] { getNum(que), i }; return new int[] { getAns(queue), i };
} }
public static void addNum(LinkedList<String> que, int num) { public static void addNum(LinkedList<String> queue, int num, char op) {
if (!que.isEmpty()) { if (!queue.isEmpty() && (queue.peekLast().equals("*") || queue.peekLast().equals("/"))) {
int cur = 0; String top = queue.pollLast();
String top = que.pollLast(); int pre = Integer.valueOf(queue.pollLast());
if (top.equals("+") || top.equals("-")) { num = top.equals("*") ? (pre * num) : (pre / num);
que.addLast(top);
} else {
cur = Integer.valueOf(que.pollLast());
num = top.equals("*") ? (cur * num) : (cur / num);
}
} }
que.addLast(String.valueOf(num)); queue.addLast(String.valueOf(num));
queue.addLast(String.valueOf(op));
} }
public static int getNum(LinkedList<String> que) { public static int getAns(LinkedList<String> queue) {
int res = 0; int ans = Integer.valueOf(queue.pollFirst());
boolean add = true; while (queue.size() > 1) {
String cur = null; String op = queue.pollFirst();
int num = 0; int num = Integer.valueOf(queue.pollFirst());
while (!que.isEmpty()) { ans += op.equals("+") ? num : -num;
cur = que.pollFirst();
if (cur.equals("+")) {
add = true;
} else if (cur.equals("-")) {
add = false;
} else {
num = Integer.valueOf(cur);
res += add ? num : (-num);
}
} }
return res; return ans;
} }
} }

Loading…
Cancel
Save