帮助理解代码 [英] Help on code understanding
问题描述
namespace VisualCalculator
{
///
public partial class MainWindow : Window
{
bool isNewEntry = true;
double currentValue = 0;
enum Operation { Add, Subtract, Multiply, Divide, Equals, Start, LastOp };
Operation currentOperation = Operation.Start;
public MainWindow()
{
InitializeComponent();
txtOut.Text = currentValue.ToString();
}
private void BtnEntry_Click(object sender, RoutedEventArgs e)
{
//dummy value for trying to parse the entry string
int result;
//Get the value from the button label
Button btn = (Button)sender;
string value = btn.Content.ToString();
//special handling for decimal point
if (value.Equals(".")) {
if (isNewEntry)
{
return;
}
if (!txtOut.Text.Contains(".")) {
txtOut.Text += value;
isNewEntry = false;
}
return;
}
//try to parse entry as int;
//if successful, append to current entry
if (Int32.TryParse(value, out result))
{
if (isNewEntry || txtOut.Text.Equals("0"))
{
txtOut.Text = "";
}
txtOut.Text += value;
isNewEntry = false;
}
}
private void Calculate(Operation op)
{
double newValue = Double.Parse(txtOut.Text);
double result;
if (op != Operation.LastOp)
{
currentOperation = op;
}
switch (currentOperation)
{
case Operation.Add:
result = currentValue + newValue;
break;
case Operation.Subtract:
if (currentValue == 0)
{
result = newValue;
}
else
{
result = currentValue - newValue;
}
break;
case Operation.Multiply:
if (currentValue == 0)
{
result = newValue;
}
else
{
result = currentValue * newValue;
}
break;
case Operation.Divide:
if (newValue == 0)
{
txtOut.Text = currentValue.ToString();
return;
}
else if (currentValue == 0)
{
currentValue = newValue;
txtOut.Text = "0";
return;
}
else
{
result = currentValue / newValue;
}
break;
default:
return;
}
currentValue = result;
txtOut.Text = result.ToString();
isNewEntry = true;
}
// 4 event handlers for operations:
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
Calculate(Operation.Add);
}
private void BtnSubtract_Click(object sender, RoutedEventArgs e)
{
Calculate(Operation.Subtract);
}
private void BtnMultiply_Click(object sender, RoutedEventArgs e)
{
Calculate(Operation.Multiply);
}
private void BtnDivide_Click(object sender, RoutedEventArgs e)
{
Calculate(Operation.Divide);
}
//Clear the current results
private void BtnClear_Click(object sender, RoutedEventArgs e)
{
txtOut.Text = "0";
currentValue = 0;
isNewEntry = true;
}
//Handle the Equals button
private void BtnEquals_Click(object sender, RoutedEventArgs e)
{
Calculate(Operation.LastOp);
}
}
}
我尝试过:
亲爱的,
我是新手C#开发者,并且有点儿从Lynda.com教程中了解代码的问题。这是一个可视化计算器的应用程序。
我理解btnEntry事件处理程序,但后来我完全迷失了...
我的问题是:
1)脚本如何在选择操作符之前输入值之后输入值?
我注意到2个vars - currentValue和newValue但完全没有得到逻辑如何完成...
2)操作Calculate方法与Operation参数:
根本不明白...
3)多(嵌套)如果声明
究竟是什么return关键字吗?
一切顺利,
Pete
What I have tried:
Dear all,
I'm novice C# developer, and have a bit of problem understanding the code from Lynda.com tutorials. It's an application for a visual calculator.
I understand the btnEntry event handler but then I'm completely lost...
My questions are:
1) How does script diffrenciate from value typed "before" choosing an operator and "after"?
I noticed 2 vars - currentValue and newValue but completely don't get the logic how it's done...
2) The operation Calculate method with Operation argument:
Don't understand that one at all...
3) Multi (nested) "if statments"
What exactly does the return keyword do?
All the best,
Pete
推荐答案
1)脚本如何在选择运算符之前之后键入值并且之后?
我注意到2个变量 - currentValue和newValue但完全没有得到逻辑如何完成...
1) How does script diffrenciate from value typed "before" choosing an operator and "after"?
I noticed 2 vars - currentValue and newValue but completely don't get the logic how it's done...
所以你的应用程序是一个计算器。如果您了解类/对象的工作原理,您将看到currentValue初始化为0.然后,一旦执行了数学运算,当前值就会成为该操作的结果。
例如:
当前值= 0;
我在txtOut文本框中输入的内容是1.
我的操作是。添加
那么1 + 0 = 1.因此根据您提供的代码,现在的值是1.
看起来你有一个名为clear的按钮,然后在你的代码中将currentValue的值重置为0.
So your app is a calculator. If you understand how classes/objects work you'll see that currentValue is initialized to 0. Then once the mathematical operation is performed, the current value then becomes the result of that operation.
Example:
Current value = 0;
What i type into txtOut textbox is 1.
My operation is .Add
So then 1 + 0 = 1. Therefore based on the code you provided, current value is now 1.
It looks as though you have a button called clear which then resets the value of currentValue to 0 in your code.
2)操作带有Operation参数的Calculate方法:
根本不明白那个.. 。
2) The operation Calculate method with Operation argument:
Don't understand that one at all...
操作计算方法使用枚举来决定它正在执行的数学运算。我假设你知道基本的计算器函数,所以你应该能够理解像Operation.Add这样的东西。
这个参数用在switch语句中有条件地执行要执行什么操作。
The operation calculate method uses an enum to decide what mathematical operation it is performing. I am assuming you know basic calculator functions so you should be able to understand what something like Operation.Add means.
This parameter is used in a switch statement to execute conditionally what operation to perform.
3)Multi(nested) if statments
return关键字到底有什么作用?
3) Multi (nested) "if statments"
What exactly does the return keyword do?
return关键字退出代码的执行。如果它只是返回;正在使用的是因为你的方法的返回类型为void,这意味着什么都不返回。如果它类似于返回1;,那么你的方法可能有一个int的返回类型,你返回1是一个整数
The return keyword exits execution of the code. If it is simply return; that is being used, it is because your method has a return type of void which means nothing is returned. If it was something like return 1;, then your method likely has a return type of int and you are returning 1 which is an integer
这篇关于帮助理解代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!