gpt4 book ai didi

C#计算器给定代码

转载 作者:行者123 更新时间:2023-11-30 14:58:00 25 4
gpt4 key购买 nike

我正在做家庭作业,在这项作业中我得到了入门代码。我们正在学习我以前做过的类(class),但似乎被这些类(class)淹没了。入门代码让我感到困惑,我个人宁愿自己开始作业。我对此有几个问题,我想知道是否有人可以帮助我解释它,以便我可以完成这项任务。

我最大的问题很简单。如果您有计算器,您会得到第一个数字,然后是第二个数字。所以如果用户输入 1 + 1 那么你基本上就有了你的数字。我们得到的代码没有 firstNumber 和 secondNumber。相反,如果我理解正确的话,它有 displayValue 和 currentValue(?)我不完全确定这些值是如何存储的,我相信它们已经存储了?

这真的是我想要找出的,然后我是否正确地编写了我的类(class)代码,因为我挂断了我输入的实际数字是如何存储在程序中的。

这是提供的代码。

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

}
}

然后是我类的一个样本(为了节省空间和时间,我只会做我的一个操作数)

 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 并且它来 self 假设它是引用 displayValue 的类?虽然我不确定这是否会回到用户刚刚输入的内容,或者它是否会基本上重复? (如果这是有道理的……但我的意思是你可以看出我对这部分有多困惑)。

所以真的是的,我只是在找人来解释它,如果我理解正确并朝着正确的方向前进,请告诉我......否则我想我搞砸了......

提前致谢。

最佳答案

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

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

这样做的原因是 btnAdd_Click 中的以下行:

displayValue = calc.CurrentValue;

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

关于C#计算器给定代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19919658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com