为什么我的计算器里的计算不准?

本文关键字:计算 不准 我的 计算器 为什么 | 更新日期: 2023-09-27 18:04:34

对于我的c#课程,我们需要编写一个简单的计算器来执行基本的数学运算(加、减、乘、除)。我看了教授的视频,他的程序运行得很好。减法命令似乎在结果前面加了一个-。divide命令会产生一些错误的结果。我做错了什么?

的例子:

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 - 123得到一个负数因此,例如,将其更改为

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

您正在获得的结果2/124 = 0.0161290322580645

把被除数和被除数倒过来;)