WindowsForm 计算器
计算器
可以分一下类
数字键
+ - * / %
=和撤销
归零C最简单
下面看一下计算器具体的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApplication4
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} //记录是否刚点过运算符
private bool isok = true;
//记录结果
private decimal Sum;
//记录上一次的运算符
private string PreYunsuanfu;
//记录表达式
private string Biaodashi; private void btnling_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
// isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnyi_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btner_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnsan_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnsi_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnwu_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnliu_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnqi_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnba_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnjiu_Click(object sender, EventArgs e)
{
if (isok)
{
txtshuru.Text = "";
isok = false;
}
else
{
txtshuru.Text += "";
}
} private void btnjia_Click(object sender, EventArgs e)
{
if (PreYunsuanfu == null)
{
Sum = decimal.Parse(txtshuru.Text);
Biaodashi = txtshuru.Text;
}
else
{
if (!isok)
{
if (PreYunsuanfu == "+")
{
Sum = Sum + decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "-")
{
Sum = Sum - decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "*")
{
Sum = Sum * decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "/")
{
Sum = Sum / decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "%")
{
Sum = Sum % decimal.Parse(txtshuru.Text);
} Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
}
} txtjisuanshi.Text = Biaodashi + "+";
PreYunsuanfu = "+";
isok = true;
txtshuru.Text = Sum.ToString();
} private void btnjian_Click(object sender, EventArgs e)
{
if (PreYunsuanfu == null)
{
Sum = decimal.Parse(txtshuru.Text);
Biaodashi = txtshuru.Text;
}
else
{
if (!isok)
{
if (PreYunsuanfu == "+")
{
Sum = Sum + decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "-")
{
Sum = Sum - decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "*")
{
Sum = Sum * decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "/")
{
Sum = Sum / decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "%")
{
Sum = Sum % decimal.Parse(txtshuru.Text);
} Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
}
} txtjisuanshi.Text = Biaodashi + "-";
PreYunsuanfu = "-";
isok = true;
txtshuru.Text = Sum.ToString();
} private void btncheng_Click(object sender, EventArgs e)
{
if (PreYunsuanfu == null)
{
Sum = decimal.Parse(txtshuru.Text);
Biaodashi = txtshuru.Text;
}
else
{
if (!isok)
{
if (PreYunsuanfu == "+")
{
Sum = Sum + decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "-")
{
Sum = Sum - decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "*")
{
Sum = Sum * decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "/")
{
Sum = Sum / decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "%")
{
Sum = Sum % decimal.Parse(txtshuru.Text);
} Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
}
} txtjisuanshi.Text = Biaodashi + "*";
PreYunsuanfu = "*";
isok = true;
txtshuru.Text = Sum.ToString();
} private void btnchu_Click(object sender, EventArgs e)
{
if (PreYunsuanfu == null)
{
Sum = decimal.Parse(txtshuru.Text);
Biaodashi = txtshuru.Text;
}
else
{
if (!isok)
{
if (PreYunsuanfu == "+")
{
Sum = Sum + decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "-")
{
Sum = Sum - decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "*")
{
Sum = Sum * decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "/")
{
Sum = Sum / decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "%")
{
Sum = Sum % decimal.Parse(txtshuru.Text);
} Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
}
} txtjisuanshi.Text = Biaodashi + "/";
PreYunsuanfu = "/";
isok = true;
txtshuru.Text = Sum.ToString();
} private void btnmo_Click(object sender, EventArgs e)
{
if (PreYunsuanfu == null)
{
Sum = decimal.Parse(txtshuru.Text);
Biaodashi = txtshuru.Text;
}
else
{
if (!isok)
{
if (PreYunsuanfu == "+")
{
Sum = Sum + decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "-")
{
Sum = Sum - decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "*")
{
Sum = Sum * decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "/")
{
Sum = Sum / decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "%")
{
Sum = Sum % decimal.Parse(txtshuru.Text);
} Biaodashi = Biaodashi + PreYunsuanfu + txtshuru.Text;
}
} txtjisuanshi.Text = Biaodashi + "%";
PreYunsuanfu = "%";
isok = true;
txtshuru.Text = Sum.ToString();
} private void btnClear_Click(object sender, EventArgs e)
{
txtshuru.Text = "";
txtjisuanshi.Text = "";
isok = true;
PreYunsuanfu = null;
Sum = ;
Biaodashi = "";
} private void btnchexiao_Click(object sender, EventArgs e)
{
if (!isok)
{
if (txtshuru.Text.Length == )
{
txtshuru.Text = "";
isok = true;
}
else
{
txtshuru.Text = txtshuru.Text.Substring(, txtshuru.Text.Length - );
}
}
} private void btndian_Click(object sender, EventArgs e)
{
if (txtshuru.Text == "")
{
txtshuru.Text = "0.";
}
if (!isok && !txtshuru.Text.Contains("."))
{
txtshuru.Text += ".";
}
isok = false;
} private void btndeng_Click(object sender, EventArgs e)
{
txtjisuanshi.Text = "";
if (PreYunsuanfu == "+")
{
Sum = Sum + decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "-")
{
Sum = Sum - decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "*")
{
Sum = Sum * decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "/")
{
Sum = Sum / decimal.Parse(txtshuru.Text);
}
if (PreYunsuanfu == "%")
{
Sum = Sum % decimal.Parse(txtshuru.Text);
}
txtshuru.Text = Sum.ToString();
isok = true;
Biaodashi = "";
Sum = ;
PreYunsuanfu = null;
} private void btnzhengfu_Click(object sender, EventArgs e)
{
if (!isok)
{
if (decimal.Parse(txtshuru.Text) > )
{
txtshuru.Text = "-" + txtshuru.Text;
}
else
{
txtshuru.Text = txtshuru.Text.Substring();
}
}
} }
}
计算器 很详细的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace FirstForm
{
public partial class JiSuanQi : Form
{
public JiSuanQi()
{
InitializeComponent();
} private double sum=;
private string biaodashi;
private string Preyunsuanfu;//存放之前的运算符
private bool isok = true;//用来判断是否刚刚点过运算符 private void btnling_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (isok)
{
txtshuru.Text = btn.Text;
isok = false;
}
else
{
txtshuru.Text += btn.Text;
} } private void btnjia_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
string yunsuanfu = btn.Text;
if (Preyunsuanfu == null)
{
biaodashi += txtshuru.Text;
sum = double.Parse(txtshuru.Text);
}
else
{
if (!isok)
{
if (Preyunsuanfu == "+")
{
sum += double.Parse(txtshuru.Text);
}
if (Preyunsuanfu == "-")
{
sum -= double.Parse(txtshuru.Text);
}
if (Preyunsuanfu == "*")
{
sum *= double.Parse(txtshuru.Text);
}
if (Preyunsuanfu == "/")
{
sum /= double.Parse(txtshuru.Text);
}
if (Preyunsuanfu == "%")
{
sum %= double.Parse(txtshuru.Text);
} biaodashi = biaodashi+ Preyunsuanfu + txtshuru.Text;
txtshuru.Text = sum.ToString();
}
}
txtjisuanshi.Text = biaodashi + yunsuanfu;
Preyunsuanfu = yunsuanfu;
isok = true; } private void btndeng_Click(object sender, EventArgs e)
{ } private void btnchexiao_Click(object sender, EventArgs e)
{ }
}
}
计算器 另一种方法
WindowsForm 计算器的更多相关文章
- C#中WindowsForm常见控件的运用
C#中WindowsForm常见控件的运用 -- 1.button(曹操,贡天子以令不臣): 属性;text:我们经常可以看见将按钮命名为“登入”,在其属性面板里面编辑text即可:如下图: ...
- 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 ...
随机推荐
- Linux下 输入 env 而得到的环境变量解读
HOSTNAME=Master.Hadoop MAHOUT_HOME=/usr/hadoop/mahout-distribution-0.8 TERM=linux SHELL=/bin/bash HA ...
- W - Bitset(第二季水)
Description Give you a number on base ten,you should output it on base two.(0 < n < 1000) ...
- E - The King
Description Once upon a time in a country far away lived a king and he had a big kingdom. He was a v ...
- 【搜索引擎Jediael开发笔记】v0.1完整代码
详细代码请见 E:\Project\[重要]归档代码\SearchEngine归档代码 或 https://code.csdn.net/jediael_lu/jediael/tree/10991c83 ...
- Python的tkinter和tkinter.messagebox应用-鼠标和键盘命令绑定
__author__ = 'Administrator' from tkinter import * import tkinter.messagebox class MainWindow: def b ...
- Sql Server专题一:索引(中)
写在前面的废话: 索引这个知识点,我前前后后不知道看了多少边,网上的文章五花八门,搞的我晕头转向,搞的牛逼点的就是测试索引带来的好处,还搞一大堆的测试数据出来,有意思吗?MS自己不会测试吗?这样的测试 ...
- jsp 2种include标签的区别
众所周知,jsp中有2种标签用于包含其他jsp或者文件 1.include指令,其实是java代码 <%@ include file="xxx.jsp"%> 2.jsp ...
- USB HID复合设备实例—键盘+鼠标
实现这种USB HID复合设备有两种方法,在<USB HID协议入门>一节已经讲到其中一种方法,说一个USB HID设备可以包含多种功能的报告描述符合集,这样可以实现复合设备,如带鼠标功能 ...
- Delphi HTML5 Canvas组件
最近去sourceforge瞎转悠,突然发了一个组件,关于Delphi下Html5的canvas的组件,大概浏览了一下源码,竟然是纯粹的Pascal代码,也就说完全的Delphi代码.不敢独享,现在上 ...
- 自己封装的一个简易的二维表类SimpleTable
在QT中,QTableWidget处理二维表格的功能很强大(QTableView更强大),但有时我们只想让它显示少量数据(文字和图片),这时,使用QTableWidget就有点不方便了(个人感觉).所 ...