按操作数多次重复计算

时间:2014-04-16 16:56:09

标签: android calculator

我有一个计算器。

例如,当我多次按加号" +"时,它只是重复计算,而且数字会上升。

这是我的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;
        }
    }

}

}

我想要的是无论我点击加号还是其他操作数,他们只计算一次不多次。

感谢您的帮助

1 个答案:

答案 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;
        }
    }

}

当然,你必须运用你的逻辑来不影响计算器的流程。