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中curl命令
linux curl是一个利用URL规则在命令行下工作的文件传输工具.它支持文件的上传和下载,所以是综合传输工具,但按传统,习惯称url为下载工具. 一,curl命令参数,有好多我没有用过,也不知道翻 ...
- oracle 数据库学习
1.更改数据库用户名密码: alter user '用户名' identified by ’password'. 2.查看数据库有哪些用户:sqlplus system/password ;selec ...
- 查询sybase DB中占用空间最多的前20张表
按照数据行数查询 name, row_count(db_id(), id) from sysobjects order by row_count(db_id(),id) desc 按照分配的空间查询 ...
- What is SignalR and Why Should I Use It?
原文地址: What is SignalR and why should I use it? When I first heard about SignalR, I was not sure what ...
- js Array 学习
摘抄自<JavaScript权威指南> 1.join()返回生成的字符串.数组元素转化为字符串,拼接.默认分隔符:“,”. 2.reverse()返回逆序的数组,替换.数组中的元素倒序. ...
- Building Android Apps 30条建议
Building Android Apps — 30 things that experience made me learn the hard way There are two kinds of ...
- php 函数 将数组转换成标量变量:extract()
格式 extract( array var_array [, int extract_type] [,string prefix] ); 例子 $array4 = array('key1'=>' ...
- Android系统信息
前提:获取的都是AndroidMainfest.xml下的信息 一.PackageManager 负责管理所有已安装的App 二.ActivityInfo 封装了Mainifest中的<acti ...
- (2012年旧文)纪念史蒂夫乔布斯---IT界的普罗米修斯
谈苹果与乔布斯系列一 IT界的普罗米修斯 纪念PC界的先驱 史蒂夫乔布斯 2012-4-5 清明节,纪念IT时代的开创人—伟大的史蒂夫 乔布斯. 没有乔布斯,计算机还是属于一群科技人士的工具,没有漂 ...
- Leetcode算法刷题:217和219题 Contains Duplicate
从题目名字就可以看出这两道题是相似的,219是217的加强版 217:Contains Duplicate 题目 给予一个数组,判断是否有重复的元素.如果有就返回True,没有就返回False.以下是 ...