c# - Why are the calculations in my calculator not correct? -


for c# class, required program simple calculator performs basic mathematical operations(add, subtract, multiply, divide). followed professor's video , program works fine. subtract command seems add - in front of result. divide command results in way off. doing wrong?

example:

123+2 = 125 (as should) 123 - 2 = -121 124 / 2 = 0.0161290322580645 12.4 * 10 = 124 (correct) 

here code:

public partial class form1 : form     {         string operand = "", operation = "", memory = "";         public form1()         {             initializecomponent();         }          private void btnnum1_click(object sender, eventargs e)         {             txtoutput.text += "1";         }          private void btnnum2_click(object sender, eventargs e)         {             txtoutput.text += "2";         }          private void btnnum3_click(object sender, eventargs e)         {             txtoutput.text += "3";         }          private void btnnum4_click(object sender, eventargs e)         {             txtoutput.text += "4";         }          private void btnnum5_click(object sender, eventargs e)         {             txtoutput.text += "5";         }          private void btnnum6_click(object sender, eventargs e)         {             txtoutput.text += "6";         }          private void btnnum7_click(object sender, eventargs e)         {             txtoutput.text += "7";         }          private void btnnum8_click(object sender, eventargs e)         {             txtoutput.text += "8";         }          private void btnnum9_click(object sender, eventargs e)         {             txtoutput.text += "9";         }          private void btnnum0_click(object sender, eventargs e)         {             txtoutput.text += "0";         }          private void btndec_click(object sender, eventargs e)         {             if (txtoutput.text.indexof(".") == -1)             {                  txtoutput.text += ".";              }         }          private void btnnegpos_click(object sender, eventargs e)         {             if (txtoutput.text.indexof("-") == -1)             {                 txtoutput.text = "-" + txtoutput.text;             }             else             {                 txtoutput.text = txtoutput.text.substring(1, (txtoutput.text.length - 1));             }         }          private void execute()         {             if (operation == "+")             {                 operand = convert.tostring(convert.todouble(txtoutput.text) + convert.todouble(operand));             }             if (operation == "-")             {                 operand = convert.tostring(convert.todouble(txtoutput.text) - convert.todouble(operand));             }             if (operation == "*")             {                 operand = convert.tostring(convert.todouble(txtoutput.text) * convert.todouble(operand));             }             if (operation == "/")             {                 operand = convert.tostring(convert.todouble(txtoutput.text) / convert.todouble(operand));             }         }          private void btnadd_click(object sender, eventargs e)         {             if (operand == "")             {                 operand = txtoutput.text;             }             else             {                 execute();             }             operation = "+";             txtoutput.text = "";         }          private void btnsubtract_click(object sender, eventargs e)         {             if (operand == "")             {                 operand = txtoutput.text;             }             else             {                 execute();             }             operation = "-";             txtoutput.text = "";         }          private void btnmultiply_click(object sender, eventargs e)         {             if (operand == "")             {                 operand = txtoutput.text;             }             else             {                 execute();             }             operation = "*";             txtoutput.text = "";         }          private void btndivide_click(object sender, eventargs e)         {             if (operand == "")             {                 operand = txtoutput.text;             }             else             {                 execute();             }             operation = "/";             txtoutput.text = "";         }          private void btnclear_click(object sender, eventargs e)         {             txtoutput.text = "";             operand = "";             operation = "";         }          private void form1_load(object sender, eventargs e)         {          }          private void btnequals_click(object sender, eventargs e)         {             if (operation == "+")             {                 txtoutput.text = convert.tostring(convert.todouble(txtoutput.text) + convert.todouble(operand));             }             if (operation == "-")             {                 txtoutput.text = convert.tostring(convert.todouble(txtoutput.text) - convert.todouble(operand));             }             if (operation == "*")             {                 txtoutput.text = convert.tostring(convert.todouble(txtoutput.text) * convert.todouble(operand));             }             if (operation == "/")             {                 txtoutput.text = convert.tostring(convert.todouble(txtoutput.text) / convert.todouble(operand));             }             operand = "";             operation = "";         }          private void btnbackspace_click(object sender, eventargs e)         {             txtoutput.text = "";         }     } 

thanks time.

you're getting result 2 / 124 = 0.0161290322580645

invert dividend , divisor ;)


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -