服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - 利用WPF窗口程序设计简单计算器

利用WPF窗口程序设计简单计算器

2022-10-14 11:58Venistarc C#

这篇文章主要为大家详细介绍了利用WPF窗口程序设计简单计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文中设计的计算器仅支持单次双目运算,可连续计算。

实验要求:

1、在wpf项目中编程实现一个简单计算器,具体要求如下:
1)实现+,-,*,/运算
2)可以连续进行计算。

效果如图:

利用WPF窗口程序设计简单计算器

*该程序中数字通过点击对应按钮输入,运算符包含四种常用运算,除此之外还有退格以及清空操作,输入以及运算结果在上方文本框内显示

1.首先,该程序中只涉及单次运算,所以我们可以在隐藏文件里声明两个全局变量来相应的保存operation前后两个数(字符串)。

?
1
2
3
string num1 = null; //运算符之前的数
string num2 = null; //运算符之后的数
string ope = null; //运算符

2.每次键入一个位数要判断放在num1里还是num2里。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
private void button1_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button1.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button1.Content;
 textBox.Text = num2;
 }
}

3.键入运算符是对变量ope赋值(因为是单次计算,所以运算符没必要在文本框里显示)

?
1
2
3
4
private void buttonADD_Click(object sender, RoutedEventArgs e)
 {
 ope = "+";
 }

4.CE清空操作,将textbox中的内容以及所有变量清空

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private void buttonCE_Click(object sender, RoutedEventArgs e)
 {
 //if (ope == null)
 //{
 // num1 = textBox.Text;
 //}
 //else
 //{
 // num2 = textBox.Text;
 //}
 this.textBox.Text = "";
 num1 = null;
 num2 = null;
 ope = null;
 }

5.退格操作

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private void buttonBK_Click(object sender, RoutedEventArgs e)
 {
 string s = textBox.Text;
 int len = s.Length;
 if (textBox.Text != null && len >= 1)
 textBox.Text = s.Remove(len - 1);
 if (ope == null)
 {
 num1 = textBox.Text;
 }
 else
 {
 num2 = textBox.Text;
 }
}

6.计算结果(利用switch case )

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void buttonEQ_Click(object sender, RoutedEventArgs e)
 {
 switch (ope)
 {
 case "+":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) + Convert.ToDouble(num2));
 break;
 case "-":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) - Convert.ToDouble(num2));
 break;
 case "*":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) * Convert.ToDouble(num2));
 break;
 case "/":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) / Convert.ToDouble(num2));
 break;
 }
 num1 = textBox.Text;
 num2 = null;
 ope = null;
}

将结果值赋给num1来实现连续计算.

效果如如下:

利用WPF窗口程序设计简单计算器

完整代码如下:

*xaml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Window x:Class="小小计算器.MainWindow"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 Title="计算器" Height="382" Width="362">
 <Grid Height="335">
 <Grid.ColumnDefinitions>
 <ColumnDefinition Width="48*" />
 <ColumnDefinition Width="23*" />
 </Grid.ColumnDefinitions>
 <Button Content="CE" Height="30" HorizontalAlignment="Left" Margin="62,87,0,0" Name="buttonCE" VerticalAlignment="Top" Width="41" Click="buttonCE_Click" />
 <Button Content="<-" Height="29" HorizontalAlignment="Left" Margin="118,87,0,0" Name="buttonBK" VerticalAlignment="Top" Width="45" Click="buttonBK_Click" />
 <Button Content="." Height="29" HorizontalAlignment="Left" Margin="185,87,0,0" Name="buttonDOT" VerticalAlignment="Top" Width="46" Click="buttonDOT_Click" />
 <Button Content="+" Height="28" HorizontalAlignment="Left" Margin="7,87,0,0" Name="buttonADD" VerticalAlignment="Top" Width="47" Click="buttonADD_Click" Grid.Column="1" />
 <Button Content="1" Height="30" HorizontalAlignment="Left" Margin="62,136,0,0" Name="button1" VerticalAlignment="Top" Width="41" Click="button1_Click" />
 <Button Content="2" Height="30" HorizontalAlignment="Left" Margin="118,136,0,0" Name="button2" VerticalAlignment="Top" Width="45" Click="button2_Click" />
 <Button Content="3" Height="30" HorizontalAlignment="Left" Margin="185,136,0,0" Name="button3" VerticalAlignment="Top" Width="46" Click="button3_Click" RenderTransformOrigin="1.217,0.437" />
 <Button Content="-" Height="30" HorizontalAlignment="Left" Margin="7,136,0,0" Name="buttonSUB" VerticalAlignment="Top" Width="47" Click="buttonSUB_Click" Grid.Column="1" />
 <Button Content="4" Height="30" HorizontalAlignment="Left" Margin="62,186,0,0" Name="button4" VerticalAlignment="Top" Width="41" Click="button4_Click" />
 <Button Content="5" Height="30" HorizontalAlignment="Left" Margin="118,186,0,0" Name="button5" VerticalAlignment="Top" Width="45" Click="button5_Click" />
 <Button Content="6" Height="30" HorizontalAlignment="Left" Margin="185,186,0,0" Name="button6" VerticalAlignment="Top" Width="46" Click="button6_Click" />
 <Button Content="*" Height="30" HorizontalAlignment="Left" Margin="7,186,0,0" Name="buttonMUP" VerticalAlignment="Top" Width="47" Click="buttonMUP_Click" Grid.Column="1" />
 <Button Content="7" Height="30" HorizontalAlignment="Left" Margin="62,231,0,0" Name="button7" VerticalAlignment="Top" Width="40" Click="button7_Click" />
 <Button Content="8" Height="30" HorizontalAlignment="Left" Margin="118,231,0,0" Name="button8" VerticalAlignment="Top" Width="45" Click="button8_Click" />
 <Button Content="9" Height="30" HorizontalAlignment="Left" Margin="185,231,0,0" Name="button9" VerticalAlignment="Top" Width="46" Click="button9_Click" />
 <Button Content="/" Height="30" HorizontalAlignment="Left" Margin="7,231,0,0" Name="buttonDIV" VerticalAlignment="Top" Width="47" Click="buttonDIV_Click" Grid.Column="1" />
 <Button Content="0" Height="25" HorizontalAlignment="Left" Margin="62,281,0,0" Name="button0" VerticalAlignment="Top" Width="101" Click="button0_Click" />
 <Button Content="=" Height="25" HorizontalAlignment="Right" Margin="0,281,59.2,0" Name="buttonEQ" VerticalAlignment="Top" Width="111" Grid.ColumnSpan="2" Click="buttonEQ_Click" RenderTransformOrigin="-0.085,0.493" />
 <TextBox Grid.ColumnSpan="2" Height="32" HorizontalAlignment="Left" Margin="88,30,0,0" Name="textBox" VerticalAlignment="Top" Width="173" />
 </Grid>
</Window>

.cs文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
namespace 小小计算器
{
 /// <summary>
 /// MainWindow.xaml 的交互逻辑
 /// </summary>
 public partial class MainWindow : Window
 {
 string num1 = null; //运算符之前的数
 string num2 = null; //运算符之后的数
 string ope = null; //运算符
 public MainWindow()
 {
 InitializeComponent();
 }
 
 private void button1_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button1.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button1.Content;
 textBox.Text = num2;
 }
 }
 
 private void button2_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button2.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button2.Content;
 textBox.Text = num2;
 }
 }
 
 private void button3_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button3.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button3.Content;
 textBox.Text = num2;
 }
 }
 
 private void button4_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button4.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button4.Content;
 textBox.Text = num2;
 }
 }
 
 private void button5_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button5.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button5.Content;
 textBox.Text = num2;
 }
 }
 
 private void button6_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button6.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button6.Content;
 textBox.Text = num2;
 }
 }
 
 private void button7_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button7.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button7.Content;
 textBox.Text = num2;
 }
 }
 
 private void button8_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button8.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button8.Content;
 textBox.Text = num2;
 }
 }
 
 private void button9_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button9.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button9.Content;
 textBox.Text = num2;
 }
 }
 private void button0_Click(object sender, RoutedEventArgs e)
 {
 if (ope == null)
 {
 num1 += button0.Content;
 textBox.Text = num1;
 }
 else
 {
 num2 += button0.Content;
 textBox.Text = num2;
 }
 }
 
 private void buttonADD_Click(object sender, RoutedEventArgs e)
 {
 ope = "+";
 }
 
 private void buttonSUB_Click(object sender, RoutedEventArgs e)
 {
 ope = "-";
 }
 
 private void buttonMUP_Click(object sender, RoutedEventArgs e)
 {
 ope = "*";
 }
 
 private void buttonDIV_Click(object sender, RoutedEventArgs e)
 {
 ope = "/";
 }
 
 private void buttonDOT_Click(object sender, RoutedEventArgs e)
 {
 
 if (ope == null &&!num1.Contains(".")) //如果num中已有小数点,则不允许再输入.
 {
 
 num1 += buttonDOT.Content;
 textBox.Text = num1;
 
 }
 if(ope!=null&&!num2.Contains(".")) 
 {
 num2 += buttonDOT.Content;
 textBox.Text = num2;
 }
 }
 
 private void buttonCE_Click(object sender, RoutedEventArgs e)
 {
 //if (ope == null)
 //{
 // num1 = textBox.Text;
 //}
 //else
 //{
 // num2 = textBox.Text;
 //}
 this.textBox.Text = "";
 num1 = null;
 num2 = null;
 ope = null;
 }
 
 private void buttonBK_Click(object sender, RoutedEventArgs e)
 {
 string s = textBox.Text;
 int len = s.Length;
 if (textBox.Text != null && len >= 1)
 textBox.Text = s.Remove(len - 1);
 if (ope == null)
 {
 num1 = textBox.Text;
 }
 else
 {
 num2 = textBox.Text;
 }
 }
 
 private void buttonEQ_Click(object sender, RoutedEventArgs e)
 {
 switch (ope)
 {
 case "+":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) + Convert.ToDouble(num2));
 break;
 case "-":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) - Convert.ToDouble(num2));
 break;
 case "*":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) * Convert.ToDouble(num2));
 break;
 case "/":
 textBox.Text = Convert.ToString(Convert.ToDouble(num1) / Convert.ToDouble(num2));
 break;
 }
 num1 = textBox.Text;
 num2 = null;
 ope = null;
 }
 
 
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/ninini12386/article/details/109540691

延伸 · 阅读

精彩推荐
  • C#C#比较日期的方法总结

    C#比较日期的方法总结

    在本篇内容中小编给大家整理了关于C#比较日期的方法和相关知识点,有需要的朋友们学习下。...

    C#教程网5392022-07-06
  • C#详解C#批量插入数据到Sqlserver中的四种方式

    详解C#批量插入数据到Sqlserver中的四种方式

    本文主要讲解一下在Sqlserver中批量插入数据。文中大数据批量插入方式一和方式四尽量避免使用,而方式二和方式三都是非常高效的批量插入数据方式,需...

    邹琼俊7882021-12-14
  • C#C# 字符串string和内存流MemoryStream及比特数组byte[]之间相互转换

    C# 字符串string和内存流MemoryStream及比特数组byte[]之间相互转换

    本文主要介绍字符串string和内存流MemoryStream及比特数组byte[]之间相互转换的方法,需要的小伙伴可以参考一下。...

    秦风10652021-11-22
  • C#详细分析c# 运算符重载

    详细分析c# 运算符重载

    这篇文章主要介绍了c# 运算符重载的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...

    菜鸟教程11942022-09-28
  • C#C#Winform窗口移动方法

    C#Winform窗口移动方法

    今天小编就为大家分享一篇C#Winform窗口移动方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    Maybe_ch8052022-03-06
  • C#C#实现文件上传与下载功能实例

    C#实现文件上传与下载功能实例

    本篇文章主要介绍了C#实现文件上传与下载,这里整理了详细的代码,有需要的小伙伴可以参考下。...

    pan_junbiao8242021-12-13
  • C#C#使用LINQ中Enumerable类方法的延迟与立即执行的控制

    C#使用LINQ中Enumerable类方法的延迟与立即执行的控制

    这篇文章主要介绍了C#的LINQ查询中Enumerable类方法的延迟与立即执行,LINQ语言集成查询可以让C#和VB以查询数据库相同的方式操作内存数据,需要的朋友可以参...

    DebugLZQ7802021-11-15
  • C#C#日历样式的下拉式计算器实例讲解

    C#日历样式的下拉式计算器实例讲解

    如果我们正在做一个类似于库存控制和计费系统的项目,有些部分可能必须手动计算数值。因此,用户就不得不使用计算器得到结果,再填入到输入字段中...

    C#教程网4122021-10-27