[英]C# language, Calculator
大家好,感谢您的帮助。
我用C#制作了这个计算器,却遇到了一个问题。 当我添加5 + 5 + 5之类的东西时,它给我正确的结果,但是当我想减去两个以上的数字并且还要除以或乘以两个以上的数字时,我不会得到正确的结果。
你知道我在做什么错吗
非常感谢你!
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 Calculator : Form
{
public Calculator()
{
InitializeComponent();
}
private void btnOne_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnOne.Text;
//txtDisplay.Text = btnOne.Text;
}
private void btnTwo_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
}
private void btnThree_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnThree.Text;
}
private void btnFour_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnFour.Text;
}
private void btnFive_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnFive.Text;
}
private void btnSix_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnSix.Text;
}
private void btnSeven_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnSeven.Text;
}
private void btnEight_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnEight.Text;
}
private void btnNine_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnNine.Text;
}
private void btnZero_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnZero.Text;
}
private void btnClear_Click(object sender, EventArgs e)
{
txtDisplay.Clear();
}
private void btnPoint_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + ",";
}
double total1 = 0;
double total2 = 0;
bool plusButtonClicked = false;
bool minusButtonClicked = false;
bool divideButtonClicked = false;
bool multiplyButtonClicked = false;
private void btnPlus_Click(object sender, EventArgs e)
{
plusButtonClicked = true;
minusButtonClicked = false;
divideButtonClicked = false;
multiplyButtonClicked = false;
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
}
private void btnMinus_Click(object sender, EventArgs e)
{
plusButtonClicked = false;
minusButtonClicked = true;
divideButtonClicked = false;
multiplyButtonClicked = false;
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
}
private void btnDivide_Click(object sender, EventArgs e)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked = false;
minusButtonClicked = false;
divideButtonClicked = true;
multiplyButtonClicked = false;
}
private void btnMultiply_Click(object sender, EventArgs e)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked = false;
minusButtonClicked = false;
divideButtonClicked = false;
multiplyButtonClicked = true;
}
private void btnEquals_Click(object sender, EventArgs e)
{
if (plusButtonClicked == true)
{
total2 = total1 + double.Parse(txtDisplay.Text);
}
else if (minusButtonClicked == true)
{
total2 = total1 - double.Parse(txtDisplay.Text);
}
else if (divideButtonClicked == true)
{
total2 = total1 / double.Parse(txtDisplay.Text);
}
else if (multiplyButtonClicked == true)
{
total2 = total1 * double.Parse(txtDisplay.Text);
}
txtDisplay.Text = total2.ToString();
total1 = 0;
}
}
}
此代码尚未经过全面测试。 您为什么不尝试以下操作:
using System;
using System.Windows.Forms;
namespace Calculator
{
public enum Operator
{
None,
Add,
Minus,
Divide,
Multiply
}
public partial class Calculator : Form
{
private double total = 0;
private double currentValue = 0;
private Operator currentOperator;
public Calculator()
{
InitializeComponent();
}
private void btnOne_Click(object sender, EventArgs e)
{
ShowInput(btnOne.Text);
}
private void btnTwo_Click(object sender, EventArgs e)
{
ShowInput(btnTwo.Text);
}
private void btnThree_Click(object sender, EventArgs e)
{
ShowInput(btnThree.Text);
}
private void btnFour_Click(object sender, EventArgs e)
{
ShowInput(btnFour.Text);
}
private void btnFive_Click(object sender, EventArgs e)
{
ShowInput(btnFive.Text);
}
private void btnSix_Click(object sender, EventArgs e)
{
ShowInput(btnSix.Text);
}
private void btnSeven_Click(object sender, EventArgs e)
{
ShowInput(btnSeven.Text);
}
private void btnEight_Click(object sender, EventArgs e)
{
ShowInput(btnEight.Text);
}
private void btnNine_Click(object sender, EventArgs e)
{
ShowInput(btnNine.Text);
}
private void btnZero_Click(object sender, EventArgs e)
{
ShowInput(btnZero.Text);
}
private void btnClear_Click(object sender, EventArgs e)
{
currentOperator = Operator.None;
txtDisplay.Clear();
total = 0;
}
private void btnPoint_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + '.';
}
private void btnPlus_Click(object sender, EventArgs e)
{
ApplyOperator(Operator.Add);
}
private void btnMinus_Click(object sender, EventArgs e)
{
ApplyOperator(Operator.Minus);
}
private void btnDivide_Click(object sender, EventArgs e)
{
ApplyOperator(Operator.Divide);
}
private void btnMultiply_Click(object sender, EventArgs e)
{
ApplyOperator(Operator.Multiply);
}
private void btnEquals_Click(object sender, EventArgs e)
{
Evaluate();
txtDisplay.Text = Convert.ToString(total);
}
private void Evaluate()
{
switch (currentOperator)
{
case Operator.Add:
total += currentValue;
break;
case Operator.Minus:
total -= currentValue;
break;
case Operator.Divide:
total /= currentValue;
break;
case Operator.Multiply:
total *= currentValue;
break;
case Operator.None:
break;
}
currentValue = 0;
currentOperator = Operator.None;
}
private void ApplyOperator(Operator op)
{
if (currentOperator != Operator.None)
{
Evaluate();
}
else
{
total = double.Parse(txtDisplay.Text);
}
txtDisplay.Clear();
currentOperator = op;
}
private void ShowInput(String n)
{
txtDisplay.Text = txtDisplay.Text + n;
currentValue = double.Parse(txtDisplay.Text);
}
}
}
我仍然建议您最终进行某种形式的运算符解析器。 在这里查看或亲自查看“分流场”算法。
计算代码中乘积,商和差的逻辑是total1 = total1 + double.Parse(txtDisplay.Text);
这就是为什么加法有效的原因,但没有别的。 因此,请更改逻辑,使其除以,乘或减去,而不是加法。
想一想。 minus_clicked代码正在执行的操作是将除最后一个操作数之外的所有操作数相加,然后equals_clicked代码对minus_clicked的结果和文本框的值(我认为是最后一个操作数)进行算术运算。 因此,由于您在minus_clicked中执行的操作是加法运算,因此x-y-z的含义实际上是:
(X + Y) - Z
我会考虑重构一点,但是如果您想按原样保留代码,则可能只需将minus_clicked代码更改为减法而不是加法即可。
另外,@ rhysw是正确的。 如果您不希望它具有全部功能,则还必须为其添加优先级逻辑。
在您的代码中:
private void btnMultiply_Click(object sender, EventArgs e)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked = false;
minusButtonClicked = false;
divideButtonClicked = false;
multiplyButtonClicked = true;
}
您没有应用正确的运算符,您有total1 = total1 + ...将运算符更改为*。
我看了一下代码,看起来就像您每次添加的每个按钮一样。 因此,任何时候单击按钮,您都将继续添加。 只需将opps更改为相应的按钮即可。 像这样:
private void btnMinus_Click(object sender, EventArgs e)
{
plusButtonClicked = false;
minusButtonClicked = true;
divideButtonClicked = false;
multiplyButtonClicked = false;
total1 = total1 - double.Parse(txtDisplay.Text);
txtDisplay.Clear();
}
private void btnDivide_Click(object sender, EventArgs e)
{
total1 = total1 / double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked = false;
minusButtonClicked = false;
divideButtonClicked = true;
multiplyButtonClicked = false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.