C#计算器类 [英] C# Calculator Class

查看:268
本文介绍了C#计算器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这个家庭作业,我一直有一个困难时期。形式是书面,我们不得不写的类。目前,当我运行程序我的等号按钮不会出现奏效。我不确定为什么,我想知道,如果有人可以帮助我了解我错过了什么。我相信我已经正确地写入我的课。在我的头是怎么回事,是计算器调用CurrentValue的,所以我不断地与我使用我的运营商的方法更新。

I was given this homework assignment, which I have been having a hard time with. The form was written and we had to write the class. Currently when I run the program my equals button does not appear to be working. I'm unsure why and I was wondering if someone could help me understand what I am missing. I believe I have written my class correctly. In my head what is going on is the calculator is calling the "currentValue" so I am constantly updating with the method I am using in my operators.

我是在正确的方向?

为什么不是我等于按钮调用当前值。上次我跑了这一点,如果我输入9 + 3 +然后我会展示12个填充,待我输入一个号码。因此,在理论上为什么不是我的等号按钮加载答案吗?我相信我打电话正确的项目,但我一直得到我最初的条目。举例来说,如果我输入9 + 9 =打我不断收到9.

And why isn't my equals button calling the current value. The last time I ran this, if I typed 9 + 3 + then my display would populate with 12 and wait for me to enter the next number. So in theory why wouldn't my equals button load the answer? I believe I am calling the correct item yet I keep getting my initial entry. For instance if I typed 9 + 9 and hit = I keep getting 9.

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

Here is the calculator code (the part provided):

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;
        }
    }

}



}

}

下面是我的课(我写的一部分):

Here is my class (The part I wrote):

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;
          }

     }
}



}

}

推荐答案

在主代码,你有人称这样的平等()方法:

In the main code, you have called the Equal() method like this:

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



所以,你应该这样做首先

So, you should do this first

//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一次。做一个破发点你的添加()方法。然后试着这样做:

And for your 9 + 9 = 9 problem, it is simply because in your code, you only press your click event Add_Button for once. Make a break point at your Add() method. Then try to do like this:

9 - >按你的ADD_BUTTON - > 9 - >按你的ADD_BUTTON - >检查您的CurrentValue的

9 --> press your Add_Button --> 9 --> press your Add_Button --> check your currentValue

这篇关于C#计算器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆