c#计算器
代码没有大的问题,但是起初点击控件无反应,原因是事件代码要自己敲,不能直接粘贴。
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 calculator
{
public partial class Form1 : Form
{
public double num1;//定义第一次输入数
public double num2;//定义第二次输入数
public string sign;//定义运算符
public double num3;//定义结果
public bool check = true;//判断是否为第一次输入数
public double ans;//定义一个ans以便实现连续运算
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button7_MouseClick(object sender, MouseEventArgs e)
{
if (check == true)
{
Button b = (Button)sender;//定义一个新的button并将点击的button属性赋予它
textBox1.Text += b.Text;//将button的text属性与textbox的text属性相加(字符串相加直接连接)
num1 = double.Parse(textBox1.Text);//将textbox的text转为double型并赋值给num1
}
else
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num2 = double.Parse(textBox1.Text);
}
}
private void button1_MouseClick(object sender, MouseEventArgs e)
{
if (check == true)
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num1 = double.Parse(textBox1.Text);
}
else
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num2 = double.Parse(textBox1.Text);
}
}
private void button2_MouseClick(object sender, MouseEventArgs e)
{
if (check == true)
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num1 = double.Parse(textBox1.Text);
}
else
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num2 = double.Parse(textBox1.Text);
}
}
private void button3_MouseClick(object sender, MouseEventArgs e)
{
if (check == true)
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num1 = double.Parse(textBox1.Text);
}
else
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num2 = double.Parse(textBox1.Text);
}
}
private void button4_MouseClick(object sender, MouseEventArgs e)
{
if (check == true)
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num1 = double.Parse(textBox1.Text);
}
else
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num2 = double.Parse(textBox1.Text);
}
}
private void button5_MouseClick(object sender, MouseEventArgs e)
{
if (check == true)
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num1 = double.Parse(textBox1.Text);
}
else
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num2 = double.Parse(textBox1.Text);
}
}
private void button6_MouseClick(object sender, MouseEventArgs e)
{
if (check == true)
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num1 = double.Parse(textBox1.Text);
}
else
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num2 = double.Parse(textBox1.Text);
}
}
private void button8_MouseClick(object sender, MouseEventArgs e)
{
if (check == true)
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num1 = double.Parse(textBox1.Text);
}
else
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num2 = double.Parse(textBox1.Text);
}
}
private void button9_MouseClick(object sender, MouseEventArgs e)
{
if (check == true)
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num1 = double.Parse(textBox1.Text);
}
else
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num2 = double.Parse(textBox1.Text);
}
}
private void button10_MouseClick(object sender, MouseEventArgs e)
{
if (check == true)
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num1 = double.Parse(textBox1.Text);
}
else
{
Button b = (Button)sender;
textBox1.Text += b.Text;
num2 = double.Parse(textBox1.Text);
}
}
private void button11_MouseClick(object sender, MouseEventArgs e)//相反数
{
if (check == true)
{
num1 = 0 - num1;
textBox1.Text = num1.ToString();
}
else
{
num2 = 0 - num2;
textBox1.Text = num2.ToString();
}
}
private void button12_MouseClick(object sender, MouseEventArgs e)//等于
{
if (check == true)
{
num3 = num1;
}
else
{
switch (sign)
{
case "+":
num3 = ans + num2;
break;
case "-":
num3 = ans - num2;
break;
case "*":
num3 = ans * num2;
break;
case "/":
num3 = ans / num2;
break;
case "x²":
num3 = Math.Pow(ans, 2);
break;
}
}
textBox1.Text = num3.ToString();
}
private void button13_MouseClick(object sender, MouseEventArgs e)//平方
{
ans = double.Parse(textBox1.Text);
sign = "x²";
textBox1.Text = "";
check = false;
}
private void button14_MouseClick(object sender, MouseEventArgs e)//除法
{
ans = double.Parse(textBox1.Text);
sign = "/";
textBox1.Text = "";
check = false;
}
private void button15_MouseClick(object sender, MouseEventArgs e)//乘法
{
ans = double.Parse(textBox1.Text);
sign = "*";
textBox1.Text = "";
check = false;
}
private void button16_MouseClick(object sender, MouseEventArgs e)//减法
{
ans = double.Parse(textBox1.Text);
sign = "-";
textBox1.Text = "";
check = false;
}
private void button17_MouseClick(object sender, MouseEventArgs e)//CE
{
textBox1.Text = "";
num1 = 0;
num2 = 0;
num3 = 0;
ans = 0;
check = true;
}
private void button18_MouseClick(object sender, MouseEventArgs e)//加法
{
ans = double.Parse(textBox1.Text);
sign = "+";
textBox1.Text = "";
check = false;
}
}
}
c#计算器的更多相关文章
- 1.C#WinForm基础制作简单计算器
利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号 MessageBox.S ...
- 自己动手写计算器v1.1
这个改动主要是使用工厂模式替代了简单工厂模式,这样做的好处是如果以后我们要扩充其他运算时,就不用总是去修改工厂类, 这是可以采取工厂模式,主要是将原来简单工厂类的逻辑判断分离出来,将它作为一个借口,与 ...
- 自己动手写计算器v1.0
今天突发奇想,想着看了还几个设计模式了,倒不如写点东西来实践它们.发现计算器这种就比较合适,打算随着设计模式的学习,会对计算器不断的做改进. 包括功能的增加和算法的改进.初学者难免犯错,希望大家不吝指 ...
- 【IOS开发笔记03-视图相关】简单计算器的实现
UIView 经过前几天的快速学习,我们初步了解的IOS开发的一些知识,中间因为拉的太急,忽略了很多基础知识点,这些知识点单独拿出来学习太过枯燥,我们在今后的项目中再逐步补齐,今天我们来学习APP视图 ...
- [LeetCode] Basic Calculator 基本计算器
Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...
- JS-自制提速小工具:开发页面时需要按比例计算宽高值的快速计算器
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <meta name= ...
- 由ArcMap属性字段自增引出字段计算器使用Python的技巧
1.前言 前些日子有人问我ArcMap中要让某个字段的值实现自增有什么方法?我首先想到像SQL Server中对于数值型字段可以设置自增.所以我打开ArcCatalog查看发现只提供默认值 ...
- 前端学PHP之面向对象系列第六篇——简单图形面积计算器实现
前面的话 本文用面向对象的技术来实现一个简单的图形面积计算器 图形类 //rect.class.php <?php abstract class Shape{ public $name; abs ...
- tn文本分析语言(四) 实现自然语言计算器
tn是desert和tan共同开发的一种用于匹配,转写和抽取文本的语言.解释器使用Python实现,代码不超过1000行. github地址:https://github.com/ferventdes ...
- Win10计算器在哪里?三种可以打开Win10计算器的方法图文介绍
全新的windows10系统带来了不少新的特性和改变,其中win10的计算器位置就发生了很多的变化,导致很多网友们都以为win10计算器不见了,那么,win10计算器在哪里?如何打开?针对此问题,本文 ...
随机推荐
- WIN10下Java环境变量配置
首先,你应该已经安装了 Java 的 JDK 了(如果没有安装JDK,请跳转到此网址:http://www.oracle.com/technetwork/java/javase/downloads/i ...
- Commander
原文:https://www.npmjs.com/package/commander Commander.js Installation npm install commander --save Op ...
- android ------ AAPT2 error: check logs for details解决方法
AAPT 是全称是 Android Asset Packaging Tool,它是构建 App,甚至是构建 Android 系统都必不可少的一个工具.它的作用是将所有资源文件压缩打包到Android ...
- win2012R2 的IIS报错HTTP404,报错在计算机上找不到服务W3SVC等等
一.背景 今天远程给客户解决IIS的默认网页浏览找不到文件夹,报错HTTP404,找了很多原因,而且也报错在计算机上找不到服务W3SVC等等,如图所示: 二.原因 试了很多方法都不可以重装IIS都不行 ...
- UnicodeEncodeError: 'gbk' codec can't encode character '\xee'
在将爬取到的内容写入文件时候报了这个错误,解决方案是在open()的时候给encoding参数传'utf-8'就好了,因为网页的编码就是utf-8. with open('douban.html',' ...
- vue图片裁剪插件vue-cropper
最近做后台管理,需要用到图片裁剪,这个组件很好用,记录一下. 工作太忙,就不总结了. 链接分享:https://github.com/xyxiao001/vue-cropper
- python数据结构与算法之单链表
表的抽象数据类型 ADT list: #一个表的抽象数据类型 List(self) #表的构造操作,创建一个空表 is_empty ...
- python3安装docx模块出现Import Error: No module named 'exceptions'
x首先 pip3 install docx 显示已经安装,但是 No module named 'exceptions' 网上查的资料命令行下载的docx安装包还没有完全兼容python3,第三方库应 ...
- jenkins log文件突然占满磁盘空间
今天早上同事反应jenkins构建job发生异常,于是登录机器查看发现磁盘空间已满.进一步排查之后发现jenkins的catalina.out文件已占满磁盘空间. 用tail看了下日志后面都是关于DN ...
- linux jpg文件查找木马
find ./ -type f -name "*.jpg" | xargs grep "eval"