需要帮助计算可重复的等号和加法运算符事件
本文关键字:事件 运算符 帮助 计算 | 更新日期: 2023-09-27 18:29:01
我正在使用MVVM模式构建一个UWP计算器应用程序,除了如何让equals按钮重复计算并使我的操作员按钮正确工作外,我几乎已经解决了所有问题。我可以做其中一个,但我不知道如何同时做这两个。下面的代码适用于可重复的等于按钮,但在添加多个值时,加号运算符将产生错误的答案。
如果我去掉第二个switch语句,并将变量改回使用一个Output变量,那么我可以让加号运算符工作,但equals按钮在重复时会失败。
我确信这只是一个添加嵌套if语句或在某个地方添加bool的问题,但我被难住了!请帮忙。我去掉了非必要的按钮和操作器。如果您对我的代码有任何其他有用的建议,我们也将不胜感激。非常感谢。
public class ComputareViewModel : ViewModelBase
{
#region Private Fields
private double resultNumber = 0;
private double xResultNumber;
private double yResultNumber;
private string _outputValue = "0";
bool isOperationPerformed = false;
bool EqualsRepeated = false;
public ComputareViewModel()
{
}
public string OutputValue
{
get
{
return _outputValue;
}
set
{
_outputValue = value;
OnPropertyChanged("OutputValue");
}
}
public void OnNumberBtnClick(object sender, RoutedEventArgs args)
{
if ((_outputValue) == "0" || (isOperationPerformed))
_outputValue = "";
isOperationPerformed = false;
Button numberBtnClick = (Button)sender;
if ((string)numberBtnClick.Content == ".")
{
if (!_outputValue.Contains("."))
{
OutputValue = OutputValue + numberBtnClick.Content;
}
}
else
{
OutputValue = OutputValue + numberBtnClick.Content;
}
}
public void OnOperatorBtnClick(object sender, RoutedEventArgs args)
{
Button operatorBtnClick = (Button)sender;
if(isOperationPerformed == false)
{
if (xResultNumber != 0)
{
OnEqualsBtnClick(this, new RoutedEventArgs());
operationPerformed = (string)operatorBtnClick.Content;
isOperationPerformed = true;
EqualsRepeated = false;
}
else
{
operationPerformed = (string)operatorBtnClick.Content;
xResultNumber = Double.Parse(OutputValue);
isOperationPerformed = true;
}
}
else
{
//Do nothing.
}
}
public void OnEqualsBtnClick(object sender, RoutedEventArgs args)
{
if (EqualsRepeated == false)
{
if (double.TryParse(OutputValue, out yResultNumber))
switch (operationPerformed)
{
case "+":
{
OutputValue = (xResultNumber + yResultNumber).ToString();
break;
}
}
isOperationPerformed = true;
EqualsRepeated = true;
}
else
{
// If equals has already been clicked
if (EqualsRepeated == true)
if (double.TryParse(OutputValue, out xResultNumber))
switch (operationPerformed)
{
case "+":
{
OutputValue = (xResultNumber + yResultNumber).ToString();
break;
}
}
}
isOperationPerformed = true;
}
}
您的代码中存在一些逻辑问题。首先,在调用OnEqualsBtnClick
之后,isOperationPerformed
为true,如果您想调用OnOperatorBtnClick
,它将始终转到else
部分,而不执行任何操作。其次,如果您重复单击euqal按钮,xResultNumber
不是0,如果您想调用OnOperatorBtnClick
,它会调用OnEqualsBtnClick
,但EqualsRepeated
现在是真的,所以您应该将EqualsRepeated
更改为false
。最后,xResultNumber
是上次计算的值,您应该在OnEqualsBtnClick
方法中将其设置为0
。
所以你可以这样更改你的代码:
public void OnOperatorBtnClick(object sender, RoutedEventArgs args)
{
Button operatorBtnClick = (Button)sender;
EqualsRepeated = false;
if (isOperationPerformed == false)
{
if (xResultNumber != 0)
{
OnEqualsBtnClick(this, new RoutedEventArgs());
operationPerformed = (string)operatorBtnClick.Content;
isOperationPerformed = true;
EqualsRepeated = false;
}
else
{
operationPerformed = (string)operatorBtnClick.Content;
xResultNumber = Double.Parse(OutputValue);
isOperationPerformed = true;
}
}
else
{
//Do nothing.
}
}
public void OnEqualsBtnClick(object sender, RoutedEventArgs args)
{
if (EqualsRepeated == false)
{
if (double.TryParse(OutputValue, out yResultNumber))
switch (operationPerformed)
{
case "+":
{
OutputValue = (xResultNumber + yResultNumber).ToString();
break;
}
}
isOperationPerformed = true;
EqualsRepeated = true;
}
else
{
// If equals has already been clicked
if (EqualsRepeated == true)
if (double.TryParse(OutputValue, out xResultNumber))
switch (operationPerformed)
{
case "+":
{
OutputValue = (xResultNumber + yResultNumber).ToString();
break;
}
}
}
isOperationPerformed = false;
xResultNumber = 0;
}