File: TestCalc.vbs

package info (click to toggle)
libbsf-java 1%3A2.3.0%2Bcvs20050308a-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,460 kB
  • ctags: 1,264
  • sloc: java: 7,643; cpp: 2,514; xml: 1,773; jsp: 463; ansic: 182; makefile: 61; python: 45; sh: 29
file content (63 lines) | stat: -rw-r--r-- 1,693 bytes parent folder | download | duplicates (2)
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
'**********************************************************
'* A simple four function calculator, written in VBScript *
'**********************************************************

' *** create a result window
Set result = createBean("java.awt.TextField", "0")

' *** create a panel of buttons
Set panel = createBean("java.awt.Panel")
panel.setLayout createBean("java.awt.GridLayout", 4, 4)

buttons = "789*456/123-C0.+"
For i = 1 To len(buttons)
   label = mid(buttons,i,1)
   Set button = createBean("java.awt.Button", label)
   panel.add button

   If InStr("*/-+", label) Then
     button.onAction="op """ & label & """"
   Elseif label="C" Then
     button.onAction="mem=0 : nextOp=""+"" : result.text=""0"""
   Else 
     button.onAction="press """ & label & """"
   End If
Next 

' *** Place everything in the frame
frame.title = "VBScript Calc"
frame.resize 130, 200
frame.add "North", result 
frame.add "Center", panel 
frame.validate

' *** Initialize the state of the calculator
mem = 0
nextOp = "+"
autoClear = True

' *** handle data entry keys
Sub press (key)
   On Error Resume Next
   If autoClear Then result.text="0"
   If result.text="0" and key<>"." Then result.text = ""
   If key="." and InStr(result.text,".") Then key=""
   result.text = result.text & key
   autoClear=False
End sub

' *** handle arithmetic keys
Sub op (key)
   err=0
   On Error Resume Next
   num = result.text+0
   If nextOp = "+" Then mem = mem + num
   If nextOp = "-" Then mem = mem - num
   If nextOp = "*" Then mem = mem * num
   If nextOp = "/" Then mem = mem / num
   nextOp = key

   result.text = "" & mem
   If err>0 then result.text="ERROR" : mem=0 : nextOp="+"
   autoClear=True
End sub