java代码解法,简单记录,方法比较笨,后续有机会继续改进。
import java.util.ArrayList;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println(cal(trans(strToExp(str))));
}
//判断当前字符串是否是数字,包括负数
public static boolean isNum(String str){
for(int i=0;i<str.length();i++){
if(i==0 && str.charAt(i) == '-' && str.length()>1){
continue;
}
if(!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
//获取符号优先级,用作比较
public static int getLv(String str){
if("(".equals(str)) return 0;
else if("-".equals(str)) return 1;
else if("+".equals(str)) return 1;
else if("/".equals(str)) return 2;
else if("*".equals(str)) return 2;
else return -1;
}
//计算后缀表达式
public static int cal(List<String> res){
if(res.size() == 1){
return Integer.parseInt(res.get(1));
}
List<String> stack = new ArrayList<>();
int top = -1;
for(int i=0;i<res.size();i++){
String str = res.get(i);
if(isNum(str)){
stack.add(str);
top++;
}
else {
int x2 = Integer.parseInt(stack.get(top));
int x1 = Integer.parseInt(stack.get(top-1));
if("+".equals(str)){
Integer ans = x1+x2;
stack.remove(top);
stack.set(--top,ans.toString());
}
else if("-".equals(str)){
Integer ans = x1-x2;
stack.remove(top);
stack.set(--top,ans.toString());
}
else if("*".equals(str)){
Integer ans = x1*x2;
stack.remove(top);
stack.set(--top,ans.toString());
}
else if("/".equals(str)){
Integer ans = x1/x2;
stack.remove(top);
stack.set(--top,ans.toString());
}
}
}
return Integer.parseInt(stack.get(top));
}
//把拆分好的字符串数组改为后缀表达式
public static List<String> trans(List<String> arr){
List<String> res = new ArrayList<>();
List<String> stack = new ArrayList<>();
int top = -1;
for(int i=0;i<arr.size();i++){
String str = arr.get(i);
if(isNum(str)){
res.add(str);
}
else if(top == -1){
stack.add(str);
top++;
}
else if("(".equals(str) || "[".equals(str) || "{".equals(str)){
stack.add(str);
top++;
}
else if(")".equals(str)){
while(!"(".equals(stack.get(top))){
res.add(stack.get(top));
stack.remove(top);
top--;
}
stack.remove(top);
top--;
}
else if("]".equals(str)){
while(!"[".equals(stack.get(top))){
res.add(stack.get(top));
stack.remove(top);
top--;
}
stack.remove(top);
top--;
}
else if("}".equals(str)){
while(!"{".equals(stack.get(top))){
res.add(stack.get(top));
stack.remove(top);
top--;
}
stack.remove(top);
top--;
}
else if(getLv(str)<=getLv(stack.get(top))){
res.add(stack.get(top));
stack.remove(top--);
while(top>-1 && getLv(str)<=getLv(stack.get(top))){
res.add(stack.get(top));
stack.remove(top--);
}
top++;
stack.add(str);
}
else if(getLv(str)>getLv(stack.get(top))){
stack.add(str);
top++;
}
}
while(top!=-1){
res.add(stack.get(top));
top--;
}
return res;
}
//将一个完整字符串拆分为一个字符串数组
public static List<String> strToExp(String str){
int len = str.length();
StringBuffer sb = new StringBuffer();
List<String> arr = new ArrayList<>();
Character ch;
for(int i=0;i<len;i++){
ch = str.charAt(i);
//考虑负号的情况
if((i==0 && len>1 && ch=='-') || (i>0 && ch=='-' && (str.charAt(i-1)=='(' || str.charAt(i-1)=='[' || str.charAt(i-1)=='{'))){
sb.append(ch);
}
//将数字合并
else if(Character.isDigit(ch)){
sb.append(ch);
if((i+1<len && !Character.isDigit(str.charAt(i+1))) || (i+1==len)){
arr.add(sb.toString());
sb.setLength(0);
}
}
//考虑其他加减乘除括号的情况
else{
sb.append(ch);
arr.add(sb.toString());
sb.setLength(0);
}
}
return arr;
}
}