1、windows7系统自带计算器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 计算器2 public partial class Form1 : Form private double fNum = 0;/输入的前一个数数字表示 private double lNum = 0
2、;/输入的后一个数 private string sfNum = null;/输入的前一个数字符表示 private string slNum = null;/输入的后一个数 private char Sym ;/确定运算的符号+- private bool ord = false;/确定是否按了+-*/若真就表示按了 private int CountNum = 0;/确定输入数的个数 private int e1 = 0;/0表示没按等号,1表示按了等号 private bool cequ = false;/表示是否是连等于 private string stemp;/用于存缓存的数值/字
3、符串 private double temp; private string strmain;/用于存储输入的所有数,在第一行显示 private string fuzhan;/用于存储选项卡中复制粘贴的数 public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) textBox1.Text = 0; private void yunsuan()/四则运算 fNum = double.Parse(sfNum); lNum = double.Parse(slNum); swi
4、tch (Sym) case a: fNum += lNum; break; case r: fNum -= lNum; break; case m: fNum *= lNum; break; case d: fNum /= lNum; break; sfNum = fNum.ToString(); slNum = null; private void lianyunsuan()/连续运算 if (CountNum 0) yunsuan(); textBox1.Text = sfNum; else CountNum+; private void btn62_Click(object sende
5、r, EventArgs e)/小数点 ord = false; cequ = true; if (e1 = 1)/前面按了等号第一个数归0 strmain = null; sfNum = null; e1 = 0; strmain += .; if (CountNum = 0) if (sfNum = null) sfNum += 0.; else sfNum += .; textBox1.Text = sfNum; else if (slNum = null) slNum += 0.; else slNum += .; textBox1.Text = slNum; private void
6、 btn61_Click(object sender, EventArgs e)/数字0 ord = false; cequ = true; if (e1 = 1) strmain = null; sfNum = null; e1 = 0; strmain += 0; if (CountNum = 0 & sfNum != 0) sfNum += 0; textBox1.Text = sfNum; else if(slNum != 0) slNum += 0; textBox1.Text = slNum; private void btn51_Click(object sender, Even
7、tArgs e)/数字1 ord = false; cequ = true; if (e1 = 1) strmain = null; sfNum = null; e1 = 0; strmain += 1; if (CountNum = 0) if (sfNum != 0) sfNum += 1; else sfNum = 1; textBox1.Text = sfNum; else if (slNum != 0) slNum += 1; else slNum = 1; textBox1.Text = slNum; private void btn52_Click(object sender,
8、EventArgs e)/数字2 ord = false; cequ = true; if (e1 = 1) strmain = null; sfNum = null; e1 = 0; strmain += 2; if (CountNum = 0) if (sfNum != 0) sfNum += 2; else sfNum = 2; textBox1.Text = sfNum; else if (slNum != 0) slNum += 2; else slNum = 2; textBox1.Text = slNum; private void btn53_Click(object send
9、er, EventArgs e)/数字3 ord = false; cequ = true; if (e1 = 1) strmain = null; sfNum = null; e1 = 0; strmain += 3; if (CountNum = 0) if (sfNum != 0) sfNum += 3; else sfNum = 3; textBox1.Text = sfNum; else if (slNum != 0) slNum += 3; else slNum = 3; textBox1.Text = slNum; private void btn41_Click(object
10、sender, EventArgs e)/数字4 ord = false; cequ = true; if (e1 = 1) strmain = null; sfNum = null; e1 = 0; strmain += 4; if (CountNum = 0) if (sfNum != 0) sfNum += 4; else sfNum = 4; textBox1.Text = sfNum; else if (slNum != 0) slNum += 4; else slNum = 4; textBox1.Text = slNum; private void btn42_Click(obj
11、ect sender, EventArgs e)/数字5 ord = false; cequ = true; if (e1 = 1) strmain = null; sfNum = null; e1 = 0; strmain += 5; if (CountNum = 0) if (sfNum != 0) sfNum += 5; else sfNum = 5; textBox1.Text = sfNum; else if (slNum != 0) slNum += 5; else slNum = 5; textBox1.Text = slNum; private void btn43_Click
12、(object sender, EventArgs e)/数字6 ord = false; cequ = true; if (e1 = 1) strmain = null; sfNum = null; e1 = 0; strmain += 6; if (CountNum = 0) if (sfNum != 0) sfNum += 6; else sfNum = 6; textBox1.Text = sfNum; else if (slNum != 0) slNum += 6; else slNum = 6; textBox1.Text = slNum; private void btn31_C
13、lick(object sender, EventArgs e)/数字7 ord = false; cequ = true; if (e1 = 1) strmain = null; sfNum = null; e1 = 0; strmain += 7; if (CountNum = 0) if (sfNum != 0) sfNum += 7; else sfNum = 7; textBox1.Text = sfNum; else if (slNum != 0) slNum += 7; else slNum = 7; textBox1.Text = slNum; private void btn
14、32_Click(object sender, EventArgs e)/数字8 ord = false; cequ = true; if (e1 = 1) strmain = null; sfNum = null; e1 = 0; strmain += 8; if (CountNum = 0) if (sfNum != 0) sfNum += 8; else sfNum = 8; textBox1.Text = sfNum; else if (slNum != 0) slNum += 8; else slNum = 8; textBox1.Text = slNum; private void
15、 btn33_Click(object sender, EventArgs e)/数字9 ord = false; cequ = true; if (e1 = 1) strmain = null; sfNum = null; e1 = 0; strmain += 9; if (CountNum = 0) if (sfNum != 0) sfNum += 9; else sfNum = 9; textBox1.Text = sfNum; else if (slNum != 0) slNum += 9; else slNum = 9; textBox1.Text = slNum; private
16、void btn63_Click(object sender, EventArgs e)/加法 if (ord) strmain = strmain.Substring(0, strmain.Length - 1); strmain += +; textBox2.Text = strmain; else strmain += +; textBox2.Text = strmain; ord = true; lianyunsuan(); cequ = true; e1 = 0; textBox1.Text = null; Sym = a; private void btn54_Click(obje
17、ct sender, EventArgs e)/减法 if (ord) strmain = strmain.Substring(0, strmain.Length - 1); strmain += -; textBox2.Text = strmain; else strmain += -; textBox2.Text = strmain; ord = true; lianyunsuan(); cequ = true; e1 = 0; textBox1.Text = null; Sym = r; private void btn44_Click(object sender, EventArgs
18、e)/乘法 if (ord) strmain = strmain.Substring(0, strmain.Length - 1); strmain += *; textBox2.Text = strmain; else strmain += *; textBox2.Text = strmain; ord = true; lianyunsuan(); cequ = true; e1 = 0; textBox1.Text = null; Sym = m; private void btn34_Click(object sender, EventArgs e)/除法 if (ord) strmai
19、n = strmain.Substring(0, strmain.Length - 1); strmain += /; textBox2.Text = strmain; else strmain += /; textBox2.Text = strmain; ord = true; lianyunsuan(); cequ = true; e1 = 0; textBox1.Text = null; Sym = d; private void btn64_Click(object sender, EventArgs e)/等于号 ord = false; textBox2.Text = null;
20、e1 = 1; if (cequ) if (Sym = d & slNum = 0) textBox1.Text = 除数不能为0; else yunsuan(); CountNum = 0; textBox1.Text = sfNum; cequ = false; else switch (Sym) case a: fNum += lNum; break; case r: fNum -= lNum; break; case m: fNum *= lNum; break; case d: fNum /= lNum; break; sfNum = fNum.ToString(); textBox
21、1.Text = sfNum; strmain = sfNum; private void btn45_Click(object sender, EventArgs e)/求倒数 if (CountNum = 0) fNum = double.Parse(sfNum); fNum = 1 / fNum; sfNum = fNum.ToString(); textBox1.Text = sfNum; else lNum = double.Parse(slNum); lNum = 1 / lNum; slNum = lNum.ToString(); textBox1.Text = slNum; p
22、rivate void btn35_Click(object sender, EventArgs e)/输入百分数并输出结果 if (CountNum = 0) fNum = double.Parse(sfNum); fNum /= 100; sfNum = fNum.ToString(); textBox1.Text = sfNum; else lNum = double.Parse(slNum); lNum /= 100; slNum = lNum.ToString(); textBox1.Text = slNum; private void btn25_Click(object send
23、er, EventArgs e)/根号 if (CountNum = 0) textBox2.Text = sqrt( + sfNum + ); fNum = double.Parse(sfNum); fNum = System.Math.Sqrt(fNum); sfNum = fNum.ToString(); textBox1.Text = sfNum; else textBox2.Text = sqrt( + slNum + ); lNum = double.Parse(slNum); lNum = System.Math.Sqrt(lNum); slNum = lNum.ToString
24、(); textBox1.Text = slNum; private void btn23_Click(object sender, EventArgs e)/清除 CountNum = 0; e1 = 0; strmain = null; sfNum = null; slNum = null; textBox1.Text = null; textBox2.Text = null; private void btn22_Click(object sender, EventArgs e)/ 归零 if (CountNum = 0) sfNum = null; textBox1.Text = sf
25、Num; else slNum = null; textBox1.Text = slNum; private void btn21_Click(object sender, EventArgs e)/删除当前数的一位一位 if (CountNum = 0) if (sfNum.Length != null) sfNum = sfNum.Substring(0, sfNum.Length - 1); textBox1.Text = sfNum; else if (slNum.Length != null) slNum = slNum.Substring(0, slNum.Length - 1);
26、 textBox1.Text = slNum; private void btn24_Click(object sender, EventArgs e)/改变数的正负号 if (CountNum = 0) fNum = double.Parse(sfNum); fNum = 0 - fNum; sfNum = fNum.ToString(); textBox1.Text = sfNum; else lNum = double.Parse(slNum); lNum = 0 - lNum; slNum = lNum.ToString(); textBox1.Text = slNum; private void btn13_Click(object sender, EventArgs e)/将当前数存入记忆缓存 label1.Text = M; if (CountNum = 0) stemp = sfNum; else stemp = slNum; private void btn12_Click(object sender, EventArgs e)/使用记忆缓存 if (CountNum = 0) sfNum = stemp; textBox1.Text = sfNum; else slNum = stemp; textBox1.Text =
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1