为什么我的计算器中的计算不正确?

时间:2015-07-23 21:02:51

标签: c# .net

对于我的C#类,我们需要编写一个简单的计算器来执行基本的数学运算(加,减,乘,除)。我按照教授的视频,他的程序运行正常。减法命令似乎在结果前添加了 - 。除法命令导致某种程度的关闭。我做错了什么?

示例:

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

这是我的代码:

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 = "";
        }
    }

感谢您的时间。

2 个答案:

答案 0 :(得分:1)

你正在向后操作你的论点。因此,在你的equals函数中,你将第二个运算符除以第一运算符给你的分数。在减法中,你正在做2 - 123并得到一个负数,所以例如改为

 txtOutput.Text = Convert.ToString( Convert.ToDouble(operand) / Convert.ToDouble(txtOutput.Text));

答案 1 :(得分:0)

你得到了结果     2/124 = 0.0161290322580645

反转你的红利和除数;)