使用c#.net實(shí)現(xiàn)簡(jiǎn)單計(jì)算器 本程序使用visual studio 2010實(shí)現(xiàn)
效果圖如下
需求分析:
實(shí)現(xiàn)計(jì)算器簡(jiǎn)單加減乘除 刪除 以及 clear功能
實(shí)現(xiàn)原理:實(shí)現(xiàn)button的click事件
獲取計(jì)算器1~9的button中的text
private void button1_Click(object sender, EventArgs e)
{
Button a=(Button)sender;
this.textBox1.Text += a.Text;
}
獲取和存儲(chǔ)運(yùn)算符
private void buttonjia_Click(object sender, EventArgs e)
{
num1 = int.Parse(this.textBox1.Text);
this.textBox1.Text = "";
Button b = (Button)sender;
f = b.Text;
}
實(shí)現(xiàn)加減乘除
private void buttonresult_Click(object sender, EventArgs e)
{
num2 = int.Parse(this.textBox1.Text);
if(f=="+")
{
this.textBox1.Text=(num1+num2).ToString();
}
if (f == "-")
{
this.textBox1.Text = (num1 - num2).ToString();
}
if (f == "*")
{
this.textBox1.Text = (num1 * num2).ToString();
}
if (f == "/")
{
this.textBox1.Text = (num1 / num2).ToString();
}
}
清空輸入框
this.textBox1.Clear();
刪除
textBox1.Text = textBox1.Text.Substring(0, textBox1.TextLength - 1);