c#计算器界面写好了代码怎么加进去让它实现windows计算器的功能

如题所述

分别点击按钮事件,加入相应的代码呀
最简单写法:
string Mynum=string.Empty;//保存按钮按下的数字
int type=-1;//记录操作类型
int shu1=0,shu2=0;
/*自己设置+1,-2,*3,/4*/
private void num1_Click(object sender, EventArgs e)
{
Mynum1 += "1";

textBox1.Text = Mynum1;
}

private void num2_Click(object sender, EventArgs e)
{
Mynum1 += "2";
textBox1.Text = Mynum1;
}

private void num3_Click(object sender, EventArgs e)
{
Mynum1 += "3";
textBox1.Text = Mynum1;
}

private void num4_Click(object sender, EventArgs e)
{
Mynum1 += "4";
textBox1.Text = Mynum1;
}

private void num5_Click(object sender, EventArgs e)
{
Mynum1 += "5";
textBox1.Text = Mynum1;
}

private void num6_Click(object sender, EventArgs e)
{
Mynum1 += "6";
textBox1.Text = Mynum1;
}

private void num7_Click(object sender, EventArgs e)
{
Mynum1 += "7";
textBox1.Text = Mynum1;
}

private void num8_Click(object sender, EventArgs e)
{
Mynum1 += "8";
textBox1.Text = Mynum1;
}

private void num9_Click(object sender, EventArgs e)
{
Mynum1 += "9";
textBox1.Text = Mynum1;
}

private void num0_Click(object sender, EventArgs e)
{
Mynum1 += "0";
textBox1.Text = Mynum1;
}

private void Point_Click(object sender, EventArgs e)
{
Mynum1 += ".";
textBox1.Text = Mynum1;
}
/*加法*/
private void plus_Click(object sender, EventArgs e)
{
type = 1;
Mynum1 = textBox1.Text;//为了可以接受键盘输入
shu1 = this.Converttodoble(Mynum1);
Mynum1 = "";
textBox1.Text = "";
}
/*减法*/
private void minus_Click(object sender, EventArgs e)
{
type = 2;
Mynum1 = textBox1.Text;//
shu1 = this.Converttodoble(Mynum1);
Mynum1 = "";
textBox1.Text = "";
}
/*乘法*/
private void multi_Click(object sender, EventArgs e)
{
type = 3;
Mynum1 = textBox1.Text;//
shu1 = this.Converttodoble(Mynum1);
Mynum1 = "";
textBox1.Text = "";
}
/*除法*/
private void div_Click(object sender, EventArgs e)
{
type = 4;
Mynum1 = textBox1.Text;//
shu1 = this.Converttodoble(Mynum1);
Mynum1 = "";
textBox1.Text = "";
}
/*按下等于按钮*/
private void Equal_Click(object sender, EventArgs e)
{

Mynum1 = textBox1.Text;//

switch (type)
{
case 1:
shu2 = shu1 + this.Converttodoble(Mynum1);
break;
case 2:
shu2 = shu1 - this.Converttodoble(Mynum1);
break;
case 3:
shu2 = shu1 * this.Converttodoble(Mynum1);
break;
case 4:
if (this.Converttodoble(Mynum1) == 0)
{

MessageBox.Show("除数不能为0!");
}
else
{
shu2 = shu1 / this.Converttodoble(Mynum1);
}
break;

}

Mynum1 = Convert.ToString(shu2);
textBox1.Text = Mynum1;
}
//防止用户不做输入进行运算
public double Converttodoble(string str)
{

if (str==""||str==".")
{
return 0;

}
else
{
return Convert.ToDouble(str);
}
}
这是一个最简单的模型了来自:求助得到的回答
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-05-08
请问楼主用什么编译器编的,如果是在visual studio里,直接运行就行了啊!!什么编译器都能运行的啊!!!!
第2个回答  2010-05-08
邮箱给我,我给你发一个
第3个回答  2010-05-08
你是说windows窗体应用程序?

那就对每个按钮添加Click事件,基本和控制台应用程序差不多,我们才做了个
你要的话baidu留言给我 我传给你
相似回答