表达式计算器java实现

    去年写的一个表达式计算器程序,支持+、-、*、%运算,代码很乱不好理解,不过可以正确运行,贴出来希望可以供大家参考。其中的LinkStack是一个栈,也是自己写的,一块贴出来。

Calculator.java

 

  
  
  
  
  1. import java.util.*; 
  2.  
  3. public class Calculator{ 
  4.  
  5.     public static String transform(String input){ 
  6.  
  7.         StringBuffer sb=new StringBuffer(); 
  8.  
  9.         LinkStack<Character> stack=new LinkStack<Character>(); 
  10.  
  11.         int charcount=input.length(); 
  12.  
  13.         char topOperator; 
  14.  
  15.         String date=""
  16.  
  17.         for(int i=0;i<charcount;i++){ 
  18.  
  19.             char test=input.charAt(i); 
  20.  
  21.             if(test>='0'&&test<='9'){ 
  22.  
  23.                 date=date+test; 
  24.  
  25.                 continue
  26.  
  27.                 } 
  28.  
  29.             else
  30.  
  31.                 if (date!="") { 
  32.  
  33.                     sb.append(date+","); 
  34.  
  35.                 } 
  36.  
  37.                 date=""
  38.  
  39.                 if(stack.Empty()){stack.push(test);continue;}    
  40.  
  41.                 switch(test){ 
  42.  
  43.                     case '+'
  44.  
  45.                     case '-'
  46.  
  47.                     case '*'
  48.  
  49.                     case '/':{ 
  50.  
  51.                             while(!stack.Empty()){ 
  52.  
  53.                                 topOperator=stack.peek(); 
  54.  
  55.                                 if(precedence(test)<=precedence(topOperator)){ 
  56.  
  57.                                     sb.append(stack.pop()+","); 
  58.  
  59.                                 } else { 
  60.  
  61.                                     break
  62.  
  63.                                 } 
  64.  
  65.                             } 
  66.  
  67.                             stack.push(test); 
  68.  
  69.                             break
  70.  
  71.                         } 
  72.  
  73.                     case '(':{stack.push(test);break;} 
  74.  
  75.                     case ')':{ 
  76.  
  77.                             while((topOperator=stack.pop())!='('
  78.  
  79.                                 { 
  80.  
  81.                                     sb.append(topOperator+","); 
  82.  
  83.                                 } 
  84.  
  85.                                 break
  86.  
  87.                             } 
  88.  
  89.                     default:break;                                   
  90.  
  91.                      
  92.  
  93.                     } 
  94.  
  95.                 } 
  96.  
  97.             } 
  98.  
  99.         sb.append(date+",");         
  100.  
  101.         while(!stack.Empty()){ 
  102.  
  103.             sb.append(stack.pop()+","); 
  104.  
  105.             } 
  106.  
  107.         return sb.toString(); 
  108.  
  109.     } 
  110.  
  111.     public  static int count(String input){ 
  112.  
  113.         LinkStack<Integer> stack=new LinkStack<Integer>(); 
  114.  
  115.         String[] str=input.split(","); 
  116.  
  117.         int result=0
  118.  
  119.         for(int i=0;i<str.length;i++){ 
  120.  
  121.             if(!str[i].equals("")){ 
  122.  
  123.                 if(!str[i].equals("+")&&!str[i].equals("-")&&!str[i].equals("*")&&!str[i].equals("/")){ 
  124.  
  125.                     stack.push(Integer.parseInt(str[i])); 
  126.  
  127.                                 } 
  128.  
  129.                 else
  130.  
  131.                     if(!stack.Empty()){ 
  132.  
  133.                         int num1=stack.pop(); 
  134.  
  135.                         int num2=stack.pop(); 
  136.  
  137.                         switch(str[i].charAt(0)){ 
  138.  
  139.                             case '+':{result=num2+num1;break;} 
  140.  
  141.                             case '-':{result=num2-num1;break;} 
  142.  
  143.                             case '*':{result=num2*num1;break;} 
  144.  
  145.                             case '/':{result=num2/num1;break;} 
  146.  
  147.                             }    
  148.  
  149.                     stack.push(result); 
  150.  
  151.                     } 
  152.  
  153.                 } 
  154.  
  155.             } 
  156.  
  157.         else  
  158.  
  159.             continue
  160.  
  161.         } 
  162.  
  163.         return result; 
  164.  
  165.     } 
  166.  
  167.     public  static int precedence(char ch){ 
  168.  
  169.         int pre=0
  170.  
  171.         switch(ch){ 
  172.  
  173.                 case '('return pre=0
  174.  
  175.                 case '+'
  176.  
  177.                 case '-'return pre=1
  178.  
  179.                 case '*'
  180.  
  181.                 case '/'return pre=2
  182.  
  183.             } 
  184.  
  185.         return pre; 
  186.  
  187.         } 
  188.  
  189.     public static void main(String[] str){ 
  190.  
  191.         Scanner input=new Scanner(System.in); 
  192.  
  193.         String st=input.nextLine().toString(); 
  194.  
  195.         String[] s=transform(st).split(","); 
  196.  
  197.         System.out.print("后缀式:"); 
  198.  
  199.         for(int i=0;i<s.length;i++){ 
  200.  
  201.             if(!s[i].equals("")) 
  202.  
  203.                 System.out.print(s[i]); 
  204.  
  205.             } 
  206.  
  207.         System.out.println("\n"+"结果:"+count(transform(st))); 
  208.  
  209.         } 
  210.  

 

LinkStack.java

 

  
  
  
  
  1. public class LinkStack<T>{ 
  2.  
  3.     private class Node{ 
  4.  
  5.         private T date; 
  6.  
  7.         private Node next; 
  8.  
  9.         public Node(){} 
  10.  
  11.         public Node(T date,Node next){ 
  12.  
  13.             this.date=date; 
  14.  
  15.             this.next=next; 
  16.  
  17.             } 
  18.  
  19.         } 
  20.  
  21.     private Node top; 
  22.  
  23.     private int size; 
  24.  
  25.     public LinkStack(){ 
  26.  
  27.         top=null
  28.  
  29.         } 
  30.  
  31.     public LinkStack(T element){ 
  32.  
  33.         top=new Node(element,null); 
  34.  
  35.         size++; 
  36.  
  37.         } 
  38.  
  39.     public int length(){ 
  40.  
  41.         return size; 
  42.  
  43.         } 
  44.  
  45.     public void push(T element){ 
  46.  
  47.         top=new Node(element,top); 
  48.  
  49.         size++; 
  50.  
  51.         } 
  52.  
  53.     public boolean Empty(){ 
  54.  
  55.         return size==0
  56.  
  57.         } 
  58.  
  59.     public T peek(){ 
  60.  
  61.         return top.date; 
  62.  
  63.         } 
  64.  
  65.     public T pop(){ 
  66.  
  67.         Node oldTop=top; 
  68.  
  69.         top=top.next; 
  70.  
  71.         oldTop.next=null
  72.  
  73.         size--; 
  74.  
  75.         return oldTop.date; 
  76.  
  77.         } 
  78.  
  79.     public void clear(){ 
  80.  
  81.         top=null
  82.  
  83.         size=0
  84.  
  85.         } 
  86.  
  87.     public String toString(){ 
  88.  
  89.         if(Empty()){ 
  90.  
  91.             return "[]"
  92.  
  93.             } 
  94.  
  95.         else
  96.  
  97.             StringBuffer sb=new StringBuffer("["); 
  98.  
  99.             for(Node current=top;current!=null;current=current.next){ 
  100.  
  101.                 sb.append(current.date.toString()); 
  102.  
  103.                 if(current.next!=null){ 
  104.  
  105.                     sb.append(","); 
  106.  
  107.                     } 
  108.  
  109.             } 
  110.  
  111.         return sb.append("]").toString(); 
  112.  
  113.         } 
  114.  
  115.     } 
  116.  
  117.      
  118.  

 

 

你可能感兴趣的:(java,职场,休闲,表达式计算器)