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计算器在哪里?如何打开?针对此问题,本文 ...
随机推荐
- UML作业第二次:类在类图中的表示
一.学习小结: 类之间的关系通过下面的符号定义 : 使用.. 来代替 -- 可以得到点 线. @startuml Class01 <|-- Class02 Class03 *-- Class04 ...
- mysql 重点性能测试指标
#qps 每秒钟查询数量 计算方式queries/seconds 查询总数/秒数show GLOBAL STATUS LIKE 'question%' #tps 每秒事务数 计算方式 (com_com ...
- Django的认证系统和Django admin的简单使用
Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Djang ...
- Jupyter Notebook 修改默认打开的文件夹的位置
初次使用Jupyter Notebook,确实好用啊!!,又好看又好用,不过还是遇到了一个问题,安装好之后,打开Jupyter Notebook 的时候,默认的文件夹的位置是C盘下面的XXX目录,但是 ...
- Centos7安装Nginx+PHP+MySQL
之前曾经在服务器上从头到尾搭过一次环境,但那时新手一枚,很多地方搞不定,是前辈帮忙解决的.这次独自一人在服务器上撘环境,感慨上次没有做好相关笔记,所以事后整理一下,下次再搭环境时可以轻车熟路. 一.准 ...
- feign调用超时
Feign调用超时 feign调用超时 默认feign调用超时是1秒,断点调试是否调用成功肯定超时 feign.hystrix.enabled=true #feign调用默认是1000毫秒=1秒 ad ...
- VS2015+VisualSVN+TortoiseSVN安装及使用
1. SVN 是什么 SVN 是 Apache Subversion 的缩写,是一个开放源代码的版本控制系.这些数据放置在一个中央资料档案库(repository) 中. 这个档案库很像一个普通的文件 ...
- C#中,子类构造函数调用父类父类构造函数的正确方式
正确调用的方式是:
- Win10安装.NetFamework3.5
步骤1:装载Win10安装镜像 本人用的是"cn_windows_10_multiple_editions_x64_dvd_6848463.iso" 如图,我把镜像装载到H盘; 步 ...
- UVa LA 4094 WonderTeam 构造 难度: 1
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...