C#实现计算器功能
短信预约 -IT技能 免费直播动态提醒
本文实例为大家分享了C#实现计算器功能的具体代码,供大家参考,具体内容如下
在刚刚接触c#的时候,就想做一个简单加减乘除计算器。这就是目标,可惜一直没有动手去做,今天特意把它简单做了。很简单,很简单,了却一个心愿了。代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MyPictureDownloader
{
//http://blog.sina.com.cn/s/blog_60d576800100tf5z.html
//http://jingyan.baidu.com/article/380abd0a6b80701d90192cde.html
public partial class JiSuanQi : Form
{
public JiSuanQi()
{
InitializeComponent();
initButton();
}
public delegate double Operater(double num1, double num2);
public void initButton()
{
var p = new Point(20, 80);
Button[] listbtn = new Button[9];
for (int i = 0; i < 9; i++)
{
listbtn[i] = new Button();
listbtn[i].Name = "btn" + (i + 1).ToString();
listbtn[i].Text = (i+1).ToString();
listbtn[i].SetBounds(p.X, p.Y, 50, 50);
listbtn[i].Click+= new System.EventHandler(ClickHandler);
this.Controls.Add(listbtn[i]);
p.X += 80;
if (p.X >= this.Width - 80)
{
p.X =20;
p.Y += 60;
}
}
}
public void ClickHandler(Object sender, System.EventArgs e)
{
Button btn = (Button)sender;
string temp=txtnum.Text.ToString()+btn.Text;//这样解决了重复点击赋值问题
txtnum.Text = temp;
}
private void btnzero_Click(object sender, EventArgs e)
{
string temp = txtnum.Text.ToString() + btnzero.Text;//这样解决了重复点击赋值问题
txtnum.Text = temp;
}
public double jisuan(string caozuofu, Operater fanga)
{
double num2 = double.Parse(txtnum.Text);
double jieguo = 0;
//switch(caozuofu){
// case"+":
// jieguo = fanga(tempnum, num2);
// break;
// case "-":
// jieguo = fanga(tempnum, num2);
// break;
// case "*":
// jieguo = fanga(tempnum, num2);
// break;
// case "/":
// jieguo = fanga(tempnum, num2);
// break;
//}
jieguo = fanga(tempnum, num2);
return jieguo;
}
public double add(double num1, double num2)
{
return num1 + num2;
}
public double jian(double num1, double num2)
{
return num1- num2;
}
public double cheng(double num1, double num2)
{
return num1 * num2;
}
public double chu(double num1, double num2)
{
double result = 0;
if (num2!=0)
{
result= num1 / num2;
}
return result;
}
public double tempnum = 0;
public string caozuofu = "";
public event Operater fangfa;
private void btnresult_Click(object sender, EventArgs e)
{
txtnum.Text = jisuan(caozuofu, fangfa).ToString();
}
private void btnadd_Click(object sender, EventArgs e)
{
tempnum = double.Parse(txtnum.Text);
caozuofu = btnadd.Text;
txtnum.Text = "";
fangfa = add;
}
private void btnde_Click(object sender, EventArgs e)
{
tempnum = double.Parse(txtnum.Text);
caozuofu = btnde.Text;
txtnum.Text = "";
fangfa = jian;
}
private void btncheng_Click(object sender, EventArgs e)
{
tempnum = double.Parse(txtnum.Text);
caozuofu = btncheng.Text;
txtnum.Text = "";
fangfa = cheng;
}
private void btnchu_Click(object sender, EventArgs e)
{
tempnum = double.Parse(txtnum.Text);
caozuofu = btnchu.Text;
txtnum.Text = "";
fangfa = chu;
}
private void btndian_Click(object sender, EventArgs e)
{
if (txtnum.Text.ToString()=="")
{
txtnum.Text = "0";
}
string temp="";
if (txtnum.Text.ToString().IndexOf(".") > 0)//解决只能包含一个小数点
{
temp = txtnum.Text.ToString();
}
else
{
temp = txtnum.Text.ToString() + btndian.Text;//这样解决了重复点击赋值问题
}
txtnum.Text = temp;
}
}
}
初始界面:
运行后的界面:
几个数字按钮是动态生成的,这就是我想要做的计算器。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341