(十三)解析算术表达式

来源:互联网 发布:php微商城 编辑:程序博客网 时间:2024/06/05 15:42

栈:先进后出

中缀表达式:A+B*C

后缀表达式:ABC*+

1.中缀表达式转后缀表达式


StackX.java(构造一个栈)


package JavaAppInFix;/** * 构造一个栈 * @author qingshuang * */public class StackX {   private char[] stackArray;   private int maxSize;   private int top;   public StackX(int s){   maxSize=s;   stackArray=new char[maxSize];   top=-1;   }   //添加数据   public void push(char j){   stackArray[++top]=j;   }   //查看并删除数据   public char pop(){   return stackArray[top--];   }   //查看数据   public char peek(){   return stackArray[top];   }   public boolean isEmpty(){   return top==-1;      }   public int size(){   return top+1;   }   public char peekN(int n){   return stackArray[n];   }   public void dispalyStack(String s){   System.out.print(s);   System.out.print("Stack (bottom-->top):");   for(int j=0;j<size();j++){   System.out.print(peekN(j)+" ");   }   System.out.println();   }}

InToPost.java(中缀表达式转成后缀表达式)


package JavaAppInFix;/** * 中缀表达式转成后缀表达式 *  * @author qingshuang *  */public class InToPost {private StackX theStack;private String input;// 中缀表达式private String output="";// 后缀表达式public InToPost(String in) {input = in;int stackSize = input.length();theStack = new StackX(stackSize);}//4+3*(6-2)public String doTrans() {for (int j = 0; j < input.length(); j++) {char ch = input.charAt(j);theStack.dispalyStack("For " + ch + " ");switch (ch) {case '+':case '-':gotOper(ch, 1);break;case '*':case '/':gotOper(ch, 2);break;case '(':theStack.push(ch);break;case ')':gotParen(ch);break;default:output = output + ch;break;}}// for循环结束while (!theStack.isEmpty()) {theStack.dispalyStack("while");output = output + theStack.pop();}theStack.dispalyStack("End");return output;}public void gotOper(char opThis, int perc1) {       while(!theStack.isEmpty()){       char opTop=theStack.pop();       if(opTop=='('){       theStack.push(opTop);       break;       }else{       int prec2;       if(opTop=='+'||opTop=='-')prec2=1;       else prec2=2;       if(prec2<perc1){       theStack.push(opTop);       break;       }else{output=output+opTop;}       }       }       theStack.push(opThis);}public void gotParen(char ch) {       while(!theStack.isEmpty()){       char chx=theStack.pop();       if(chx=='('){       break;       }else{       output=output+chx;       }       }}}

InfixApp.java(中缀表达式转后缀表达式测试)


package JavaAppInFix;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class InfixApp {/**中缀表达式转后缀表达式测试 * @param args * @throws IOException  */public static void main(String[] args) throws IOException {         String input,output;         while(true){         System.out.print("Enter infix:");         System.out.flush();         input=getString();         if(input.equals(""))break;         InToPost theTrans=new InToPost(input);         output=theTrans.doTrans();         System.out.println("Postfix is "+output+"\n");         }         }public static String getString() throws IOException{InputStreamReader isr=new InputStreamReader(System.in);BufferedReader br=new BufferedReader(isr);String s=br.readLine();return s;}}

2.中缀表达式转后缀表达式后可运算


StackY.java(构造一个栈)


package JavaAppInFix;/** * 中缀表达式转后缀表达式后可运算 * @author qingshuang * */public class StackY {   private int[] stackArray;   private int maxSize;   private int top;   public StackY(int s){   maxSize=s;   stackArray=new int[maxSize];   top=-1;   }   //添加数据   public void push(int j){   stackArray[++top]=j;   }   //查看并删除数据   public int pop(){   return stackArray[top--];   }   //查看数据   public int peek(){   return stackArray[top];   }   public boolean isEmpty(){   return top==-1;   }   public boolean isFull(){   return top==maxSize-1;   }   public int size(){   return top+1;   }   public int peekN(int n){   return stackArray[n];   }   public void dispalyStack(String s){   System.out.print(s);   System.out.print("Stack (bottom-->top):");   for(int j=0;j<size();j++){   System.out.print(peekN(j)+" ");   }   System.out.println();   }}

ParsePost.java(运算后的后缀表达式)


package JavaAppInFix;/** * 运算后缀表达式 * @author qingshuang * */public class ParsePost {    private StackY theStack;    private String input;    public ParsePost(String s){    input=s;    }    public int doParse(){    theStack=new StackY(20);    char ch;    int j;    int num1,num2,interAns;//num1,num2取出来的两操作数,interAns这两数的运算结果    for(j=0;j<input.length();j++){    ch=input.charAt(j);    theStack.dispalyStack(" "+ch+" ");    if(ch>='0'&&ch<='9'){    theStack.push((int)(ch-'0'));    }else{    num2=theStack.pop();    num1=theStack.pop();    switch(ch){    case'+':    interAns=num1+num2;    break;    case'-':    interAns=num1-num2;    break;    case'*':    interAns=num1*num2;    break;    case'/':    interAns=num1/num2;    break;    default:    interAns=0;    }    theStack.push(interAns);    }    }    interAns=theStack.pop();    return interAns;    }}

PostfixApp.java(运算后缀表达式测试类)


package JavaAppInFix;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;/** * 运算后缀表达式 * @author qingshuang * */public class PostfixApp {public static void main(String[] args) throws IOException {         String input;         int output;         InToPost inobj;         while(true){         System.out.print("Enter infix:");         System.out.flush();         input=getString();         if(input.equals(""))break;         inobj=new InToPost(input);              input=inobj.doTrans();//中缀表达式变后缀表达式              System.out.println("后缀表达式 to  "+input);              ParsePost theTrans=new ParsePost(input);              output=theTrans.doParse();              System.out.println("Evaluates to  "+output);         }}public static String getString() throws IOException{InputStreamReader isr=new InputStreamReader(System.in);BufferedReader br=new BufferedReader(isr);String s=br.readLine();return s;}}


0 0
原创粉丝点击