import java.util.Deque;
import java.util.LinkedList;
/**
* Created by lxw, liwei4939@126.com on 2017/11/1.
* 公式字符串求值
*/
public class stringValue {
public int getValue(String str){
return value(str.toCharArray(), 0)[0];
}
public int[] value(char[] chars, int i){
Deque<String> deq = new LinkedList<String>();
int pre = 0;
int[] bar = [];
while (i < chars.length && chars[i] != ')'){
if(chars[i] > '0' && chars[i] < '9'){
pre = pre * 10 + chars[i++] - '0';
} else if(chars[i] != '('){
addNum(deq, pre);
deq.addLast(String.valueOf(chars[i++]));
} else {
bar = value(chars, i+1);
pre = bar[0];
i = bar[1] + 1;
}
}
addNum(deq, pre);
return int[]{getNum(deq), i};
}
public void addNum(Deque<String> deque, int num){
if(!deque.isEmpty()){
int cur = 0;
String top = deque.pollLast();
if(top.equals("+") || top.equals("-")){
deque.addLast(top);
} else {
cur = Integer.valueOf(deque.pollLast());
num = top.equals("*") ? (cur * num) : (cur / num);
}
}
deque.addLast(String.valueOf(num));
}
public int getNum(Deque<String> deque){
int res = 0;
boolean add = true;
String cur = null;
int num = 0;
while (!deque.isEmpty()){
cur = deque.pollFirst();
if(cur.equals("+")){
add= true;
} else if(cur.equals("-")){
add = false;
} else {
num = Integer.valueOf(cur);
res += add ? num : (-num);
}
}
return res;
}
}
公式字符串求值
最新推荐文章于 2022-12-28 20:03:17 发布