代码没有大的问题,但是起初点击控件无反应,原因是事件代码要自己敲,不能直接粘贴。

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. 1.C#WinForm基础制作简单计算器

    利用c#语言编写简单计算器: 核心知识点: MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex));//下拉序号 MessageBox.S ...

  2. 自己动手写计算器v1.1

    这个改动主要是使用工厂模式替代了简单工厂模式,这样做的好处是如果以后我们要扩充其他运算时,就不用总是去修改工厂类, 这是可以采取工厂模式,主要是将原来简单工厂类的逻辑判断分离出来,将它作为一个借口,与 ...

  3. 自己动手写计算器v1.0

    今天突发奇想,想着看了还几个设计模式了,倒不如写点东西来实践它们.发现计算器这种就比较合适,打算随着设计模式的学习,会对计算器不断的做改进. 包括功能的增加和算法的改进.初学者难免犯错,希望大家不吝指 ...

  4. 【IOS开发笔记03-视图相关】简单计算器的实现

    UIView 经过前几天的快速学习,我们初步了解的IOS开发的一些知识,中间因为拉的太急,忽略了很多基础知识点,这些知识点单独拿出来学习太过枯燥,我们在今后的项目中再逐步补齐,今天我们来学习APP视图 ...

  5. [LeetCode] Basic Calculator 基本计算器

    Implement a basic calculator to evaluate a simple expression string. The expression string may conta ...

  6. JS-自制提速小工具:开发页面时需要按比例计算宽高值的快速计算器

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <meta name= ...

  7. 由ArcMap属性字段自增引出字段计算器使用Python的技巧

    1.前言       前些日子有人问我ArcMap中要让某个字段的值实现自增有什么方法?我首先想到像SQL Server中对于数值型字段可以设置自增.所以我打开ArcCatalog查看发现只提供默认值 ...

  8. 前端学PHP之面向对象系列第六篇——简单图形面积计算器实现

    前面的话 本文用面向对象的技术来实现一个简单的图形面积计算器 图形类 //rect.class.php <?php abstract class Shape{ public $name; abs ...

  9. tn文本分析语言(四) 实现自然语言计算器

    tn是desert和tan共同开发的一种用于匹配,转写和抽取文本的语言.解释器使用Python实现,代码不超过1000行. github地址:https://github.com/ferventdes ...

  10. Win10计算器在哪里?三种可以打开Win10计算器的方法图文介绍

    全新的windows10系统带来了不少新的特性和改变,其中win10的计算器位置就发生了很多的变化,导致很多网友们都以为win10计算器不见了,那么,win10计算器在哪里?如何打开?针对此问题,本文 ...

随机推荐

  1. MySQL 存储过程参数

    MySQL  存储过程参数 MySQL存储过程参数简介 在现实应用中,开发的存储过程几乎都需要参数.这些参数使存储过程更加灵活和有用. 在MySQL中,参数有三种模式:IN,OUT或INOUT. IN ...

  2. 第 10 章 容器监控 - 080 - Weave Scope 容器地图

    Weave Scope 容器地图 Weave Scope 的最大特点是会自动生成一张 Docker 容器地图,让我们能够直观地理解.监控和控制容器. 安装 执行如下脚本安装运行 Weave Scope ...

  3. python ssh登录linux 上传和下载文件

    #!usr/bin/python# coding: utf-8 import paramikoimport jsonremotedir='/tmp/log'remotefile = 'bst_mana ...

  4. Spring Boot 配置加载顺序详解

    使用 Spring Boot 会涉及到各种各样的配置,如开发.测试.线上就至少 3 套配置信息了.Spring Boot 可以轻松的帮助我们使用相同的代码就能使开发.测试.线上环境使用不同的配置. 在 ...

  5. Netty官网首页(翻译)

    官网:https://netty.io/ Netty是一个异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端. Netty是一个NIO客户端服务器框架,可以快速轻松地开发协议 ...

  6. Kotlin 随笔小计

    最近准备学Kotlin 现在Kotlin也能支持IOS开发了,准备后面买个Mac也能进行IOS开发 当然目标还是看着能不能把一些小的Android项目重构下 也算是定个目标吧,由于沉迷吃鸡,日志都没怎 ...

  7. pycharm配置QtDesigner

    QtDesigner C:\Qt\Qt5.12.2\5.12.2\mingw73_64\bin\designer.exe $ProjectFileDir$ Pyuic C:\Anaconda3\pyt ...

  8. 使用XStream解析复杂XML并插入数据库(一)

    环境: Springboot+mysql 我只想说jpa真的超级好用,准备深入研究一下~ 导入依赖: <dependency> <groupId>org.projectlomb ...

  9. java知识随笔

    Servlet: void init(ServletConfig var1) throws ServletException; ServletConfig getServletConfig(); vo ...

  10. EF中关于日期字值的处理

    一.SQL语句方式 var datefrom = DateTime.Parse(fromdate);   var dateto = DateTime.Parse(todate); var sql = ...