首先要知道什么是前缀表达式,什么是中缀表达式,什么是后缀表达式
所谓的中缀表达式就是类似于这种的运算 1+((2+3)×4)-5
所谓的前缀表达式就是符号在两个操作数的前面 - + 1 × + 2 3 4 5
所谓的后缀表达式就是两个数在运算符的右边 3 4 + 5 × 6 -
这样我们就知道了前中后缀表达式所代表的含义是什么,在我们人的脑子看来,这个是一个很简单的运算操作,但是你要把他放在电脑上面,中缀表达式我感觉是比较难的,因为你要考虑运算符的优先级,括号的情况等等,我们可以将其转换成为前缀表达式或者后缀表达式来进行操作,其实原理都是一样的。
我们这里用前缀表达式做例子:
我们使用一个栈来运算,我们的优先顺序是从右往左遍历
- 当遇到数字的时候,压栈
- 当遇到运算符的时候,栈顶的两个元素出栈,计算之后再入栈
就像我们的
-×+3456
- 遇到6,压栈
- 遇到5,压栈
- 遇到4,压栈
- 遇到3,压栈
- 遇到+,取出栈顶的3和4,计算之后,压栈
- 遇到*,取出栈顶的7和5,计算之后,压栈
- 遇到-,取出栈顶的35和6,计算之后,压栈
- 最后存在栈里面的就是最终的结果
package com.zhongruan.testDemo;
import java.util.*;
/**
* @author weirdo
* @date 2020-10-30 11:24
* @email 1435380700@qq.com
*/
public class Main {
public static void main(String[] args) {
Main m = new Main();
String str = "-×+3456";
System.out.println(m.preCalculate(str));
}
/**
* 通过前缀表达式求值
* 从右到左扫描,扫到数字进栈,扫到操作运算符就取出两个,进行计算然后压栈
* @param str
* @return
*/
public int preCalculate(String str){
//bianli
Stack<Integer> number = new Stack<>();
for(int i=str.length()-1;i>=0;i--){
if(str.charAt(i)>='0' && str.charAt(i)<='9'){
number.push(str.charAt(i)-'0');
}else{
switch (str.charAt(i)){
case '+':{
int num1 = number.pop();
int num2 = number.pop();
number.push(num1+num2);
break;
}
case '-':{
int num1 = number.pop();
int num2 = number.pop();
number.push(num1-num2);
break;
}
case '×':{
int num1 = number.pop();
int num2 = number.pop();
number.push(num1*num2);
break;
}
case '/':{
int num1 = number.pop();
int num2 = number.pop();
number.push(num1/num2);
break;
}
default:
break;
}
}
}
//最后应该只剩下一个数字
return number.pop();
}
}
那么我们的中缀表达式这么变成前缀表达式/后缀表达式呢?其实都是一样的,一个正序,一个倒序。。
我们使用两个栈,stack1 和 stack2 来存储
- 将前缀表达式从后往前遍历,如果当前的是数字的话,压入stack1
- 如果是操作运算符的话,且stack2为空的话,直接压栈
- 否则则要比较运算符的优先级(第一优先级()、第二优先级* /、第三优先级 + -)
- 如果当前的优先级比栈顶的运算符优先级高的话,则栈顶的运算符出栈stack1.push(stack2.pop());
- 最后将stack2种的数字压入stack1,得到的就是后缀表达式,反转一下就是前缀表达式
package com.zhongruan.testDemo;
import java.util.*;
/**
* @author weirdo
* @date 2020-10-30 11:24
* @email 1435380700@qq.com
*/
public class Main {
public static void main(String[] args) {
Main m = new Main();
// String str = "-×+3456";
// System.out.println(m.preCalculate(str));
System.out.println(m.toPreExpression("1+((2+3)*4)-5"));
System.out.println("- + 1 × + 2 3 4 5");
}
/**
* 通过前缀表达式求值
* 从右到左扫描,扫到数字进栈,扫到操作运算符就取出两个,进行计算然后压栈
* @param str
* @return
*/
public int preCalculate(String str){
//bianli
Stack<Integer> number = new Stack<>();
for(int i=str.length()-1;i>=0;i--){
if(str.charAt(i)>='0' && str.charAt(i)<='9'){
number.push(str.charAt(i)-'0');
}else{
switch (str.charAt(i)){
case '+':{
int num1 = number.pop();
int num2 = number.pop();
number.push(num1+num2);
break;
}
case '-':{
int num1 = number.pop();
int num2 = number.pop();
number.push(num1-num2);
break;
}
case '×':{
int num1 = number.pop();
int num2 = number.pop();
number.push(num1*num2);
break;
}
case '/':{
int num1 = number.pop();
int num2 = number.pop();
number.push(num1/num2);
break;
}
default:
break;
}
}
}
//最后应该只剩下一个数字
return number.pop();
}
/**
* 中缀表达式转化成为前缀表达式
* @return
*/
public String toPreExpression(String str){
Map<Character,Integer> map = new HashMap<>();
map.put('+',0);
map.put('-',0);
map.put('*',1);
map.put('/',1);
map.put(')',2);
Stack<Character> stack1 = new Stack<>();
Stack<Character> stack2 = new Stack<>();
for(int i=str.length()-1;i>=0;i--){
char c = str.charAt(i);
if(c >='0' && c <='9'){
//如果是数字
stack1.push(c);
}else{
if(stack2.isEmpty()){
stack2.push(c);
}else{
if(c==')'){
stack2.push(c);
}else if(c=='('){
while (stack2.peek()!=')' && !stack2.isEmpty()){
stack1.push(stack2.pop());
}
stack2.pop();
}else{
//比较运算符优先级
while (!stack2.isEmpty() && map.get(stack2.peek())<map.get(c)){
stack1.push(stack2.pop());
}
stack2.push(c);
}
}
}
}
while (!stack2.isEmpty()){
stack1.push(stack2.pop());
}
return stack1.toString();
}
}