C#计算器应用程序

时间:2015-02-27 14:53:51

标签: c# calculator

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

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

    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;
        }

2 个答案:

答案 0 :(得分:1)

您正在使用math_operator来存储所选的数学运算符。 它的值仅在second_click == false(在btn_operator_Click()中)时设置,因此很明显为什么第二次点击不会改变事物。

答案 1 :(得分:0)

我找到答案,任何对此感兴趣的人都是。

    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重置为无效,然后将double_operation设置为false,以便在用户点击正确的运算符时可以执行下面的代码。