public int pik_doCalculate() { try { double fn, sn, rn; int error = 0; fn = 0; sn = 0; rn = 0; if (firstNumber.OToString() == "END" && secondNumber.OToString() == "END") { if (mycalcOperator == MyInternalKey.IK_CANCEL_START) { this.initGlobal(); } firstNumber.setText(resultNumber.getText()); secondNumber.setText(lastSecond.getText()); fn = firstNumber.getNumber(); sn = secondNumber.getNumber(); mycalcOperator = lastOperator; switch (mycalcOperator) { case MyInternalKey.IK_OPERATOR_SIGN: rn = 0 - fn; break; case MyInternalKey.IK_OPERATOR_PLUS: rn = fn + sn; break; case MyInternalKey.IK_OPERATOR_SUB: rn = fn - sn; break; case MyInternalKey.IK_OPERATOR_MUL: rn = fn * sn; break; case MyInternalKey.IK_OPERATOR_DIV: if (sn != 0) { rn = fn / sn; } else { error = 1; } break; case MyInternalKey.IK_OPERATOR_RESULT: break; default: break; } lastFirst.setText(firstNumber.getText()); lastSecond.setText(secondNumber.getText()); lastOperator = mycalcOperator; firstNumber.setText("END"); secondNumber.setText("END"); resultNumber.setNumber(rn); ikm_textRefreshed(resultNumber.toString()); if (error == 1) { ikm_textRefreshed("Error : Divide By Zero !"); } } else { if (firstNumber.OToString() != "END" && secondNumber.OToString() == "END") { fn = firstNumber.getNumber(); sn = firstNumber.getNumber(); } if (firstNumber.OToString() != "END" && secondNumber.OToString() != "END" /*&& firstNumber.OToString() != resultNumber.OToString()*/) { fn = firstNumber.getNumber(); sn = secondNumber.getNumber(); } rn = 0.0; switch (mycalcOperator) { case MyInternalKey.IK_OPERATOR_SIGN: rn = 0 - fn; break; case MyInternalKey.IK_OPERATOR_PLUS: rn = fn + sn; break; case MyInternalKey.IK_OPERATOR_SUB: rn = fn - sn; break; case MyInternalKey.IK_OPERATOR_MUL: rn = fn * sn; break; case MyInternalKey.IK_OPERATOR_DIV: if (sn != 0) { rn = fn / sn; } else { error = 1; } break; case MyInternalKey.IK_OPERATOR_RESULT: break; default: break; } lastFirst.setText(firstNumber.getText()); lastSecond.setText(secondNumber.getText()); lastOperator = mycalcOperator; firstNumber.setText("END"); secondNumber.setText("END"); resultNumber.setNumber(rn); ikm_textRefreshed(resultNumber.toString()); if (error == 1) { ikm_textRefreshed("Error : Divide By Zero !"); } } } catch { this.initGlobal(); } return(0); }
public int pik_processOperator(MyInternalKey ik) { //double fn, sn, rn; if (g_getInternalKeyType(ik) != MyCalcInternalKeyType.IKT_OPERATOR) { return(1); } if (ik == MyInternalKey.IK_OPERATOR_SIGN) { if (mycalcState == MyCalcState.CS_STATE_FIRSTOPERAND) { firstNumber.setNumber(0 - firstNumber.getNumber()); ikm_textRefreshed(firstNumber.toString()); } if (mycalcState == MyCalcState.CS_STATE_SECONDOPERAND) { secondNumber.setNumber(0 - secondNumber.getNumber()); ikm_textRefreshed(secondNumber.toString()); } if (mycalcState == MyCalcState.CS_STATE_OPERATOR) { secondNumber.setNumber(0 - secondNumber.getNumber()); ikm_textRefreshed(secondNumber.toString()); } if (mycalcState == MyCalcState.CS_STATE_READY) { resultNumber.setNumber(0 - resultNumber.getNumber()); ikm_textRefreshed(resultNumber.toString()); } } else { if (ik == MyInternalKey.IK_OPERATOR_RESULT) { mycalcState = MyCalcState.CS_STATE_SECONDOPERAND; } switch (mycalcState) { case MyCalcState.CS_STATE_READY: if (firstNumber.getText() == "END" && secondNumber.getText() == "END") { firstNumber.setText(resultNumber.getText()); mycalcOperator = ik; mycalcState = MyCalcState.CS_STATE_OPERATOR; } break; case MyCalcState.CS_STATE_FIRSTOPERAND: mycalcOperator = ik; mycalcState = MyCalcState.CS_STATE_OPERATOR; break; case MyCalcState.CS_STATE_OPERATOR: { if (ik == MyInternalKey.IK_OPERATOR_RESULT && firstNumber.getText() == resultNumber.getText() && secondNumber.getText() == "END") { secondNumber.setNumber(firstNumber.getNumber()); pik_doCalculate(); mycalcState = MyCalcState.CS_STATE_READY; } else { mycalcOperator = ik; } } break; case MyCalcState.CS_STATE_SECONDOPERAND: pik_doCalculate(); mycalcState = MyCalcState.CS_STATE_READY; if (ik != MyInternalKey.IK_OPERATOR_RESULT) { firstNumber.setText(resultNumber.getText()); mycalcOperator = ik; mycalcState = MyCalcState.CS_STATE_OPERATOR; } break; } } return(0); }