-1

我试图不断让这个计算器显示输入,例如1 + 1 = 2,我相信代码是正确的,但是当我遇到+ - / *它时需要解析。我没有最接近的想法如何做到这一点。

我已经尝试了很长一段时间,所以我 char.Parse 如果是的话在哪里?

public partial class Form1 : Form
{
    char c;
    double num1;
    double num2;

    public Form1()
    {
        InitializeComponent();
    }
    private void btn0_Click(object sender, EventArgs e)
    {
        txtBox.Text += 0;
    }
    private void btn1_Click(object sender, EventArgs e)
    {
        txtBox.Text += 1;
    }
    private void btn2_Click(object sender, EventArgs e)
    {
        txtBox.Text += 2;
    }
    private void btn3_Click(object sender, EventArgs e)
    {
        txtBox.Text += 3;
    }
    private void btn4_Click(object sender, EventArgs e)
    {
        txtBox.Text += 4;
    }
    private void btn5_Click(object sender, EventArgs e)
    {
        txtBox.Text += 5;
    }
    private void btn6_Click(object sender, EventArgs e)
    {
        txtBox.Text += 6;
    }
    private void btn7_Click(object sender, EventArgs e)
    {
        txtBox.Text += 7;
    }
    private void btn8_Click(object sender, EventArgs e)
    {
        txtBox.Text += 8;
    }
    private void btn9_Click(object sender, EventArgs e)
    {
        txtBox.Text += 9;
    }
    private void btnDecimal_Click(object sender, EventArgs e)
    {
        if (!txtBox.Text.Contains('.'))
            txtBox.Text += '.';
    }
    private void btnAddition_Click(object sender, EventArgs e)
    {
        c = '+';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty; // txtBox.Text += c;
    }
    private void btnSubtraction_Click(object sender, EventArgs e)
    {
        c = '-';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnMultiplication_Click(object sender, EventArgs e)
    {
        c = '*';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnDivision_Click(object sender, EventArgs e)
    {
        c = '/';
        num1 = double.Parse(txtBox.Text);
        txtBox.Text = string.Empty;
    }
    private void btnClear_Click(object sender, EventArgs e)
    {
        txtBox.Clear();
    }
    private void btnEqual_Click(object sender, EventArgs e)
    {
        double result;
        num2 = double.Parse(txtBox.Text);
        switch (c)
        {
            case '+':
                result = num1 + num2;
                txtBox.Text = result.ToString(); // txtBox.Text += result.ToString();
                break;
            case '-':
                result = num1 - num2;
                txtBox.Text = result.ToString();
                break;
            case '/':
                if (num2 != 0)
                {
                    result = num1 / num2;
                    txtBox.Text = result.ToString();
                }
                else
                {
                    txtBox.Text = "You can't divide by zero... sign up for Math 100 please =)";
                }
                break;
            case '*':
                result = num1 * num2;
                txtBox.Text = result.ToString();
                break;
        }
    }
}

}

4

3 回答 3

2

您的代码中的逻辑完全有缺陷。

你可以做三件事:

首先,您可以放弃您正在做的事情并使用一个为您执行此操作的库......例如NCalc

或者,您可以放弃您正在做的事情并查找执行此操作的算法......例如Shunting Yard算法。

或者,您可以继续尝试改进您所拥有的......但最好寻找其他方法来做到这一点。

于 2013-09-03T00:53:37.740 回答
0

通过查看您的代码,您有问题的代码行是:

txtBox.Text = result.ToString(); // txtBox.Text += result.ToString();

您将文本框的文本设置为计算结果(a + b、a - b、a * b 或 a / b),而不是您想要的表达式和结果。

结果,我认为您正在寻找类似的东西

txtBox.Text = num1 + " + " + num2 + " = " + result.ToString();
于 2013-09-03T00:55:55.923 回答
0

我不知道这是否适合您的需求,但也许您可以尝试以下方法:

.NET 中是否有字符串数学评估器?

于 2013-09-03T00:51:08.240 回答