(十三)解析算术表达式
来源:互联网 发布: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
- (十三)解析算术表达式
- 解析算术表达式字符串
- 堆栈解析算术表达式
- 堆栈-解析算术表达式
- 解析算术表达式
- 解析算术表达式
- 堆栈解析算术表达式(总结)
- 利用堆栈解析算术表达式
- dbms_aw.eval_number 解析算术表达式
- 利用堆栈解析算术表达式
- 利用栈解析算术表达式
- 利用栈解析算术表达式
- 解析简单的算术表达式(第1版)
- 后缀表达式与解析算术表达式
- 用栈解析算术表达式[Python版]
- 栈的应用:解析算术表达式
- 用MoonScript实现算术表达式解析
- 栈的应用-解析算术表达式
- Linux打开vim编辑器
- MySQL权限系统
- poj 3259 Wormholes【spfa判负环】
- centos 安装MySQL
- UIWebView与WKWebView
- (十三)解析算术表达式
- ubuntu远程桌面连接windows系统
- ubuntu12.04编译Android 4.0.3 Source+Kernel记
- 欧拉函数euler
- TCP协议如何保证可靠传输
- 矩阵论:向量求导/微分和矩阵微分
- spring整合hibernate事务管理的四种方式,以及事务的传播行为和隔离级别介绍
- XCTest单元测试
- vim插件介绍(一)之Tabular