计算器c#实现

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication43 { public partial class Form3 : WindowsFormsApplication40.Form1 { string op1, op2;//预输入操作数 bool decimalflag;//小数点是否存在 int numops; string lastinput; int opflag; string tempreadout; public Form3() { InitializeComponent(); } private void Form3_Load(object sender, EventArgs e) { decimalflag=false ; numops =0; lastinput ="none"; opflag =0; textBox1.Text = "0."; } public void calculate(int mynumber) { int index = mynumber; if (lastinput !="nums") { textBox1 .Text ="0."; decimalflag =false ; } if (decimalflag) { textBox1.Text = textBox1.Text + mynumber; } else { if (textBox1.Text == "0.") { textBox1.Text = mynumber + "."; } else { textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1) + mynumber + "."; } } if (lastinput == "neg") { textBox1.Text = "-" + textBox1.Text; } lastinput = "nums"; }//运算符的click事件全过程 public void opertorcal(int myoperator) { int index = myoperator; tempreadout = textBox1.Text; if (lastinput == "nums") { numops = numops + 1; } string muop; muop = numops.ToString(); switch (numops) { case 0: if (myoperator == 3 && lastinput != "neg") { textBox1.Text = "-" + textBox1.Text; lastinput = "neg"; } break; //一个操作数 case 1: op1 = textBox1.Text; if (myoperator ==3&&lastinput !="nums"&&opflag !=4) { textBox1 .Text ="-"; lastinput ="neg"; } break ; case 2: op2=tempreadout ; //根据操作数进行不同处理 switch (opflag ) { case 0: if (Double .Parse (op2 )==0) { MessageBox .Show ("除数不能为零","提示"); } else { op1 =(double .Parse (op1 )/double.Parse (op2 )).ToString (); } break ; //加法 case 1: op1=(double .Parse (op1 )+double .Parse (op2 )).ToString (); break ; //乘法 case 2: op1 =(double .Parse (op1 )*double .Parse (op2)).ToString (); break ; //减法 case 3: op1 =(double .Parse (op1)-double .Parse (op2)).ToString (); break ; //等于 case 4: op1=op2 ; break; } textBox1 .Text =op1;//显示计算结果 numops =1; break ; } if (lastinput !="neg") { lastinput ="ops"; opflag =myoperator ; } } private void num0_Click(object sender, EventArgs e) { calculate (0); } private void num1_Click(object sender, EventArgs e) { calculate (1); } private void num2_Click(object sender, EventArgs e) { calculate (2); } private void num3_Click(object sender, EventArgs e) { calculate (3); } private void num4_Click(object sender, EventArgs e) { calculate (4); } private void num5_Click(object sender, EventArgs e) { calculate (5); } private void num6_Click(object sender, EventArgs e) { calculate (6); } private void num7_Click(object sender, EventArgs e) { calculate (7); } private void num8_Click(object sender, EventArgs e) { calculate (8); } private void num9_Click(object sender, EventArgs e) { calculate (9); } private void decimaldot_Click(object sender, EventArgs e) {//若输入为负数 if (lastinput =="neg") { textBox1 .Text ="-0."; }//若输入为正数 else if (lastinput !="nums") { textBox1 .Text ="0."; } decimalflag =true ; lastinput ="nums"; } private void percent_Click(object sender, EventArgs e) { textBox1 .Text =(double .Parse (textBox1 .Text )/100).ToString (); lastinput ="ops"; numops=numops +1; decimalflag =true ; } private void opeadd_Click(object sender, EventArgs e) { opertorcal (1); } private void opesub_Click(object sender, EventArgs e) { opertorcal (3); } private void opemul_Click(object sender, EventArgs e) { opertorcal (2); } private void opediv_Click(object sender, EventArgs e) { opertorcal (0); } private void opeequ_Click(object sender, EventArgs e) { opertorcal (4); } private void cancel_Click(object sender, EventArgs e) { textBox1.Text ="0."; op1 =" "; op2=" "; Form3_Load (this ,new System.EventArgs ()); } private void cancelentry_Click(object sender, EventArgs e) { textBox1 .Text ="0."; decimalflag =false ; lastinput ="CE "; } } } 计算器实现了 简单的程序做了好久 过程中收获了继承窗体的使用 还有其中两个重要的处理函数 第一个用来读取操作数, 该函数有个整型参数,改参数用来确定用户单击按钮所对应的数字。函数将这个数字字符增加到文本框显示的文本结束,就是先了数字的输入操作,然后将该结果保存到操作数种,为以后的运算做好准备 该函数就是数字键的click事件进行调用。其中主要的是对于小数点的处理方法。 第二部分用来实现各种计算操作。改计算函数有个参数,这个参数用来标志是哪一种计算,并且根据操作数的个数和操作符好的种类分别进行操作。最后将操作结果返回给文本框的文本,显示给用户。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值