|
-
Oct 3rd, 2000, 04:23 AM
#1
Thread Starter
New Member
1.
Can I do calculations in text boxes? Suppose the text property of a textbox is "2*4+5" and I want the result in an integer type variable iResult so that iResult = 13. So whenever I call iResult it will give me the integer 13, not the string "2*4+5".
2.
Suppose, this is the code I have
for x = 1 to 10
y = text1.text
print x
next x
Now I will enter "2 * x + 5" in the textbox and the output would be 7,9,11...
-
Oct 3rd, 2000, 05:14 AM
#2
Lively Member
I think you will have to make your own calculator. That last part there with X can be complicated. Anyway, I made this code for you to help you get started.
Code:
Sub Main()
Dim Acc As Double
Dim A(100) As Variant
Dim i As Integer
Dim c As Integer
Dim Calculate As String
Dim curUnit As String
Dim pos As Long
Calculate = "20*3+42/3"
pos = 1
i = 1
'Gets all the components.
While pos < Len(Calculate) + 1
curUnit = getUnit(Calculate, pos)
pos = pos + Len(curUnit)
A(i) = curUnit
i = i + 1
Wend
'The calculator part
Acc = Val(A(1))
For c = 2 To i - 1 Step 2
Select Case A(c)
Case "*": Acc = Acc * Val(A(c + 1))
Case "-": Acc = Acc - Val(A(c + 1))
Case "+": Acc = Acc + Val(A(c + 1))
Case "/": Acc = Acc / Val(A(c + 1))
Case Else: MsgBox "unknown"
End Select
Next c
MsgBox Acc
End Sub
Function isNumber(number As String) As Boolean
If Trim(Str(Val(number))) = number Then isNumber = True
End Function
Function getUnit(Calc As String, pos As Long) As String
getUnit = Mid(Calc, pos, 1)
If isNumber(getUnit) Then
getUnit = getUnit & getNumber(Calc, pos + 1)
End If
End Function
Function getNumber(Calc As String, pos As Long) As String
getNumber = Mid(Calc, pos, 1)
If isNumber(getNumber) Then
getNumber = getNumber & getNumber(Calc, pos + 1)
Else
getNumber = ""
End If
End Function
-
Oct 3rd, 2000, 09:35 AM
#3
Frenzied Member
If you feel that you could follow some C or pascal code and are interested, I have a small calculator program written in both Turbo Pascal and Turbo C. It inputs an equation and displays the result.
It is a very good example of parsing.
If interested, contact me offline.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|