本文实例为大家分享了c#实现计算器窗体程序的具体代码,供大家参考,具体内容如下
1、计算器算天数中,添加 0-9 共十个数字键。
2、计算器中,增添 加、减、乘、除、等于五个功能键。
3、计算器中,增加四个功能键:x2,sqrt,log, ln 四个键,分别计算求平方,开方。
using system;using system.collections.generic;using system.com茶叶小知识ponentmodel;using system.data;using system.drawing;using system.linq;using system.text;using system.threading.tasks;using system.windows.forms;namespace test3_1{ public partial class form1 : form { double result = 0; //存储计算结果 double number = 0; //存储输入的数字 bool exist_value = fal; //判断文本框中是否有值 string operation; //存储输入的运算符 /* * 初始化 */ public form1() { initializecomponent(); } /* * 数字键触发事件实现 */ private void ven_click(object nder, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = fal; } textbox1.text += "7"; } private void eight_click(object nder, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = fal; } textbox1.text += "8"; } private void nine_click(object nder, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = fal; } textbox1.text += "9"; } private void four_click(object nder, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = fal; } textbox1.text += "4"; } private void five_click(object nder, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = fal; } textbox1.text += "5"; } private void six_click(object nder, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = fal; } textbox1.text += "6"; } private void one_click(object nder, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = fal; } textbox1.text += "1"; } private void two_click(object nder, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = fal; } textbox1.text += "2"; } private void three_click(object nder, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = fal; } textbox1.text += "3"; } private void zero_click(object nder, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = fal; } textbox1.text += "0"; } /* * 功能键触发事件 */ private void add_click(object nder, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } el { exist_value = true; number = double.par(textbox1.text); operation = "+"; } } private void sub_click(object nder, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } el { exist_value = true; number = double.par(textbox1.text); operation = "-"; } } private void mul_click(object nder, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } el { exist_value = true; number = double.par(textbox1.text); operation = "*"; } } private void div_click(object nder, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } el { exist_value = true; number = double.par(textbox1.text); operation = "/"; } } private void squ_click(object nder, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } el { exist_value = true; number = double.par(textbox1.tex形容人多的四字词语t); operation = "x^2"; } } private void sqrt_click(object nder, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } el { exist_value = true; number = double.par(textbox1.text); operation = "sqrt"; } } private void log_click(object nder, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } el 表语是什么意思{ exist_value = true; number = double.par(textbox1.text); operation = "log"; } } private void ln_click(object nder, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } el { exist_value = true; number = double.par(textbox1.text); operation = "ln"; } } private void del_click(object nder, eventargs e) { textbox1.text = ""; } private void equ_click(object nder, eve作文我的烦恼ntargs e) { switch (operation) { ca "+": result = number + double.par(textbox1.text); break; ca "-": result = number - double.par(textbox1.text); break; ca "*": result = number * double.par(textbox1.text); break; ca "/": { double temp=double.par(textbox1.text); if (temp != 0) result = number / temp; el messagebox.show("除数不能为零", "错误提示", messageboxbuttons.ok, messageboxicon.warning); break; } ca "x^2": result = number * number; break; ca "sqrt": result = math.sqrt(number); break; ca "log": result = math.log10(number); break; ca "ln": result = math.log(number); break; } textbox1.text = result + ""; exist_value = true; } }}
界面设计
运行结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-04 21:04:30,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/3efe97587305e1a888c5c7a7006c409a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C#实现计算器窗体程序.doc
本文 PDF 下载地址:C#实现计算器窗体程序.pdf
留言与评论(共有 0 条评论) |