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


猜你喜欢
- 前言Android 8.0系统更新之后,app的更新将不再像之前的系统版本一样能够直接下载安装包之后直接安装(以前安装未知来源应用的时候一般
- 前言哈哈哈哈哈。。。。。。。。问题终于解决了,让我得瑟一会(吗卖批,折腾了两天)~~~如果你的Android Studio出现以下错误,那么
- 冒泡排序法:关键字较小的记录好比气泡逐趟上浮,关键字较大的记录好比石块下沉,每趟有一块最大的石块沉底。算法本质:(最大值是关键点,肯定放到最
- AndroidStudio 的SVN 安装和使用方法与我以前用的其他IDE 都有很大差别,感觉特麻烦,网上相关资料很少,貌似现在 Git 比
- 这篇文章主要介绍了Spring Bean初始化及销毁多种实现方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- 我们知道,进入百度图片后,输入一个关键字后,首先看到的是很多缩略图,当我们点击某张缩略图时,我们就可以进入到大图显示页面,在大图显示页面,中
- 多态概述多态概念:所谓多态就是指程序中定义的引用变量所指向的具体类型和通过该引用变量发出的方法调用在编程时并不确定,而是在程序运行期间才确定
- 本文实例讲述了C#中HttpWebRequest的用法。分享给大家供大家参考。具体如下:HttpWebRequest类主要利用HTTP 协议
- springboot启动时自动加载application.properties或者application.yml,如何定义自己的配置让spr
- 本文以eclipse4.7安装sts3.9.0为例,解决报错An error occurred while collecting items
- 本文要解决在侧滑菜单右边加个文本框,并能实现文本的上下滑动和菜单的左右滚动。这里推荐可以好好看看android的触摸事件的分发机制,这里我就
- import java.lang.reflect.InvocationHandler;import java.lang.reflect.Me
- 一、概述今天给大家带来SurfaceView的一个实战案例,话说自定义View也是各种写,一直没有写过SurfaceView,这个玩意是什么
- newInstance()使用类加载机制,new是创建一个新类。从JVM角度看,使用new创建一个类的时候,这个类可以没有被加载。但是使用n
- try { using (TransactionScope tr = new Transact
- 使用java自带的Point类import java.awt.Point;//引用awt包下的Point类,此类的功能是表示 (x,y) 坐
- 随着现在分布式架构越来越盛行,在很多场景下需要使用到分布式锁。很多小伙伴对于分布式锁还不是特别了解,所以特地总结了一篇文章,让大家一文读懂分
- 环境:apache-tomcat-8.5.15jdk1.8.0_172IDEA建立一个maven-webapp项目:Create New P
- 前提前面写过一篇关于Environment属性加载的源码分析和扩展,里面提到属性的占位符解析和类型转换是相对复杂的,这篇文章就是要分析和解读
- 什么是AOPAOP是 Aspect Oriented Programming 的缩写,即面向切面编程,和平常遇到的面向对象OOP编程不一样的