2012-05-04 71 views
2

大家好,感谢您的帮助。C#语言,计算器

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




    } 
} 
+5

地方一个破发点,并通过代码逐行步骤,看看结果是在每个点期望,一旦你看到它出错的时候,你就会知道如何解决它 – RhysW

+5

提示:科学计算器使用括号来分割每个数字以显示优先级是有原因的,例如,你的数字是10,你想除以10,然后除以2,10/10 = 1,1/2 = 0.5但这不同于10除以(10/2),其中10/5是2而不是一半,BIDMAS对于理解 – RhysW

回答

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

是非常重要的。更不用说BIDMAS在确定每个动作具有什么优先级时非常重要 – RhysW

+0

@RhysW绝对。我想过说这个。实际上,我会加上它。但是,他一次只能应用一项操作。 – BeRecursive

+0

虽然虽然我是关于BIDMAS和优先级的重要性,我甚至没有注意到所有人都添加了它给出的数字XD很好的捕获 – RhysW

1

用于计算产品,商和您的代码的差异的逻辑是total1 = total1 + double.Parse(txtDisplay.Text);这就是为什么加法有效,但没有别的。所以改变逻辑,使其分开,相乘或相减,而不是相加。

1

想一想。除了最后一个操作数以外,所有的操作数都加在一起,然后,等号按键代码对minus_clicked的结果和文本框的值(我认为是最后一个操作数)进行算术运算。所以,既然你在做minus_clicked操作除此之外,你得到什么X - Y - Z是真的:

(X + Y) - Z 

我会考虑重构了一点点,但如果你想保持代码的方式,我可能只是改变minus_clicked代码来减去而不是添加。

此外,@rhysw是正确的。如果你不想完全实现这个功能,你还必须为它添加优先级逻辑。

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; 
     } 
你没有应用正确的操作

,你有共1 =共1 + ...更改操作员*。

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