首页 > Windows开发 > 详细

C#简易计算器设计

时间:2021-04-11 21:03:24      阅读:12      评论:0      收藏:0      [点我收藏+]

计应192(西)1组李俊

C#简易计算器设计

PSP图:技术分享图片

程序设计思路:项目中使用了两个控件:数子按钮控件和操作符控件。计算器项目中处理共享事件的处理过程,其中首先通过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;

 


}

 

}
}

 

 

代码复审:复审总体没问题

测试:技术分享图片

报告

测试报告:总的来说+,-,*,/都没问题可以实现连加。

计算工作量:这个计费器工作量并不大,但本人基础代码并不熟练所以所用时间较多。

事后总结:总体效果并不理想,视觉效果过于简陋,且功能单一,没有开平方,求根等功能。

 

 

C#简易计算器设计

原文:https://www.cnblogs.com/2250li/p/14643891.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!