2011-05-27 89 views
0

我做了一個自定義對話框,它是一個簡單的加法,減法,乘法和除法計算器。我無法正確地獲取我的代碼的邏輯。有沒有人有任何智慧或知道的資源我可以檢查,可以幫助。Android - 計算器的幫助邏輯

以下是我正在處理的自定義對話框和計算器邏輯的代碼。因爲它是所有的計算工作正常,除了當我使用等於按鈕。在這之後我做的計算並不正確。

謝謝!

Calculator http://img546.imageshack.us/img546/9534/devicee.png

public class CustomCalcDialog extends Dialog { 

    public CustomCalcDialog(Context context) { 
     super(context); 
    } 

    EditText calcDialogDisplay; 
    TextView enterTotal; 
    TextView allClear; 
    TextView seven; 
    TextView eight; 
    TextView nine; 
    TextView division; 
    TextView four; 
    TextView five; 
    TextView six; 
    TextView multiply; 
    TextView one; 
    TextView two; 
    TextView three; 
    TextView subtract; 
    TextView decimal; 
    TextView zero; 
    TextView equals; 
    TextView addition; 

    ArrayList<Float> mathVariables = new ArrayList<Float>(); 
    float mathVariable1; 
    float mathVariable2; 

    int currentOperation = 0; 
    int nextOperation; 

    final static int ADD = 1; 
    final static int SUBTRACT = 2; 
    final static int MULTIPLY = 3; 
    final static int DIVISION = 4; 
    final static int EQUALS = 5; 

    final static int CLEAR = 1; 
    final static int DONT_CLEAR = 0; 
    int clearCalcDisplay = 0; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.calculator_dialog); 

     calcDialogDisplay = (EditText) findViewById(R.id.calc_dialog_display); 
     enterTotal = (TextView) findViewById(R.id.enter_total); 
     allClear = (TextView) findViewById(R.id.all_clear); 
     seven = (TextView) findViewById(R.id.seven); 
     eight = (TextView) findViewById(R.id.eight); 
     nine = (TextView) findViewById(R.id.nine); 
     division =(TextView) findViewById(R.id.division); 
     four = (TextView) findViewById(R.id.four); 
     five = (TextView) findViewById(R.id.five); 
     six =(TextView) findViewById(R.id.six); 
     multiply = (TextView) findViewById(R.id.multiply); 
     one = (TextView) findViewById(R.id.one); 
     two = (TextView) findViewById(R.id.two); 
     three = (TextView) findViewById(R.id.three); 
     subtract = (TextView) findViewById(R.id.subtract); 
     decimal = (TextView) findViewById(R.id.decimal); 
     zero = (TextView) findViewById(R.id.zero); 
     equals = (TextView) findViewById(R.id.equals); 
     addition = (TextView) findViewById(R.id.addition); 

     calcDialogDisplay.setKeyListener(DigitsKeyListener.getInstance(true,true)); 

     registerListeners(); 
    } 

    public void registerListeners() { 

     enterTotal.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       activeTextView.setText(calcDialogDisplay.getText().toString()); 
       mathCalculations(); 
       CustomCalcDialog.this.dismiss(); 
      } 
     }); 

     allClear.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       calcDialogDisplay.setText(""); 
       mathVariable1 = 0; 
       mathVariable2 = 0; 
       mathVariables.removeAll(mathVariables); 
       currentOperation = 0; 
       nextOperation = 0;     
      } 
     }); 

     seven.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearCalcDisplay == CLEAR) { 
        calcDialogDisplay.setText(""); 
       } 
       clearCalcDisplay = DONT_CLEAR; 
       calcDialogDisplay.append("7"); 

      } 
     }); 

     eight.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearCalcDisplay == CLEAR) { 
        calcDialogDisplay.setText(""); 
       } 
       clearCalcDisplay = DONT_CLEAR; 
       calcDialogDisplay.append("8"); 

      } 
     }); 

     nine.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearCalcDisplay == CLEAR) { 
        calcDialogDisplay.setText(""); 
       } 
       clearCalcDisplay = DONT_CLEAR; 
       calcDialogDisplay.append("9"); 

      } 
     }); 

     division.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       calcLogic(DIVISION);      
      } 
     }); 

     four.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearCalcDisplay == CLEAR) { 
        calcDialogDisplay.setText(""); 
       } 
       clearCalcDisplay = DONT_CLEAR; 
       calcDialogDisplay.append("4"); 

      } 
     }); 

     five.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearCalcDisplay == CLEAR) { 
        calcDialogDisplay.setText(""); 
       } 
       clearCalcDisplay = DONT_CLEAR; 
       calcDialogDisplay.append("5"); 

      } 
     }); 

     six.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearCalcDisplay == CLEAR) { 
        calcDialogDisplay.setText(""); 
       } 
       clearCalcDisplay = DONT_CLEAR; 
       calcDialogDisplay.append("6"); 

      } 
     }); 

     multiply.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       calcLogic(MULTIPLY); 

      } 
     }); 

     one.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearCalcDisplay == CLEAR) { 
        calcDialogDisplay.setText(""); 
       } 
       clearCalcDisplay = DONT_CLEAR; 
       calcDialogDisplay.append("1"); 

      } 
     }); 

     two.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearCalcDisplay == CLEAR) { 
        calcDialogDisplay.setText(""); 
       } 
       clearCalcDisplay = DONT_CLEAR; 
       calcDialogDisplay.append("2"); 

      } 
     }); 

     three.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearCalcDisplay == CLEAR) { 
        calcDialogDisplay.setText(""); 
       } 
       clearCalcDisplay = DONT_CLEAR; 
       calcDialogDisplay.append("3"); 

      } 
     }); 

     subtract.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       calcLogic(SUBTRACT); 
      }    
     }); 

     decimal.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearCalcDisplay == CLEAR) { 
        calcDialogDisplay.setText(""); 
       } 
       clearCalcDisplay = DONT_CLEAR; 
       calcDialogDisplay.append("."); 

      } 
     }); 

     zero.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (clearCalcDisplay == CLEAR) { 
        calcDialogDisplay.setText(""); 
       } 
       clearCalcDisplay = DONT_CLEAR; 
       calcDialogDisplay.append("0"); 

      } 
     }); 

     equals.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       calcLogic(EQUALS); 

      } 
     }); 

     addition.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       calcLogic(ADD); 
      } 
     }); 
    } 

    private void calcLogic(int operator) { 

     mathVariables.add(Float.parseFloat(calcDialogDisplay.getText().toString())); 

     if (operator != EQUALS) { 
      nextOperation = operator; 
     }else if (operator == EQUALS){ 
      nextOperation = 0; 
     } 

     switch (currentOperation) { 
     case ADD:    
      mathVariable1 = mathVariables.get(0); 
      mathVariable2 = mathVariables.get(1); 

      mathVariables.removeAll(mathVariables); 

      mathVariables.add(mathVariable1 + mathVariable2); 

      calcDialogDisplay.setText(String.format("%.3f", mathVariables.get(0))); 
      break; 
     case SUBTRACT: 
      mathVariable1 = mathVariables.get(0); 
      mathVariable2 = mathVariables.get(1); 

      mathVariables.removeAll(mathVariables); 

      mathVariables.add(mathVariable1 - mathVariable2); 

      calcDialogDisplay.setText(String.format("%.3f", mathVariables.get(0))); 
      break; 
     case MULTIPLY: 
      mathVariable1 = mathVariables.get(0); 
      mathVariable2 = mathVariables.get(1); 

      mathVariables.removeAll(mathVariables); 

      mathVariables.add(mathVariable1 * mathVariable2); 

      calcDialogDisplay.setText(String.format("%.3f", mathVariables.get(0))); 
      break; 
     case DIVISION: 
      mathVariable1 = mathVariables.get(0); 
      mathVariable2 = mathVariables.get(1); 

      mathVariables.removeAll(mathVariables); 

      mathVariables.add(mathVariable1/mathVariable2); 

      calcDialogDisplay.setText(String.format("%.3f", mathVariables.get(0))); 
      break; 
     } 

     clearCalcDisplay = CLEAR; 
     currentOperation = nextOperation; 
     if (operator == EQUALS) { 
      mathVariable1 = 0; 
      mathVariable2 = 0; 
      mathVariables.removeAll(mathVariables); 
     } 
    } 
} 
+0

這是一個很好的圖片。 – bdares 2011-05-27 15:55:07

+0

你目前有什麼代碼?特別是你的問題在哪裏? – NickAldwin 2011-05-27 16:08:32

+0

我剛剛添加了代碼,發生了什麼。計算器邏輯在最後稱爲「calcLogic」。 – KevinM 2011-05-27 16:20:30

回答

0

考慮您的應用程序分成兩個部分。 gui收集輸入並選擇操作。現在創建另一個類來完成計算,比如Model.java。這個類應該提供獲取參數並提供返回值的方法。測試這些隔離方法的準確性和無效輸入,超出範圍值和無意義的輸入。爲您的模型設計例外情況。現在將GUI連接到模型。