package com.gxmedu.reverse_polish_notation_calculator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
public class Calculator {
public static void main(String[] args) {
Calculator calculator = new Calculator();
List<String> strings = calculator.toInfixExpression("1+((2+3)*4)-5");
System.out.println(strings);
List<String> suffixExpression = calculator.toSuffixExpression(strings);
System.out.println(suffixExpression);
System.out.println(calculator.calculate(suffixExpression));
}
/**
* 将一个字符串按数字和符号存入到一个ArrayList中
*/
public List<String> toInfixExpression(String str){
ArrayList<String> infixExpression = new ArrayList<>();
// int index = 0;
char c;
String concatNum = "";
for (int i = 0; i < str.length(); i++) {
c = str.charAt(i);
if(c >= '0' && c <= '9'){
concatNum += c;
// 接着判断下一位是不是数字,这里不能用j,还是要用i,不然数字要重复。
int j = i + 1;
// i++;
while(j < str.length() && str.charAt(j) >= '0' && str.charAt(j) <= '9'){
concatNum += str.charAt(j);
i = j++;
}
infixExpression.add(concatNum);
concatNum = "";
}else{
infixExpression.add(c + "");
}
}
return infixExpression;
// String[] s = str.split(" ");
遍历此字符串数组
// for (String s1 : s) {
// String concat = "";
// if(s1.matches("\\d+")){
// concat += s1;
// }else{
判断如果此时concat不为空,说明已经到了一段数字的最后一位,可以整体放入list中
放完后再置空
// if(!"".equals(concat)){
// infixExpression.add(concat);
// concat = "";
// }else{
// infixExpression.add(s1);
// }
// }
// }
// return infixExpression;
}
/**
* 将中缀表达式转化为后缀表达式
*/
public List<String> toSuffixExpression(List<String> list){
// 定义一个符号栈
Stack<String> signs = new Stack<>();
ArrayList<String> suffixExpression = new ArrayList<>();
for (String s : list) {
if(s.matches("\\d+")){
suffixExpression.add(s);
}else if("(".equals(s)){
signs.push(s);
}else if(")".equals(s)){
while(!signs.peek().equals("(") && !signs.isEmpty()){
suffixExpression.add(signs.pop());
}
signs.pop();
}else {
while(!signs.isEmpty() && operatorPriority(signs.peek()) >= operatorPriority(s)){
suffixExpression.add(signs.pop());
}
signs.push(s);
}
}
// 将剩下符号栈中的符号加入后缀表达式中
while(!signs.isEmpty()){
suffixExpression.add(signs.pop());
}
return suffixExpression;
}
/**
* 判断运算符优先级
* @param operator
* @return
*/
public int operatorPriority(String operator){
int priority = 0;
switch(operator){
case "+":
case "-":
priority = 1;
break;
case "*":
case "/":
priority = 2;
break;
case "(":
case ")":
break;
default:
System.out.println("运算符有误!");
}
return priority;
}
/**
* 用后缀表达式进行计算
* @param suffixExpression
* @return
*/
public int calculate(List<String> suffixExpression){
Stack<String> calculator = new Stack<>();
int result = 0;
Iterator<String> iterator = suffixExpression.iterator();
while(iterator.hasNext()){
String s = iterator.next();
if(s.matches("\\d+")){
calculator.push(s);
}else{
int num2 = Integer.parseInt(calculator.pop());
int num1 = Integer.parseInt(calculator.pop());
result = getResult(num1,num2,s);
calculator.push(result + "");
}
}
return result;
}
/**
* 识别运算符进行具体计算
* @return
*/
public int getResult(int num1,int num2,String operator){
switch(operator){
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "*":
return num1 * num2;
case "/":
return num1 / num2;
default:
throw new RuntimeException("运算符有误");
}
}
}
java reverse_polish_notation_calculator
最新推荐文章于 2024-06-05 19:09:50 发布