大家好,谢谢你的帮助。
我用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;
}
}
}
答案 0 :(得分:2)
此代码尚未经过全面测试。为什么不尝试以下内容:
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);
}
}
}
我仍然建议您最终制作某种形式的运算符解析器。看看here或亲自查看“Shunting Yard”算法。
答案 1 :(得分:1)
计算代码中的乘积,商和差异的逻辑是total1 = total1 + double.Parse(txtDisplay.Text);
,这就是添加有效的原因,但没有别的。因此,改变逻辑,使其分割,乘法或减法,而不是添加。
答案 2 :(得分:1)
想一想。 minus_clicked代码正在做的是添加所有操作数,除了最后一个操作数,然后equals_clicked代码对minus_clicked的结果和文本框的值进行算术运算(我假设是最后一个操作数)。所以,既然你在minus_clicked中所做的操作是另外的,你得到的x - y - z真的是:
(X + Y) - Z
我会考虑重构一下,但是如果你想保持代码的原样,我可能只是将minus_clicked代码更改为减去而不是添加。
另外,@ rhysw是对的。如果你不想完全发挥作用,那么你也必须为它添加优先级逻辑。
答案 3 :(得分:1)
:
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 + ...将运算符更改为*。
答案 4 :(得分:1)
我查看了代码,看起来每个按钮都只是每次添加。所以任何时候点击按钮你都会继续添加。只需将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;
}