C# 简易计算器的实现
第一次学习C#,练习编写了一个简单的基本运算的计算器。
双击文本框可提示本人作者。
基本四则运算运算,可开根号,实现正负符号按钮,计算错误时提示ERROR。
连续运算是以两个操作数的运算结果等于第一操作数,从而继续运算。
以加法为例:
Num1=1;
Num2=2;
Sum=Num1+Num2;
Num1=Sum;
界面如图所示:
代码部分示例:
private double Num1 = 0; //第一操作数
private double Num2; //第二操作数
private string Sign; //运算符号
private double Sum; //结果
private bool First = true; //第一操作数判断
private bool Continue = false; //连续操作判断
private void textBox1_TextChanged(object sender, EventArgs e) //文本框
{
if (textBox1.Text == "Error")
{
Num1 = 0;
Num2 = 0;
Sign = "";
}
}
private void button1_Click(object sender, EventArgs e) //数字
{
Button Num = (Button)sender;
if (textBox1.Text == "0")
{
textBox1.Text = Num.Text;
}
else if (textBox1.Text == "Error")
{
textBox1.Text = Num.Text;
}
else
textBox1.Text += Num.Text;
if (First == true)
{
Num1 = double.Parse(textBox1.Text);
}
else
{
Num2 = double.Parse(textBox1.Text);
}
}
private void button21_Click(object sender, EventArgs e) //退格
{
if (textBox1.Text != "0")
{
if (textBox1.Text.Length != 1)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}
else
{
textBox1.Text = "0";
}
}
}
private void button10_Click(object sender, EventArgs e) //小数点
{
Button Num = (Button)sender;
if (textBox1.Text.IndexOf(".") < 0)
{
if (First == true)
{
textBox1.Text += Num.Text;
Num1 = double.Parse(textBox1.Text);
}
else if (First == false)
{
textBox1.Text += Num.Text;
Num2 = double.Parse(textBox1.Text);
}
}
}
private void button16_Click(object sender, EventArgs e) //C
{
textBox1.Text = "0";
Num1 = 0;
Num2 = 0;
Sum = 0;
}
private void button19_Click(object sender, EventArgs e) //正负号
{
if (First == true)
{
Num1 = -Num1;
textBox1.Text = Convert.ToString(Num1);
}
else if (First == false)
{
Num2 = -Num2;
textBox1.Text = Convert.ToString(Num2);
}
}
private void button17_Click(object sender, EventArgs e) //CE
{
if (First == true)
{
textBox1.Text = "0";
Num1 = 0;
Sign = "";
}
else if (First == false)
{
textBox1.Text = "0";
Num2 = 0;
}
}
private void button22_Click(object sender, EventArgs e) //百分号
{
if (First == true)
{
Num1 = Num1 / 100;
textBox1.Text = Convert.ToString(Num1);
}
else if (First == false)
{
Num2 = Num2 / 100;
textBox1.Text = Convert.ToString(Num2);
}
}
private void button20_Click(object sender, EventArgs e) //根号
{
if (Num1 > 0 || Num2 > 0)
{
if (First == true)
{
Num1 = Math.Pow(Num1, 0.5);
textBox1.Text = Convert.ToString(Num1);
}
else if (First == false)
{
Num2 = Math.Pow(Num2, 0.5);
textBox1.Text = Convert.ToString(Num2);
}
}
else if (Num1 < 0 || Num2 < 0)
{
textBox1.Text = "Error";
}
}
private void button15_Click(object sender, EventArgs e) //+
{
if (Continue == false)
{
Sign = "+";
textBox1.Text = "0";
First = false;
Continue = true;
}
else if (Continue == true)
{
switch (Sign)
{
case "+":
Num1 = Num1 + Num2;
break;
case "-":
Num1 = Num1 - Num2;
break;
case "*":
Num1 = Num1 * Num2;
break;
case "/":
if (Num2 != 0)
{
Num1 = Num1 / Num2;
}
else
{
textBox1.Text = "Error";
}
break;
default:
break;
}
Sign = "+";
Sum = Num1;
textBox1.Text = "0";
}
}
private void button18_Click(object sender, EventArgs e) //等于
{
First = true;
Continue = false;
switch (Sign)
{
case "+":
Sum = Num1 + Num2;
textBox1.Text = Sum.ToString();
break;
case "-":
Sum = Num1 - Num2;
textBox1.Text = Sum.ToString();
break;
case "*":
Sum = Num1 * Num2;
textBox1.Text = Sum.ToString();
break;
case "/":
if (Num2 != 0)
{
Sum = Num1 / Num2;
textBox1.Text = Sum.ToString();
}
else
{
textBox1.Text = "Error";
}
break;
default:
break;
}
Num1 = Sum; //持续按等号连续运算
}
private void textBox1_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show("制作:Cieux","关于"); //双击文本框
}