简单的计算器问题(无法使等于按钮多次工作)

Simple Calculator Woes(Can't get the equals button to work more than once)

我是一个编程菜鸟,我似乎无法让这个计算器正常工作。我已经对所有按钮进行了编码,并允许用户使用键盘输入(我没有将其放入代码摘录中,因为它无关紧要),但正如标题所暗示的那样,等号按钮不能用于超过一个总和。我不确定是什么导致了这个问题。如有任何建议,我们将不胜感激。

这是我的代码。

public partial class MainWindow : Window
{
    string input = string.Empty;            //String storing user input
    string operand1 = string.Empty;         //String storing first operand
    string operand2 = string.Empty;         //String storing second operand
    char operation;                         //char for operarion
    double result = 0.0;                    //calculated result
    bool operationCompleted = false;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void btn_Zero_Click(object sender, RoutedEventArgs e) 
    {
        this.textBox.Text = "";
        input += "0";
        this.textBox.Text += input;

        if(operationCompleted)
        {
            btn_Clear_Click(sender, e);
            operationCompleted = false;
        }
        btn_Equals.Focus();
    }

    private void btn_One_Click(object sender, RoutedEventArgs e)
    {
        this.textBox.Text = "";
        input += "1";
        this.textBox.Text += input;

        if (operationCompleted)
        {
            btn_Clear_Click(sender, e);
            operationCompleted = false;
        }
        btn_Equals.Focus();
    }

    private void btn_Two_Click(object sender, RoutedEventArgs e)
    {
        this.textBox.Text = "";
        input += "2";
        this.textBox.Text += input;

        if (operationCompleted)
        {
            btn_Clear_Click(sender, e);
            operationCompleted = false;
        }
        btn_Equals.Focus();
    }

    private void btn_Three_Click(object sender, RoutedEventArgs e)
    {
        this.textBox.Text = "";
        input += "3";
        this.textBox.Text += input;

        if (operationCompleted)
        {
            btn_Clear_Click(sender, e);
            operationCompleted = false;
        }
        btn_Equals.Focus();
    }


    private void btn_Four_Click(object sender, RoutedEventArgs e)
    {
        this.textBox.Text = "";
        input += "4";
        this.textBox.Text += input;

        if (operationCompleted)
        {
            input = string.Empty;
            textBox.Text = string.Empty;
            operand1 = string.Empty;
            operand2 = string.Empty;
            operationCompleted = false;
        }
        btn_Equals.Focus();
    }

    private void btn_Five_Click(object sender, RoutedEventArgs e)
    {
        this.textBox.Text = "";
        input += "5";
        this.textBox.Text += input;

        if (operationCompleted)
        {
            btn_Clear_Click(sender, e);
            operationCompleted = false;
        }
        btn_Equals.Focus();
    }

    private void btn_Six_Click(object sender, RoutedEventArgs e)
    {
        this.textBox.Text = "";
        input += "6";
        this.textBox.Text += input;

        if (operationCompleted)
        {
            btn_Clear_Click(sender, e);
            operationCompleted = false;
        }
        btn_Equals.Focus();
    }

    private void btn_Seven_Click(object sender, RoutedEventArgs e)
    {
        this.textBox.Text = "";
        input += "7";
        this.textBox.Text += input;
        if (operationCompleted)
        {
            btn_Clear_Click(sender, e);
            operationCompleted = false;
        }
        btn_Equals.Focus();

    }

    private void btn_Eight_Click(object sender, RoutedEventArgs e)
    {
        this.textBox.Text = "";
        input += "8";
        this.textBox.Text += input;

        if (operationCompleted)
        {
            btn_Clear_Click(sender, e);
            operationCompleted = false;
        }
        btn_Equals.Focus();
    }

    private void btn_Nine_Click(object sender, RoutedEventArgs e)
    {
        this.textBox.Text = "";
        input += "9";
        this.textBox.Text += input;

        if (operationCompleted)
        {
            btn_Clear_Click(sender, e);
            operationCompleted = false;
        }
        btn_Equals.Focus();
    }

    private void btn_Dot_Click(object sender, RoutedEventArgs e)
    {
        this.textBox.Text = "";
        input += ".";
        this.textBox.Text += input;
        btn_Equals.Focus();
    }

    private void btn_Minus_Click(object sender, RoutedEventArgs e)
    {
        operand1 = input;
        operation += '-';
        input = string.Empty;
        textBox.Text = string.Empty;
        btn_Equals.Focus();
    }

    private void btn_Plus_Click(object sender, RoutedEventArgs e)
    {
        operand1 = input;
        operation += '+';
        input = string.Empty;
        textBox.Text = string.Empty;
        btn_Equals.Focus();
    }

    private void btn_Multiply_Click(object sender, RoutedEventArgs e)
    {
        operand1 = input;
        operation += '*';
        input = string.Empty;
        textBox.Text = string.Empty;
        btn_Equals.Focus();
    }

    private void btn_Divide_Click(object sender, RoutedEventArgs e)
    {
        operand1 = input;
        operation += '/';
        input = string.Empty;
        textBox.Text = string.Empty;
        btn_Equals.Focus();

    }


    // The equals works for the first sum but not for any after it

    private void btn_Equals_Click(object sender, RoutedEventArgs e)
    {
        {
            operand2 = input;
            double num1, num2;
            double.TryParse(operand1, out num1);
            double.TryParse(operand2, out num2);

            if (operation == '+')
            {
                result = num1 + num2;
                textBox.Text = result.ToString();
            }

            else if (operation == '-')
            {
                result = num1 - num2;
                textBox.Text = result.ToString();
            }
            else if (operation == '*')
            {
                result = num1 * num2;
                textBox.Text = result.ToString();
            }
            else if (operation == '/')
            {
                if (num1 != 0 || num2 != 0)
                {
                    result = num1 / num2;
                    textBox.Text = result.ToString();
                }

                else
                {
                    textBox.Text = "Cannot divide by zero";
                }

            }
            operationCompleted = true;
            this.Focus();
        }
    }

    private void btn_Clear_Click(object sender, RoutedEventArgs e)
    {
        this.textBox.Text = "";
        this.input = string.Empty;
        this.operand1 = string.Empty;
        this.operand2 = string.Empty;
        this.result = 0.0;
        operationCompleted = false;
    }

}

您需要清除操作。你有操作 += '/'。因此,每次有人单击其中一个操作按钮时,您就将该操作附加到字符串中。当您单击等于按钮时,您不会捕获错误操作,因此什么也不会发生。

建议:

完成时清除操作。 添加默认的 else if 以解决错误的操作,以便您可以处理错误。