3

I'm working on homework and in this assignment I was given starter code. We are working with classes which I have done before and seem to get overwhelmed by. The starter code is confusing me, I personally would have rather just started the assignment by myself. I have a couple of questions about it and I was wondering if someone could help me interpret it so I could get this assignment cranked out.

The biggest question that I have is pretty simple. If you have a calculator you have your first number, then your second number. So if the user enters 1 + 1 then you basically have your numbers. The code we were given doesn't have like a firstNumber and secondNumber. Instead if I am understanding this correctly it has displayValue and currentValue (?) I'm not entirely sure how these values are being stored, and I believe they already are?

That's really what I am trying to find out, and then whether or not I am coding my class properly since I'm hung up on what my actual numbers are that are entered are being stored as in the program.

Here is the code that was 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;
        }
    }

}
}

Then a sample from my class (to save space and time I will only do one of my operands)

 namespace Calculator
{
public class Calculator
{

    public Decimal displayValue;
    public Decimal currentValue;
    public Decimal firstNumber;
    public Decimal secondNumber;

     public decimal Add(Decimal displayValue)
    {

     return displayValue + currentValue;

    }

and then I'm assuming since there isn't a currentValue and that comes from the class I'm assuming it would be reference displayValue? Although I'm not sure if that goes back to what the user just entered, or if it will basically repeat itself? (if that makes sense... but I mean you can see how I am confused by this part).

So really yeah, I'm just looking for someone to interpret that and just let me know if I am understanding that correctly and going in the right direction... otherwise I guess I'm screwed....

Thanks in advance.

4

3 回答 3

4

您必须将结果存储在current Value变量中,因为稍后会在代码中使用它。此外,计算器方法的使用表明它们不应返回任何值。

public void Add(Decimal displayValue) 
{ 
    currentValue += display Value;
}

这样做的原因是以下行btnAdd_Click

displayValue = calc.CurrentValue;

它期望CurrentValue属性保持当前值。

于 2013-11-12T02:32:04.557 回答
1

有问题的代码是模拟科学计算器的完整过程的一部分。

在其他变量中, 您有 adisplayValue并且您有 a 。currentValue

如果您考虑一下真正的计算器的工作方式,当您输入一个数字时,它会显示在显示屏上。当您输入第二个号码时,它会将已经存在的号码推向左侧,并将新号码放在单打位置。因此,如果您输入 1,则您有 1。如果您接下来按 0,则现在您有 10。这已经由 click 方法处理,displayValue您按顺序键入的数字也始终如此。当您点击 + - * / 时,这些应该正在执行缺少的加法、减法、乘法、除法方法。

计算器的“显示”部分已完成,因此您可以专注于这些计算方法的工作原理。他们应该对displayValue执行操作currentValue,更新过程中的当前值。

于 2013-11-12T02:38:39.783 回答
0

当前值和显示值的原因是因为您实际上没有第一个数字和第二个数字。在真正的计算器中,您通常可以根据需要继续添加值 - 所以按键可以像 2 + 2 = + 3 = +3 + 3 +3 或其他东西。尝试存储将是浪费的,并且仅使用第一个数字第二个数字限制。因此,当按下操作符时,您可以告诉代码获取显示的值,并将其设置为当前值。然后,当您在运算符后面按一个数字时,该值即为显示值,并根据运算符执行计算。

因此,例如,您按

2

然后你按

+

. 您可以做的是获取显示值 - 2 - 并将其保持为 currentValue。然后你可以选择按

3

, 接着

=

. 它将对指定的数字 - 2 + 3 执行 + 的操作,并给出答案 - 5。

这样做的好处是您不必停在那里。然后你可以按

 + 5

接着

 =

你会得到一个 10 的值。所以你可以根据需要继续这样做,而不会占用大量内存。

如果你想要一个计算器的完整代码,那么我不得不为一个项目制作一个,所以你可以要求它,我会将代码添加到这个回复中。

于 2013-11-12T03:13:57.303 回答