2013-07-15 27 views
-1

我完全知道有更好的方式來做到這一點,但只要它終於奏效,只要我做了像4 + 4這樣的操作,它就會正確但如果我做的東西像4 + 4 * 2它將等於32計算器只有2個數字 - 與更多失敗

package calc; 
import java.awt.FlowLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JTextField; 

/** 
* 
* @author Ben 
*/ 
public class GUI extends JFrame { 

    int response, operator; 
    double num1, num2, total = 0; 
    String operation, answer, num, testnum; 
    private JButton one, two, three, four, five, six, seven, eight, 
    nine, zero, multiply, divide, subtract, add, equals, clear; 
    private JTextField display, fakedisplay; 

    public GUI() { 
     super("Calculator"); 
     setLayout(new FlowLayout()); 

     fakedisplay = new JTextField(10); 
     display = new JTextField(10);; 
     add(display); 
     one = new JButton("1"); 
     add(one); 
     two = new JButton("2"); 
     add(two); 
     three = new JButton("3"); 
     add(three); 
     four = new JButton("4"); 
     add(four); 
     five = new JButton("5"); 
     add(five); 
     six = new JButton("6"); 
     add(six); 
     seven = new JButton("7"); 
     add(seven); 
     eight = new JButton("8"); 
     add(eight); 
     nine = new JButton("9"); 
     add(nine); 
     zero = new JButton("0"); 
     add(zero); 
     multiply = new JButton("*"); 
     add(multiply); 
     divide = new JButton("/"); 
     add(divide); 
     subtract = new JButton("-"); 
     add(subtract); 
     add = new JButton("+"); 
     add(add); 
     equals = new JButton("="); 
     add(equals); 
     clear = new JButton("Clear"); 
     add(clear); 


     handler handle = new handler(); 

     one.addActionListener(handle); 
     two.addActionListener(handle); 
     three.addActionListener(handle); 
     four.addActionListener(handle); 
     five.addActionListener(handle); 
     six.addActionListener(handle); 
     seven.addActionListener(handle); 
     eight.addActionListener(handle); 
     nine.addActionListener(handle); 
     zero.addActionListener(handle); 
     multiply.addActionListener(handle); 
     divide.addActionListener(handle); 
     subtract.addActionListener(handle); 
     add.addActionListener(handle); 
     equals.addActionListener(handle); 
     clear.addActionListener(handle); 

    } 
    private class handler implements ActionListener { 

     @ 
     Override 
     public void actionPerformed(ActionEvent e) { 

      if(e.getSource() == one) { 
       response = 1; 
       display.setText(display.getText() + 1); 
       fakedisplay.setText(fakedisplay.getText() + 1); 
      } else if(e.getSource() == two) { 
       response = 2; 
       display.setText(display.getText() + 2); 
       fakedisplay.setText(fakedisplay.getText() + 2); 
      } else if(e.getSource() == three) { 
       response = 3; 
       display.setText(display.getText() + 3); 
       fakedisplay.setText(fakedisplay.getText() + 3); 
      } else if(e.getSource() == four) { 
       response = 4; 
       display.setText(display.getText() + 4); 
       fakedisplay.setText(fakedisplay.getText() + 4); 
      } else if(e.getSource() == five) { 
       response = 5; 
       display.setText(display.getText() + 5); 
       fakedisplay.setText(fakedisplay.getText() + 5); 
      } else if(e.getSource() == six) { 
       response = 6; 
       display.setText(display.getText() + 6); 
       fakedisplay.setText(fakedisplay.getText() + 6); 
      } else if(e.getSource() == seven) { 
       response = 7; 
       display.setText(display.getText() + 7); 
       fakedisplay.setText(fakedisplay.getText() + 7); 
      } else if(e.getSource() == eight) { 
       response = 8; 
       display.setText(display.getText() + 8); 
       fakedisplay.setText(fakedisplay.getText() + 8); 
      } else if(e.getSource() == nine) { 
       response = 9; 
       display.setText(display.getText() + 9); 
       fakedisplay.setText(fakedisplay.getText() + 9); 
      } else if(e.getSource() == zero) { 
       response = 0; 
       display.setText(display.getText() + 0); 
       fakedisplay.setText(fakedisplay.getText() + 0); 
      } else if(e.getSource() == multiply) { 
       if(operator == 0 && num1 == 0) { 
        num1 = Double.parseDouble(display.getText()); 
        operator = 3; 
        fakedisplay.setText(""); 
       } else if(operator != 0) { 
        operator = 3; 
        num2 = Double.parseDouble(fakedisplay.getText()); 
        fakedisplay.setText(""); 
        if(operator == 1) { 
         total = num1 + num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        if(operator == 2) { 
         total = num1 - num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        if(operator == 3) { 
         total = num1 * num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        if(operator == 4) { 
         total = num1/num2 + num1 % num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        num2 = 0; 
        total = 0; 
        fakedisplay.setText(""); 
        operator = 0; 
       } 
       operation = "*"; 
       display.setText(display.getText() + operation); 

      } else if(e.getSource() == divide) { 
       if(operator == 0 && num1 == 0) { 
        num1 = Double.parseDouble(display.getText()); 
        operator = 4; 
        fakedisplay.setText(""); 
       } else if(total == 0) { 
        operator = 3; 
       } else if(operator != 0) { 
        operator = 4; 
        num2 = Double.parseDouble(fakedisplay.getText()); 
        fakedisplay.setText(""); 
        if(operator == 1) { 
         total = num1 + num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        if(operator == 2) { 
         total = num1 - num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        if(operator == 3) { 
         total = num1 * num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        if(operator == 4) { 
         total = num1/num2 + num1 % num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        num2 = 0; 
        total = 0; 
        fakedisplay.setText(""); 
        operator = 0; 

       } 
       operation = "/"; 
       display.setText(display.getText() + operation); 

      } else if(e.getSource() == add) { 
       if(operator == 0 && num1 == 0) { 
        num1 = Double.parseDouble(display.getText()); 
        operator = 1; 
        fakedisplay.setText(""); 
       } else if(operator != 0) { 
        operator = 1; 
        num2 = Double.parseDouble(fakedisplay.getText()); 
        fakedisplay.setText(""); 
        if(operator == 1) { 
         total = num1 + num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        if(operator == 2) { 
         total = num1 - num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        if(operator == 3) { 
         total = num1 * num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        if(operator == 4) { 
         total = num1/num2 + num1 % num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        num2 = 0; 
        total = 0; 
        fakedisplay.setText(""); 
        operator = 0; 
       } 
       operation = "+"; 
       display.setText(display.getText() + operation); 

      } else if(e.getSource() == subtract) { 

       if(operator == 0 && num1 == 0) { 
        num1 = Double.parseDouble(display.getText()); 
        operator = 2; 
        fakedisplay.setText(""); 
       } else if(operator != 0) { 
        operator = 2; 
        num2 = Double.parseDouble(fakedisplay.getText()); 
        fakedisplay.setText(""); 
        if(operator == 1) { 
         total = num1 + num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        if(operator == 2) { 
         total = num1 - num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        if(operator == 3) { 
         total = num1 * num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        if(operator == 4) { 
         total = num1/num2 + num1 % num2; 
         display.setText(display.getText() + operation + total); 
         num1 = total; 

        } 
        num2 = 0; 
        total = 0; 
        fakedisplay.setText(""); 
        operator = 0; 
       } 
       operation = "-"; 
       display.setText(display.getText() + operation); 

      } else if(e.getSource() == equals) { 
       if(operator == 0) { 
        display.setText("Error"); 
       } else if(operator != 0) { 
        num2 = Double.parseDouble(fakedisplay.getText()); 
        fakedisplay.setText(""); 
        if(operator == 1) { 
         total = num1 + num2; 
         display.setText(display.getText() + "=" + total); 
        } 
        if(operator == 2) { 
         total = num1 - num2; 
         display.setText(display.getText() + "=" + total); 
        } 
        if(operator == 3) { 
         total = num1 * num2; 
         display.setText(display.getText() + "=" + total); 
        } 
        if(operator == 4) { 
         total = num1/num2 + num1 % num2; 
         display.setText(display.getText() + "=" + total); 
        } 

       } 
       operation = "="; 

      } else if(e.getSource() == clear) { 
       display.setText(""); 
       fakedisplay.setText(""); 
       operator = 0; 
       total = 0; 
       num1 = 0; 
       num2 = 0; 


      } 
      System.out.println("num1: " + num1 + " num2: " + num2 + "\ndisplay: " + display.getText() + " fakedisplay: " + fakedisplay.getText() + "\nresponse: " + response + "\noperator: " + operator + "\ntotal: " + total + "\n=========================="); 

     } 



    } 
} 
+0

8 * 2是32 .... – MadProgrammer

+1

然後,你需要添加一個「計算」或「=」按鈕,讓它看到所有具有相關優先級的運算符。就像編寫一個迷你編譯器的解析器一樣。做+ -/*會立即改變計算順序。你需要優先考慮。所以請將計算線路移動到「=」按鈕。 –

+0

8 * 2是一個不好的例子,它需要第二個運算符,並將它用於第一個運算符,而不是隻有2個數字。 – user2555459

回答

0

我只能指導你一些在線信息,你需要工作的解析。將最終命令字符串的每個令牌(+ - * /)放在一個列表中(可能是二叉樹)並對該列表進行排序,以告訴計算器,除法和乘法具有最高優先級,同時加法和減法的優先級較低。

您可以按照這種方式對命令數組進行排序,以便在右邊排列優先級。然後,首先選擇div/mult,然後從左到右開始計算。

http://www.slideshare.net/dabeaz/writing-parsers-and-compilers-with-ply

http://parsingintro.sourceforge.net/

http://www.dreamincode.net/forums/topic/268945-an-introduction-to-compiler-design-part-ii-parsing/

https://en.wikipedia.org/wiki/Parsing

http://arantxa.ii.uam.es/~modonnel/Compilers/03_1_Parsing_Intro.pdf

如果你是太累了,你可以使用script engine of java,從用戶 「安德魯·湯普森」 的建議。

0

我想你可能會得到一個不同的方式。您正在爲一個非常簡單的問題設計一個複雜的解決方案 。

1. First of all Use a single JTextField for the user to enter the entire expression. 
    i.e. [ 4+4*2]. 

2. Second Provide a Submit Buttons(jbutton) along with text field. 
    Now when the User is finished with entering expression he will click the submit button. 

    i.e [4+4*2] [Click to Submit] 

不,你寫這個按鈕的處理程序。 只需從 文本字段 中讀取完整表達式,然後按照 逆向波蘭表達式來評估您的結果。 其基於avery簡單堆棧的算法。通常在數據結構類中教授。 看到這篇文章http://en.wikipedia.org/wiki/Reverse_Polish_notation