3

私は宿題に取り組んでおり、この割り当てでスターター コードが与えられました。私たちは以前に行ったクラスに取り組んでおり、圧倒されているようです。スターター コードは私を混乱させます。個人的には、自分で割り当てを開始したほうがよかったでしょう。それについていくつか質問があります。誰かが私が解釈するのを手伝ってくれないかと思っていました。

私が持っている最大の質問は非常に簡単です。電卓を持っている場合は、最初の数字があり、次に 2 番目の数字があります。したがって、ユーザーが 1 + 1 を入力すると、基本的に数字が表示されます。与えられたコードには、firstNumber と secondNumber のようなものはありません。代わりに、これを正しく理解している場合、displayValue と currentValue (?) があります。これらの値がどのように保存されているのか完全にはわかりません。

それは本当に私が見つけようとしているものであり、入力された実際の数値がプログラムのように保存されていることに夢中になっているため、クラスを適切にコーディングしているかどうか。

提供されたコードは次のとおりです。

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

}
}

次に、クラスのサンプル (スペースと時間を節約するために、オペランドの 1 つだけを実行します)

 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;

    }

そして、 currentValue がなく、それが参照 displayValue であると想定しているクラスから来ているので、私は想定していますか? それがユーザーが入力したものに戻るのか、それとも基本的に繰り返されるのかはわかりませんが?(それが理にかなっている場合...しかし、この部分で私がどのように混乱しているかがわかります)。

本当にそうです、私はそれを解釈してくれる人を探していて、それを正しく理解し、正しい方向に進んでいるかどうかを教えてください...そうでなければ、私はめちゃくちゃだと思います....

前もって感謝します。

4

3 に答える 3

1

問題のコードは、関数電卓をエミュレートするための完全なプロセスの一部です。

あなたには がありdisplayValuecurrentValue他の変数の中でも があります。

実際の電卓のしくみを考えてみると、数値を入力すると、それがディスプレイに表示されます。2 番目の数字を入力すると、既存の数字が左に押し出され、新しい数字がシングルの位置に固定されます。したがって、1 を入力すると 1 になります。次に 0 を押すと、10 になります。これは既にクリック メソッドによって処理されているため、displayValue常に入力した数字が順番に入力されます。+ - * / を押すと、これらは足し算、引き算、掛け算、割り算の不足しているメソッドを実行するはずです。

電卓の「表示」部分が完成したので、それらの計算方法がどのように機能するかに集中できます。を取得してdisplayValueに対して操作を実行currentValueし、プロセスの現在の値を更新する必要があります。

于 2013-11-12T02:38:39.783 に答える
0

現在の値と表示値の理由は、実際には最初の数と 2 番目の数がないためです。実際の電卓では、通常、好きなだけ値を追加し続けることができます。したがって、キーを押すと、2 + 2 = + 3 = +3 + 3 +3 などのようになります。それを保存しようとすると無駄が生じ、最初の数だけを使用して 2 番目の数を制限します。したがって、演算子が押されたときに、コードに表示された値を取得し、それを現在の値として設定するように指示できます。次に、演算子の後に数字を押すと、その値が表示値になり、演算子に基づいて計算を実行します。

たとえば、

2

それからあなたは押します

+

. できることは、表示値 - 2 - を取り、それを currentValue として保持することです。次に、押すことを選択できます

3

、 その後

=

. 指定された数字 - 2 + 3 に対して + のアクションを実行し、答え - 5 を返します。

これの利点は、そこで止まる必要がないことです。その後、押すことができます

 + 5

その後

 =

値は 10 になります。したがって、大量のメモリを使用することなく、必要なだけこれを実行し続けることができます。

電卓の完全なコードが必要な場合は、プロジェクトのためにしばらく前に作成しなければならなかったので、リクエストしていただければ、この返信にコードを追加します。

于 2013-11-12T03:13:57.303 に答える