C# Windows form制作简易计算器
2020-12-13 15:48
标签:style blog http io color ar os for sp 在经过一段时间的C#学习后,试着做了一个计算器的小工程。 计算器的界面如上图,包含基本的+、-、*、/运算以及1/x运算和清零功能。代码如下: 经过多次运行,解决了诸多bug,例如第一个数为0的拼接问题、小数点的问题、0.0的问题,相信其中应该还存在着bug,若有朋友看到,敬请评论指点。C#新手,没什么高质量,高手勿笑。 C# Windows form制作简易计算器 标签:style blog http io color ar os for sp 原文地址:http://www.cnblogs.com/linyh/p/4077157.html 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Windows.Forms;
10
11 namespace calcDemo_2_
12 {
13 public partial class Form1 : Form
14 {
15 public Form1()
16 {
17 InitializeComponent();
18 }
19 double step1, step2, step3;
20 string str;//定义运算符
21 int i = 0;
22 string numberStr = "";//两个以上的数字拼接
23 private void button0_Click(object sender, EventArgs e)//按钮0
24 {
25 if (double.Parse(textBox1.Text) > 0)
26 {
27
28 numberStr = numberStr + "0";
29 textBox1.Text = numberStr;
30 }
31 else if (textBox1.Text == "0.")
32 {
33 numberStr = numberStr + "0";
34 textBox1.Text = numberStr;
35 }
36 else
37 {
38 textBox1.Text = "0";
39 numberStr = "";
40 }
41 }
42 private void button1_Click(object sender, EventArgs e)//按钮1
43 {
44 numberStr += "1";
45 textBox1.Text = numberStr;
46 }
47 private void button2_Click(object sender, EventArgs e)//按钮2
48 {
49 numberStr = numberStr + "2";
50 textBox1.Text = numberStr;
51 }
52 private void button3_Click(object sender, EventArgs e)//按钮3
53 {
54 numberStr = numberStr + "3";
55 textBox1.Text = numberStr;
56 }
57 private void button4_Click(object sender, EventArgs e)//按钮4
58 {
59 numberStr = numberStr + "4";
60 textBox1.Text = numberStr;
61 }
62
63 private void button5_Click(object sender, EventArgs e)//按钮5
64 {
65 numberStr = numberStr + "5";
66 textBox1.Text = numberStr;
67 }
68
69 private void button6_Click(object sender, EventArgs e)//按钮6
70 {
71 numberStr = numberStr + "6";
72 textBox1.Text = numberStr;
73 }
74
75 private void button7_Click(object sender, EventArgs e)//按钮7
76 {
77 numberStr = numberStr + "7";
78 textBox1.Text = numberStr;
79 }
80
81 private void button8_Click(object sender, EventArgs e)//按钮8
82 {
83 numberStr = numberStr + "8";
84 textBox1.Text = numberStr;
85 }
86
87 private void button9_Click(object sender, EventArgs e)//按钮9
88 {
89 numberStr = numberStr + "9";
90 textBox1.Text = numberStr;
91 }
92 private void buttonPoint_Click(object sender, EventArgs e)//小数点
93 {
94 i = i + 1;
95 if (i == 1 && textBox1.Text.Length != 0)
96 {
97 numberStr = textBox1.Text.ToString();
98 numberStr += ".";
99 }
100 textBox1.Text = numberStr;
101 }
102 private void buttonadd_Click(object sender, EventArgs e)//加法代码
103 {
104 str = "+";
105 if (textBox1.Text.Length != 0)
106 {
107 step1 = double.Parse(textBox1.Text);
108 }
109 else
110 {
111 step1 = 0;
112 }
113 textBox1.Text = "0";
114 numberStr = "";
115 i = 0;//按下+号键后获取第一个数step1;
116 }
117
118 private void buttonsub_Click(object sender, EventArgs e)//减法代码
119 {
120 str = "-";
121 if (textBox1.Text.Length != 0)
122 {
123 step1 = double.Parse(textBox1.Text);
124 }
125 else
126 {
127 step1 = 0;
128 }
129 textBox1.Text = "0";
130 numberStr = "";
131 i = 0;//按下-号键后获取第一个数step1;
132 }
133
134 private void buttonmul_Click(object sender, EventArgs e)//乘法代码
135 {
136 str = "*";
137 if (textBox1.Text.Length != 0)
138 {
139 step1 = double.Parse(textBox1.Text);
140 }
141 else
142 {
143 step1 = 0;
144 }
145 textBox1.Text = "0";
146 numberStr = "";
147 i = 0;//按下*号键后获取第一个数step1;
148 }
149
150 private void buttondiv_Click(object sender, EventArgs e)//除法代码
151 {
152 str = "/";
153 if (textBox1.Text.Length != 0)
154 {
155 step1 = double.Parse(textBox1.Text);
156 }
157 else
158 {
159 step1 = 0;
160 }
161 textBox1.Text = "0";
162 numberStr = "";
163 i = 0;//按下/号键后获取第一个数step1;
164 }
165 ///
上一篇:python3使除法结果为整数
下一篇:Java中小数精度问题