简体   繁体   中英

Calculator project c#

I want to make Button Equal repeat the last operation but don't know how to. I have tested different ways of doing it but have not gotten a correct answer. This is my windows form code. I tried having a variable that remembers the output but when using that method if i write 3+3=6 and then hit equal again i get 12 because the calculator adds 6+6 instead of 6+3.

    Double tal = 0;
    string operation = "";
    bool operationKlickad = false;


    public Form1()
    {
        InitializeComponent();
    }

    private void button_click(object sender, EventArgs e)
    {
        if (txbWindow.Text == "0" || operationKlickad)
        {
            txbWindow.Clear();
        }

        operationKlickad = false;
        Button b = (Button)sender;//Konverterar object sender till en button. 
        if(b.Text == ",")
        {
            if(!txbWindow.Text.Contains(","))//Om det inte finns något komma så skriver den det men om det finns händer inget.
                txbWindow.Text = txbWindow.Text + b.Text;
        }
        else
            txbWindow.Text = txbWindow.Text + b.Text; // Tar det som står på knappen

    }

    private void btnCE_Click(object sender, EventArgs e) 
    {
        txbWindow.Text = "0";// tar bort allt i rutan
    }

    private void operator_click(object sender, EventArgs e)//När man klickar på +, -, * osv...
    {
        Button b = (Button)sender;

        if (tal == 0) // om inte tal är 0. Vilket innebär att man har klickat på en av operatör knapparna igen
        {

            operation = b.Text;// sparar vilken operation som blir klickad
            tal = Double.Parse(txbWindow.Text);//sparar talet som är i rutan
            operationKlickad = true;
            lblEkvation.Text += tal + " " + operation;
        }
        else
        {
            btnEqual.PerformClick(); //turn on equal button
            operationKlickad = true;
            operation = b.Text;
            lblEkvation.Text += tal + " " + operation;//Visar föregående tal och vilken operation
        }

    }

    private void btnEqual_Click(object sender, EventArgs e) //Equalbutton
    {

        lblEkvation.Text = "";//rensar ekvationen 

        switch (operation)
        {
                case "+":
                    txbWindow.Text = Convert.ToString(tal + Double.Parse(txbWindow.Text)); //sets tbxwindo to the number written plus number in the window
                    break;

                case "-":
                    txbWindow.Text = Convert.ToString((tal) - Double.Parse(txbWindow.Text));
                    break;

                case "*":
                    txbWindow.Text = Convert.ToString(tal * Double.Parse(txbWindow.Text));
                    break;

                case "/":
                    txbWindow.Text = Convert.ToString(tal / Double.Parse(txbWindow.Text));
                    break;

                case "√":
                    txbWindow.Text = Convert.ToString(Math.Sqrt(Double.Parse(txbWindow.Text)));
                    break;

                case "±":
                    txbWindow.Text = Convert.ToString(-1 * Double.Parse(txbWindow.Text));
                    break;

                case "1 / X":
                    txbWindow.Text = Convert.ToString(1 / (Double.Parse(txbWindow.Text)));
                    break;

                default:
                    break;

        }
        tal = Double.Parse(txbWindow.Text);
        operation = "";

    }

    private void btnC_Click(object sender, EventArgs e)
    {
        txbWindow.Text = "0"; //tar bort allt i rutan
        tal = 0;//tar bort talet som sparats 
        lblEkvation.Text = "";
    }

    private void btnBack_Click(object sender, EventArgs e)
    {
        txbWindow.Text = txbWindow.Text.Remove(txbWindow.Text.Length - 1); // Tar stringen och tar bort sista karaktären

After going through your code, I am assuming the variable 'tal' is responsible to store last operand value (expected - 3, actual - 6, as per your cited example). Have you tried debugging through code to check if the value of 'tal' is getting updated somewhere? That would ideally help you figure out what is the actual root cause.

Also, once you figure that out, another observation I had was for the minus and division operators (which I believe you would have figured out anyway in your due course of testing)

                case "-":
                    txbWindow.Text = Convert.ToString(Double.Parse(txbWindow.Text) - tal);
                    break;

                case "*":
                    //No change required here

                case "/":
                    txbWindow.Text = Convert.ToString(Double.Parse(txbWindow.Text) / tal);
                    break;

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