can perform some operations but not all on c# calculator? -
so far, addition , subtraction work. but, multiplication , division not. because first 2 numbers put in added , them operation done. such as, if 9 * 9 + 3 should 84, 21 on calculator. because taking 9+9 + 3; sees last operator. have absolutely no idea how fix this. there helpful insight?
public partial class form1 : form { double num2; double num1; string c; public form1() { initializecomponent(); } private void btn0_click(object sender, eventargs e) { txtbox.text = txtbox.text + btn0.text; } private void btn1_click(object sender, eventargs e) { txtbox.text = txtbox.text + btn1.text; } private void btn2_click(object sender, eventargs e) { txtbox.text = txtbox.text + btn2.text; } private void btn3_click(object sender, eventargs e) { txtbox.text = txtbox.text + btn3.text; } private void btn4_click(object sender, eventargs e) { txtbox.text = txtbox.text + btn4.text; } private void btn5_click(object sender, eventargs e) { txtbox.text = txtbox.text + btn5.text; } private void btn6_click(object sender, eventargs e) { txtbox.text = txtbox.text + btn6.text; } private void btn7_click(object sender, eventargs e) { txtbox.text = txtbox.text + btn7.text; } private void btn8_click(object sender, eventargs e) { txtbox.text = txtbox.text + btn8.text; } private void btn9_click(object sender, eventargs e) { txtbox.text = txtbox.text + btn9.text; } private void btndecimal_click(object sender, eventargs e) { if (!txtbox.text.contains('.')) txtbox.text += '.'; } private void btnclear_click(object sender, eventargs e) { txtbox.clear(); } private void btnaddition_click(object sender, eventargs e) { num1 = num1 + double.parse(txtbox.text); c = "+"; txtbox.clear(); } private void btnsubtraction_click(object sender, eventargs e) { num1 = num1 + double.parse(txtbox.text); c = "-"; txtbox.clear(); } private void btnmultiplication_click(object sender, eventargs e) { num1 = num1 + double.parse(txtbox.text); c = "*"; txtbox.clear(); } private void btndivision_click(object sender, eventargs e) { num1 = num1 + double.parse(txtbox.text); c = "/"; txtbox.clear(); } private void btnequals_click(object sender, eventargs e) { double result; num2 = double.parse(txtbox.text); switch (c) { case "+": result = num1 + num2; txtbox.text = result.tostring(); num1 = 0; break; case "-": result = num1 - num2; txtbox.text = result.tostring(); num1 = 0; break; case "*": result = num1 * num2; txtbox.text = result.tostring(); num1 = 0; break; case "/": if (num2 != 0) { result = num1 / num2; txtbox.text = result.tostring(); } else { txtbox.text = "you can't divide zero... sign math 100 please =)"; } break; default: result = 0; break; } } }
}
you need previous operation before overwriting new one:
private void btnaddition_click(object sender, eventargs e) { num2 = double.parse(txtbox.text); num1 = calc(num1, num2, c); c = "+"; txtbox.clear(); }
where calc operation on "=" now.
Comments
Post a Comment