winform制作简单计算器

时间:2023-08-09 16:00:56

winform制作简单计算器

 public Form1()
{
InitializeComponent();
textBox2.Text = "";//主显示屏
textBox1.Text = "";//顶端副显示屏
textBox3.Text = "";//中间变量,隐藏 } private void Form1_Load(object sender, EventArgs e)
{ }
//数字按键
private void button21_Click(object sender, EventArgs e)
{
if (sender is Button)
{
Button btn = sender as Button;
if (textBox2.Text == "")//如果主屏幕为0
{
textBox2.Text = btn.Text;//输入几就显示几
}
else
{
if (yunsuanfu)//上个按键是运算符
{
textBox2.Text = btn.Text;//主屏幕只显示本次输入的数字
yunsuanfu = false;
}
else
{
textBox2.Text += btn.Text;//上个按键不是运算符,主屏幕右侧累添上这个数字
}
}
}
} //加减乘除
private bool yunsuanfu = false;
private double jieguo1 = ;//点击加减乘除运算符显示的结果
private string text = "";
private void button26_Click(object sender, EventArgs e)
{
if (sender is Button)
{
Button btn = sender as Button;
text = btn.Text;
if(textBox3.Text=="")
{
textBox3.Text = btn.Text;
}
//判断上一步点击的是否是运算符
if (yunsuanfu==false)//不是运算符
{
textBox1.Text += textBox2.Text + text;
yunsuanfu = true;
if(textBox3.Text=="+")
{
jieguo1 += Convert.ToDouble(textBox2.Text);
textBox2.Text = jieguo1.ToString();
}
else if (textBox3.Text == "-")
{
jieguo1 -= Convert.ToDouble(textBox2.Text);
textBox2.Text = jieguo1.ToString();
}
else if (textBox3.Text == "*")
{
if (jieguo1 != )
{
jieguo1 = jieguo1 * Convert.ToDouble(textBox2.Text);
}
else
{
jieguo1 = Convert.ToDouble(textBox2.Text);
}
textBox2.Text = jieguo1.ToString();
}
else if (textBox3.Text == "/")
{
if (Convert.ToDouble(textBox2.Text) != )
{
if (jieguo1 != )
{
jieguo1 = jieguo1 / Convert.ToDouble(textBox2.Text);
}
else
{
jieguo1 = Convert.ToDouble(textBox2.Text); ;
}
textBox2.Text = jieguo1.ToString();
}
else
{
textBox2.Text = "除数不能为0";
}
} textBox3.Text = text;
}
else
{
if (textBox1.Text.Length > )
{
textBox1.Text = textBox1.Text.Substring(, textBox1.Text.Length - ) + text;
textBox3.Text = text;
}
else
{
textBox3.Text = text;
}
yunsuanfu = false;
}
}
} //小数点
private void button25_Click(object sender, EventArgs e)
{
//判断上一步点击的是否是运算符
if (yunsuanfu == false)//不是运算符
{
if (textBox2.Text.Contains(".") == false)
{
textBox2.Text = textBox2.Text + ".";
}
}
else
{
textBox2.Text = "0.";
yunsuanfu = false;
}
} //等号
private double jieguo = ;
private string text4 = "";
private string text5 = "";
private void button28_Click(object sender, EventArgs e)
{
jieguo = jieguo1;
jieguo1 = ;
text4 = textBox3.Text;
textBox3.Text = "";
text = "";
if (text4 == "")
{
text4 = button28.Text;
} textBox1.Clear();
textBox1.Text = "";
//判断上一步点击的是否是运算符
if (yunsuanfu == false)//不是运算符
{
yunsuanfu = true;
if (text4 == "+")
{
jieguo += Convert.ToDouble(textBox2.Text);
textBox2.Text = jieguo.ToString();
}
else if (text4 == "-")
{
jieguo -= Convert.ToDouble(textBox2.Text);
textBox2.Text = jieguo.ToString();
}
else if (text4 == "*")
{
if (jieguo != )
{
jieguo = jieguo * Convert.ToDouble(textBox2.Text);
}
else
{
jieguo = Convert.ToDouble(textBox2.Text);
}
textBox2.Text = jieguo.ToString();
}
else if (text4 == "/")
{
if (textBox2.Text != "")
{
if (jieguo != )
{
jieguo = jieguo / Convert.ToDouble(textBox2.Text);
}
else
{
jieguo = Convert.ToDouble(textBox2.Text); ;
}
textBox2.Text = jieguo.ToString();
}
else
{
textBox2.Text = "除数不能为0";
}
}
else if (text4 == "=")
{
jieguo = jieguo;
} text5 = text4;
text4 = button28.Text;
}
else
{
yunsuanfu = false;
double jishu = ;
if (text4 == "+")
{
jishu = jieguo;
jieguo += jishu; textBox2.Text = jieguo.ToString();
}
else if (text4 == "-")
{
jishu = jieguo;
jieguo -=jishu; textBox2.Text = jieguo.ToString();
}
else if (text4 == "*")
{
jishu = jieguo;
jieguo=jieguo*jishu; textBox2.Text = jieguo.ToString();
}
else if (text4 == "/")
{
if (jieguo != )
{
jishu = jieguo;
jieguo = jieguo / jishu; textBox2.Text = jieguo.ToString();
}
else
{
textBox2.Text = "除数不能为0";
}
}
else if (text4 == "=")
{
jishu =Convert.ToDouble(textBox1.Text);
if (text5 == "+")
{
jieguo+=jishu ;
}
else if (text5 == "-")
{
jieguo-=jishu;
}
else if (text5 == "*")
{
jieguo *=jishu;
}
else if (text5 == "/")
{
if (jishu != )
{
jieguo /= jishu;
}
else
{
textBox2.Text = "除数不能为0";
}
}
}
}
yunsuanfu = false; }
//按键C
private void button8_Click(object sender, EventArgs e)
{
textBox2.Clear();
textBox1.Clear();
textBox3.Clear();
textBox2.Text = "";
textBox1.Text = "";
textBox3.Text = "";
jieguo = ;
jieguo1 = ;
yunsuanfu = false;
text = "";
text4 = "";
text5 = "";
}
//CE键
private void button7_Click(object sender, EventArgs e)
{ textBox2.Clear();
textBox2.Text = "";
}
//退格键
private void button6_Click(object sender, EventArgs e)
{
if (textBox2.Text.Length > )
{
textBox2.Text = textBox2.Text.Substring(, textBox2.Text.Length - );
}
else if (textBox2.Text.Length == )
{
textBox2.Text = textBox2.Text.Substring(, textBox2.Text.Length - );
textBox2.Text = "";
}
else
{
textBox2.Text = "";
}
}
//主屏幕字体随个数变化而变化
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (textBox2.Text.Length < )
{
this.textBox2.Font = new System.Drawing.Font("宋体", 21.75f);
}
if (textBox2.Text.Length > )
{
this.textBox2.Font = new System.Drawing.Font("宋体", 15f);
}
if (textBox2.Text.Length > )
{
this.textBox2.Font = new System.Drawing.Font("宋体", 13f);
}
if (textBox2.Text.Length > )
{
this.textBox2.Font = new System.Drawing.Font("宋体", 11f);
}
}