C#怎么实现计算器窗体程序
发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,本文小编为大家详细介绍"C#怎么实现计算器窗体程序",内容详细,步骤清晰,细节处理妥当,希望这篇"C#怎么实现计算器窗体程序"文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。功
千家信息网最后更新 2025年02月04日C#怎么实现计算器窗体程序
本文小编为大家详细介绍"C#怎么实现计算器窗体程序",内容详细,步骤清晰,细节处理妥当,希望这篇"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#怎么实现计算器窗体程序"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。
输入
错误
提示
计算器
功能
程序
窗体
C#
文章
存储
事件
内容
数字
结果
设计
妥当
代码
十个数字
思路
文本
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
gome服务器异常怎么办
多买网络技术有限公司好不好
图书管理 数据库
现代网络技术用的软件
天津网络安全宣传
浙江信子互联网络科技有限公司
mdb 数据库连接失败
江苏智慧景区软件开发
软件开发综合实验室的特点
服务器行业研究报告
网络安全应用的四个特征
11种常用文本类型数据库
剑三找不到服务器
方舟端游进服务器怎么调亮度
mysql数据库访问权限
关于社区网络安全宣传周简报
维护网络安全有利于经济繁荣
数据库ldf文件增加快
太原网络技术培训机构
电力企业网络安全技术应用
做网络技术的院士叫什么
后端程序员需要创建数据库吗
上海水电缴费软件开发团队
中宏网络技术有限公司
软件工程vr软件开发适合考研吗
ktv网络技术员是什么意思
哔哩哔哩无法与服务器建立连接
社交软件开发的连接方式
网络技术的发展的利弊
连接不上网络电台服务器