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计算器在哪里?如何打开?针对此问题,本文 ...
随机推荐
- ape 文件 转化为mp3 文件
试了很多软件,最后才发觉 any-audio-converter最好用. 可以吧ape 按 cue切割好,然后转化成 MP3 官网可以免费下载: https://www.any-audio-conve ...
- Arthur and Questions CodeForces - 518E (贪心模拟)
大意: 给定序列$a$, 某些位置为'?', 求给'?'赋值使得序列$(a_1+a_2+...+a_k,a_2+a_3+...+a_{k+1},...)严格递增, 且$\sum|a_i|$最小. 化简 ...
- oracle创建删除表空间
create [undo|temporary] tablespace orcp datafile|tempfile 'E:\orcle\oracleBaseDir\oradata\orcp\orcp. ...
- angular配置路由/子页面+vue配置路由/子页面
1.在vue.js中组件可以复用,然后最近配置了几个子页面 在 这个文件中配置路由,子页面的配置跟其他一样,只不过path不同. routes: [ { path: '/', ...
- word中一页中添加两种不同的页码
,在文档编写的过程中,可能一个页面需要编写两个不同的页码,举个例子,在页脚有一个页码是整个文档的页码,页眉有一个页码,是每个章节的页码: 设置如下: 此处选中这个图标是为了能够看到分节符和其他的符号 ...
- vue2.0自学笔记
前言: 一.优点: 轻量级.高效率.上手快.简单易学.文档全面而简洁 二.功能: 1.模板渲染 2.模块化 3.扩展功能:路由.Ajax 三.课程包含: 1.Vue实例 2.Vue组件 3.Vue指令 ...
- k8s 部署rabbitmq单节点
apiVersion: extensions/v1beta1 kind: Deployment metadata: annotations: fabric8.io/iconUrl: https://r ...
- 跟随我在oracle学习php(19)
Order by子句 形式: order by 排序字段1 [排序方式], 排序字段2 [排序方式], ..... 说明: 对前面取得的数据(含from子句,where子句,group子句, ...
- linux-centos系统下安装python3.5.4步骤
查看当前python版本:python -V 查看Python可执行文件位置:which python [root@localhost bin]# which python/usr/bin/pytho ...
- BPM与OA区别
核心差异: OA解决的是流程有无问题 BPM解决的是流程更好更优的问题 主要差异如下: 1.BPM有更好的广度跟深度 这里的广度是指应用场景的广度. BPM一般都会以端到端的方式衔接企业运营过程的上下 ...