java表达式计算
来源:互联网 发布:产品演示动画制作软件 编辑:程序博客网 时间:2024/05/21 01:43
这两天看数据结构,看到处理表达式结果这部分,书上用的办法是把表达式转化为后缀的,然后用栈计算。算法想明白了,但是一直没有时间实现,先把别人的代码贴上来。
class Node{ //o:object public E o; //p:priority public int p; //l:left,r:right public Node l,r; public Node(E o){ this.o = o; }}class Stack { private Node cur; private int size = 0; public void push(E o){ Node n = new Node (o); if(cur!=null){ cur.r = n; n.l = cur; } cur = n; size++; } public E pop(){ if(size==0) return null; try{ size--; return (E)cur.o; }finally{ cur = cur.l; if(cur!=null) cur.r = null; } } public int size(){ return size; }}class Tree{ private Node cur,root,start; private Stack nodes; private String s[],result; public void insert(String s[]){ if(s.length<=2) return; int x = Parser.X_INIT; nodes = new Stack (); //create a root in order to get a start //and solve the condition of starting with "(" cur = new Node (Parser.operators[0]); cur.l = new Node ("0"); cur.p = Parser.p(Parser.operators[0])+x; root = cur; start = root; for(int i=0;i n = new Node (s[i]); //while s is () , increase or decrease its priority x += s[i].equals("(")?Parser.X_STEP:s[i].equals(")")?-Parser.X_STEP:0; if(Parser.isOperator(s[i])){ n.p = Parser.p(s[i])+x; //while this node's priority is less than the previous' //then roll back while(cur.p>n.p) rollBack(); //while this node's priority is bigger than the previous' //then connect this to the right child //and move the origin right child to its left child if(cur.p0) rollBack(); //remove the temp start: //find the node which left child is the temp start //then connect its left child to the temp start's right child if(start==root){ root = start.r; }else{ cur = root; while(cur.l.l.l!=null) cur = cur.l; cur.l = cur.l.r; } } //roll back to the parent tree private void rollBack(){ Node temp = nodes.pop(); temp.r = cur; cur = temp; if(nodes.size()==0) root = temp; } //iterate the tree prefixally public String prefix(){ result = ""; prefixIterate(root); return result; } private void prefixIterate(Node n){ if(n==null) return; result += n.o+" "; prefixIterate(n.l); prefixIterate(n.r); } //iterate the tree postfixally public String postfix(){ result = ""; postfixIterate(root); return result; } private void postfixIterate(Node n){ if(n==null) return; postfixIterate(n.l); postfixIterate(n.r); result += n.o+" "; }}class Calculator{ //get the result from postfix expression public static double calculateInPostfix(String s[]){ Stack num = new Stack (); double result = 0; for(int i=0;i
- java表达式计算
- 表达式计算java实现
- 表达式计算//java类
- 表达式计算//java类
- Java 简单表达式计算
- Java 表达式的计算
- Java表达式计算-Fel
- JAVA 计算算式表达式
- java计算表达式
- 蓝桥杯 JAVA 表达式计算
- java字符串表达式计算
- 计算后缀表达式、Java
- 表达式计算 Java算法
- 数学表达式计算程序(java)
- java数学表达式计算算法
- Java实现四则运算表达式计算
- java计算表达式的值
- java中计算字符串表达式
- 第二十四課 文法
- 编程实例:输出50以内7的倍数
- js实现多个打开一个或多个链接
- SQL Server 索引基础知识(3)----测试中一些常看的指标和清除缓存的方法
- TCP数据流传输遇到的问题
- java表达式计算
- C语言Blog
- 使用Ajax自动填写表单
- asfafasf
- YetAnotherForum备忘录2
- SQL Server 索引基础知识(4)----主键与聚集索引
- jquery下拉菜单
- Java设计模式-----Bridge桥接模式
- [转载]java中用URLConnection 类post方式提交表单