繁体   English   中英

C#计算器,等于按钮重复操作无法正常工作

[英]C# Calculator, Equals button repeat operation not working properly

我正在构建一个简单的C#计算器,并且在equals按钮上遇到一些麻烦。 第一次单击可以计算指定的方程,效果很好,但是我希望每次用户持续单击equals按钮时都使用第二个数字(在这种情况下为y)重复该操作。 当前,以全部操作重复操作,而不是原始的第二个数字。 任何帮助将不胜感激! 这是课程:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace BMW_CALC_UI
   {
    public class Calculator
    {
        public double Add(double x, double y)
        {
            // Addition
            return x + y;
        }

        public double Subtract(double x, double y)
        {
            // Subtraction
            return x - y;
        }

        public double Multiply(double x, double y)
        {
            // Multiplication
            return x * y;
        }

        public double Divide(double x, double y)
        {
            // Division
            return x / y;
        }

        public double SquareRoot(double x)
        {
            // Square Root (must be called as a double)
            return Math.Sqrt(x);
        }

        public double Reciprocal(double x)
        {
            // Reciprocal
            return 1 / x;
        }

        public double ChangeSign(double x)
        {
            // Change sign
            return -x;
        }
    }
    }

形式如下:

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;

namespace BMW_CALC_UI
{
public partial class frmCalculator : Form
{
    public frmCalculator()
    {
        InitializeComponent();
    }

    // Set variables
    double x;
    double y;
    char operation;


    private void btnEquals_Click(object sender, EventArgs e)
    {
        if (txtDisplay.Text != "")
        {


            // Instantiate the instance
            Calculator myCalculator = new Calculator();

            // Store second number
           if (double.TryParse(txtDisplay.Text, out y))

            switch (operation)
            {
                case '+':
                    // Add addition method
                    txtDisplay.Text = (myCalculator.Add(x, y)).ToString();
                    break;

                case '-':
                    // Add subtraction method
                    txtDisplay.Text = (myCalculator.Subtract(x, y)).ToString();
                    break;

                case '*':
                    // Add multiplication method
                    txtDisplay.Text = (myCalculator.Multiply(x, y)).ToString();
                    break;

                case '/':
                    if (y == 0)
                    {
                        // Display error message
                        txtDisplay.Text = "Cannot divide by zero";
                        return;
                    }
                    // Add division method
                    txtDisplay.Text = (myCalculator.Divide(x, y)).ToString();
                    break;
            }

            // Reset 
            x = y;
        }                        

    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        // Clear all data
        txtDisplay.Clear();
        x = 0;
        y = 0;
    }

    private void btnBack_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

            // Clear last number entered
            if (txtDisplay.Text != "" && txtDisplay.TextLength > 0)
            {
                txtDisplay.Text = txtDisplay.Text.Remove(txtDisplay.TextLength - 1);
            }
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
            operation = '+';
            txtDisplay.Clear();
        }
    }

    private void btnSubtract_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
            operation = '-';
            txtDisplay.Clear();
        }
    }

    private void btnMultiply_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
            operation = '*';
            txtDisplay.Clear();
        }
    }

    private void btnDivide_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
        operation = '/';
        txtDisplay.Clear();
        }
    }

    private void btnSquareRoot_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Instantiate the instance
        Calculator myCalculator = new Calculator();

        // Perform square root operation
        if (double.TryParse(txtDisplay.Text, out x))
        {
            txtDisplay.Text = (myCalculator.SquareRoot(x)).ToString();
        }
    }

    private void btnReciprocal_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Instantiate the instance
        Calculator myCalculator = new Calculator();

        // Perform reciprocal operation
        if (double.TryParse(txtDisplay.Text, out x))
        {
            txtDisplay.Text = (myCalculator.Reciprocal(x)).ToString();
        }
    }

    private void btnSign_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Instantiate the instance
        Calculator myCalculator = new Calculator();

        // Perform sign operation
        if (double.TryParse(txtDisplay.Text, out x))
        {
            txtDisplay.Text = (myCalculator.ChangeSign(x)).ToString();
        }
    }

    private void btnDecimalPoint_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Add decimal point if none exist
        if (txtDisplay.Text.Contains("."))
        {
            txtDisplay.Text = txtDisplay.Text;
        }
        else
        {
            txtDisplay.Text = txtDisplay.Text + ".";
        }
    }

    private void btnZero_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // If textbox starts doesn't start with 0 or Textbox
        // contains a decimal point then it is ok to add a zero
        else if (!txtDisplay.Text.StartsWith("0") || txtDisplay.Text.Contains("."))
        {
            // Add 0 to display
            txtDisplay.Text += "0";
        }
    }

    private void btnOne_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 1 to display
        txtDisplay.Text += "1";
    }

    private void btn4_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 4 to display
        txtDisplay.Text += "4";
    }

    private void btnSeven_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 7 to display
        txtDisplay.Text += "7";
    }

    private void btnTwo_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 2 to display
        txtDisplay.Text += "2";
    }

    private void btnFive_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 5 to display
        txtDisplay.Text += "5";
    }

    private void btnEight_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 8 to display
        txtDisplay.Text += "8";
    }

    private void btnThree_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 3 to display
        txtDisplay.Text += "3";
    }

    private void btnSix_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 6 to display
        txtDisplay.Text += "6";
    }

    private void btn9_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 9 to display
        txtDisplay.Text += "9";
    }   
}
}

您从存储输出的同一文本框中获取第二个数字

这里是第二个: if (double.TryParse(txtDisplay.Text, out y))

在这里存储输出: txtDisplay.Text = (myCalculator.Add(x, y)).ToString();

只需将结果存储在其他位置即可,例如,您可以在定义x,y的位置定义“结果”。 将结果存储在那里。

并写入“等于”函数:

case '+':
// Add addition method
txtDisplay.Text = (myCalculator.Add(result, y)).ToString();
break;

当然,如果这是您第一次添加数字,则需要知道它(存储在布尔值中)

感谢您的帮助,下面的代码是我最终得到的,效果很好。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BMW_CALC_UI
{
public partial class frmCalculator : Form
{
    public frmCalculator()
    {
        InitializeComponent();
    }

    // Set variables
    double x;
    double y;
    char operation;
    bool EqualsRepeated = false;

    private void btnEquals_Click(object sender, EventArgs e)
    {

        if (txtDisplay.Text != "")
        {

            // Instantiate the instance
            Calculator myCalculator = new Calculator();

            // Store second number
            if (EqualsRepeated == false)
            {
                if (double.TryParse(txtDisplay.Text, out y))

                    switch (operation)
                    {
                        case '+':
                            // Add addition method
                            txtDisplay.Text = (myCalculator.Add(x, y)).ToString();
                            break;

                        case '-':
                            // Add subtraction method
                            txtDisplay.Text = (myCalculator.Subtract(x, y)).ToString();
                            break;

                        case '*':
                            // Add multiplication method
                            txtDisplay.Text = (myCalculator.Multiply(x, y)).ToString();
                            break;

                        case '/':
                            if (y == 0)
                            {
                                // Display error message
                                txtDisplay.Text = "Cannot divide by zero";
                                return;
                            }
                            // Add division method
                            txtDisplay.Text = (myCalculator.Divide(x, y)).ToString();
                            break;                   
                    }
                // Set button clicked to true
                EqualsRepeated = true;               
            }

            else
            {
                // If equals has already been clicked
                if (EqualsRepeated == true)

                    if (double.TryParse(txtDisplay.Text, out x))

                    switch (operation)
                    {
                        case '+':
                            // Add addition method
                            txtDisplay.Text = (myCalculator.Add(x, y)).ToString();
                            break;

                        case '-':
                            // Add subtraction method
                            txtDisplay.Text = (myCalculator.Subtract(x, y)).ToString();
                            break;

                        case '*':
                            // Add multiplication method
                            txtDisplay.Text = (myCalculator.Multiply(x, y)).ToString();
                            break;

                        case '/':
                            if (y == 0)
                            {
                                // Display error message
                                txtDisplay.Text = "Cannot divide by zero";
                                return;
                            }
                            // Add division method
                            txtDisplay.Text = (myCalculator.Divide(x, y)).ToString();
                            break;
                    }
            }

        }                        

    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        // Clear all data
        txtDisplay.Clear();
        EqualsRepeated = false;
        x = 0;
        y = 0;
    }

    private void btnBack_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

            // Clear last number entered
            if (txtDisplay.Text != "" && txtDisplay.TextLength > 0)
            {
                txtDisplay.Text = txtDisplay.Text.Remove(txtDisplay.TextLength - 1);
            }
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
            operation = '+';
            txtDisplay.Clear();
        }
    }

    private void btnSubtract_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
            operation = '-';
            txtDisplay.Clear();
        }
    }

    private void btnMultiply_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
            operation = '*';
            txtDisplay.Clear();
        }
    }

    private void btnDivide_Click(object sender, EventArgs e)
    {
        // Store first number, operation, then clear textbox
        if (double.TryParse(txtDisplay.Text, out x))
        {
        operation = '/';
        txtDisplay.Clear();
        }
    }

    private void btnSquareRoot_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Instantiate the instance
        Calculator myCalculator = new Calculator();

        // Perform square root operation
        if (double.TryParse(txtDisplay.Text, out x))
        {
            txtDisplay.Text = (myCalculator.SquareRoot(x)).ToString();
        }
    }

    private void btnReciprocal_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Instantiate the instance
        Calculator myCalculator = new Calculator();

        // Perform reciprocal operation
        if (double.TryParse(txtDisplay.Text, out x))
        {
            txtDisplay.Text = (myCalculator.Reciprocal(x)).ToString();
        }
    }

    private void btnSign_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Instantiate the instance
        Calculator myCalculator = new Calculator();

        // Perform sign operation
        if (double.TryParse(txtDisplay.Text, out x))
        {
            txtDisplay.Text = (myCalculator.ChangeSign(x)).ToString();
        }
    }

    private void btnDecimalPoint_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // Add decimal point if none exist
        if (txtDisplay.Text.Contains("."))
        {
            txtDisplay.Text = txtDisplay.Text;
        }
        else
        {
            txtDisplay.Text = txtDisplay.Text + ".";
        }
    }

    private void btnZero_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }

        // If textbox starts doesn't start with 0 or Textbox
        // contains a decimal point then it is ok to add a zero
        else if (!txtDisplay.Text.StartsWith("0") || txtDisplay.Text.Contains("."))
        {
            // Add 0 to display
            txtDisplay.Text += "0";
        }
    }

    private void btnOne_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 1 to display
        txtDisplay.Text += "1";
    }

    private void btn4_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 4 to display
        txtDisplay.Text += "4";
    }

    private void btnSeven_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 7 to display
        txtDisplay.Text += "7";
    }

    private void btnTwo_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 2 to display
        txtDisplay.Text += "2";
    }

    private void btnFive_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 5 to display
        txtDisplay.Text += "5";
    }

    private void btnEight_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 8 to display
        txtDisplay.Text += "8";
    }

    private void btnThree_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 3 to display
        txtDisplay.Text += "3";
    }

    private void btnSix_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 6 to display
        txtDisplay.Text += "6";
    }

    private void btn9_Click(object sender, EventArgs e)
    {
        // Clear display if error message is present
        if (txtDisplay.Text.Contains("Cannot divide by zero"))
        {
            txtDisplay.Clear();
        }
        // Add 9 to display
        txtDisplay.Text += "9";
    }   
 }
 }

暂无
暂无

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

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