开发工具:
文件大小: 249kb
下载次数: 0
上传时间: 2013-12-11
详细说明: public partial class FormMain : Form { public FormMain() { InitializeComponent(); } //记录所要执行的运算 private string operation; //记录用户是否已经输入小数点 private bool isDot; //记录用户是否已经单击等号 private bool isEqual; //被操作数 private double dblacc; //操作数 private double dbldes; //记录运算结果 private double dblresult; private void subReset() { operation = ""; dblacc = 0; dbldes = 0; isDot = false; isEqual = false; tResult.Text = ""; pKey.Focus(); } private void frmMain_Load(object sender, EventArgs e) { Button[] bt = new Button[28]; int i = 0; bt[1] = btNum1; bt[2] = btNum2; bt[3] = btNum3; bt[4] = btnum4; bt[5] = btNum5; bt[6] = btNum6; bt[7] = btNum7; bt[8] = btNum8; bt[9] = btNum9; bt[10] = btNum0; bt[11] = btNeg; bt[12] = btDot; bt[13] = btDiv; bt[14] = btMul; bt[15] = btAdd; bt[16] = btSub; bt[17] = btEqu; bt[18] = btBackspace; bt[19] = btCE; bt[20] = btC; for (i = 1; i <= 20; i++) { bt[i].Click += new EventHandler(this.bt_Click); } subReset(); (pKey as Control).KeyPress += new KeyPressEventHandler(pKey_KeyPress); } private void frmMain_Activated(object sender, EventArgs e) { pKey.Focus(); } private void bt_Click(object sender, EventArgs e) { string stext = null; Button bclick = new Button(); bclick = sender as Button; stext = bclick.Text; switch (stext) { case "1": case "2": case "3": case "4": case "5": case "6": case "7": case "8": case "9": case "0": if (isEqual) { tResult.Text = ""; //如果已经执行一次计算,那么再次输入数字时,应清空tresult } isEqual = false; tResult.Text = tResult.Text + stext; //将输入字符累加 break; case ".": if (!isDot) { tResult.Text = tResult.Text + stext; isDot = true; } break; //如果此前有小数点输入,那么这次的小数点输入将被忽略 case "+": case "-": case "*": case "/": if (!string.IsNullOrEmpty(tResult.Text.Trim())) { dblacc = Convert.ToDouble(tResult.Text); } tResult.Text = ""; operation = stext; break; //记下被操作数及操作符 case "=": isDot = false; if (!isEqual) { if (!string.IsNullOrEmpty(tResult.Text.Trim())) { dbldes = Convert.ToDouble(tResult.Text); } } //如果本次对"="的单击是连续的第二次单击,那么操作数不变 isEqual = true; switch (operation) { case "+": dblresult = dblacc + dbldes; break; //执行加法操作 case "-": dblresult = dblacc - dblacc; break; //执行减法操作 case "*": dblresult = dblacc * dbldes; break; //执行乘法操作 case "/": dblresult = dblacc / dbldes; break; //执行除法操作 } //根据操作符的不同执行相应的计算 tResult.Text = dblresult.ToString(); dblacc = dblresult; break; //将计算结果赋给被操作数,以便执行连续的第二次操作 case "+-": if (tResult.Text.Substring(0, 1) == "-") { tResult.Text = tResult.Text.Substring(1, tResult.Text.Length - 1); //如果当前数值为负,则去掉数字最前面的"-"将其变为正数 } else { tResult.Text = "-" + tResult.Text; //如果当前数值为正,则在数字最前面加上"-"将其变为负数 } break; //通过对tresult.text字符串的首位的判断来确定当前数值的正负 case "C": subReset(); break; //清除所有操作数,被操作数及运算结果,即将计算器重置 case "CE": tResult.Text = ""; break; //清除文本框内容 case "BackSpace": if (!string.IsNullOrEmpty(tResult.Text)) { //当tResult.Text非空时执行退格操作,以免出现溢出 if (tResult.Text.Substring(tResult.Text.Length - 1) == ".") { isDot = false; } //当小数点被删除时,将小数点标志位bDot重置 tResult.Text = tResult.Text.Substring(0, tResult.Text.Length - 1); //执行退格操作 } break; } pKey.Focus(); //每次单击按钮后,将焦点重新转移到Panel上 } private void MenuItem4_Click(Object sender, EventArgs e) { FormAbout frm = new FormAbout(); frm.Show(); //显示关于窗口 } private void MenuItem2_Click(object sender, EventArgs e) { Close(); } private void pKey_KeyPress(object sender, KeyPressEventArgs e) { switch (e.KeyChar) { case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': //输入为数字 if (isEqual) { tResult.Text = ""; } //如果已经执行过一次计算,那么再次输入数字时,应清空tresult isEqual = false; tResult.Text = tResult.Text + e.KeyChar; break; //将输入的字符累加 case '.': if (!isDot) { tResult.Text = tResult.Text + e.KeyChar; isDot = true; } break; //如果此前已经有小数点输入,那么这次的小数点输入将被忽略 case '+': case '-': case '*': case '/': dblacc = Convert.ToDouble(tResult.Text); tResult.Text = ""; operation = e.KeyChar.ToString(); break; //记下被操作数及操作符 case '=': isDot = false; if (!isEqual) { dbldes = Convert.ToDouble(tResult.Text); } //如果本次输入的“=”是第二次输入,那么操作数不变 isEqual = true; switch (operation) { case "+": dblresult = dblacc + dbldes; break; //执行加法操作 case "-": dblresult = dblacc - dbldes; break; //执行减法操作 case "*": dblresult = dblacc * dbldes; break; //执行乘法操作 case "/": dblresult = dblacc / dbldes; break; //执行除法操作 } //根据操作符的不同执行相应的计算 tResult.Text = dblresult.ToString(); dblacc = dblresult; break; //将计算结果赋给被操作数,以便执行连续的第二次操作 case (char)8: if (!string.IsNullOrEmpty(tResult.Text)) { //当tresult.text非空时执行退格操作,以免出现溢出 if (tResult.Text.Substring(tResult.Text.Length - 1) == ".") { isDot = false; } //当小数点被删掉时,将小数点标志位bdot重置 tResult.Text = tResult.Text.Substring(0, tResult.Text.Length - 1); //执行退格操作 } break; } pKey.Focus(); } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.