计算器
namespace jisuanqi{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool first;
private bool clear;
private double a;
private double b;
private string open;
private void num(int i)
{
if (clear)
{
if (first == true)
a = Convert.ToDouble(textBox1 .Text );
else b = Convert.ToDouble(textBox1.Text);
textBox1.Text = "";
clear = false;
}
if (((i == 0) && (textBox1.Text != "")) || (i != 0))
textBox1.Text = textBox1.Text + i.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
if (sender == button1) num(1);
}
private void button2_Click(object sender, EventArgs e)
{
if (sender == button2) num(2);
}
private void button3_Click(object sender, EventArgs e)
{
if (sender == button3) num(3);
}
private void button4_Click(object sender, EventArgs e)
{
if (sender == button4) num(4);
}
private void button5_Click(object sender, EventArgs e)
{
if (sender == button5) num(5);
}
private void button6_Click(object sender, EventArgs e)
{
if (sender == button6) num(6);
}
private void button7_Click(object sender, EventArgs e)
{
if (sender == button7) num(7);
}
private void button8_Click(object sender, EventArgs e)
{
if (sender == button8) num(8);
}
private void button9_Click(object sender, EventArgs e)
{
if (sender == button9) num(9);
}
private void button10_Click(object sender, EventArgs e)
{
if (sender == button10) num(0);
}
private void button11_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
textBox1.Text = "0.";
else textBox1.Text = textBox1.Text + ".";
}
private void button12_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
if (first == true)
a = Convert.ToDouble(textBox1.Text);
else b = Convert.ToDouble(textBox1.Text);
}
switch (open)
{
case "+":
a += b;
break;
case "-":
textBox1 .Text = Convert.ToString(a -b);
break;
case "*":
textBox1.Text = Convert.ToString(a - b);
break;
case "/":
textBox1 .Text = Convert.ToString(a - b);
break;
}
open = "=";
private void button17_Click(object sender, EventArgs e)
{
a = 0;
b = 0;
first = true;
clear = false;
textBox1.Text = "";
textBox1.Focus();
}
这是+-*/的不知道怎么写。按照现在写的运行的时候要按1+3=+才会显示4出来,还有=那边也有问题当我输完一些数字按=在输得时候它就不清零直接在后面显示了
private void button13_Click(object sender, EventArgs e)
{
/*if (textBox1.Text != "")
if (first == true)
a = Convert.ToDouble(textBox1 .Text );
else b = Convert.ToDouble(textBox1.Text);*/
b = Convert.ToDouble(textBox1.Text);
switch (open)
{
case"+":
a += b;
break ;
case "-":
textBox1.Text = Convert.ToString(a - b);
break;
case "*":
textBox1.Text = Convert.ToString(a * b);
break;
case "/":
textBox1.Text = Convert.ToString(a / b);
break;
}
//b = Convert.ToDouble(textBox1.Text);
if (sender == button13)
{
open = "+";
first = false;
textBox1.Text = Convert.ToString(a);
clear = true;
}
/*if (sender == button14)
textBox1.Text = Convert.ToString(a - b);
if (sender == button15)
textBox1.Text = Convert.ToString(a * b);
if (sender == button16)
textBox1.Text = Convert.ToString(a / b);
//first = false;
//textBox1.Text = Convert.ToString(a);
//clear = true;*/
}
--------------------编程问答-------------------- --------------------编程问答-------------------- 推荐一个示例程序,不过是VB.NET写的,但是NET代码都是可以转的,可以直接用,还有源代码,有参考价值LZ一定试下。
http://download.csdn.net/detail/txzsp/3781512
补充:.NET技术 , C#