基于java的数据结构学习手记5——后缀表达式生成与计算
来源:互联网 发布:源码资本官网 编辑:程序博客网 时间:2024/05/17 09:27
解释算术表达式:算术表达式,例如:4+5,3*4+7,(3+5)*(7-4),它用到的存储结构是栈。对于计算机的算法来说如果要直接求算术表达式的值,还是相当困难的。因此分两步实现算法会较容易。
1.将算术表达式转换成另一种形式:后缀表达式。
2.计算后缀表达式的值。
后缀表达式:日常算术表达式将操作符(operator)(+,-,*,/)放在两个操作数(operands)(数字,或代表数字的字母)之间的。因为操作符写在操作数的中间,所以把这种写法称为中缀表达法。例如2+3,5*7或者字母代替数字,如A+B,A/B等。在后缀表达式(也叫波兰逆序表达式Reverse Polish Notation RPN,由波兰数学家发明)中,操作符跟在两个操作数的后面,这样A+B就成为AB+,A/B成为AB/。更复杂的中缀表达式同样可以转换为后缀表达式。
将中缀表达式转化为后缀表达式的Java代码如下:
- package Stack;
- public class InToPost //infix to postfix conversion
- {
- private infix theInfix;
- private String input;
- private String output="";
- //...........................................................
- public InToPost(String in) //constructor
- {
- input=in;
- int stackSize=input.length();
- theInfix=new infix(stackSize);
- }
- //...........................................................
- public String doTrans() //do translation to postfix
- {
- for(int j=0;j<input.length();j++)
- {
- char ch=input.charAt(j);
- theInfix.displayStack("For "+ch+" ");//diagnostic
- switch(ch)
- {
- case'+':
- case'-':
- gotOper(ch,1);
- break;
- case'*':
- case'/':
- gotOper(ch,2);
- break;
- case'(':
- theInfix.push(ch);
- break;
- case')':
- gotParen(ch);
- break;
- default:
- output=output+ch;
- break;
- } //end switch
- } //end for
- while(!theInfix.isEmpty())
- {
- theInfix.displayStack("while");
- output=output+theInfix.pop();
- }
- theInfix.displayStack("End ");
- return output;
- }
- //...........................................................
- public void gotOper(char opThis,int prec1)
- {
- while(!theInfix.isEmpty())
- { //got operator from input
- char opTop=theInfix.pop();
- if(opTop=='(')
- {
- theInfix.push(opTop);
- break;
- }
- else
- {
- int prec2; //precedence of new op
- if(opTop=='+'||opTop=='-')
- prec2=1;
- else
- prec2=2;
- if(prec2<prec1)
- {
- theInfix.push(opTop);
- break;
- }
- else
- output=output+opTop;
- }//end else
- }//end while
- theInfix.push(opThis);//push new operator
- }//end gotOp()
- //...........................................................
- public void gotParen(char ch)
- {
- while(!theInfix.isEmpty())
- {
- char chx=theInfix.pop();
- if(chx=='(')break;
- else
- output=output+chx;
- }//end while
- } //end popOps()
- //...........................................................
- }//end class InToPost
应用类创建一个InToPost对象,初始化参数是输入的字符串。接着带哦用对象的doTrans()方法执行转换。这个方法返回后缀表达式字符串,并显示返回结果。
doTrans()方法用switch语句来出来输入字符串中的每个字符,当读到操作符时,它调用gotOper()方法,而读到右括号“)”时调用gotParen()方法,这些方法实现了对所有操作符的复杂规则。
2.后缀表达的计算:
后缀表达式的计算相对比较简单,所不同的是,这次就把操作数用栈保存起来,遇到操作符则取出栈顶的2个数计算后再入栈,依次直到操作符处理完全。计算后缀表达式的类如下:
- package Stack;
- public class ParsePost {
- private StackX theStack;
- private String input;
- //...................................................
- public ParsePost(String s)
- {input=s;}
- //...................................................
- public int doParse()
- {
- theStack=new StackX(20);
- char ch;
- int j;
- int num1,num2,interAns;
- for(j=0;j<input.length();j++)
- {
- ch=input.charAt(j);
- theStack.displayStack("For "+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;
- }//end switch
- theStack.push(interAns);
- }//end else
- }//end for
- return theStack.pop();
- }//end doParse()
- }//end class parsePost
其应用类如下:
- package Stack;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class PostfixApp {
- /**
- * @param args
- */
- public static void main(String[] args)throws IOException
- {
- // TODO Auto-generated method stub
- String input;
- int output;
- while(true)
- {
- System.out.print("Enter Postfix: ");
- System.out.flush();
- input=getString();
- if(input.equals(""))break;
- ParsePost theParse=new ParsePost(input);
- output=theParse.doParse();
- System.out.println("Evaluates to: "+output+'/n');
- }//end while
- }//end main
- //...........................................................
- public static String getString()throws IOException
- {
- InputStreamReader in=new InputStreamReader(System.in);
- BufferedReader br=new BufferedReader(in);
- String s=br.readLine();
- return s;
- }
- }//end stackApps
计算结果如下:
- Enter Postfix: 345+*612+/-
- For 3 Stack(bottom->top):
- For 4 Stack(bottom->top):3
- For 5 Stack(bottom->top):3 4
- For + Stack(bottom->top):3 4 5
- For * Stack(bottom->top):3 9
- For 6 Stack(bottom->top):27
- For 1 Stack(bottom->top):27 6
- For 2 Stack(bottom->top):27 6 1
- For + Stack(bottom->top):27 6 1 2
- For / Stack(bottom->top):27 6 3
- For - Stack(bottom->top):27 2
- Evaluates to: 25
- Enter Postfix:
- 基于java的数据结构学习手记5——后缀表达式生成与计算
- 基于java的数据结构学习手记3——栈
- 基于java的数据结构学习手记4——队列
- 基于java的数据结构学习手记1
- 基于java的数据结构学习手记2
- 数据结构学习实录四—后缀表达式的计算(C++)
- 数据结构与算法分析计算后缀表达式
- 基于java的数据结构学习手记9--双向链表
- 基于栈计算后缀表达式的值
- 数据结构与算法Java版——中缀表达式转后缀表达式
- 《java数据结构与算法》笔记-CH4-8栈结构实现后缀表达式计算结果
- 计算后缀表达式、Java
- 大话数据结构——栈的应用四则元算表达式求值(后缀表达式计算结果)
- 基于java的数据结构学习手记13-基于递归的快速排序
- C++数据结构与STL--栈的应用--后缀表达式的计算
- 【Java数据结构的实现】之系列三栈的实现(使用栈计算后缀表达式)
- 编译原理之后缀表达式生成与计算
- 编译原理之后缀表达式生成与计算(2)
- 在ASP处理程序时显示进度
- 响应鼠标的图片明暗变化
- 矢量字库的设计与实现
- 胡润:中国什么行业最赚钱
- 嗨~~~~
- 基于java的数据结构学习手记5——后缀表达式生成与计算
- 嘘~~~
- 一些错误需要知道
- 向您请教 点击看看
- lincux下C语言嵌入汇编
- 驱动程序的注册
- 时间不等人
- 无题
- 一位黑客眼中的国产“杀毒软件”