C#实现计算器窗体程序
作者:羽路星尘 发布时间:2023-04-09 01:25:42
标签: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;
}
}
}
界面设计
运行结果
来源:https://blog.csdn.net/qq_43869033/article/details/110793893
0
投稿
猜你喜欢
- Kotlin 支持泛型, 语法和 Java 类似。例如,泛型类:class Hello<T>(val value: T)val
- 目录springboot autoconfig的一些实验SpringBoot autoconfig部分注解说明SpringBoot auto
- 接口定义了一系列的行为规范,为类型定义一种Can-Do的功能。例如,实现IEnumerable接口定义了GetEnumerator方法,用于
- SpringBoot找不到javax.servlet.Filter的问题新创建一个SpringBoot项目,编译时出现了找不到javax.s
- 问题使用前后端分离模式开发项目时,往往会遇到这样一个问题 -- 无法跨域获取服务端数据这是由于浏览器的同源策略导致的,目的是为了安全。在前后
- 平时在开发时经常会用到 const,readonly,static 关键字,可以肯定这些关键词是完全不同的概念,但有时候他们在用法上很相似以
- 对于使用文件进行交换数据的应用来说,使用FTP 服务器是一个很不错的解决方案。关于FileZilla Server服务器的详细搭建配置过程,
- TCPServer 1、使用的通讯通道:socket2、用到的基本功能:①Bind,②Listen,③BeginAccept④En
- 前言哎呀,妈呀,又出异常了!俗话说:“代码虐我千百遍,我待代码如初恋”。小Alan最近一直在忙着工作,已经很久没有写写东西来加深自己的理解了
- 一、简介JetCache是一个基于Java的缓存系统封装,提供统一的API和注解来简化缓存的使用。 JetCache提供了比SpringCa
- 一、问题Flutter原有的图片缓存机制,是通过PaintingBinding.instance!.imageCache来管理缓存的,这个缓
- MyBatis在SQL语句中获取list大小需求:使用MyBatis进行开发时,在一个SQL语句中需要拼接list的大小。大家都知道,当我们
- 这篇文章主要介绍了SpringBoot项目的测试类实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要
- Java事件处理机制和适配器最重要的是理解事件源,监视器,处理事件的接口的概念。1.事件源:是能够产生时间的对象都可以叫事件源,比如文本框,
- Java synchronized 关键字 可以将一个代码块或一个方法标记为同步代码块。同步代码块是指同一时间只能有一个线程执行的代码,并且
- 现象:安装失败,具体信息:Installation did not succeed.The application could not be
- 协同过滤简单来说是利用某兴趣相投、拥有共同经验之群体的喜好来推荐用户感兴趣的信息,个人通过合作的机制给予信息相当程度的回应(如评分)并记录下
- 介绍责任链模式是一种行为型设计模式,其目的是将请求从一个对象传递到另一个对象,直到找到能够处理该请求的对象为止.再责任链模式中,每个对象都持
- QR 二维码中插入图片二维码终于火了,现在大街小巷大小商品广告上的二维码标签都随处可见,而且大都不是简单的纯二维码,而是中间有个性图标的二维
- 1 pom.xml文件注:热部署功能spring-boot-1.3开始有的<!--添加依赖--><dependency&g