-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
149 lines (132 loc) · 4.23 KB
/
Form1.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button17_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text = textBox1.Text + " " + btn.Text + "";
}
private void btn1_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void btn2_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void btn3_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void btn4_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void btn5_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void btn6_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void btn7_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void btn8_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void btn9_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void btn0_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text += btn.Text;
}
private void btnAdd_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text = textBox1.Text + " " + btn.Text + " ";
}
private void btnSub_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text = textBox1.Text + " " + btn.Text + " ";
}
private void btnMul_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
textBox1.Text = textBox1.Text + " " + btn.Text + " ";
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
private void btnCal_Click(object sender, EventArgs e)
{
Single r;//用于保存计算结果
string t = textBox1.Text;//t用于保存文本框中的算数表达式
int space = t.IndexOf(' ');//用于搜索空格位置
string s1 = t.Substring(0, space);//s1用于保存第一个运算数
char op = Convert.ToChar(t.Substring(space + 1, 1));
//op 用于保存运算符
string s2 = t.Substring(space + 3);//s2用于保存第二个运算数
Single arg1 = Convert.ToSingle(s1);
//将运算数从string 转换为Single
Single arg2 = Convert.ToSingle(s2);
switch (op)
{
case '+':
r = arg1 + arg2;
break;
case '-':
r = arg1 - arg2;
break;
case '*':
r = arg1 * arg2;
break;
case '/':
if (arg2 == 0)
{
throw new ApplicationException();
}
else
{
r = arg1 / arg2;
break;
}
break;
default:
throw new ApplicationException();
}
textBox1.Text = r.ToString();
}
}
}