简体   繁体   中英

C# calculator application

This is the code for the calculator in which i have been attempting to make. I have got it to work except i have one error that i cant seem to figure out how to fix it.

whenever the user clicks the wrong operator then clicks the correct one that they wish to use the operator (+ button - button ect)will not change to the correct one and the function carried out will be in incorrect one.

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

You're using math_operator to store the selected math operator. Its value is only set when second_click == false (in btn_operator_Click() ), therefore it seems clear why the second click doesn't change things.

I figured out the answer and anyone who is interested here it is.

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

By clicking the wrong operator it will reset the math_operator to equal nothing then setting double_operation to false so that the rest if the code below can be preformed when the user hits the correct operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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