class 的 C# 计算器未显示所需的输出

C# Calculator with class not displaying desired output

我在使用这个程序时遇到问题 - 我有 frm 代码并创建了一个 class 来计算。该程序运行并接受输入 - 但是,当我按下等于时,它显示第一个操作数。因此,5 + 3 = 5、1 + 8 = 1 等等。任何人都可以阐明为什么会发生这种情况吗?我在这个问题上转了好几天,尝试用不同的方法来完成这个,但一直没能弄明白。

frm计算器


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 Project_3_A_Create_a_basic_calculator
{
    public partial class frmCalculator : Form
    {

        //Calculator calc;


        //variables for storing input, operands, and result
        //string input = string.Empty;
        //string operand1 = string.Empty;
        //string operand2 = string.Empty;
        //char operation;
        //double result = 0.0;


        public frmCalculator()
        {
            InitializeComponent();
        }
        private string displayString;
        private decimal displayValue;

        private bool newValue;
        private bool decimalEntered;

        private Calculator calc = new Calculator();
        
        private void frmCalculator_Load(object sender, System.EventArgs e)
        {
            displayValue = 0;
            displayString = displayValue.ToString();
            newValue = true;
            decimalEntered = false;

        }
        private void btnNumber_Click (object sender, System.EventArgs e)
        {
            if (newValue)
            {
                displayString = "";
                newValue = false;
            }
            displayString += ((Button)sender).Tag.ToString();
            displayValue = Convert.ToDecimal(displayString);
            txtDisplay.Text = displayValue.ToString();
        }

        private void btnBackSpace_Click(object sender, EventArgs e)
        {
            if (displayString.Length > 1)
            {
                displayString = displayString.Substring(0, displayString.Length - 1);
                displayValue = Convert.ToDecimal(displayString);
                txtDisplay.Text = displayValue.ToString();
            }
            else
            {
                displayString = "";
                displayValue = 0;
                txtDisplay.Text = displayValue.ToString();
            }
        }

        private void btnClear_Click (object sender, System.EventArgs e)
        {
            calc.Clear();
            displayString = "";
            displayValue = 0;
            txtDisplay.Text = displayValue.ToString();
            newValue = true;
            decimalEntered = false;
        }

        private void btnDecimal_Click(object sender, EventArgs e)
        {
            if (newValue)
            {
                displayString = "0";
                newValue = false;
            }
            if (!decimalEntered)
            {
                displayString += ".";
                displayValue = Convert.ToDecimal(displayString);
                txtDisplay.Text = displayValue.ToString();
                decimalEntered = true;
            }
        }

        private void btnSign_Click(object sender, EventArgs e)
        {
            displayValue = -displayValue;
            txtDisplay.Text = displayValue.ToString();
        }

        private void btnAdd_Click (object sender, EventArgs e)
        {
            calc.Add(displayValue);
            newValue = true;
            decimalEntered = false;
            displayValue = calc.CurrentValue;
            txtDisplay.Text = displayValue.ToString();
        }

        private void btnSubtract_Click(object sender, EventArgs e)
        {
            calc.Subtract(displayValue);
            newValue = true;
            decimalEntered = false;
            displayValue = calc.CurrentValue;
            txtDisplay.Text = displayValue.ToString();
        }

        private void btnMultiply_Click(object sender, EventArgs e)
        {
            calc.Multiply(displayValue);
            newValue = true;
            decimalEntered = false;
            displayValue = calc.CurrentValue;
            txtDisplay.Text = displayValue.ToString();
        }

        private void btnDivide_Click(object sender, EventArgs e)
        {
            calc.Divide(displayValue);
            newValue = true;
            decimalEntered = false;
            displayValue = calc.CurrentValue;
            txtDisplay.Text = displayValue.ToString();
        }

        private void btnSqrt_Click(object sender, EventArgs e)
        {
            calc.SquareRoot(displayValue);
            displayValue = calc.CurrentValue;
            txtDisplay.Text = displayValue.ToString();
        }

        private void btnReciprocal_Click(object sender, EventArgs e)
        {
            try
            {
                calc.Reciprocal(displayValue);
                displayValue = calc.CurrentValue;
                txtDisplay.Text = displayValue.ToString();
            }
            catch (DivideByZeroException)
            {
                displayValue = 0;
                txtDisplay.Text = "Can't divide by zero";
                newValue = true;
                decimalEntered = false;
            }
        }

        private void btnEquals_Click(object sender, System.EventArgs e)
        {
            try
            {
                if (newValue)
                    calc.Equals();
                else
                    calc.Equals(displayValue);
                displayValue = calc.CurrentValue;

                txtDisplay.Text = displayValue.ToString();
                newValue = true;
                decimalEntered = false;
            }
            catch (DivideByZeroException)
            {
                displayValue = 0;
                txtDisplay.Text = "Can't divide by zero";
                newValue = true;
                decimalEntered = false;
                
                
            }

        }


    }



}

和计算器class

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

namespace Project_3_A_Create_a_basic_calculator
{

    public class Calculator
    {

        public Decimal displayValue;
        public Decimal currentValue;
       

        private Decimal Operand1;
        private Decimal Operand2;

        private bool Operand1Add = false;
        private bool Operand2Add = false;

        private string Operation = "";

        public Calculator()
        {
            Clear();
        }

        private void AddOperand(Decimal Operand)
        {
            if (Operand1Add)
            {
                Operand1 = Operand;
                Operand1Add = true;
            }
            else
            {
                Operand2 = Operand;
                Operand2Add = true;
                currentValue = Operand2;
            }
        }



        public void Add(Decimal op)
        {
            this.AddOperand(op);
            Operation = "Addition";
        }

        public void Reciprocal(Decimal op1)
        {
            this.AddOperand(op1);
            Operation = "Reciprocal";
        }

        public void Subtract(Decimal op2)
        {
            this.AddOperand(op2);
            Operation = "Subtract";
        }

        public void Multiply(Decimal op5)
        {
            this.AddOperand(op5);
            Operation = "Multiply";

        }
        public void SquareRoot(Decimal op3)
        {
            this.AddOperand(op3);
            Operation = "Sqrt";
        }

        public void Divide(Decimal op4)
        {
            this.AddOperand(op4);
            Operation = "Divide";
        }


        public void Clear()
        {
            currentValue = 0;
            displayValue = 0;
            Operand1 = 0;
            Operand2 = 0;
        }

        public void Equals()
        {
            switch (Operation)
            {
                case "Addition":
                    currentValue = Operand1 + Operand2;
                    break;
                case "Reciprocal":
                    currentValue = 1 / Operand1;
                    break;
                case "Subtraction":
                    currentValue = Operand1 - Operand2;
                    break;
                case "Multiply":
                    currentValue = 1 * Operand1;
                    break;

                case "Divide":
                    currentValue = Operand1 / Operand2;
                    break;
                default:
                    currentValue = 0;
                    break;
            }
        }
        

        public void Equal(Decimal displayValue)
        {
            currentValue = displayValue;
        }
        public decimal CurrentValue
        {
            get
            {
                return currentValue;
            }

        }
    }
}

您的 AddOperant 逻辑有错误

    private void AddOperand(Decimal Operand)
    {
        if (Operand1Add)   // if(true) then set to true?
        {
            Operand1 = Operand;
            Operand1Add = true;
        }
        else
        {
            Operand2 = Operand;
            Operand2Add = true;
            currentValue = Operand2;
        }
    }

你能从上面的提示中看出吗?