计算器程序
第一步:创建一个Windows窗体应用程序项目CalcDemo。
第二步:设计如下界面,并编写相关代码
int i = 0;
double x = 0.0;
double y = 0.0;
int z = 0;
int j = 0; //标识是何种运算
private void IncreaseI()
{
if( i%2 == 0)
x=z;
else
y=z;
i++;
if( i>1)
i=0;
textBox1.Text = z.ToString();
}
private void button10_Click(object sender, EventArgs e)
{
//将按钮上的数字赋值给z
z = Convert.ToInt32(((Button)sender).Text);
//根据i的值来决定把z赋值给x还是y
IncreaseI();
}
private void button11_Click(object sender, EventArgs e)
{
j = 1;
}
private void button12_Click(object sender, EventArgs e)
{
j = 2;
}
private void button13_Click(object sender, EventArgs e)
{
j = 3;
}
private void button14_Click(object sender, EventArgs e)
{
j = 4;
}
private void button15_Click(object sender, EventArgs e)
{
switch(j)
{
case 1: textBox1.Text = Convert.ToString(x+y);
break;
case 2: textBox1.Text = Convert.ToString(x-y);
break;
case 3: textBox1.Text = Convert.ToString(x*y);
break;
case 4: textBox1.Text = Convert.ToString(x/y);
break;
}
}
}
|