繁体   English   中英

C#计算器类

[英]C# Calculator Class

我得到了这份家庭作业,这让我很辛苦。 表格已经写好了,我们不得不写课。 当前,当我运行程序时,我的equals按钮似乎无法正常工作。 我不确定为什么,我想知道是否有人可以帮助我了解我的缺失。 我相信我的课堂写得正确。 在我的脑海中,发生了什么事,计算器将其称为“ currentValue”,因此我不断更新自己在运算符中使用的方法。

我的方向正确吗?

为什么我的equals按钮不调用当前值。 上次运行此命令时,如果键入9 + 3 +,则显示屏将填充12,并等待我输入下一个数字。 那么从理论上讲,为什么我的等号按钮不会加载答案? 我相信我打的是正确的物品,但我一直保持着最初的名字。 例如,如果我输入9 + 9并命中=我继续得到9。

这是计算器代码(提供的部分):

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

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

    // The following fields are used to store the value that's currently
    // displayed by the calculator. displayString is a string value that's
    // constructed as the user clicks numeric keys and the decimal and +/-
    // key. The Convert.ToDecimal method is then used to convert this to a decimal
    // field that's stored in displayValue.
    private string displayString;
    private decimal displayValue;

    // The following bool fields are used to control numeric entry.
    // newValue indicates whether the calculator is ready to receive a
    // new numeric value. Once the user clicks a digit button, newValue is
    // set to false. When the user clicks a button that "enters" the value, 
    // such as Add or Equals, newValue is set to true so the user can enter 
    // another value.
    // decimalEntered is used to restrict the entry to a single decimal point.
    // It is set to true whenever newValue is set to true, and it is set to 
    // false whenever the user clicks the decimal point button.
    private bool newValue;
    private bool decimalEntered;

    private Calculator calc = new Calculator();

    private void Form1_Load(object sender, System.EventArgs e)
    {
        displayValue = 0;
        displayString = displayValue.ToString();
        newValue = true;
        decimalEntered = false;
    }

    // This method handles the 0 through 9 keys, appending the digit clicked
    // to the displayString field. 
    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();
    }

    // This method removes the last character from the displayString field.
    private void btnBackSpace_Click(object sender, System.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;
    }

    // This method appends a decimal point to the displayString field if the
    // user has not already entered a decimal point.
    private void btnDecimal_Click(object sender, System.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, System.EventArgs e)
    {
        displayValue = -displayValue;
        txtDisplay.Text = displayValue.ToString();
    }

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

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

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

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

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

    private void btnReciprocal_Click(object sender, System.EventArgs e)
    {
        try
        {
            calc.Reciprocal(displayValue);
            displayValue = calc.CurrentValue;
            txtDisplay.Text = displayValue.ToString();
        }
        catch (DivideByZeroException)
        {
            displayValue = 0;
            txtDisplay.Text = "Cannot 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 = "Cannot divide by zero.";
            newValue = true;
            decimalEntered = false;
        }
    }

}

}

这是我的课(我写的部分):

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

namespace Calculator
{
public class Calculator
{

    public Decimal displayValue;
    public Decimal currentValue;

     public void Add(Decimal displayValue)
    {

        currentValue += displayValue;

    }

     public void Subtract(Decimal displayValue)
     {
         currentValue -= displayValue;
     }

     public void Multiply(Decimal displayValue)
     {
         currentValue *= displayValue;
     }


     public void Divide(Decimal displayValue)
     {
         currentValue /= displayValue;
     }

     public void SquareRoot(Decimal displayValue)
     {
         currentValue = (decimal)Math.Sqrt(Convert.ToDouble(displayValue));
     }

     public void Reciprocal(Decimal displayValue)
     {
         currentValue = 1 / displayValue;
     }

     public decimal Equals()
     {
         return currentValue;
     }


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

     public decimal CurrentValue
     {
       get
          {
           return currentValue;
          }

     }
}

}

在主代码中,您像这样调用了Equal()方法:

if (newValue)
    calc.Equals();
else
    calc.Equals(displayValue); //Your class do not have this.

所以,你应该先做

//I am not sure why you need to pass in the displayValue parameter, so I presume it would not return anything.
public void Equal(Decimal displayValue)
{
        //Do the things you suppose to do
}

对于9 + 9 = 9的问题,这仅仅是因为在您的代码中,您只按下了一次单击事件Add_Button。 在您的Add()方法上创建一个断点。 然后尝试这样做:

9->按下您的Add_Button-> 9->按下您的Add_Button->检查您的currentValue

displayValue既是类字段又是方法参数。 那是你的意图吗? 分配给field参数时,需要使this.displayValue = ...以使其清楚您在做什么。 当前,您正在覆盖参数的本地副本,并且字段值始终为0。

只需删除decimal displayValue; 声明(以及从Clear()函数),然后将displayValue存储在表单之外的类之外。

public class Calculator
{

    //public Decimal displayValue;
    public Decimal currentValue;

    public void Add(Decimal displayValue)
    {

        currentValue+=displayValue;

    }
    ...
    public void Clear()
    {
        currentValue=0;
        //displayValue=0;
    }

    public decimal CurrentValue
    {
        get { return currentValue; }

    }
}

class Program
{
    static void Main(string[] args)
    {
        Calculator calc=new Calculator();
        calc.Add(1000m);
        calc.Divide(25m);
        calc.Subtract(8m);

        Console.WriteLine(calc.CurrentValue);
        // (1000/25)-8 = 32
    }
}

我在代码中看到的主要问题是您只添加了一个操作数,例如8 + 8总是等于8,您必须执行以下操作(仅包括Add和倒数函数):

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

namespace Calculator
{
public class Calculator
{

    public Decimal displayValue;
    public Decimal currentValue;

    private Decimal Operand_1;
    private Decimal Operand_2;

    private bool Operand_1_Added = false;
    private bool Operand_2_Added = false;

    private string Operation = "";

   private void AddOperand(Decimal Operand)
    {
            if(Operand_1_Added) 
                {
                  Operand_2 = Operand;
                  Operand_2_Added = true;
                }
            else {
                  Operand_1 = Operand;
                  Operand_1_Added = true;
                  currentValue = Operand_1;
                 }
     }



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

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


     public void Clear()
     {
         currentValue = 0;
         displayValue = 0;
         Operand_1 = 0;
         Operand_2 = 0;
     }

     public void Equals()
      {
         switch(Operation)
             {
                 case "Addition": 
                          currentValue = Operand_1 + Operand_2;
                          break;
                 case "Reciprocal":
                          currentValue = 1/Operand_1;
                          break;
                 default: break; 
              }
      }

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

     }
}

尚未测试代码,但这应该与form类一起使用。

让我们看一下需求:

“乘法运算的结果应显示的小数位数不应少于第一个数字的结果,而不是第二个数字。示例55.5 * 89.68 = 4977.240”

基本算术中乘法的自然含义是默认情况下都会发生这种情况。 例如。 您永远不会将XX与X.XX相乘,而得到结果X.XXXXXXX ...-这简直是不可能的。

因此,格式化很容易-如果您需要显式格式化,则将其格式化为num.ToString(“#。##############”),这样可以保留一堆数字。

您尚未概述“划分”的任何要求,因此我无法发表评论。

顺便说一句-如果您说x = 1/3和y = z * x,那么您将在小数点后得到很多数字,因为x开头是.333333333...。 这在AFAICT的要求之内。

暂无
暂无

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

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