简体   繁体   English

计算器项目 c#

[英]Calculator project c#

I want to make Button Equal repeat the last operation but don't know how to.我想让 Button Equal 重复上一次操作,但不知道如何操作。 I have tested different ways of doing it but have not gotten a correct answer.我已经测试了不同的方法,但没有得到正确的答案。 This is my windows form code.这是我的 windows 表单代码。 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.我尝试使用一个可以记住 output 的变量,但是当使用该方法时,如果我写 3+3=6 然后再次点击相等,我得到 12,因为计算器添加了 6+6 而不是 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).浏览完您的代码后,我假设变量“tal”负责存储最后一个操作数值(预期 - 3,实际 - 6,根据您引用的示例)。 Have you tried debugging through code to check if the value of 'tal' is getting updated somewhere?您是否尝试过通过代码进行调试以检查“tal”的值是否在某处得到更新? 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;

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

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