2014-04-16 130 views
0

我有一個計算器。按操作數多次重複計算

例如,當我在加號「+」上多次按下時,它只是重複計算而且數字是上升的。

這裏是我的CalcActivity.class

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View myView = inflater.inflate(R.layout.calc_inflate, container, false); 

    mCalculation = new Calculation(); 
    mCalculatorDisplay = (TextView) myView.findViewById(R.id.textView1); 
    mCalculatorDisplayUP = (TextView) myView.findViewById(R.id.textView0); 

    /** SharedPreferences */ 
    sharedpreferences = getActivity().getSharedPreferences(MyPREFERENCES, 
      Context.MODE_PRIVATE); 

    if (sharedpreferences.contains(inputValueNamber)) { 
     mCalculatorDisplay.setText(sharedpreferences.getString(
       inputValueNamber, "")); 
     if (sharedpreferences.contains(inputValueNamber)) { 
      mCalculatorDisplayUP.setText(sharedpreferences.getString(
        inputValueNamber, "")); 
     } 
    } 

    df.setMinimumFractionDigits(0); 
    df.setMinimumIntegerDigits(1); 
    df.setMaximumIntegerDigits(20); 

    myView.findViewById(R.id.button00).setOnClickListener(this); 
    myView.findViewById(R.id.button0).setOnClickListener(this); 
    myView.findViewById(R.id.button1).setOnClickListener(this); 
    myView.findViewById(R.id.button2).setOnClickListener(this); 
    myView.findViewById(R.id.button3).setOnClickListener(this); 
    myView.findViewById(R.id.button4).setOnClickListener(this); 
    myView.findViewById(R.id.button5).setOnClickListener(this); 
    myView.findViewById(R.id.button6).setOnClickListener(this); 
    myView.findViewById(R.id.button7).setOnClickListener(this); 
    myView.findViewById(R.id.button8).setOnClickListener(this); 
    myView.findViewById(R.id.button9).setOnClickListener(this); 

    myView.findViewById(R.id.buttonAdd).setOnClickListener(this); 
    myView.findViewById(R.id.buttonSubtract).setOnClickListener(this); 
    myView.findViewById(R.id.buttonMultiply).setOnClickListener(this); 
    myView.findViewById(R.id.buttonDivide).setOnClickListener(this); 
    myView.findViewById(R.id.buttonDecimalPoint).setOnClickListener(this); 
    myView.findViewById(R.id.buttonEquals).setOnClickListener(this); 
    myView.findViewById(R.id.buttonClear).setOnClickListener(this); 


    return myView; 

} 

@Override 
public void onClick(View v) { 

    String buttonPressed = ((Button) v).getText().toString(); 

    if (DIGITS.contains(buttonPressed)) { 

     if (userIsInTheMiddleOfTypingANumber) { 

      if (buttonPressed.equals(".") 
        && mCalculatorDisplay.getText().toString() 
        .contains(".")) { 
      } else { 
       mCalculatorDisplay.append(buttonPressed); 
      } 

     } else { 

      if (buttonPressed.equals(".")) { 
       mCalculatorDisplay.setText(0 + buttonPressed); 
      } else { 
       mCalculatorDisplay.setText(buttonPressed); 
      } 

      userIsInTheMiddleOfTypingANumber = true; 
     } 

    } else { 
     if (userIsInTheMiddleOfTypingANumber) { 

      mCalculation.setOperand(Double.parseDouble(mCalculatorDisplay 
        .getText().toString())); 
      userIsInTheMiddleOfTypingANumber = false; 
     } 

     mCalculation.performOperation(buttonPressed); 
     mCalculatorDisplay.setText(df.format(mCalculation.getResult())); 
     mCalculatorDisplayUP.setText(df.format(mCalculation.getResult())); 

    } 
} 

這裏是Calculation.class

public class Calculation { 
private double mOperand; 
private double mWaitingOperand; 
private String mWaitingOperator; 
private double mCalculatorMemory; 

// operator types 
public static final String ADD = "+"; 
public static final String SUBTRACT = "-"; 
public static final String MULTIPLY = "*"; 
public static final String DIVIDE = "/"; 

public static final String CLEAR = "C" ; 
public static final String CLEARMEMORY = "MC"; 
public static final String ADDTOMEMORY = "M+"; 
public static final String SUBTRACTFROMMEMORY = "M-"; 
public static final String RECALLMEMORY = "MR"; 
public static final String SQUAREROOT = "√"; 
public static final String SQUARED = "x²"; 
public static final String INVERT = "1/x"; 
public static final String TOGGLESIGN = "+/-"; 
public static final String SINE = "sin"; 
public static final String COSINE = "cos"; 
public static final String TANGENT = "tan"; 


// constructor 
public Calculation() { 
    mOperand = 0; 
    mWaitingOperand = 0; 
    mWaitingOperator = ""; 
    mCalculatorMemory = 0; 
} 

public void setOperand(double operand) { 
    mOperand = operand; 
} 

public double getResult() { 
    return mOperand; 
} 

public void setMemory(double calculatorMemory) { 
    mCalculatorMemory = calculatorMemory; 
} 

public double getMemory() { 
    return mCalculatorMemory; 
} 

public String toString() { 
    return Double.toString(mOperand); 
} 

protected double performOperation(String operator) { 

    if (operator.equals(CLEAR)) { 
     mOperand = 0; 
     mWaitingOperator = ""; 
     mWaitingOperand = 0; 
    } else if (operator.equals(CLEARMEMORY)) { 
     mCalculatorMemory = 0; 
    } else if (operator.equals(ADDTOMEMORY)) { 
     mCalculatorMemory = mCalculatorMemory + mOperand; 
    } else if (operator.equals(SUBTRACTFROMMEMORY)) { 
     mCalculatorMemory = mCalculatorMemory - mOperand; 
    } else if (operator.equals(RECALLMEMORY)) { 
     mOperand = mCalculatorMemory; 
    } else if (operator.equals(SQUAREROOT)) { 
     mOperand = Math.sqrt(mOperand); 

    } else if (operator.equals(SQUARED)) { 
     mOperand = mOperand * mOperand; 

    } else if (operator.equals(INVERT)) { 
     if (mOperand != 0) { 
      mOperand = 1/mOperand; 
     } 
    } else if (operator.equals(TOGGLESIGN)) { 
     mOperand = -mOperand; 
    } else if (operator.equals(SINE)) { 
     mOperand = Math.sin(Math.toRadians(mOperand)); 
    } else if (operator.equals(COSINE)) { 
     mOperand = Math.cos(Math.toRadians(mOperand)); 
    } else if (operator.equals(TANGENT)) { 
     mOperand = Math.tan(Math.toRadians(mOperand)); 
    } else { 
     performWaitingOperation(); 
     mWaitingOperator = operator; 
     mWaitingOperand = mOperand; 
    } 

    return mOperand; 
} 

protected void performWaitingOperation() { 

    if (mWaitingOperator.equals(ADD)) { 
     mOperand = mWaitingOperand + mOperand; 
    } else if (mWaitingOperator.equals(SUBTRACT)) { 
     mOperand = mWaitingOperand - mOperand; 
    } else if (mWaitingOperator.equals(MULTIPLY)) { 
     mOperand = mWaitingOperand * mOperand; 
    } else if (mWaitingOperator.equals(DIVIDE)) { 
     if (mOperand != 0) { 
      mOperand = mWaitingOperand/mOperand; 
     } 
    } 

} 

}

我要的是什麼我點擊加號或其他操作,他們只是一次計算次數不多。

感謝您的幫助

+0

當他們多次按下「+」時,你想要發生什麼? – codeMagic

+0

我希望在第一次按訂單時按,然後無論我按什麼都不做。 –

+0

檢查我的答案。 –

回答

2

你可以做一些簡單的像這樣:

private boolean additionPerformed; 

public Calculation() { 
    this.additionPerformed = false; 
    mOperand = 0; 
    mWaitingOperand = 0; 
    mWaitingOperator = ""; 
    mCalculatorMemory = 0; 
} 

protected void performWaitingOperation() { 

    if (mWaitingOperator.equals(ADD) && additionPerformed == false) { 
     mOperand = mWaitingOperand + mOperand; 
     additionPerformed = true; 
    } else if (mWaitingOperator.equals(SUBTRACT)) { 
     mOperand = mWaitingOperand - mOperand; 
    } else if (mWaitingOperator.equals(MULTIPLY)) { 
     mOperand = mWaitingOperand * mOperand; 
    } else if (mWaitingOperator.equals(DIVIDE)) { 
     if (mOperand != 0) { 
      mOperand = mWaitingOperand/mOperand; 
     } 
    } 

} 

當然,你將不得不工作,你的邏輯周圍不會影響計算器的流動。

+0

所以你給了plusaPerformed錯誤只是爲了加「+」? casue我對所有其他操作數都有同樣的問題,並且如果我將additionPerformed false賦予其中的每一個,則計算將停止工作! –

+1

嗯,我專門回答你的問題,但是邏輯可以針對每一項操作實施。正如我在腳註中所述,一旦計算結束,您必須找到一種方法將變量設置爲「true」。我的建議是不要在'onCreate()'中創建'Calculation()'的'new'實例。嘗試在別的地方做,所以你可以根據需要創建新的實例。 –