winform 计算器

  1 bool a; //判断上一次按键是加减乘除还是数字
  2         string c = ""; //判断加减乘除操作
  3         decimal f;//记录第一个值以及接收运算结果
  4         /// <summary>
  5         /// 数字按键
  6         /// </summary>
  7         /// <param name="sender"></param>
  8         /// <param name="e"></param>
  9         private void button1_Click(object sender, EventArgs e)
 10         {
 11             Button b = sender as Button;
 12             if (textBox2.Text == "0" || a == false)
 13             {
 14                 textBox2.Text = b.Text;
 15             }
 16             else
 17             {
 18                 textBox2.Text += b.Text;
 19             }
 20             if (textBox1.Text == "")
 21             {
 22                 f = Convert.ToDecimal(textBox2.Text);
 23             }
 24             a = true;
 25         }
 26         /// <summary>
 27         /// 加减乘除
 28         /// </summary>
 29         /// <param name="sender"></param>
 30         /// <param name="e"></param>
 31         private void button4_Click(object sender, EventArgs e)
 32         {
 33             Button b = sender as Button;
 34             if (textBox2.Text.LastIndexOf(".") == (textBox2.Text.Length - 1))
 35             {
 36                 textBox2.Text = textBox2.Text.Substring(0, (textBox2.Text.Length - 1));
 37             }
 38             if (a == false)
 39             {
 40                 textBox1.Text = textBox1.Text.Substring(0, (textBox1.Text.Length - 1)) + b.Text;
 41             }
 42             else
 43             {
 44                 textBox1.Text += textBox2.Text + b.Text;
 45                 if (c == "1")
 46                 {
 47                     f = f + Convert.ToDecimal(textBox2.Text);
 48                 }
 49                 else if (c == "2")
 50                 {
 51                     f = f - Convert.ToDecimal(textBox2.Text);
 52                 }
 53                 else if (c == "3")
 54                 {
 55                     f = f * Convert.ToDecimal(textBox2.Text);
 56                 }
 57                 else if (c == "4")
 58                 {
 59                     f = f / Convert.ToDecimal(textBox2.Text);
 60                 }
 61                 textBox2.Text = f.ToString();
 62             }
 63             if (b.Text == "+")
 64             {
 65                 c = "1";
 66             }
 67             else if (b.Text == "-")
 68             {
 69                 c = "2";
 70             }
 71             else if (b.Text == "*")
 72             {
 73                 c = "3";
 74             }
 75             else if (b.Text == "/")
 76             {
 77                 c = "4";
 78             }
 79             a = false;
 80         }
 81         /// <summary>
 82         /// 点
 83         /// </summary>
 84         /// <param name="sender"></param>
 85         /// <param name="e"></param>
 86         private void button17_Click(object sender, EventArgs e)
 87         {
 88             if (textBox2.Text.Contains(".") == false)
 89             {
 90                 textBox2.Text += ".";
 91             }
 92         }
 93         /// <summary>
 94         /// 等于
 95         /// </summary>
 96         /// <param name="sender"></param>
 97         /// <param name="e"></param>
 98         private void button15_Click(object sender, EventArgs e)
 99         {
100             if (textBox2.Text.LastIndexOf(".") == (textBox2.Text.Length - 1))
101             {
102                 textBox2.Text = textBox2.Text.Substring(0, (textBox2.Text.Length - 1));
103             }
104             if (c == "1")
105             {
106                 f = f + Convert.ToDecimal(textBox2.Text);
107             }
108             else if (c == "2")
109             {
110                 f = f - Convert.ToDecimal(textBox2.Text);
111             }
112             else if (c == "3")
113             {
114                 f = f * Convert.ToDecimal(textBox2.Text);
115             }
116             else if (c == "4")
117             {
118                 f = f / Convert.ToDecimal(textBox2.Text);
119             }
120             textBox1.Text = "";
121             textBox2.Text = f.ToString();
122         }
123         /// <summary>
124         /// 清空
125         /// </summary>
126         /// <param name="sender"></param>
127         /// <param name="e"></param>
128         private void button14_Click(object sender, EventArgs e)
129         {
130             textBox1.Text = "";
131             textBox2.Text = "0";
132             a = true;
133             c = "";
134         }

时间: 2024-08-04 08:44:16

winform 计算器的相关文章

Winform——计算器局部功能(+-*/%)

namespace 计算器2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool Isok = true;//记录刚刚是否点过运算符,或是否第一次进入界面 private string Biaodashi; //记录运算符之前的表达式(在第一栏) private decimal Sum;//记录之前计算的结果(在第二栏) private string Pre

Winform——计算器(大部分功能)

namespace 计算器2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool Isok = true;//记录刚刚是否点过运算符,或是否第一次进入界面 private string Biaodashi; //记录运算符之前的表达式(在第一栏) private decimal Sum;//记录之前计算的结果(在第二栏) private string Pre

Winform——计算器

namespace 计算器2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool Isok = true;//记录刚刚是否点过运算符,或是否第一次进入界面(用来存储判断是否刚刚点过运算符) private string Biaodashi; //记录运算符之前的表达式(在第一栏) private decimal Sum;//记录之前计算的结果(在第二栏) p

winform计算器练习

临近年关,今日在学习的过程中感觉甚是无聊,便想用C#来开发一个简易的计算器程序,这里记录下今日下午的实现过程,同时也记录下自己的第一遍博客. 一,需求 首先我们先来决定我们的计算器要实现什么功能 功能需求:1.能够实现加.减.乘.除.求余等两个操作数的运算,以及开方.平方单个操作数的运算 2.能够清除错误的输入,能够实现清零操作 显示需求:能够显示操作数与运算内容,显示结果 二.设计界面 1.在明白我们的功能需求后,我们来设计界面,界面主要包括三个部分,用于显示的两个textBox,以及数字键B

winform 计算器 两步走

namespace WindowsFormsApplication4 { public partial class Form2 : Form { public Form2() { InitializeComponent(); } //记录是否刚点过运算符 private bool isok = true; //记录结果 private decimal Sum; //记录上一次的运算符 private string PreYunsuanfu; //记录表达式 private string Biao

winform计算器

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 { publi

Winform——计算器进制转换

namespace 进制转换2._0 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public string Shizhuaner(int a, int jinzhi) //十转二 { string jieguo = ""; while (true) { string s = (a % jinzhi).ToString(); //s的值为a%2后的数,因为进制是2. ji

1.C#WinForm基础制作简单计算器

利用c#语言编写简单计算器: 源码如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace 简单计算器 { public partial class Form1 : Form { public For

[原][C#][winForm]分级基金折溢价WinForm网络计算器

分级基金折溢价WinForm网络计算器 通过子/母基金代码,从 [ 东方财富网,天天基金网,新浪 ] 抓取分级基金的子母基金数据(代码,名称,净值,价格), 并计算出子基金(A基金,B基金)以及母基金的溢价率与折价率, 便于投资/投机客从中套利. 数据通过网站获取,使用基金净值,非估值,一般晚间网站会更新当天基金净值,不可用于实时计算. ------ 效果图: ---- 代码: 1.窗体如效果图 2.常用变量与数据抓取代码: 1 namespace fundGetAndCal 2 { 3 pub