使用数据结构实现计算器功能-java

java 代码
 
  1. package cacu;  
  2. /** 
  3.  * myejb6.0 CacuStack.java 
  4.  */  
  5.   
  6. /** 
  7.  * @author sunxboy 
  8.  * 
  9.  * 10:19:05 AM May 30, 2007 
  10.  */  
  11. public class CacuStack {  
  12.   
  13.     CacuNode top;  
  14.       
  15.     public CacuStack() {  
  16.         top = null;  
  17.     }  
  18.       
  19.     public boolean isEmpty() {  
  20.           
  21.         return top==null;  
  22.     }  
  23.       
  24.     public boolean push(int data) {  
  25.         CacuNode node = new CacuNode(data);  
  26.         if(isEmpty()) {  
  27.             top = node;  
  28.             return true;  
  29.         }else {  
  30.             node.next=top;  
  31.             top = node;  
  32.             return true;  
  33.         }  
  34.           
  35.     }  
  36.       
  37.     public int pop() {  
  38.         int data = top.data;  
  39.         top = top.next;  
  40.         return data;  
  41.     }  
  42.       
  43.     public static void main(String[] args) {  
  44.   
  45.         CacuStack stack=new CacuStack ();  
  46.         stack.push(1);  
  47.         stack.push(2);  
  48.         stack.push(3);  
  49.         stack.push(4);  
  50.         stack.pop();  
  51.         stack.pop();  
  52.         while(!stack.isEmpty())  
  53.         {  
  54.             System.out.println(stack.pop());  
  55.         }  
  56.     }  
  57. }  

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

java 代码
 
  1. /** 
  2.  *  
  3.  */  
  4. package cacu;  
  5.   
  6. /** 
  7.  * @author sunxboy 
  8.  *  
  9.  */  
  10. public class Cacu {  
  11.   
  12.     CacuStack cs;  
  13.   
  14.     public int cacu(String source) {  
  15.         cs = new CacuStack();  
  16.         for (int i = 0; i < source.length(); i++) {  
  17.             char ch = source.charAt(i);  
  18.             // 如果是数字  
  19.             if (ch <= '9' && ch >= '0') {  
  20.                 cs.push(Integer.parseInt(ch+""));   //注意这里不能用Integer.valueOf(ch);  
  21.             } else {  
  22.                 int num2 = cs.pop();  
  23.                 int num1 = cs.pop();  
  24.                 switch (ch) {  
  25.                 case '+':  
  26.                     cs.push(num1+num2);  
  27.                     break;  
  28.                 case '-':  
  29.                     cs.push(num1-num2);  
  30.                     break;  
  31.                 case '*':  
  32.                     cs.push(num1*num2);  
  33.                     break;  
  34.                 case '/':  
  35.                     cs.push(num1/num2);  
  36.                     break;  
  37.                   
  38.                 }  
  39.             }  
  40.         }  
  41.         return cs.pop();  
  42.     }  
  43.       
  44.     public static void main(String[] args) {  
  45.         InToPost post=new InToPost();  
  46.         Cacu cacu=new Cacu();  
  47.     //  (a+b*c)*(e+f+m+n*j)  
  48. //      System.out.println(post.doTrans("1+1"));  
  49. //      System.out.println(cacu.cacu(post.doTrans(("1+1"))));  
  50.         System.out.println(post.doTrans("(9+2*7)*(5+6+8+9*5)"));  
  51.         System.out.println(cacu.cacu(post.doTrans("(9+2*7)*(5+6+8+9*5)")));  
  52.           
  53.     }  


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

java 代码
 
  1. /** 
  2.  *  
  3.  */  
  4. package cacu;  
  5.   
  6. /** 
  7.  * @author sunxboy 
  8.  * 
  9.  */  
  10. public class CacuNode {  
  11.   
  12.     int data;  
  13.     CacuNode next;  
  14.     public CacuNode(int data) {  
  15.         this.data = data;  
  16.     }  
  17. }  

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

java 代码
 
  1. /** 
  2.  * struture Stack.java 
  3.  */  
  4. package cacu;  
  5.   
  6. /** 
  7.  * @author sunxboy 
  8.  * 
  9.  * 10:20:22 AM May 30, 2007 
  10.  */  
  11. public class Stack {  
  12.   
  13.     private Node top;  
  14.     public Stack() {  
  15.         top=null;  
  16.     }  
  17.       
  18.     public boolean push(char data) {  
  19.         Node newNode = new Node(data);  
  20.         if(isEmpty()) {  
  21.             top= newNode;  
  22.             return true;  
  23.         }else {  
  24.             newNode.next = top;  
  25.             top = newNode;  
  26.             return true;  
  27.         }  
  28.     }  
  29.       
  30.     public char pop(){  
  31.         char data = top.data;  
  32.         top =top.next;  
  33.         return data;  
  34.     }  
  35.       
  36.     public boolean isEmpty() {  
  37.         return top==null;  
  38.     }  
  39.       
  40.     public static void main(String[] args) {  
  41.         Stack stack=new Stack();  
  42.         stack.push('a');  
  43.         stack.push('b');  
  44.         stack.push('c');  
  45.         stack.push('d');  
  46.         stack.pop();  
  47.         stack.pop();  
  48.         while(!stack.isEmpty())  
  49.         {  
  50.             System.out.println(stack.pop());  
  51.         }  
  52.     }  
  53. }  

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

java 代码
 
  1. /** 
  2.  * struture Node.java 
  3.  */  
  4. package cacu;  
  5.   
  6. /** 
  7.  * @author sunxboy 
  8.  * 
  9.  * 10:20:13 AM May 30, 2007 
  10.  */  
  11. public class Node {  
  12.   
  13.     public char data;  
  14.     public Node next;  
  15.     public Node(char data) {  
  16.         this.data = data;  
  17.     }  
  18. }  

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

java 代码
 
  1. /** 
  2.  * struture InToPost.java 
  3.  */  
  4. package cacu;  
  5.   
  6. /** 
  7.  * @author sunxboy 
  8.  *  
  9.  * 10:20:04 AM May 30, 2007 
  10.  */  
  11. public class InToPost {  
  12.   
  13.     String output = "";  
  14.   
  15.     Stack stack;  
  16.   
  17.     public String doTrans(String source) {  
  18.   
  19.         stack = new Stack();  
  20.         for (int i = 0; i < source.length(); i++) {  
  21.             char ch = source.charAt(i);  
  22.             switch (ch) {  
  23.             case '+':  
  24.             case '-':  
  25.                 doOper(ch,1);  
  26.                 break;  
  27.             case '*':  
  28.             case '/':  
  29.                 doOper(ch,2);  
  30.                 break;  
  31.             case '(':  
  32.                 stack.push(ch);  
  33.                 break;  
  34.             case ')':  
  35.                 getParent();  
  36.                 break;  
  37.             default:   
  38.                 output = output + ch;  
  39.                 break;  
  40.             }  
  41.         }  
  42. //       将栈中全部取出  
  43.         while (!stack.isEmpty()) {  
  44.   
  45.             output = output + stack.pop();  
  46.         }  
  47.         return this.output;  
  48.     }  
  49.   
  50.     private void getParent() {  
  51.         while(!stack.isEmpty()) {  
  52.             char ch = stack.pop();  
  53.             if(ch=='('){  
  54.                 break;  
  55.             }else{  
  56.                 output=output+ch;  
  57.             }  
  58.         }  
  59.           
  60.     }  
  61.   
  62.     public void doOper(char ch1, int lev) {  
  63.   
  64.         while (!stack.isEmpty()) {  
  65.             char popch = stack.pop();  
  66.             if (popch == '(') {  
  67.                 stack.push(popch);  
  68.                 break;  
  69.             } else {  
  70.                 int lev2;  
  71.                 if (popch == '+' || popch == '-') {  
  72.                     lev2 = 1;  
  73.                 } else {  
  74.                     lev2 = 2;  
  75.                 }  
  76.   
  77.                 // 将栈中取出的与传入的比较优先级  
  78.                 if (lev > lev2) {  
  79.                     stack.push(popch); // 将低级别的放回去  
  80.                     break;  
  81.                 } else {  
  82.                     output = output + popch;  
  83.                 }  
  84.             }  
  85.   
  86.         }  
  87.         stack.push(ch1);    //将高级别放在外层  
  88.     }  
  89.       
  90.     public static void main(String[] args) {  
  91.         InToPost post=new InToPost();  
  92.         String temp="(a+b*c)*(e+f+m+n*j)";  
  93.         //abc*+ef+m+nj*+*  
  94.         System.out.println(post.doTrans(temp));  
  95.           
  96.     }  
  97.   
  98. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值