2011-08-11 36 views
0

私はアンドロイドの電卓アプリを開発しており、初心者のユーザーはほとんど問題に直面していません。のみに署名し、それは現在、私は下に次の午前8(IE = 8 5 + 3) が表示されます ユーザーは2 + 3 = 5 再びユーザーのヒットを打つ=: 次のように私は何を実現しようとしていることです私の仕事をするアプローチ:計算機の数値を再帰的に加算、減算、除算、または乗算する方法は?

private TextView txtCalc=null; 
private double num = 0; 
num = num + Double.parseDouble(txtCalc.getText().toString()); 

String txt = Double.toString(num); 
txtCalc.setText(txt); 

ユーザーが入力した演算子(+、 - など)に応じて、数学演算を実行します。

何か提案がありますか? 誰かが提供できる場合、コードスニペットは非常に役に立ちます。 @Sandyの場合 :ここでは私の完全なコードです:

public class Calculator extends Activity { 
    private final String SDK_VERSION = "1"; 
    private final int MENUITEM_CLOSE = 300; 

    /* 
    * Edit Text and Button object initialization for simple 
    * calculator design. 
    */ 
    private TextView txtCalc=null; 
    private Button btnZero=null; 
    private Button btnOne=null; 
    private Button btnTwo=null; 
    private Button btnThree=null; 
    private Button btnFour=null; 
    private Button btnFive=null; 
    private Button btnSix=null; 
    private Button btnSeven=null; 
    private Button btnEight=null; 
    private Button btnNine=null; 
    private Button btnPlus=null; 
    private Button btnMinus=null; 
    private Button btnMultiply=null; 
    private Button btnDivide=null; 
    private Button btnEquals=null; 
    private Button btnAC=null; 
    private Button btnDecimal=null; 
    private Button btnPT=null; 
    private Button btnMR=null; 
    private Button btnMM=null; 
    private Button btnMP=null; 
    private Button btnCE=null; 
    private Button btnPerc=null; 
    private Button btnSqrRoot=null; 
    private Button btnGT=null; 

    private double num = 0; 
    private double memNum = 0; 
    private int operator = 1; 

    // 0 = nothing, 1 = plus, 2 = minus, 3 = 
    // multiply, 4 = divide 

    private boolean readyToClear = false; 
    private boolean hasChanged = false; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setTheme(android.R.style.Theme_Black); 
    setContentView(R.layout.calculator); 

    this.setTitle("Dual Memory Calculator"); 

    initControls(); 
    initScreenLayout(); 
    reset(); 
    } 

    private void initScreenLayout() { 

    /* 
    * The following three command lines you can use depending 
    * upon the emulator device you are using. 
    */ 

    // 320 x 480 (Tall Display - HVGA-P) [default] 
    // 320 x 240 (Short Display - QVGA-L) 
    // 240 x 320 (Short Display - QVGA-P) 

    DisplayMetrics dm = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 

    // this.showAlert(dm.widthPixels +" "+ dm.heightPixels, dm.widthPixels 
    // +" "+ dm.heightPixels, dm.widthPixels +" "+ dm.heightPixels, false); 

    int height = dm.heightPixels; 
    int width = dm.widthPixels; 

    if (height < 400 || width < 300) { 
    txtCalc.setTextSize(20); 

    } 

    if (width < 300) { 
    /*btnPT.setTextSize(18); 
    btnMR.setTextSize(18); 
    btnMP.setTextSize(18); 
    btnMM.setTextSize(18); 
    btnCE.setTextSize(18); 
    btnDivide.setTextSize(18); 
    btnPlus.setTextSize(18); 
    btnMinus.setTextSize(18); 
    btnMultiply.setTextSize(18); 
    btnEquals.setTextSize(18); 
    btnGT.setTextSize(18); 
    btnPerc.setTextSize(18); 
    btnAC.setTextSize(18); 
    btnSqrRoot.setTextSize(18); 
    btnNine.setTextSize(18); 
    btnEight.setTextSize(18); 
    btnSeven.setTextSize(18); 
    btnSix.setTextSize(18); 
    btnFive.setTextSize(18); 
    btnFour.setTextSize(18); 
    btnThree.setTextSize(18); 
    btnTwo.setTextSize(18); 
    btnOne.setTextSize(18); 
    btnZero.setTextSize(18); 
    btnDecimal.setTextSize(18);*/ 
    } 

    btnZero.setTextColor(Color.BLACK); 
    btnOne.setTextColor(Color.BLACK); 
    btnTwo.setTextColor(Color.BLACK); 
    btnThree.setTextColor(Color.BLACK); 
    btnFour.setTextColor(Color.BLACK); 
    btnFive.setTextColor(Color.BLACK); 
    btnSix.setTextColor(Color.BLACK); 
    btnSeven.setTextColor(Color.BLACK); 
    btnEight.setTextColor(Color.BLACK); 
    btnNine.setTextColor(Color.BLACK); 
    btnGT.setTextColor(Color.BLACK); 
    btnDecimal.setTextColor(Color.BLACK); 

    btnMP.setTextColor(Color.BLACK); 
    btnMM.setTextColor(Color.BLACK); 
    btnMR.setTextColor(Color.BLACK); 
    btnPT.setTextColor(Color.BLACK); 
    btnCE.setTextColor(Color.RED); 
    btnAC.setTextColor(Color.BLACK); 
    btnPerc.setTextColor(Color.BLACK); 
    btnSqrRoot.setTextColor(Color.BLACK); 
    } 

    private void initControls() { 
    txtCalc = (TextView) findViewById(R.id.txtCalc); 
    btnZero = (Button) findViewById(R.id.btnZero); 
    btnOne = (Button) findViewById(R.id.btnOne); 
    btnTwo = (Button) findViewById(R.id.btnTwo); 
    btnThree = (Button) findViewById(R.id.btnThree); 
    btnFour = (Button) findViewById(R.id.btnFour); 
    btnFive = (Button) findViewById(R.id.btnFive); 
    btnSix = (Button) findViewById(R.id.btnSix); 
    btnSeven = (Button) findViewById(R.id.btnSeven); 
    btnEight = (Button) findViewById(R.id.btnEight); 
    btnNine = (Button) findViewById(R.id.btnNine); 
    btnPlus = (Button) findViewById(R.id.btnPlus); 
    btnMinus = (Button) findViewById(R.id.btnMinus); 
    btnMultiply = (Button) findViewById(R.id.btnMultiply); 
    btnDivide = (Button) findViewById(R.id.btnDivide); 
    btnEquals = (Button) findViewById(R.id.btnEquals); 
    btnAC = (Button) findViewById(R.id.btnAC); 
    btnDecimal = (Button) findViewById(R.id.btnDecimal); 
    btnPT = (Button) findViewById(R.id.btnPT); 
    btnMR = (Button) findViewById(R.id.btnMR); 
    btnMM = (Button) findViewById(R.id.btnMM); 
    btnMP = (Button) findViewById(R.id.btnMP); 
    btnCE = (Button) findViewById(R.id.btnCE); 
    btnPerc = (Button) findViewById(R.id.btnPerc); 
    btnSqrRoot = (Button) findViewById(R.id.btnSqrRoot); 
    btnGT = (Button) findViewById(R.id.btnGT); 


    //txtCalc.setOnKeyListener(new On) 
    btnZero.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleNumber(0); 
    } 
    }); 
    btnOne.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleNumber(1); 
    } 
    }); 
    btnTwo.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleNumber(2); 
    } 
    }); 
    btnThree.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleNumber(3); 
    } 
    }); 
    btnFour.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleNumber(4); 
    } 
    }); 
    btnFive.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleNumber(5); 
    } 
    }); 
    btnSix.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleNumber(6); 
    } 
    }); 
    btnSeven.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleNumber(7); 
    } 
    }); 
    btnEight.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleNumber(8); 
    } 
    }); 
    btnNine.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleNumber(9); 
    } 
    }); 
    btnPlus.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleEquals(1); 
    } 
    }); 
    btnMinus.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleEquals(2); 
    } 
    }); 
    btnMultiply.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleEquals(3); 
    } 
    }); 
    btnDivide.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleEquals(4); 
    } 
    }); 
    btnEquals.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    /*String tempNum = txtCalc.getText().toString().trim(); 
    Double tempDoubleNum = Double.valueOf(tempNum); 
    //num = tempDoubleNum + num; 
    if(!hasChanged){ 
      if(txtCalc.getText().toString() != ""){ 

       switch(operator){ 

       case 0: 
        handleEquals(0); 
        break; 
       case 1: 
        num = tempDoubleNum + Double.parseDouble(txtCalc.getText().toString()); 
        handleEquals(1); 
        break; 
       case 2: 
        num = tempDoubleNum - Double.parseDouble(txtCalc.getText().toString()); 
        handleEquals(2); 
        break; 
       case 3: 
        num = tempDoubleNum * Double.parseDouble(txtCalc.getText().toString()); 
        handleEquals(3); 
        break; 
       case 4: 
        num = tempDoubleNum/Double.parseDouble(txtCalc.getText().toString()); 
        handleEquals(4); 
        break; 
       } 
      } 
     } */ 
    handleEquals(0); 
    } 
    }); 
    btnAC.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    reset(); 
    } 
    }); 
    btnDecimal.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleDecimal(); 
    } 
    }); 
    btnGT.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handlePlusMinus(); 
    } 
    }); 
    btnPT.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    memNum = 0; 
    } 
    }); 
    btnMR.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    setValue(Double.toString(memNum)); 
    } 
    }); 
    btnMM.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    memNum = memNum 
    - Double.parseDouble(txtCalc.getText().toString()); 
    operator = 0; 
    } 
    }); 
    btnMP.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    memNum = memNum 
    + Double.parseDouble(txtCalc.getText().toString()); 
    operator = 0; 
    } 
    }); 
    btnCE.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    handleBackspace(); 
    } 
    }); 
    btnSqrRoot.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    setValue(Double.toString(Math.sqrt(Double.parseDouble(txtCalc 
    .getText().toString())))); 
    } 
    }); 
    btnPerc.setOnClickListener(new Button.OnClickListener() { 
    public void onClick(View v) { 
    setValue(Double.toString(num 
    * (0.01 * Double.parseDouble(txtCalc.getText() 
    .toString())))); 
    } 
    }); 

    txtCalc.setOnKeyListener(new OnKeyListener() { 
    public boolean onKey(View v, int i, android.view.KeyEvent e) { 
    if (e.getAction() == KeyEvent.ACTION_DOWN) { 
    int keyCode = e.getKeyCode(); 

    // txtCalc.append("["+Integer.toString(keyCode)+"]"); 

    switch (keyCode) { 
    case KeyEvent.KEYCODE_0: 
    handleNumber(0); 
    break; 

    case KeyEvent.KEYCODE_1: 
    handleNumber(1); 
    break; 

    case KeyEvent.KEYCODE_2: 
    handleNumber(2); 
    break; 

    case KeyEvent.KEYCODE_3: 
    handleNumber(3); 
    break; 

    case KeyEvent.KEYCODE_4: 
    handleNumber(4); 
    break; 

    case KeyEvent.KEYCODE_5: 
    handleNumber(5); 
    break; 

    case KeyEvent.KEYCODE_6: 
    handleNumber(6); 
    break; 

    case KeyEvent.KEYCODE_7: 
    handleNumber(7); 
    break; 

    case KeyEvent.KEYCODE_8: 
    handleNumber(8); 
    break; 

    case KeyEvent.KEYCODE_9: 
    handleNumber(9); 
    break; 

    case 43: 
    handleEquals(1); 
    break; 

    case KeyEvent.KEYCODE_EQUALS: 
     hasChanged = false; 
     if(!hasChanged && txtCalc.getText().toString()!= ""){ 
      String strNum = txtCalc.getText().toString(); 
      Double tmpNum = Double.valueOf(strNum); 
      handleEquals(keyCode); 
     } 
    handleEquals(0); 
    break; 

    case KeyEvent.KEYCODE_MINUS: 
    handleEquals(2); 
    break; 

    case KeyEvent.KEYCODE_PERIOD: 
    handleDecimal(); 
    break; 

    case KeyEvent.KEYCODE_C: 
    reset(); 
    break; 

    case KeyEvent.KEYCODE_SLASH: 
    handleEquals(4); 
    break; 

    case KeyEvent.KEYCODE_DPAD_DOWN: 
    return false; 
    } 
    } 

    return true; 
    } 
    }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    menu.add(0,1, MENUITEM_CLOSE, "Close"); 

    return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case MENUITEM_CLOSE: 
    finish(); 
    break; 
    } 

    return super.onOptionsItemSelected(item); 
    } 

    public static double round(double unrounded, int precision, int roundingMode) 
    { 
     BigDecimal bd = new BigDecimal(unrounded); 
     BigDecimal rounded = bd.setScale(precision, roundingMode); 
     return rounded.doubleValue(); 
    } 

    private void handleEquals(int newOperator) { 
    if (hasChanged) { 
    switch (operator) { 
    case 1: 
     num = num + Double.parseDouble(txtCalc.getText().toString()); 
     break; 
    case 2: 
     num = num - Double.parseDouble(txtCalc.getText().toString()); 
     break; 
    case 3: 
     num = num * Double.parseDouble(txtCalc.getText().toString()); 
     String strNum = null; 
     strNum = Double.toString(num); 
     if(strNum.contains("E")){ 
     strNum = strNum.substring(0, 6) + strNum.substring(strNum.indexOf("E")); 
     } 
     Log.i("MULTIPLICATION","Checking Precision"); 
     System.out.println("New StrNum is: " + strNum); 
     num = Double.valueOf(strNum); 
     break; 
    case 4: 
     num = num/Double.parseDouble(txtCalc.getText().toString()); 
     break; 
    } 

    String txt = Double.toString(num); 
    txtCalc.setText(txt); 
    //txtCalc.setSelection(txt.length()); 

    readyToClear = true; 
    hasChanged = false; 
    } 

    operator = newOperator; 
    } 

    private void handleNumber(int num) { 
    if (operator == 0) 
    reset(); 

    String txt = txtCalc.getText().toString(); 
    if (readyToClear) { 
    txt = ""; 
    readyToClear = false; 
    } else if (txt.equals("0")) 
    txt = ""; 

    txt = txt + Integer.toString(num); 

    txtCalc.setText(txt); 
    //txtCalc.setSelection(txt.length()); 

    hasChanged = true; 
    } 

    private void setValue(String value) { 
    if (operator == 0) 
    reset(); 

    if (readyToClear) { 
    readyToClear = false; 
    } 

    txtCalc.setText(value); 
    //txtCalc.setSelection(value.length()); 

    hasChanged = true; 
    } 

    private void handleDecimal() { 
    if (operator == 0) 
    reset(); 

    if (readyToClear) { 
    txtCalc.setText("0."); 
    //txtCalc.setSelection(2); 
    readyToClear = false; 
    hasChanged = true; 
    } else { 
    String txt = txtCalc.getText().toString(); 

    if (!txt.contains(".")) { 
    txtCalc.append("."); 
    hasChanged = true; 
    } 
    } 
    } 

    private void handleBackspace() { 
    if (!readyToClear) { 
    String txt = txtCalc.getText().toString(); 
    if (txt.length() > 0) { 
    txt = txt.substring(0, txt.length() - 1); 
    if (txt.equals("")) 
    txt = "0"; 

    txtCalc.setText(txt); 
// txtCalc.setSelection(txt.length()); 
    } 
    } 
    } 

    private void handlePlusMinus() { 
    if (!readyToClear) { 
    String txt = txtCalc.getText().toString(); 
    if (!txt.equals("0")) { 
    if (txt.charAt(0) == '-') 
    txt = txt.substring(1, txt.length()); 
    else 
    txt = "-" + txt; 

    txtCalc.setText(txt); 
// txtCalc.setSelection(txt.length()); 
    } 
    } 
    } 

    private void reset() { 
    num = 0; 
    txtCalc.setText("0"); 
    //txtCalc.setSelection(1); 
    operator = 1; 
    } 
    } 

答えて

0

は単にintとして最後のオペレータを格納します。このようにして、switchには、最後の操作として、実行する操作をとして選択することができます。

0

ここ

2 + 3 = 5. 
When user hits "=",save 3 in a variable.In this case, 
double tmp = 3; 
whenever user press "=" 
just do the following 
num = num+tmp; 
then show the result. 
when user press another digit/symbol etc. then set tmp = 0; 
+0

しかし私の数学的操作は、単一のオペランドだけを使って実行されています。そして、この場合、それは "num"変数です。今、ユーザーがオペレータのサインや数字の記号を何回押したのかを検出するにはどうすればいいですか? – YuDroid

+0

ユーザーヒット "="の検出方法。ここで – jainal

+0

私が試してみましたそのサンプルコード:「ケースKeyEvent.KEYCODE_EQUALS: \t \t偽HASCHANGED =; \t \t場合(!。!HASCHANGED && txtCalc.getTextは()のtoString()= ""){ \t \t \t文字列strNum = txtCalc.getText()のtoString(); \t \t \tダブルtmpNum =は、Double.valueOf(strNum); \t \t \t handleEquals(のkeyCode); \t \t} \tハンdleEquals(0); \t break; " – YuDroid

0

あなたがコードに従うことによって、 "=" を検出することができ、またはあなたが他のkeylisteners

txtCalc.setOnKeyListener(new OnKeyListener() { 
      @Override 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 

       if (keyCode == 70) { 

        Log.d("My Log"," = entered"); 
        return false; 
       } 
       return false; 
      } 


     }); 

を使用することができますKeyCode 70は、 '='

のキーコードであるfolowsとして試してみてください
+0

あなたの提案に感謝し、{setOnKeyListeners(...)}を使用してキーコードを取得していますが、問題は、以前の値を一時変数に保存してから何もしないことです。ここで私が試したサンプルコード: "ケースKeyEvent.KEYCODE_EQUALS: \t \t hasChanged = false; \t \t { \t \t \tストリングstrNum = txtCalc.getText()のtoString()IF(HASCHANGED && txtCalc.getText()のtoString()= ""!!)。 \t \t \tダブルtmpNum =ダブルです。valueOf(strNum); \t \t \t handleEquals(keyCode); \t \t} \t handleEquals(0); \t break; " – YuDroid

+0

もう少しコードを投稿できますか? – Sandy

+0

@ Sandy:ここに私の完全なコードです: – YuDroid

関連する問題