繁体   English   中英

C#计算器应用程序

[英]C# calculator application

这是我一直试图制作的计算器代码。 我有它的工作,除了我有一个错误,我似乎无法弄清楚如何解决它。

每当用户单击错误的操作员,然后单击他们希望使用的正确操作员时(+按钮-按钮ect)都不会更改为正确的操作员,并且执行的功能将不正确。

    double value_1 = 0;
    bool double_operation = false;
    bool clickable_decimal = true;
    bool operation_pressed = false;
    bool second_click = false;
    bool second_equals = false;
    String math_operator = "";


    private void btn_Click(object sender, EventArgs e)
    {
        if (second_equals == true)
        {
            txt_display.Text = "0";
            //value_1 = 0;
            second_equals = false;
        }
        if ((txt_display.Text == "0") || (operation_pressed == true))
        {
            txt_display.Clear();
        }
        operation_pressed = false;
        Button btn = (Button)sender;
        txt_display.Text = txt_display.Text + btn.Text;
        double_operation = false;

    }

    private void btn_clear_Click(object sender, EventArgs e)
    {
        txt_display.Text = "0";
        value_1 = 0;
    }

    private void btn_deci_Click(object sender, EventArgs e)
    {
        {
            if (clickable_decimal == true)
            {
                Button btn = (Button)sender;
                txt_display.Text = txt_display.Text + btn.Text;
                clickable_decimal = false;
            }

        }
    }

    private void btn_operator_Click(object sender, EventArgs e)
    {
        if (double_operation == false)
        {
            if (second_click == false)
            {

                Button btn = (Button)sender;
                math_operator = btn.Text;
                value_1 = double.Parse(txt_display.Text);
                operation_pressed = true;
                clickable_decimal = true;
                second_equals = false;
            }

            if (second_click == true)
            {
                Button btn = (Button)sender;

                //value_2 = double.Parse(txt_display.Text);
                operation_pressed = true;
                clickable_decimal = true;

                switch (math_operator)
                {
                    case "+":
                        txt_display.Text = (value_1 + double.Parse(txt_display.Text)).ToString();
                        break;

                    case "-":
                        txt_display.Text = (value_1 - double.Parse(txt_display.Text)).ToString();
                        break;

                    case "/":
                        txt_display.Text = (value_1 / double.Parse(txt_display.Text)).ToString();
                        break;

                    case "*":
                        txt_display.Text = (value_1 * double.Parse(txt_display.Text)).ToString();
                        break;

                }//end switch
                value_1 = double.Parse(txt_display.Text);
                math_operator = btn.Text;
                second_equals = true;
            }
            second_click = true;
        }
        double_operation = true;

}

    private void btn_equals_Click(object sender, EventArgs e)
    {
        if (second_equals == false)
        {
            switch (math_operator)
            {
                case "+":
                    txt_display.Text = (value_1 + double.Parse(txt_display.Text)).ToString();
                    break;

                case "-":
                    txt_display.Text = (value_1 - double.Parse(txt_display.Text)).ToString();
                    break;

                case "/":
                    txt_display.Text = (value_1 / double.Parse(txt_display.Text)).ToString();
                    break;

                case "*":
                    txt_display.Text = (value_1 * double.Parse(txt_display.Text)).ToString();
                    break;

            }//end switch
            value_1 = double.Parse(txt_display.Text);
            second_click = false;
            second_equals = true;
        }

您正在使用math_operator存储选定的数学运算符。 仅当second_click == false时才设置其值(在btn_operator_Click() ),因此似乎很清楚为什么第二次单击不改变任何东西。

我想出了答案,任何对此感兴趣的人都是。

    double value_1 = 0;
    bool double_operation = false;
    bool clickable_decimal = true;
    bool operation_pressed = false;
    bool second_click = false;
    bool second_equals = false;
    String math_operator = "";


    private void btn_Click(object sender, EventArgs e)
    {
        if (second_equals == true)
        {
            txt_display.Text = "0";
            //value_1 = 0;
            second_equals = false;
        }
        if ((txt_display.Text == "0") || (operation_pressed == true))
        {
            txt_display.Clear();
        }
        operation_pressed = false;
        Button btn = (Button)sender;
        txt_display.Text = txt_display.Text + btn.Text;
        double_operation = false;

    }

    private void btn_clear_Click(object sender, EventArgs e)
    {
        txt_display.Text = "0";
        value_1 = 0;
    }

    private void btn_deci_Click(object sender, EventArgs e)
    {
        {
            if (clickable_decimal == true)
            {
                Button btn = (Button)sender;
                txt_display.Text = txt_display.Text + btn.Text;
                clickable_decimal = false;
            }

        }
    }

    private void btn_operator_Click(object sender, EventArgs e)
    {
//==========================================================================
        if (double_operation == true)
        {
            math_operator = "";
            double_operation = false;
            second_click = false; // however it will work without this but i       
            //do not know why so ill leave it in 
        }
 // this is the solution 
 //=========================================================================
        if (double_operation == false)
        {
            if (second_click == false)
            {

                Button btn = (Button)sender;
                math_operator = btn.Text;
                value_1 = double.Parse(txt_display.Text);
                operation_pressed = true;
                clickable_decimal = true;
                second_equals = false;
            }

            if (second_click == true)
            {
                Button btn = (Button)sender;
                operation_pressed = true;
                clickable_decimal = true;
                switch (math_operator)
                {
                    case "+":
                        txt_display.Text = (value_1 + double.Parse(txt_display.Text)).ToString();
                        break;

                    case "-":
                        txt_display.Text = (value_1 - double.Parse(txt_display.Text)).ToString();
                        break;

                    case "/":
                        txt_display.Text = (value_1 / double.Parse(txt_display.Text)).ToString();
                        break;

                    case "*":
                        txt_display.Text = (value_1 * double.Parse(txt_display.Text)).ToString();
                        break;

                }//end switch
                value_1 = double.Parse(txt_display.Text);
                math_operator = btn.Text;
                second_equals = true;
            }
            second_click = true;
        }
        double_operation = true;

}

    private void btn_equals_Click(object sender, EventArgs e)
    {
        if (second_equals == false)
        {
            switch (math_operator)
            {
                case "+":
                    txt_display.Text = (value_1 + double.Parse(txt_display.Text)).ToString();
                    break;

                case "-":
                    txt_display.Text = (value_1 - double.Parse(txt_display.Text)).ToString();
                    break;

                case "/":
                    txt_display.Text = (value_1 / double.Parse(txt_display.Text)).ToString();
                    break;

                case "*":
                    txt_display.Text = (value_1 * double.Parse(txt_display.Text)).ToString();
                    break;

            }//end switch
            value_1 = double.Parse(txt_display.Text);
            second_click = false;
            second_equals = true;
        }

通过单击错误的运算符,它将把math_operator重置为math_operator等于任何值,然后将double_operation设置为false,以便其余的(如果下面的代码可以在用户点击正确的运算符时执行)而得以执行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM