2012-05-04 58 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; 
}