2015-10-29 119 views
0

當我按9,然後按時間顯示器顯示9,當我再次按9時,它會將它添加到第一個數字(9),而不是清除第一個9.所以它認爲它的9 * 99 。操作後,如何清除第一個輸入上的顯示?C#計算器邏輯

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace WindowsFormsApplication2 
{ 

    public partial class Form1 : Form 
    { 
     string input = string.Empty; 
     string operand1 = string.Empty; 
     string operand2 = string.Empty; 
     char operation; 
     double result = 0.0; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Multiply_Click(object sender, EventArgs e) 
     { 
      operation = '*'; 
      if (operand1 != "") 
      { 
       calculate(); 
      } 
      else 
      { 
       operand1 = input; 
      } 
     } 

     private void Minus_Click(object sender, EventArgs e) 
     { 
      operation = '-'; 
      if (operand1 != "") 
      { 
       calculate(); 
      } 
      else 
      { 
       operand1 = input; 
      } 
     } 

     private void Addition_Click(object sender, EventArgs e) 
     { 
      operation = '+'; 
      if (operand1 != "") 
      { 
       calculate(); 
      } 
      else 
      { 
       operand1 = input; 
      } 
     } 

     private void Divide_Click(object sender, EventArgs e) 
     { 
      operation = '/'; 
      if (operand1 != "") 
      { 
       calculate(); 
      } 
      else 
      { 
       operand1 = input; 
      } 
     } 

     private void textBox1_TextChanged(object sender, EventArgs e) 
     { 

     } 

     private void five_Click(object sender, EventArgs e) 
     { 
      this.textBox1.Text = ""; 
      input += "5"; 
      this.textBox1.Text += input; 
     } 

     private void Zero_Click(object sender, EventArgs e) 
     { 
      this.textBox1.Text = ""; 
      input += "0"; 
      this.textBox1.Text += input; 
     } 

     private void one_Click(object sender, EventArgs e) 
     { 
      this.textBox1.Text = ""; 
      input += "1"; 
      this.textBox1.Text += input; 
     } 

     private void two_Click(object sender, EventArgs e) 
     { 
      this.textBox1.Text = ""; 
      input += "2"; 
      this.textBox1.Text += input; 
     } 

     private void three_Click(object sender, EventArgs e) 
     { 
      this.textBox1.Text = ""; 
      input += "3"; 
      this.textBox1.Text += input; 
     } 

     private void four_Click(object sender, EventArgs e) 
     { 
      this.textBox1.Text = ""; 
      input += "4"; 
      this.textBox1.Text += input; 
     } 

     private void six_Click(object sender, EventArgs e) 
     { 
      this.textBox1.Text = ""; 
      input += "6"; 
      this.textBox1.Text += input; 
     } 

     private void seven_Click(object sender, EventArgs e) 
     { 
      this.textBox1.Text = ""; 
      input += "7"; 
      this.textBox1.Text += input; 
     } 

     private void eight_Click(object sender, EventArgs e) 
     { 
      this.textBox1.Text = ""; 
      input += "8"; 
      this.textBox1.Text += input; 
     } 

     private void nine_Click(object sender, EventArgs e) 
     { 
      this.textBox1.Text = ""; 
      input += "9"; 
      this.textBox1.Text += input; 
     } 

     private void Equal_Click(object sender, EventArgs e) 
     { 
      calculate(); 
     } 

     private void Decimal_Click(object sender, EventArgs e) 
     { 
      this.textBox1.Text = ""; 
      input += "."; 
      this.textBox1.Text += input; 
     } 

     private void Clear_Click(object sender, EventArgs e) 
     { 
      textBox1.Clear(); 
     } 

     public void calculate() 
     { 
      Console.WriteLine("performing operation" + operand1 + " " + operation + " " + operand2 + "= ???" + "the input is?" + input); 
      operand2 = input; 
      double num1, num2; 
      double.TryParse(operand1, out num1); 
      double.TryParse(operand2, out num2); 

      if (operation == '*') 
      { 
       result = num1 * num2;  
      } 

      if (operation == '-') 
      { 
       result = num1 * num2; 
      } 

      if (operation == '+') 
      { 
       result = num1 * num2; 
      } 

      if (operation == '/') 
      { 
       result = num1 * num2; 
      } 
      textBox1.Text = result.ToString(); 
      input = result.ToString(); 
     } 
    } 
} 

回答

0

以下添加到您的Form1類:

bool operationExcecuted = false; 

而在你calculate方法中添加以下行:

operationExcecuted = true; 

而且在如果操作excecuted每一個數字輸入檢查。如果清楚了輸入並添加一個新號碼。例如,數字9:

private void nine_Click(object sender, EventArgs e) 
{ 
    if(operationExcecuted) 
    { 
     input = String.Empty; 
     operationExcecuted = false; 
    } 

    this.textBox1.Text = ""; 
    input += "9"; 
    this.textBox1.Text += input; 
} 
+0

乾杯隊友,我重新定義了邏輯,它按預期工作 –

0

我想,那是因爲你添加

input += "9" 
this.textBox1.text += input; 

你總是加上9變量輸入。嘗試清潔輸入。添加新號碼之前。

+0

如果我清除輸入,每個數字將清除顯示,我將只能輸入單個數字的數字。感謝您的回覆 –

+1

然後檢查要點擊的操作數。如果用戶點擊一個操作數,程序應該知道,現在是下一個數字。 –