1<html>
2<head>
3<title>Calculator</title>
4<script language="vb" runat="server">
5dim c as char
6dim str as string
7dim len as integer
8dim Is2 as Boolean
9dim num1 as double
10dim num2 as double
11dim result as double
12'**************************************************
13Sub btClick(Sender as Object , E as EventArgs)
14If(Sender.Equals(btResult)) Then
15Len = LoadStr.Text.Length()
16If(Len = 0) Then Exit Sub
17Len = LoadStr.Text.Length()
18c = Right(LoadStr.Text,1)
19str = Left(LoadStr.Text,Len-1)
20num1 = Val(str)
21num2 = Val(tbExps.Text)
22result = num2
23Select Case Asc(c)
24Case 43
25result = num1 + num2
26Case 45
27result = num1 - num2
28Case 42
29result = num1 * num2
30Case 47
31If(num2 = 0) Then
32tbExps.Text = "除数不可以为零"
33Exit Sub
34End If
35result = num1/num2
36End Select
37str = CStr(result)
38Ltrim(str)
39tbExps.Text = str
40LoadStr.Text = ""
41End If
42'**************************************************
43If(Sender.Equals(btAdd)) Then
44LoadStr.Text = tbExps.Text + "+"
45tbExps.Text = ""
46End If
47'**************************************************
48If(Sender.Equals(btMin)) Then
49LoadStr.Text = tbExps.Text + "-"
50tbExps.Text = ""
51End If
52'**************************************************
53If(Sender.Equals(btMul)) Then
54LoadStr.Text = tbExps.Text + "*"
55tbExps.Text = ""
56End If
57'**************************************************
58If(Sender.Equals(btDiv)) Then
59LoadStr.Text = tbExps.Text + "/"
60tbExps.Text =""
61End If
62'**************************************************
63If(Sender.Equals(btCls)) Then
64tbExps.Text = ""
65End If
66'**************************************************
67If(Sender.Equals(btBack)) Then
68If(tbExps.Text.Length() <> 0) Then
69str = tbExps.Text
70len = tbExps.Text.Length()
71tbExps.Text = Left(str,len-1)
72End If
73End If
74'**************************************************
75If(Sender.Equals(bt)) Then
76If(Left(tbExps.Text,1) = "-") Then
77str = tbExps.Text
78len = tbExps.Text.Length()
79tbExps.Text = Right(str,len-1)
80Else
81str = "-"
82str += tbExps.Text
83tbExps.Text = str
84End If
85End If
86'**************************************************
87If(Sender.Equals(btDot)) Then
88If(tbExps.Text.Length() <> 0) Then
89If(tbExps.Text.Length() <10) Then
90tbExps.Text += "."
91End If
92End If
93End If
94'**************************************************
95If(Sender.Equals(bt1)) Then
96If(tbExps.Text.Length() <10) Then
97If(tbExps.Text = "0") Then
98tbExps.Text = "1"
99Else
100tbExps.Text += "1"
101End If
102End If
103End If
104'**************************************************
105If(Sender.Equals(bt2)) Then
106If(tbExps.Text.Length() <10) Then
107If(tbExps.Text = "0") Then
108tbExps.Text = "2"
109Else
110tbExps.Text += "2"
111End If
112End If
113End If
114'**************************************************
115If(Sender.Equals(bt3)) Then
116If(tbExps.Text.Length() <10) Then
117If(tbExps.Text = "0") Then
118tbExps.Text = "3"
119Else
120tbExps.Text += "3"
121End If
122End If
123End If
124'**************************************************
125If(Sender.Equals(bt4)) Then
126If(tbExps.Text.Length() <10) Then
127If(tbExps.Text = "0") Then
128tbExps.Text = "4"
129Else
130tbExps.Text += "4"
131End If
132End If
133End If
134'**************************************************
135If(Sender.Equals(bt5)) Then
136If(tbExps.Text.Length() <10) Then
137If(tbExps.Text = "0") Then
138tbExps.Text = "5"
139Else
140tbExps.Text += "5"
141End If
142End If
143End If
144'**************************************************
145If(Sender.Equals(bt6)) Then
146If(tbExps.Text.Length() <10) Then
147If(tbExps.Text = "0") Then
148tbExps.Text = "6"
149Else
150tbExps.Text += "6"
151End If
152End If
153End If
154'**************************************************
155If(Sender.Equals(bt7)) Then
156If(tbExps.Text.Length() <10) Then
157If(tbExps.Text = "0") Then
158tbExps.Text = "7"
159Else
160tbExps.Text += "7"
161End If
162End If
163End If
164'**************************************************
165If(Sender.Equals(bt8)) Then
166If(tbExps.Text.Length() <10) Then
167If(tbExps.Text = "0") Then
168tbExps.Text = "8"
169Else
170tbExps.Text += "8"
171End If
172End If
173End If
174'**************************************************
175If(Sender.Equals(bt9)) Then
176If(tbExps.Text.Length() <10) Then
177If(tbExps.Text = "0") Then
178tbExps.Text = "9"
179Else
180tbExps.Text += "9"
181End If
182End If
183End If
184'**************************************************
185If(Sender.Equals(bt0)) Then
186If(tbExps.Text.Length() <10) Then
187If(tbExps.Text <> "0") Then
188tbExps.Text += "0"
189End If
190End If
191End If
192
193End Sub
194'****************************************************
195Sub Help(Sender as Object, E as EventArgs)
196if(lbHelp.visible) Then
197lbHelp.visible = False
198Else
199lbHelp.visible = True
200End if
201End Sub
202</script>
203</head>
204<body ms_positioning="GridLayout">
205 简单计算器
206<form id="Form1" method="post" runat="server">
207<asp:panel backcolor="#ccffff" bordercolor="#ffcc33" borderstyle="Solid" height="220" id="Pl" runat="server" width="200">
208<hr/>
209<asp:textbox id="tbExps" maxlength="10" runat="server" textalign="Right" width="195"></asp:textbox>
210<hr/>
211<asp:label runat="server" width="5/">
212<asp:button height="25" id="btCls" onclick="btClick" runat="server" text="Clear" width="60"></asp:button>
213<asp:button height="25" id="btBack" onclick="btClick" runat="server" text="Back" width="60"></asp:button>
214<asp:label runat="server" width="25"></asp:label>
215<asp:button height="25" id="btResult" onclick="btClick" runat="server" text="=" width="30"></asp:button>
216<hr/>
217<asp:label runat="server" width="5/">
218<asp:button height="25" id="bt7" onclick="btClick" runat="server" text="7" width="30"></asp:button>
219<asp:button height="25" id="bt8" onclick="btClick" runat="server" text="8" width="30"></asp:button>
220<asp:button height="25" id="bt9" onclick="btClick" runat="server" text="9" width="30"></asp:button>
221<asp:label runat="server" width="50"></asp:label>
222<asp:button height="25" id="btMul" onclick="btClick" runat="server" text="*" width="30"></asp:button>
223<asp:label runat="server" width="5/">
224<asp:button height="25" id="bt4" onclick="btClick" runat="server" text="4" width="30"></asp:button>
225<asp:button height="25" id="bt5" onclick="btClick" runat="server" text="5" width="30"></asp:button>
226<asp:button height="25" id="bt6" onclick="btClick" runat="server" text="6" width="30"></asp:button>
227<asp:label runat="server" width="50"></asp:label>
228<asp:button height="25" id="btDiv" onclick="btClick" runat="server" text="/" width="30"></asp:button>
229<asp:label runat="server" width="5/">
230<asp:button height="25" id="bt1" onclick="btClick" runat="server" text="1" width="30"></asp:button>
231<asp:button height="25" id="bt2" onclick="btClick" runat="server" text="2" width="30"></asp:button>
232<asp:button height="25" id="bt3" onclick="btClick" runat="server" text="3" width="30"></asp:button>
233<asp:label runat="server" width="50"></asp:label>
234<asp:button height="25" id="btAdd" onclick="btClick" runat="server" text="+" width="30"></asp:button>
235<asp:label runat="server" width="5/">
236<asp:button height="25" id="bt0" onclick="btClick" runat="server" text="0" width="30"></asp:button>
237<asp:button height="25" id="bt" onclick="btClick" runat="server" text="-/+" width="30"></asp:button>
238<asp:button height="25" id="btDot" onclick="btClick" runat="server" text="." width="30"></asp:button>
239<asp:label runat="server" width="50"></asp:label>
240<asp:button height="25" id="btMin" onclick="btClick" runat="server" text="-" width="30"></asp:button>
241</asp:label></asp:label></asp:label></asp:label></asp:label></asp:panel>
242<br/>
243<asp:label id="LoadStr" runat="server" visible="False"></asp:label>
244<asp:button id="btHelp" onclick="Help" runat="server" text="使用帮助==>>"></asp:button><br/>
245<asp:label id="lbHelp" runat="server" text="这是一个简单的计算器,实现的功能很简单。可以运算四则运算,包括小数,负数等。输入时最多只能为10位数。谢谢大家的使用,有什么建议可以联系我." visible="False" wrap="True"></asp:label>
246</form>
247</body>
248</html>
刚学ASP.Net,学了个简单的计算器
comments powered by Disqus