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计算器在哪里?如何打开?针对此问题,本文 ...
随机推荐
- Angular7.1.4+Typescript3.1框架学习(一)
起因:学习ionic4之前先学习下angular+ts 以win10为开发平台:当前最新版本为angular7;根据官网资料做如下总结: 1. angular安装 前提:Node.js 的 8.x 或 ...
- Azkaban工作流调度器
Azkaban工作流调度器 在Hadoop领域常用的工作流调度系统 Oozie,Azkaban,Cascading,Hamake等等. 性能对比: 安装: 创建ssl配置 keytool -keyst ...
- C# Winform 通过Socket实现客户端和服务端TCP通信
操作界面如下: 1.声明Socket 第一个参数:寻址方式,第二个参数:传输数据的方式,第三个参数:通信协议 Socket socket = new Socket(AddressFamily.Inte ...
- java多线程高并发知识总结
1. 计算机系统 使用高速缓存来作为内存与处理器之间的缓冲,将运算需要用到的数据复制到缓存中,让计算能快速进行:当运算结束后再从缓存同步回内存之中,这样处理器就无需等待缓慢的内存读写了. 缓 ...
- 总结get和post区别---面试用
总结get和post区别---面试用 我是搬运工!!!! get参数通过url传递,post放在request body中. get请求在url中传递的参数是有长度限制的,而post没有. get比p ...
- 五、Vi和Vim编辑器
1. Vim编辑器: 在Linux下一般使用vi编辑器来编辑文件.vi既可以查看文件也可以编辑文件.三种模式: 命令行.插入.底行模式 切换到命令行模式:按Esc键: 切换到插入模式:按 i .o.a ...
- C#连接sql server windows 和 sqlserver 身份验证的两种连接字符串
//sql server 身份验证 连接字符串 private string ConnstrSqlServer = "server=服务器名称;uid=登录名称;pwd=登录密码;datab ...
- 使用 spring封装的javamail linux服务器发送邮件失败解决
原文参考:https://blog.csdn.net/a540891049/article/details/79385471 由于某些平台的linxu服务器为了安全起见 屏蔽了发送邮件的常用端口 25 ...
- 活代码LINQ——03
一.主模块代码: 'Fig.4.13:GradeBookTest.vb 'GradeBook constructor used to specify the course name at the 't ...
- 【基于微信小程序的社区电商平台】第一次迭代心得(非正式版本
一.迭代任务 团队在第八周确认迭代计划时,是想要在第一阶段实现电商小程序的核心功能,就是买和卖,也是前端和后台数据交换的核心模块.涉及到首页浏览商品信息,查看商品详情及评论,选择加入购物车.关注卖家以 ...