5.28 中缀转后缀及指定变量运算 作业
Topic source解决多位数分离,只需要可在遍历字符并压栈时添加一flag,判断上一字符是否为数字类型:
1.如果上了字符为数字,删除栈尾元素,将删除的元素与当前数字字符组合压入到栈尾;
2.如果上一字符不为数字,直接将当前数字压入栈尾。
原代码处:
static SuffixExpression compile(String exp) {...}
修改为:
static SuffixExpression compile(String exp) {
// TODO:
Deque<String> deque = new LinkedList<>();
char[] chars = exp.toCharArray();
boolean lastisnum=false;//添加判断旗帜
for (char c : chars) {
if (getYorN(c)) {
deque.offerFirst(String.valueOf(c));
lastisnum=false;//非数字
} else if (getIntegYorN(c)) {
if (lastisnum){
deque.offerLast(deque.pollLast()+String.valueOf(c));
}
else {
deque.offerLast(String.valueOf(c));
}
lastisnum=true;//是数字
} else {
lastisnum=false;//非数字
continue;
}
}
System.out.println(deque.toString());
return new SuffixExpression(deque);
}
- 1
ANGERIED