简要的计算器

//以上为运行结果

//以下为借助Button按键完成的

static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}

 

public partial class Form1 : Form

{
public Form1()
{
InitializeComponent();
}
private double One = 0, Two = 0;//定义变量用来存储操作数
private void button1_Click(object sender, EventArgs e)//0
{
this.textBox1.Text += button1.Text;
this.textBox2.Text += button1.Text;
}
private void button2_Click(object sender, EventArgs e)// .点
{
this.textBox1.Text += button2.Text;
this.textBox2.Text += button2.Text;
}
private void button3_Click(object sender, EventArgs e)// 1
{
this.textBox1.Text += button3.Text;
this.textBox2.Text += button3.Text;
}
private void button4_Click(object sender, EventArgs e)// 2
{
this.textBox1.Text += button4.Text;
this.textBox2.Text += button4.Text;
}
private void button5_Click(object sender, EventArgs e)// 3
{
this.textBox1.Text += button5.Text;
this.textBox2.Text += button5.Text;
}
private void button6_Click(object sender, EventArgs e)// 4
{
this.textBox1.Text += button6.Text;
this.textBox2.Text += button6.Text;
}
private void button7_Click(object sender, EventArgs e)// 5
{
this.textBox1.Text += button7.Text;
this.textBox2.Text += button7.Text;
}
private void button8_Click(object sender, EventArgs e)//6
{
this.textBox1.Text += button8.Text;
this.textBox2.Text += button8.Text;
}
private void button9_Click(object sender, EventArgs e)//7
{
this.textBox1.Text += button9.Text;
this.textBox2.Text += button9.Text;
}
private void button10_Click(object sender, EventArgs e)//8
{
this.textBox1.Text += button10.Text;
this.textBox2.Text += button10.Text;
}
private void button11_Click(object sender, EventArgs e)//9
{
this.textBox1.Text += button11.Text;
this.textBox2.Text += button11.Text;
}
private string s = string.Empty;//用来存放运算符
private bool b = true;
private void button12_Click(object sender, EventArgs e)// +加
{
if (b)
{
this.textBox1.Text += button12.Text;
One = double.Parse(textBox2.Text);//存储操作数一
s = this.button12.Text;
this.textBox2.Text = "";
b = false;
}
}

private void button13_Click(object sender, EventArgs e)// -减
{if (b)
{
this.textBox1.Text += button13.Text;
One = double.Parse(textBox2.Text);
s = this.button13.Text;
this.textBox2.Text = "";
b = false;
}
}

private void button14_Click(object sender, EventArgs e)// *乘
{if (b)
{
this.textBox1.Text += button14.Text;
One = double.Parse(textBox2.Text);
s = this.button14.Text;
this.textBox2.Text = "";
b = false;
}
}

private void button15_Click(object sender, EventArgs e)// /除
{if (b)
{
this.textBox1.Text += button15.Text;
One = double.Parse(textBox2.Text);
s = this.button15.Text;
this.textBox2.Text = "";
b = false;
}
}

private void button16_Click(object sender, EventArgs e)// C归零
{
this.textBox1.Text = "";
this.textBox2.Text = "";
}

private void button17_Click(object sender, EventArgs e)// =等号
{
if (b != true)
{
this.textBox1.Text += this.button17.Text;
Two = double.Parse(this.textBox2.Text);//存储操作数一
switch (s)//判断运算符
{
case "+":
double sum = One + Two;
this.textBox2.Text = sum.ToString();
this.textBox1.Text += sum.ToString();
b = true;
break;
case "-":
sum = One - Two;
this.textBox2.Text = sum.ToString();
this.textBox1.Text += sum.ToString();
b = true;
break;
case "*":
sum = One * Two;
this.textBox2.Text = sum.ToString();
this.textBox1.Text += sum.ToString();
b = true;
break;
case "/":
sum = One / Two;
this.textBox2.Text = sum.ToString();
this.textBox1.Text += sum.ToString();
b = true;
break;
}
}

}
}

 

posted @ 2015-05-22 00:39  ronger918  阅读(116)  评论(0编辑  收藏  举报