visual studio 2010 - C# Calculator working incorrectly -
the calculator works fine if keep using same operator(*, /, +, -) if example decide multiply total of 2 added numbers it'll give me wrong answer. i've looked solution can't seem find one.
public partial class frmmain : form { public frmmain() { initializecomponent(); } bool multiply = false; bool divide = false; bool add = false; bool subtract = false; private void btnone_click(object sender, eventargs e) { txtdisplay.text = txtdisplay.text + "1"; } private void btntwo_click(object sender, eventargs e) { txtdisplay.text = txtdisplay.text + "2"; } private void btnthree_click(object sender, eventargs e) { txtdisplay.text = txtdisplay.text + "3"; } private void btnfour_click(object sender, eventargs e) { txtdisplay.text = txtdisplay.text + "4"; } private void btnfive_click(object sender, eventargs e) { txtdisplay.text = txtdisplay.text + "5"; } private void btnsix_click(object sender, eventargs e) { txtdisplay.text = txtdisplay.text + "6"; } private void btnseven_click(object sender, eventargs e) { txtdisplay.text = txtdisplay.text + "7"; } private void btneight_click(object sender, eventargs e) { txtdisplay.text = txtdisplay.text + "8"; } private void btnnine_click(object sender, eventargs e) { txtdisplay.text = txtdisplay.text + "9"; } private void btnzero_click(object sender, eventargs e) { if (txtdisplay.text.length > 0) { txtdisplay.text = txtdisplay.text + "0"; } } private void btnclear_click(object sender, eventargs e) { txtdisplay.clear(); } private void btndecimalpoint_click(object sender, eventargs e) { if (txtdisplay.text.contains(".")) { return; } else { txtdisplay.text = txtdisplay.text + "."; } } private void btnnegative_click(object sender, eventargs e) { if (txtdisplay.text.contains("-")) { txtdisplay.text = txtdisplay.text.remove(0,1); } else { txtdisplay.text = "-" + txtdisplay.text; } } private void btnmultiply_click(object sender, eventargs e) { if (txtdisplay.text == "") { return; } else { multiply = true; txtdisplay.tag = txtdisplay.text; txtdisplay.text = ""; } } private void btnadd_click(object sender, eventargs e) { if (txtdisplay.text == "") { return; } else { add = true; txtdisplay.tag = txtdisplay.text; txtdisplay.text = ""; } } private void btnsubtract_click(object sender, eventargs e) { if (txtdisplay.text == "") { return; } else { subtract = true; txtdisplay.tag = txtdisplay.text; txtdisplay.text = ""; } } private void btnequals_click(object sender, eventargs e) { if (multiply) { decimal dec = convert.todecimal(txtdisplay.tag) * convert.todecimal(txtdisplay.text); txtdisplay.text = dec.tostring(); } if (divide) { decimal dec = convert.todecimal(txtdisplay.tag) / convert.todecimal(txtdisplay.text); txtdisplay.text = dec.tostring(); } if (add) { decimal dec = convert.todecimal(txtdisplay.tag) + convert.todecimal(txtdisplay.text); txtdisplay.text = dec.tostring(); } if (subtract) { decimal dec = convert.todecimal(txtdisplay.tag) - convert.todecimal(txtdisplay.text); txtdisplay.text = dec.tostring(); } else { return; } } private void btndivide_click(object sender, eventargs e) { if (txtdisplay.text == "") { return; } else { divide = true; txtdisplay.tag = txtdisplay.text; txtdisplay.text = ""; } } }
it looks problem aren't clearing operation commands. i.e. set add, never clear add when set multiply. if in btnequals_click method, it's possible have several operations active @ once, , execute of them.
Comments
Post a Comment