2016-05-16 3 views
0

私はプログラミングのノブです。この電卓を正しく動作させることができません。私はすでにすべてのボタンをコーディングしており、キーボードからの入力を許可しています(これは無関係なのでコード抜粋に入れませんでしたが)。タイトルから分かるように、等価ボタンは複数の合計で機能しません。私は何が問題を引き起こしているのか分かりません。任意の提案をいただければ幸いです。シンプルな計算機の問題(同等のボタンを2回以上動かすことはできません)

ここに私のコードです。

public partial class MainWindow : Window 
{ 
    string input = string.Empty;   //String storing user input 
    string operand1 = string.Empty;   //String storing first operand 
    string operand2 = string.Empty;   //String storing second operand 
    char operation;       //char for operarion 
    double result = 0.0;     //calculated result 
    bool operationCompleted = false; 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void btn_Zero_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "0"; 
     this.textBox.Text += input; 

     if(operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_One_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "1"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_Two_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "2"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_Three_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "3"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 


    private void btn_Four_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "4"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      input = string.Empty; 
      textBox.Text = string.Empty; 
      operand1 = string.Empty; 
      operand2 = string.Empty; 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_Five_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "5"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_Six_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "6"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_Seven_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "7"; 
     this.textBox.Text += input; 
     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 

    } 

    private void btn_Eight_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "8"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_Nine_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "9"; 
     this.textBox.Text += input; 

     if (operationCompleted) 
     { 
      btn_Clear_Click(sender, e); 
      operationCompleted = false; 
     } 
     btn_Equals.Focus(); 
    } 

    private void btn_Dot_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     input += "."; 
     this.textBox.Text += input; 
     btn_Equals.Focus(); 
    } 

    private void btn_Minus_Click(object sender, RoutedEventArgs e) 
    { 
     operand1 = input; 
     operation += '-'; 
     input = string.Empty; 
     textBox.Text = string.Empty; 
     btn_Equals.Focus(); 
    } 

    private void btn_Plus_Click(object sender, RoutedEventArgs e) 
    { 
     operand1 = input; 
     operation += '+'; 
     input = string.Empty; 
     textBox.Text = string.Empty; 
     btn_Equals.Focus(); 
    } 

    private void btn_Multiply_Click(object sender, RoutedEventArgs e) 
    { 
     operand1 = input; 
     operation += '*'; 
     input = string.Empty; 
     textBox.Text = string.Empty; 
     btn_Equals.Focus(); 
    } 

    private void btn_Divide_Click(object sender, RoutedEventArgs e) 
    { 
     operand1 = input; 
     operation += '/'; 
     input = string.Empty; 
     textBox.Text = string.Empty; 
     btn_Equals.Focus(); 

    } 


    // The equals works for the first sum but not for any after it 

    private void btn_Equals_Click(object sender, RoutedEventArgs e) 
    { 
     { 
      operand2 = input; 
      double num1, num2; 
      double.TryParse(operand1, out num1); 
      double.TryParse(operand2, out num2); 

      if (operation == '+') 
      { 
       result = num1 + num2; 
       textBox.Text = result.ToString(); 
      } 

      else if (operation == '-') 
      { 
       result = num1 - num2; 
       textBox.Text = result.ToString(); 
      } 
      else if (operation == '*') 
      { 
       result = num1 * num2; 
       textBox.Text = result.ToString(); 
      } 
      else if (operation == '/') 
      { 
       if (num1 != 0 || num2 != 0) 
       { 
        result = num1/num2; 
        textBox.Text = result.ToString(); 
       } 

       else 
       { 
        textBox.Text = "Cannot divide by zero"; 
       } 

      } 
      operationCompleted = true; 
      this.Focus(); 
     } 
    } 

    private void btn_Clear_Click(object sender, RoutedEventArgs e) 
    { 
     this.textBox.Text = ""; 
     this.input = string.Empty; 
     this.operand1 = string.Empty; 
     this.operand2 = string.Empty; 
     this.result = 0.0; 
     operationCompleted = false; 
    } 

} 
+0

私はそれがあなたの 'operation'変数に関係していると思います。あなたは 'char'と宣言しますが、演算子ボタンを押すたびにその状態を「破損」させます。 '+ ='の代わりに '='を使うか、計算の間にクリアするべきです。 –

答えて

2

操作をクリアする必要があります。 + = '/'の操作があります。したがって、誰かが操作ボタンの1つをクリックするたびに、その操作を文字列に追加しています。等しいボタンをクリックすると、不正操作のキャッチがなくなり何も起こりません。

提案:

終了時に操作をクリアします。 エラーを処理できるように、不良操作を考慮する場合は、デフォルトのelseを追加します。

+0

ありがとうございました:D –

関連する問題