3+2*3
4+5*(6-7)
3*(2*(5-3)+1)/2+8-9*(8-1)
算数表达式,比如: a*(b+c)/d+e
如果求值?
一般的过程是分两步,
一、将中缀式-》后缀式。
算法过程是:
栈底放‘#’,从左至右逐字读取中缀式:
a.当当前字符为数字时,直接输出;
b.当当前字符为"("时,将其压栈;
c.当当前字符为")"时,则弹出堆栈中最上的"("之前的所有运算符并输出,然后删除堆栈中的"(" ;
d.当当前字符为运算符时,则依次弹出堆栈中优先级大于等于当前运算符的,输出,再将当前运算符压栈;
e.当为"#"时,弹出所有栈中的内容输出
中缀式:a*(b+c)/d+e
后缀式:abc+*d/e+
代码实现:
import java.util.Scanner; import java.util.Stack; public class Poland { public boolean isOpr(char c){ if(c=='+'||c=='-'||c=='*'||c=='/'){ return true; } return false; } public int getOprValue(char c){ if(c=='*'||c=='/'){ return 3; } else if(c=='+'||c=='-'){ return 2; } else if(c=='#'){ return 0; } else if(c=='('){ return 1; } else { return 0; } } public String convert(String infixExpression){ Stack<Character> stack=new Stack<Character>(); String postfixExpression=""; stack.push('#'); postfixExpression=""; int i=0; int n=infixExpression.length(); while(!stack.isEmpty()){ for(;i<n;i++){ char mychar=infixExpression.charAt(i); if(Character.isWhitespace(mychar)){ continue; } else if(Character.isDigit(mychar)||mychar=='.'){ if(i+1<=n-1&&Character.isDigit(infixExpression.charAt(i+1))){ postfixExpression+=mychar; } else { postfixExpression+=mychar+" "; } } else if(mychar=='('){ stack.push(mychar); //System.out.println(Arrays.toString(stack.toArray())); } else if(mychar==')'){ char c=stack.pop(); while(c!='('){ postfixExpression+=c+" "; c=stack.pop(); } //System.out.println(Arrays.toString(stack.toArray())); } else if(isOpr(mychar)){ if(mychar=='-'){ int j=i-1; if(j<0){ postfixExpression+=mychar; continue; } char tc=infixExpression.charAt(j); while(Character.isSpaceChar(tc)){ j--; if(j<0)break; tc=infixExpression.charAt(j); } if(j<0||tc=='('||isOpr(tc)){ postfixExpression+=mychar; continue; } } char c=stack.pop(); if(getOprValue(mychar)<=getOprValue(c)) while(getOprValue(mychar)<=getOprValue(c)){ postfixExpression+=c+" "; c=stack.pop(); } stack.push(c); stack.push(mychar); //System.out.println(Arrays.toString(stack.toArray())); } } char cc=stack.pop(); if(cc!='#'){ postfixExpression+=cc+" "; } } return postfixExpression; } private boolean isOpr2(String opr){ if(opr.equals("+")||opr.equals("-")||opr.equals("/")||opr.equals("*")){ return true; } return false; } private String doCalculate(String str1, String str2, String opr){ double a=Double.parseDouble(str1); double b=Double.parseDouble(str2); double c=0; if(opr.equals("+")){ c=a+b; } else if(opr.equals("-")){ c=b-a; } else if(opr.equals("*")){ c=a*b; } else if(opr.equals("/")){ c=b/a; } //System.out.println(str2+opr+str1+"=="+c); return c+""; } public void calculate(String postfixExpr){ Stack<String> stack=new Stack<String>(); stack.push("#"); String s[]=postfixExpr.split(" "); for(int i=s.length-1;i>=0;i--){ stack.push(s[i]); } Stack<String> stack2=new Stack<String>(); while(!stack.isEmpty()){ String str=stack.pop(); if(str.equals("#")){ break; } while(!isOpr2(str)){ stack2.push(str); str=stack.pop(); } String str1,str2; str1=stack2.pop(); str2=stack2.pop(); stack2.push(doCalculate(str1,str2,str)); } System.out.println(stack2.pop()); } public static void main(String args[]){ Poland po=new Poland(); Scanner sc=new Scanner(System.in); while(sc.hasNext()){ String str=sc.nextLine(); String pos=""; pos=po.convert(str.trim()); System.out.println(pos); po.calculate(pos); } } }