可以在c#计算器上执行某些操作但不是全部操作?

时间:2013-09-04 00:02:01

标签: c#

到目前为止,我的加法和减法工作。但是,我的乘法和除法不是。这是因为我输入的前两个数字被添加,操作完成。例如,如果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;
        }

    }
}

}

5 个答案:

答案 0 :(得分:1)

您需要先执行上一个操作,然后再使用新操作覆盖它:

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

其中calc执行你现在对“=”执行的操作。

答案 1 :(得分: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;
    }
}

答案 2 :(得分: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();
}

和类似的分和减法。我认为减法只适用于你的例子,因为它是你做的最后一件事,而equals按钮正确处理它。

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

答案 3 :(得分:0)

错误在于你为每个操作员做的事情:

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

这是错误的,因为你只是添加你在计算器中写的每个数字,除了最后一个由于btnEquals_Click而正确计算的数字。 实际上,只有在按下“=”时才检查您正在使用的运算符,您必须为您选择的每个运算符执行此操作。

答案 4 :(得分:0)

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

查看您的标准,这是每次行动之前/之后的状态:

  • 2 + 2 + 2 = 6
    • 2+
      • 之前:firstNumber = 0(默认值),operation =“+”(默认,隐藏),secondNumber = 2
      • 之后:firstNumber = 2(calc 0 + 2),operation =“+”,secondNumber = empty(等待输入)
    • 2 + 2 +
      • 之前:firstNumber = 2,operation =“+”,secondNumber = 2
      • 之后:firstNumber = 4(计算2 + 2),operation =“+”,secondNumber =空(等待输入)
    • 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 +(推断)(空)

<强>更新 根据您的代码来说,您需要以相同的方式将+, - ,/,*和=按钮更改为所有工作。也就是说,取num1cdouble.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的所有目的,所以它现在是上面例子中的firstNumber。

您可能希望添加一个全部清除,将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();
    }
}
相关问题