首先利用visual studio里面的button和textbox模块建立上图模块。
然后就是编写程序:
该程序最重要的是要理解清楚计算器的运算逻辑。
public partial class Form1 : Form
{
string a, b, q, ac;
int c = 0, d = 0, n = 0, p = 0, m = 0;
double temp1 = -1;
int pos = 0;
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
public void addNum(int num)
{
= + ();
if (c > 0) ;;
}
}
private void button4_Click(object sender, EventArgs e)
{
pos = 1;
temp1 = Convert .ToDouble ;
=temp1+"+";
d++;
c++;
p = 0;
}
private void button8_Click(object sender, EventArgs e)
{
pos = 2;
temp1 = ;
= temp1 + "-";
c++; p = 0;
}
private void button12_Click(object sender, EventArgs e)
{
pos = 3;
temp1 = ;
= temp1 + "*";
c++; p = 0;
}
private void button16_Click(object sender, EventArgs e)
{
pos = 4;
temp1 = ;
= temp1 + "/";
c++; p = 0;
}
private void button13_Click(object sender, EventArgs e)
{
double temp2 = (b);
switch (pos)
{
case 1:
= (temp1 + temp2).ToString();
break;
case 2:
= (temp1 - temp2).ToString();
break;
case 3:
= (temp1 * temp2).ToString();
break;
case 4:
= (temp1 / temp2).ToString();
break;
}
}
private void button17_Click(object sender, EventArgs e)
{
= "";//清?空?显?示o?器?? temp1 = 0;
pos = 0;
b = "";
a = "";
n = 0;
c = 0;
p = 0;
}
}
}