C#实现计算器窗体程序
程序员文章站
2022-03-01 13:28:45
本文实例为大家分享了c#实现计算器窗体程序的具体代码,供大家参考,具体内容如下功能设计1、计算器中,添加 0-9 共十个数字键。2、计算器中,增添 加、减、乘、除、等于五个功能键。3、计算器中,增加四...
本文实例为大家分享了c#实现计算器窗体程序的具体代码,供大家参考,具体内容如下
功能设计
1、计算器中,添加 0-9 共十个数字键。
2、计算器中,增添 加、减、乘、除、等于五个功能键。
3、计算器中,增加四个功能键:x2,sqrt,log, ln 四个键,分别计算求平方,开方。
实现代码
using system; using system.collections.generic; using system.componentmodel; 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 = false; //判断文本框中是否有值 string operation; //存储输入的运算符 /* * 初始化 */ public form1() { initializecomponent(); } /* * 数字键触发事件实现 */ private void seven_click(object sender, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = false; } textbox1.text += "7"; } private void eight_click(object sender, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = false; } textbox1.text += "8"; } private void nine_click(object sender, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = false; } textbox1.text += "9"; } private void four_click(object sender, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = false; } textbox1.text += "4"; } private void five_click(object sender, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = false; } textbox1.text += "5"; } private void six_click(object sender, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = false; } textbox1.text += "6"; } private void one_click(object sender, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = false; } textbox1.text += "1"; } private void two_click(object sender, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = false; } textbox1.text += "2"; } private void three_click(object sender, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = false; } textbox1.text += "3"; } private void zero_click(object sender, eventargs e) { if (exist_value == true) { textbox1.text = ""; exist_value = false; } textbox1.text += "0"; } /* * 功能键触发事件 */ private void add_click(object sender, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } else { exist_value = true; number = double.parse(textbox1.text); operation = "+"; } } private void sub_click(object sender, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } else { exist_value = true; number = double.parse(textbox1.text); operation = "-"; } } private void mul_click(object sender, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } else { exist_value = true; number = double.parse(textbox1.text); operation = "*"; } } private void div_click(object sender, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } else { exist_value = true; number = double.parse(textbox1.text); operation = "/"; } } private void squ_click(object sender, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } else { exist_value = true; number = double.parse(textbox1.text); operation = "x^2"; } } private void sqrt_click(object sender, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } else { exist_value = true; number = double.parse(textbox1.text); operation = "sqrt"; } } private void log_click(object sender, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } else { exist_value = true; number = double.parse(textbox1.text); operation = "log"; } } private void ln_click(object sender, eventargs e) { if (textbox1.text == "") { messagebox.show("请先输入值再计算!", "错误提示", messageboxbuttons.ok, messageboxicon.warning); } else { exist_value = true; number = double.parse(textbox1.text); operation = "ln"; } } private void del_click(object sender, eventargs e) { textbox1.text = ""; } private void equ_click(object sender, eventargs e) { switch (operation) { case "+": result = number + double.parse(textbox1.text); break; case "-": result = number - double.parse(textbox1.text); break; case "*": result = number * double.parse(textbox1.text); break; case "/": { double temp=double.parse(textbox1.text); if (temp != 0) result = number / temp; else messagebox.show("除数不能为零", "错误提示", messageboxbuttons.ok, messageboxicon.warning); break; } case "x^2": result = number * number; break; case "sqrt": result = math.sqrt(number); break; case "log": result = math.log10(number); break; case "ln": result = math.log(number); break; } textbox1.text = result + ""; exist_value = true; } } }
界面设计
运行结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: C#基于Sockets类实现TCP通讯
下一篇: C#实现简单的计算器功能(窗体)