0

到目前为止,我的加法和减法工作。但是,我的乘法和除法没有。这是因为我输入的前两个数字被添加并且操作完成。例如,如果 9 * 9 + 3 应该是 84,那么在我的计算器上是 21。那是因为它占用了 9+9 + 3;因为它只看到最后一个运算符。我完全不知道如何解决这个问题。有什么有用的见解吗?

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

    public Form1()
    {
        InitializeComponent();
    }

    private void btn0_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + btn0.Text;
    }

    private void btn1_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + btn1.Text;
    }

    private void btn2_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + btn2.Text;
    }

    private void btn3_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + btn3.Text;
    }

    private void btn4_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + btn4.Text;
    }

    private void btn5_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + btn5.Text;
    }

    private void btn6_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + btn6.Text;
    }

    private void btn7_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + btn7.Text;
    }

    private void btn8_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + btn8.Text;
    }

    private void btn9_Click(object sender, EventArgs e)
    {
        txtBox.Text = txtBox.Text + btn9.Text;
    }

    private void btnDecimal_Click(object sender, EventArgs e)
    {
        if (!txtBox.Text.Contains('.'))
            txtBox.Text += '.';
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        txtBox.Clear();
    }

    private void btnAddition_Click(object sender, EventArgs e)
    {
        num1 = num1 + double.Parse(txtBox.Text);
        c = "+";
        txtBox.Clear();
    }

    private void btnSubtraction_Click(object sender, EventArgs e)
    {
        num1 = num1 + double.Parse(txtBox.Text);
        c = "-";
        txtBox.Clear();
    }

    private void btnMultiplication_Click(object sender, EventArgs e)
    {
        num1 = num1 + double.Parse(txtBox.Text);
        c = "*";
        txtBox.Clear();
    }

    private void btnDivision_Click(object sender, EventArgs e)
    {
        num1 = num1 + double.Parse(txtBox.Text);
        c = "/";
        txtBox.Clear();
    }
    private void btnEquals_Click(object sender, EventArgs e)
    {

        double result;

        num2 = double.Parse(txtBox.Text);

        switch (c)
        {
            case "+":
                result = num1 + num2;
                txtBox.Text = result.ToString();
                num1 = 0;
                break;
            case "-":
                result = num1 - num2;
                txtBox.Text = result.ToString();
                num1 = 0;
                break;
            case "*":
                result = num1 * num2;
                txtBox.Text = result.ToString();
                num1 = 0;
                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;
            default:
                result = 0;
                break;
        }

    }
}

}

4

5 回答 5

1

两个问题:

num1每次单击除等于以外的运算符后,请查看您的变量。假设我从 6*4-3 开始。我按 6,然后按 *。此时 num1 现在变为 6。按 4 和 - 下一步。现在将 4 添加到 Num1 得到 10。然后按下 3 和等于,这可能给你 7。

第二个问题:每次按下不同的运算符(例如 + 或减号)时,c 都会被覆盖。

解决方案:

1)您可以制作中缀解析器或

2)将您的代码修改为如下内容(给出减法示例)

private void btnSubtraction_Click(object sender, EventArgs e)
{
    Equals()
    c = "-";
    txtBox.Clear();
}

private void btnEquals_Click(object sender, EventArgs e)
{
    Equals();
    txtBox.Text = Result.ToString();
    result = 0;
}

private void Equals()
{
    if (!double.TryParse(txtBox.Text, out num2)) return;

    switch (c)
    {
        case "+":
            result = result + num2;
            break;
        case "-":
            result = result - num2;
            break;
        case "*":
            result = result * num2;
            break;
        case "/":
            result = result / num2;
            break;
        default:
            result = num2;
            break;
    }
}
于 2013-09-04T00:41:45.483 回答
1

在用新的覆盖之前,您需要执行上一个操作:

private void btnAddition_Click(object sender, EventArgs e)
{
    num2 = double.Parse(txtBox.Text);
    num1 = calc(num1, num2, c);
    c = "+";
    txtBox.Clear();
}

calc 在哪里执行您现在对“=”执行的操作。

于 2013-09-04T00:20:09.257 回答
0

当执行任何操作(+、-、/、*、=)时,应评估现有状态(包括最后一个操作)并替换您的第一个数字、操作和第二个数字。

查看您的标准,这是每个操作之前/之后的状态:

  • 2+2+2= 6
    • 2+
      • before: firstNumber=0 (默认), operation="+" (默认, 隐藏), secondNumber=2
      • 之后:firstNumber=2 (calc 0+2), operation="+", secondNumber=empty (等待输入)
    • 2+2+
      • 之前:firstNumber=2, operation="+", secondNumber=2
      • 之后:firstNumber=4(calc 2+2), operation="+", secondNumber=empty (等待输入)
    • 2+2+2=
      • 之前:4 + 2
      • 之后:6 +(空)
  • 2+3-1= 4
    • 2+
      • 之前:0(默认)+(默认)2
      • 之后:2 +(空)
    • 2+3-
      • 之前:2 + 3
      • 之后:5 -(空)
    • 2+3-1=
      • 之前:5 - 1
      • 之后:4 +(推断)(空)
  • 6*4-3=21
    • 6*
      • 之前:0(默认)+(默认)6
      • 之后:6 *(空)
    • 6*4-
      • 之前:6 * 4
      • 之后:24 - (空)
    • 6*4-3=
      • 之前:24 - 3
      • 之后:21 +(推断)(空)

这更有意义吗?

还要遵循标准计算器约定,如果第二个数字当前为空,您可能需要考虑允许人们更改操作,例如

给定这个序列:

  • 6*4=-3= - 执行 6*4 并查看结果 ("="),然后从中减去 3 并显示结果 ("=")
    • 6*
      • 之前:0(默认)+(默认)6 -> 6 *(空)
    • 6*4=: 6 * 4
      • 之后:24 +(推断)(空)
    • 6*4=-
      • 之前:24 +(空)
      • 特殊情况后:24 - (空)
    • 6*4=-3=
      • 之前:24 - 3
      • 之后:21 +(推断)(空)

更新: 要根据您的代码说明这一点,您需要将 +、-、/、* 和 = 按钮更改为以相同的方式工作。也就是说,取num1c的当前值double.Parse(txtBox.Text),执行该操作,然后根据新操作进行更新。

// is the current txtBox value new, so will be cleared and allow op change
private bool isNew = true;
// change this to default to +
string c = "+";
double num1 = 0;

private void Evaluate(string newOperand)
{
    if (isNew)
    {
        // just update the operand, don't perform an evaluate
        c = newOperand;
        return;
    }

    double num2 = double.Parse(txtBox.Text);
    switch (c)
    {
        case "+":
            num1 = num1 + num2;
            break;
        // etc for -, /, *

        // for "="
        default:
            num1 = num1 + num2;
            break;
    }
    isNew = true;
    c = newOperand;
}

// this can be assigned as the handler for buttons 0 - 9
public void btnNumber_Click(object sender, EventArgs e)
{
    var button = sender as Button;
    txtBox.Text += button.Text;
    isNew = false;
}

// this can be assigned as the event handler for all operations: +, -, /, *, =
public void btnOperation_Click(object sender, EventArgs e)
{
    var button = sender as Button;
    Evaluate(button.Text);
}

我已经改变了num1你这样做的全部目的,所以它现在是第一个数字,就像我上面的例子一样。

您可能想要添加一个清除所有将重置num1为 0 和c+。您也可以btnClear_Click通过检测第二次按下来在现有设备中执行此操作,就像许多计算器一样:

public void btnClear_Click(object sender, EventArgs e)
{
    if (isNew)
    {
        num1 = 0;
        c = "";
        // text box should already be empty
    }
    else
    {
        isNew = true;
        txtBox.Clear();
    }
}
于 2013-09-04T01:12:03.223 回答
0

我认为你的问题是 num1。每次您进行操作时,您只需将 num1 与文本框的值相加。

所以我认为当你按下按钮时:6*4-3=你应该得到 21,但实际上你得到了 7

这是因为当您按下 * 和 - 按钮时会添加 num1,因此您最终会执行 6+4-3 = 7。

您需要更改btnMultiplication_Click为:

private void btnMultiplication_Click(object sender, EventArgs e)
{
    num1 = num1 * double.Parse(txtBox.Text);
    c = "*";
    txtBox.Clear();
}

以及类似的除法和减法。我认为减法仅在您的示例中有效,因为这是您做的最后一件事,并且等号按钮可以正确处理它。

我还没有测试过,但我认为这是你的问题

于 2013-09-04T00:15:40.353 回答
0

错误是您正在为每个操作员做:

num1 = num1 + double.Parse(txtBox.Text);

这是错误的,因为你只是把你在计算器里写的每一个数字都加起来,除了最后一个,它是正确计算的,因为btnEquals_Click. 实际上,您仅在按下“=”时检查您正在使用的运算符,您必须为您选择的每个运算符执行此操作。

于 2013-09-04T00:44:14.193 回答