仅适用于习题类型的表达式,对于 x*y*z+m、x+(y-z)*m + n这类型的表达式题并不适合
package StackTest;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static tools.LogUtils.log;
public class rulesCalcInStack {
public static void main(String[] args) {
String exp = "1 + 2 * (9 - 5)";
SuffixExpression se = compile(exp);
int result = se.execute();
log.info(exp + " = " + result + " " + (result == 1 + 2 * (9 - 5) ? "✓" : "✗"));
}
static SuffixExpression compile(String exp) {
Deque<String> num=new LinkedList<>();
Deque<String> opr=new LinkedList<>();
String[] list =null;
String regEx="[^0-9]";
String regExOpr="[^+-/%/*]";
//从exp提取数字并入栈
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(exp);
if (m.find()) {
list=m.replaceAll(",").trim().toString().replaceAll(",+", ",").split(",");
//log.info(list[1]);
for(String str:list) {
num.push(str);
//从exp提取运算符号并入栈
p= Pattern.compile(regExOpr);
m = p.matcher(exp);
list =null;
String str=trimChar(m.replaceAll(",").trim().toString().replaceAll(",+", ","),',');
log.info(str);
list=str.split(",");
for(String operator:list) {
opr.push(operator);
return new SuffixExpression(num,opr);
//去除字符串首尾指定的字符
public static String trimChar(String str,char ch) {
if(str.charAt(0)==ch) {
str=str.substring(1);
};
if(str.charAt(str.length()-1)==ch) {
str=str.substring(0, str.length()-1);
//log.info(str);
return str;
class SuffixExpression {
private Deque<String> num=new LinkedList<>();
private Deque<String> opr=new LinkedList<>();
public SuffixExpression(Deque<String> num,Deque<String> opr) {
this.num=num;
this.opr=opr;
//计算
int execute() {
int result=0;
do {
String operator=this.opr.pop();
String num1=this.num.pop();
String num2=this.num.pop();
switch (operator) {
case "-":
result=Integer.parseInt(num2)-Integer.parseInt(num1);
break;
case "+":
result=Integer.parseInt(num2)+Integer.parseInt(num1);
case "/":
result=Integer.parseInt(num2)/Integer.parseInt(num1);
case "*":
result=Integer.parseInt(num2)*Integer.parseInt(num1);
case "%":
default:
throw new IllegalArgumentException("Unexpected operator: " + operator);
this.num.push(String.valueOf(result));
} while (this.num.size()>1);
log.info(this.num.peek());
return Integer.valueOf(this.num.peek());
更正
result=Integer.parseInt(num2)%Integer.parseInt(num1);
Sign in to make a reply
净净一隅
仅适用于习题类型的表达式,对于 x*y*z+m、x+(y-z)*m + n这类型的表达式题并不适合
package StackTest;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static tools.LogUtils.log;
public class rulesCalcInStack {
public static void main(String[] args) {
String exp = "1 + 2 * (9 - 5)";
SuffixExpression se = compile(exp);
int result = se.execute();
log.info(exp + " = " + result + " " + (result == 1 + 2 * (9 - 5) ? "✓" : "✗"));
}
static SuffixExpression compile(String exp) {
Deque<String> num=new LinkedList<>();
Deque<String> opr=new LinkedList<>();
String[] list =null;
String regEx="[^0-9]";
String regExOpr="[^+-/%/*]";
//从exp提取数字并入栈
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(exp);
if (m.find()) {
list=m.replaceAll(",").trim().toString().replaceAll(",+", ",").split(",");
//log.info(list[1]);
}
for(String str:list) {
num.push(str);
}
//从exp提取运算符号并入栈
p= Pattern.compile(regExOpr);
m = p.matcher(exp);
list =null;
if (m.find()) {
String str=trimChar(m.replaceAll(",").trim().toString().replaceAll(",+", ","),',');
log.info(str);
list=str.split(",");
}
for(String operator:list) {
opr.push(operator);
}
return new SuffixExpression(num,opr);
}
//去除字符串首尾指定的字符
public static String trimChar(String str,char ch) {
if(str.charAt(0)==ch) {
str=str.substring(1);
};
if(str.charAt(str.length()-1)==ch) {
str=str.substring(0, str.length()-1);
}
//log.info(str);
return str;
}
}
class SuffixExpression {
private Deque<String> num=new LinkedList<>();
private Deque<String> opr=new LinkedList<>();
public SuffixExpression(Deque<String> num,Deque<String> opr) {
this.num=num;
this.opr=opr;
}
//计算
int execute() {
int result=0;
do {
String operator=this.opr.pop();
String num1=this.num.pop();
String num2=this.num.pop();
switch (operator) {
case "-":
result=Integer.parseInt(num2)-Integer.parseInt(num1);
break;
case "+":
result=Integer.parseInt(num2)+Integer.parseInt(num1);
break;
case "/":
result=Integer.parseInt(num2)/Integer.parseInt(num1);
break;
case "*":
result=Integer.parseInt(num2)*Integer.parseInt(num1);
break;
case "%":
result=Integer.parseInt(num2)*Integer.parseInt(num1);
break;
default:
throw new IllegalArgumentException("Unexpected operator: " + operator);
}
this.num.push(String.valueOf(result));
} while (this.num.size()>1);
log.info(this.num.peek());
return Integer.valueOf(this.num.peek());
}
}