import java.util.Stack;publicclassSuffix{static Stack<Character> op =newStack<>();/*将中缀表达式转化为后缀表达式*/publicstatic String tosuffix(String str){char[] str1=str.toCharArray();int len=str1.length;
String res="";for(int i=0;i<len;i++){char ch=str1[i];if(ch==' '){continue;}if(ch>='0'&&ch<='9'){
res=res+ch;continue;}if(ch=='('){
op.push(ch);}if(ch=='+'||ch=='-'){while(!op.isEmpty()&&op.peek()!='('){
res+=op.pop();}
op.push(ch);continue;}if(ch=='*'||ch=='/'){while(!op.isEmpty()&&(op.peek()=='*'||op.peek()=='/')){res=res+op.pop();}
op.push(ch);continue;}if(ch==')'){while(!op.isEmpty()&&op.peek()!='('){
res+=op.pop();}
op.pop();continue;}}while(!op.empty()){res+=op.pop();}return res;}//求值publicstatic Float getValue(String str){
Stack<Float> v =newStack<>();char[] arr = str.toCharArray();int len = arr.length;for(int i =0; i < len; i++){
Character ch = arr[i];// if is operand, push to the stack if(ch >='0'&& ch <='9')
v.push(Float.valueOf(ch -'0'));// if is operator, calculate the result // with top 2 operands in the stack, // push the result into the stack else v.push(caculate(ch, v.pop(), v.pop()));}return v.pop();}publicstatic Float caculate(char op,Float num1,Float num2){switch(op){case'+':return num1+num2;case'-':return num2-num1;case'/':return num2/num1;case'*':return num2*num1;default:return Float.valueOf(0);}}publicstaticvoidmain(String[] args){
String str="5+4*3";
System.out.println(getValue(tosuffix(str)));}}
栈的一个应用—关于中缀和后缀表达式求值中缀表达式直接转化为后缀表达式求值:import java.util.Stack;public class Suffix { static Stack<Character> op = new Stack<>(); /*将中缀表达式转化为后缀表达式*/ public static String tosuffix...