程序设计思路:项目中使用了两个控件:数子按钮控件和操作符控件。计算器项目中处理共享事件的处理过程,其中首先通过sendenr对象获取按钮的Text属性,然后通过按钮Text属性可以可以判断是哪个Button被单击,并执行相应操作。
需求分析:新建WIndos应用程序项目,在Windows窗体上添加1个文本框控件TextBox1,其余按钮可在运行时自动建立。
具体设计:
private void Form1_Load(object sender, EventArgs e)
{
//计算计算器窗口居中
int left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
int top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
this.Location = new Point(left, top);
}
double num1 = 0;
double num2 = 0;
bool iskey = false;
//1
private void Button7_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "1";
}
//2
private void Button6_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "2";
}
//3
private void Button5_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "3";
}
//4
private void Button11_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "4";
}
//5
private void Button10_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "5";
}
//6
private void Button9_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "6";
}
//7
private void Button15_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "7";
}
//8
private void Button14_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "8";
}
//9
private void Button12_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "9";
}
//0
private void Button18_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += "0";
}
//小数点.
private void Button17_Click(object sender, EventArgs e)
{
if (iskey)
{
textBox1.Text = "";
iskey = false;
}
textBox1.Text += ".";
}
//CE
private void Button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
string type = "";
//=
private void Button2_Click(object sender, EventArgs e)
{
if (iskey)
{
return;
}
iskey = true;
if (textBox1.Text != "")
{
num2 = Convert.ToDouble(textBox1.Text);
}
switch (type)
{
case "+":
textBox1.Text = (num1 + num2).ToString();
break;
case "-":
textBox1.Text = (num1 - num2).ToString();
break;
case "×":
textBox1.Text = (num1 * num2).ToString();
break;
case "/":
textBox1.Text = (num1 / num2).ToString();
break;
case "%":
textBox1.Text = (num1 % num2).ToString();
break;
default:
break;
}
}
//-
private void Button3_Click(object sender, EventArgs e)
{
if (textBox1.Text!="")
{
num1 = Convert.ToDouble(textBox1.Text);
}
type = "-";
iskey = true;
}
//×
private void Button4_Click(object sender, EventArgs e)
{
type = "×";
if (textBox1.Text!="")
{
num1 = Convert.ToDouble(textBox1.Text);
}
iskey = true;
}
//除
private void Button8_Click(object sender, EventArgs e)
{
type = "/";
if (textBox1.Text!="")
{
num1 = Convert.ToDouble(textBox1.Text);
}
iskey = true;
}
//+
private void Button13_Click(object sender, EventArgs e)
{
type = "+";
if (textBox1.Text!="")
{
num1 = Convert.ToDouble(textBox1.Text);
}
iskey = true;
}
//取余%
private void Button16_Click(object sender, EventArgs e)
{
type = "%";
if (textBox1.Text!="")
{
num1 = Convert.ToDouble(textBox1.Text);
}
iskey = true;
}
}
}
代码复审:复审总体没问题
测试:
报告
测试报告:总的来说+,-,*,/都没问题可以实现连加。
计算工作量:这个计费器工作量并不大,但本人基础代码并不熟练所以所用时间较多。
事后总结:总体效果并不理想,视觉效果过于简陋,且功能单一,没有开平方,求根等功能。
原文:https://www.cnblogs.com/2250li/p/14643891.html