2015-10-14 4 views
0

Я строю простой калькулятор C#, и у меня возникают проблемы с кнопкой равенства. Первый щелчок для вычисления указанного уравнения работает нормально, но я хочу, чтобы операция повторялась со вторым номером (y в этом случае) каждый раз, когда пользователь продолжает нажимать кнопку равенства. В настоящее время операция повторяется с общей операцией, а не с исходным вторым номером. Любая помощь будет принята с благодарностью! Вот класс:Калькулятор C#, операция повторения кнопок Equals не работает должным образом

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 

    namespace BMW_CALC_UI 
    { 
    public class Calculator 
    { 
     public double Add(double x, double y) 
     { 
      // Addition 
      return x + y; 
     } 

     public double Subtract(double x, double y) 
     { 
      // Subtraction 
      return x - y; 
     } 

     public double Multiply(double x, double y) 
     { 
      // Multiplication 
      return x * y; 
     } 

     public double Divide(double x, double y) 
     { 
      // Division 
      return x/y; 
     } 

     public double SquareRoot(double x) 
     { 
      // Square Root (must be called as a double) 
      return Math.Sqrt(x); 
     } 

     public double Reciprocal(double x) 
     { 
      // Reciprocal 
      return 1/x; 
     } 

     public double ChangeSign(double x) 
     { 
      // Change sign 
      return -x; 
     } 
    } 
    } 

А вот форма:

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 BMW_CALC_UI 
{ 
public partial class frmCalculator : Form 
{ 
    public frmCalculator() 
    { 
     InitializeComponent(); 
    } 

    // Set variables 
    double x; 
    double y; 
    char operation; 


    private void btnEquals_Click(object sender, EventArgs e) 
    { 
     if (txtDisplay.Text != "") 
     { 


      // Instantiate the instance 
      Calculator myCalculator = new Calculator(); 

      // Store second number 
      if (double.TryParse(txtDisplay.Text, out y)) 

      switch (operation) 
      { 
       case '+': 
        // Add addition method 
        txtDisplay.Text = (myCalculator.Add(x, y)).ToString(); 
        break; 

       case '-': 
        // Add subtraction method 
        txtDisplay.Text = (myCalculator.Subtract(x, y)).ToString(); 
        break; 

       case '*': 
        // Add multiplication method 
        txtDisplay.Text = (myCalculator.Multiply(x, y)).ToString(); 
        break; 

       case '/': 
        if (y == 0) 
        { 
         // Display error message 
         txtDisplay.Text = "Cannot divide by zero"; 
         return; 
        } 
        // Add division method 
        txtDisplay.Text = (myCalculator.Divide(x, y)).ToString(); 
        break; 
      } 

      // Reset 
      x = y; 
     }       

    } 

    private void btnClear_Click(object sender, EventArgs e) 
    { 
     // Clear all data 
     txtDisplay.Clear(); 
     x = 0; 
     y = 0; 
    } 

    private void btnBack_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 

      // Clear last number entered 
      if (txtDisplay.Text != "" && txtDisplay.TextLength > 0) 
      { 
       txtDisplay.Text = txtDisplay.Text.Remove(txtDisplay.TextLength - 1); 
      } 
    } 

    private void btnAdd_Click(object sender, EventArgs e) 
    { 
     // Store first number, operation, then clear textbox 
     if (double.TryParse(txtDisplay.Text, out x)) 
     { 
      operation = '+'; 
      txtDisplay.Clear(); 
     } 
    } 

    private void btnSubtract_Click(object sender, EventArgs e) 
    { 
     // Store first number, operation, then clear textbox 
     if (double.TryParse(txtDisplay.Text, out x)) 
     { 
      operation = '-'; 
      txtDisplay.Clear(); 
     } 
    } 

    private void btnMultiply_Click(object sender, EventArgs e) 
    { 
     // Store first number, operation, then clear textbox 
     if (double.TryParse(txtDisplay.Text, out x)) 
     { 
      operation = '*'; 
      txtDisplay.Clear(); 
     } 
    } 

    private void btnDivide_Click(object sender, EventArgs e) 
    { 
     // Store first number, operation, then clear textbox 
     if (double.TryParse(txtDisplay.Text, out x)) 
     { 
     operation = '/'; 
     txtDisplay.Clear(); 
     } 
    } 

    private void btnSquareRoot_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 

     // Instantiate the instance 
     Calculator myCalculator = new Calculator(); 

     // Perform square root operation 
     if (double.TryParse(txtDisplay.Text, out x)) 
     { 
      txtDisplay.Text = (myCalculator.SquareRoot(x)).ToString(); 
     } 
    } 

    private void btnReciprocal_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 

     // Instantiate the instance 
     Calculator myCalculator = new Calculator(); 

     // Perform reciprocal operation 
     if (double.TryParse(txtDisplay.Text, out x)) 
     { 
      txtDisplay.Text = (myCalculator.Reciprocal(x)).ToString(); 
     } 
    } 

    private void btnSign_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 

     // Instantiate the instance 
     Calculator myCalculator = new Calculator(); 

     // Perform sign operation 
     if (double.TryParse(txtDisplay.Text, out x)) 
     { 
      txtDisplay.Text = (myCalculator.ChangeSign(x)).ToString(); 
     } 
    } 

    private void btnDecimalPoint_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 

     // Add decimal point if none exist 
     if (txtDisplay.Text.Contains(".")) 
     { 
      txtDisplay.Text = txtDisplay.Text; 
     } 
     else 
     { 
      txtDisplay.Text = txtDisplay.Text + "."; 
     } 
    } 

    private void btnZero_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 

     // If textbox starts doesn't start with 0 or Textbox 
     // contains a decimal point then it is ok to add a zero 
     else if (!txtDisplay.Text.StartsWith("0") || txtDisplay.Text.Contains(".")) 
     { 
      // Add 0 to display 
      txtDisplay.Text += "0"; 
     } 
    } 

    private void btnOne_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 1 to display 
     txtDisplay.Text += "1"; 
    } 

    private void btn4_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 4 to display 
     txtDisplay.Text += "4"; 
    } 

    private void btnSeven_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 7 to display 
     txtDisplay.Text += "7"; 
    } 

    private void btnTwo_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 2 to display 
     txtDisplay.Text += "2"; 
    } 

    private void btnFive_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 5 to display 
     txtDisplay.Text += "5"; 
    } 

    private void btnEight_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 8 to display 
     txtDisplay.Text += "8"; 
    } 

    private void btnThree_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 3 to display 
     txtDisplay.Text += "3"; 
    } 

    private void btnSix_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 6 to display 
     txtDisplay.Text += "6"; 
    } 

    private void btn9_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 9 to display 
     txtDisplay.Text += "9"; 
    } 
} 
} 
+0

Вам необходимо добавить код, который вызывает эти функции. Я думаю, проблема в том, что есть – OopsUser

+0

Форм код отправлен, не сделал это в первый раз, спасибо! – MrSmash

+0

А, это имеет смысл. Как я могу сохранить второй номер по-другому? Спасибо – MrSmash

ответ

0

вы берете второй номер из того же текстового поля хранятся в выходной

Здесь вы берете второй: if (double.TryParse(txtDisplay.Text, out y))

Здесь вы сохраняете вывод: txtDisplay.Text = (myCalculator.Add(x, y)).ToString();

Просто сохраните результат в другом месте, например, вы можете определить «результат», где вы определили x, y. магазин есть результат.

И писать в «равно» функция:

case '+': 
// Add addition method 
txtDisplay.Text = (myCalculator.Add(result, y)).ToString(); 
break; 

Конечно, если это первый раз, когда вы добавить номера, вы должны знать его (магазин в некоторой булевой)

+0

Будет ли это требовать отдельного оператора switch? благодаря – MrSmash

0

Спасибо за help, код ниже - это то, с чем я закончил, что хорошо работает.

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 BMW_CALC_UI 
{ 
public partial class frmCalculator : Form 
{ 
    public frmCalculator() 
    { 
     InitializeComponent(); 
    } 

    // Set variables 
    double x; 
    double y; 
    char operation; 
    bool EqualsRepeated = false; 

    private void btnEquals_Click(object sender, EventArgs e) 
    { 

     if (txtDisplay.Text != "") 
     { 

      // Instantiate the instance 
      Calculator myCalculator = new Calculator(); 

      // Store second number 
      if (EqualsRepeated == false) 
      { 
       if (double.TryParse(txtDisplay.Text, out y)) 

        switch (operation) 
        { 
         case '+': 
          // Add addition method 
          txtDisplay.Text = (myCalculator.Add(x, y)).ToString(); 
          break; 

         case '-': 
          // Add subtraction method 
          txtDisplay.Text = (myCalculator.Subtract(x, y)).ToString(); 
          break; 

         case '*': 
          // Add multiplication method 
          txtDisplay.Text = (myCalculator.Multiply(x, y)).ToString(); 
          break; 

         case '/': 
          if (y == 0) 
          { 
           // Display error message 
           txtDisplay.Text = "Cannot divide by zero"; 
           return; 
          } 
          // Add division method 
          txtDisplay.Text = (myCalculator.Divide(x, y)).ToString(); 
          break;     
        } 
       // Set button clicked to true 
       EqualsRepeated = true;    
      } 

      else 
      { 
       // If equals has already been clicked 
       if (EqualsRepeated == true) 

        if (double.TryParse(txtDisplay.Text, out x)) 

        switch (operation) 
        { 
         case '+': 
          // Add addition method 
          txtDisplay.Text = (myCalculator.Add(x, y)).ToString(); 
          break; 

         case '-': 
          // Add subtraction method 
          txtDisplay.Text = (myCalculator.Subtract(x, y)).ToString(); 
          break; 

         case '*': 
          // Add multiplication method 
          txtDisplay.Text = (myCalculator.Multiply(x, y)).ToString(); 
          break; 

         case '/': 
          if (y == 0) 
          { 
           // Display error message 
           txtDisplay.Text = "Cannot divide by zero"; 
           return; 
          } 
          // Add division method 
          txtDisplay.Text = (myCalculator.Divide(x, y)).ToString(); 
          break; 
        } 
      } 

     }       

    } 

    private void btnClear_Click(object sender, EventArgs e) 
    { 
     // Clear all data 
     txtDisplay.Clear(); 
     EqualsRepeated = false; 
     x = 0; 
     y = 0; 
    } 

    private void btnBack_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 

      // Clear last number entered 
      if (txtDisplay.Text != "" && txtDisplay.TextLength > 0) 
      { 
       txtDisplay.Text = txtDisplay.Text.Remove(txtDisplay.TextLength - 1); 
      } 
    } 

    private void btnAdd_Click(object sender, EventArgs e) 
    { 
     // Store first number, operation, then clear textbox 
     if (double.TryParse(txtDisplay.Text, out x)) 
     { 
      operation = '+'; 
      txtDisplay.Clear(); 
     } 
    } 

    private void btnSubtract_Click(object sender, EventArgs e) 
    { 
     // Store first number, operation, then clear textbox 
     if (double.TryParse(txtDisplay.Text, out x)) 
     { 
      operation = '-'; 
      txtDisplay.Clear(); 
     } 
    } 

    private void btnMultiply_Click(object sender, EventArgs e) 
    { 
     // Store first number, operation, then clear textbox 
     if (double.TryParse(txtDisplay.Text, out x)) 
     { 
      operation = '*'; 
      txtDisplay.Clear(); 
     } 
    } 

    private void btnDivide_Click(object sender, EventArgs e) 
    { 
     // Store first number, operation, then clear textbox 
     if (double.TryParse(txtDisplay.Text, out x)) 
     { 
     operation = '/'; 
     txtDisplay.Clear(); 
     } 
    } 

    private void btnSquareRoot_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 

     // Instantiate the instance 
     Calculator myCalculator = new Calculator(); 

     // Perform square root operation 
     if (double.TryParse(txtDisplay.Text, out x)) 
     { 
      txtDisplay.Text = (myCalculator.SquareRoot(x)).ToString(); 
     } 
    } 

    private void btnReciprocal_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 

     // Instantiate the instance 
     Calculator myCalculator = new Calculator(); 

     // Perform reciprocal operation 
     if (double.TryParse(txtDisplay.Text, out x)) 
     { 
      txtDisplay.Text = (myCalculator.Reciprocal(x)).ToString(); 
     } 
    } 

    private void btnSign_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 

     // Instantiate the instance 
     Calculator myCalculator = new Calculator(); 

     // Perform sign operation 
     if (double.TryParse(txtDisplay.Text, out x)) 
     { 
      txtDisplay.Text = (myCalculator.ChangeSign(x)).ToString(); 
     } 
    } 

    private void btnDecimalPoint_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 

     // Add decimal point if none exist 
     if (txtDisplay.Text.Contains(".")) 
     { 
      txtDisplay.Text = txtDisplay.Text; 
     } 
     else 
     { 
      txtDisplay.Text = txtDisplay.Text + "."; 
     } 
    } 

    private void btnZero_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 

     // If textbox starts doesn't start with 0 or Textbox 
     // contains a decimal point then it is ok to add a zero 
     else if (!txtDisplay.Text.StartsWith("0") || txtDisplay.Text.Contains(".")) 
     { 
      // Add 0 to display 
      txtDisplay.Text += "0"; 
     } 
    } 

    private void btnOne_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 1 to display 
     txtDisplay.Text += "1"; 
    } 

    private void btn4_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 4 to display 
     txtDisplay.Text += "4"; 
    } 

    private void btnSeven_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 7 to display 
     txtDisplay.Text += "7"; 
    } 

    private void btnTwo_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 2 to display 
     txtDisplay.Text += "2"; 
    } 

    private void btnFive_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 5 to display 
     txtDisplay.Text += "5"; 
    } 

    private void btnEight_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 8 to display 
     txtDisplay.Text += "8"; 
    } 

    private void btnThree_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 3 to display 
     txtDisplay.Text += "3"; 
    } 

    private void btnSix_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 6 to display 
     txtDisplay.Text += "6"; 
    } 

    private void btn9_Click(object sender, EventArgs e) 
    { 
     // Clear display if error message is present 
     if (txtDisplay.Text.Contains("Cannot divide by zero")) 
     { 
      txtDisplay.Clear(); 
     } 
     // Add 9 to display 
     txtDisplay.Text += "9"; 
    } 
} 
} 
Смежные вопросы