簡體   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