我正在构建一个简单的C#计算器,我在使用equals按钮时遇到了一些麻烦。第一次单击以计算指定的方程式工作正常,但我希望每次用户单击等于按钮时,使用第二个数字(在本例中为y)重复该操作。目前,操作的总次数不是原始的第二个数字,而是重复操作。任何帮助将不胜感激! 这是班级:
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 :(得分:1)
从存储输出的同一文本框中取第二个数字
这里你拿第二个:
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;
当然如果这是你第一次添加数字,你需要知道它(以某种布尔值存储)
答案 1 :(得分:0)
感谢您的帮助,下面的代码是我最终得到的,效果很好。
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";
}
}
}