|
-
Mar 14th, 2006, 10:36 AM
#1
Thread Starter
New Member
-
Mar 15th, 2006, 03:30 PM
#2
Hyperactive Member
Re: need help!!!
Well.... maybe you will need some stuff to make the code better and error-proof.
Hope i understood what you wanted, if not just tell me.
VB Code:
Private Sub cmdTest_Click()
Dim X
Dim Y
Dim equalChar As Integer
equalChar = InStr(1, Text1, "=")
If equalChar = 0 Then
MsgBox "No '=' was found on your entry." '
Exit Sub
End If
X = Left(Text1, equalChar - 1)
Y = Right(Text1, Len(Text1) - equalChar)
If Y = X Then
MsgBox "TRUE"
Else
MsgBox "FALSE"
End If
End Sub
-
Mar 15th, 2006, 09:34 PM
#3
Addicted Member
Re: need help!!!
Im also from philippines... from what school are you... i dont know if i still have my case studies on my pc... as i remember i have a case study like yours...
is your input is from 1 textbox only?
me i used split() function for validating if it is invalid or not
-
Mar 15th, 2006, 09:51 PM
#4
Fanatic Member
Re: need help!!!
<-------- is also a filipino... welll, some reunion huh?
sori but i dont really understand ur problem... have u tried to do some coding with this?
remember that this forums doesnt give you the codes...it helps you solve your roblem with them so you've got to start somewhere b4 we can help u...
WARNING: Excessive coding is dangerous to your health... if symptoms persist insult your doctor...
-
Mar 15th, 2006, 11:50 PM
#5
Addicted Member
Re: need help!!!
i will not actually giving her my case study... i just want to know which school she came from...
We might have same school...
and another thing... i can't give my case study to her because mine is in c language not in vb
Last edited by o0yuna0o; Mar 15th, 2006 at 11:56 PM.
-
Mar 16th, 2006, 03:36 AM
#6
Thread Starter
New Member
Re: need help!!!
thanks guys!!!
Kanbei's code was somewhat similar with my codes but my professor said it still need improvement. The program should allow the user to enter an if condition in 1 textbox only. The program should accept recursive or nested condition like for example:
(x+y=o) || (x/y=p) or it can also accept < > symbols like (x>y) && (y=z).
I really need this by tomorrow...so guys im begging you..please help me!
...by the way, im also a filipino.
-
Mar 16th, 2006, 03:38 AM
#7
Re: need help!!!
Pinoy din ako...
Ano ba problema?
-
Mar 16th, 2006, 03:40 AM
#8
Addicted Member
Re: need help!!!
am i allowed to speak filipino language here... so i can explain it well to glyz???
-
Mar 16th, 2006, 03:43 AM
#9
Re: need help!!!
Sure, but we'd prefer it if you also provided an English translation to assist non-Filipino speakers who may have the same problem
-
Mar 16th, 2006, 03:43 AM
#10
Re: need help!!!
As stated earlier you could use the Instr function for such validation but I am not sure if you could achieve 100% fool-proof validation with such cases...
I haven't done Regulare Expressions but it may be of help altogether...
-
Mar 16th, 2006, 10:06 AM
#11
Hyperactive Member
Re: need help!!!
1st: this guy seems to be in a problem.... use somewhere else as chat room!
i knew my code wasnt perfect, just wanted to see if the idea was what you wanted:
You should in fact use InStr (only way i see)
This is what i recommend (cant write the whole code right now... but tell me if you have problems and maybe later).
1st make sure there is a equal sign BUT NO MORE:
VB Code:
Dim X 'Will be the first part of the math
Dim Y 'Will be the second part of the math
Dim equalChar As Integer 'Position of the equal
equalChar = InStr(1, Text1, "=")
If equalChar = 0 Then 'If not equal sign found, exit
MsgBox "No '=' was found on your entry." '
Exit Sub
Elseif InStr(1, Text1, "=") > 0 then 'If more than 1 equal sign found, exit
MsgBox "Please don't enter more than one '=' sign." '
Exit Sub
End If
X = Left(Text1, equalChar - 1)
Y = Right(Text1, Len(Text1) - equalChar)
'Right now you have both parts each one in one variable, maybe you would like yo make sure also some stuff...
'like no letters are on them or chars like & or #, since i dont know 100% cant do that.
'Also i dont know if there is a chance than more than 1 operation will be done in each, like 7*1+12=33/3*7+4,
' but i think you can do this using the InStr to find the +,-,*,/,etc. and then useing a "select case"... ill try a few stuff and post again since im not sure how to do it 100%.
BTW.... why do you need this? cause its much easier if you 4 example type everything like a calculator, with one button for "+" another for "*", etc.
Last edited by Kanbei; Mar 16th, 2006 at 10:19 AM.
-
Mar 16th, 2006, 10:23 AM
#12
Thread Starter
New Member
Re: need help!!!
Tnx a lot!!!
I need this because its my requirement to be able to pass the final exam.
I dont think its the same as a calculator, to be more specific...it is a Turbo C if condition syntax checker.
-
Mar 16th, 2006, 10:53 AM
#13
Hyperactive Member
Re: need help!!!
ok. so tell me if you have any problem with the rest of the code and if you want me to write it for you then plz tell me EXACTLY what can and cannot be entered.
Good Luck!
-
Mar 16th, 2006, 02:25 PM
#14
Re: Validating Algebraic Equations
here's a solution:
VB Code:
Private Function Validate(ByVal sEquation As String, _
ByRef sError As String, _
Optional ByVal IsTopLevel As Boolean = True) As Boolean
Const sComparison As String = " < > = || && & " ' Add Valid Comparison operators here
Const sOperators As String = " + - * / ^ " ' Add valid Calculation operators here
Const sIndicator As String = "?" ' A character which doesn't clash with anything else
Const sAlgebra As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Const sInteger As String = "0123456789."
Dim N As Integer, lPos As Integer
Dim arrComp() As String, sChar As String
Dim bValue As Boolean
' Check the equation isn't ""
If LenB(Trim$(sEquation)) = 0 Then
If Not bTopLevel Then sError = "Empty Paretheses"
Exit Function
End If
'Check For Brackets
Do While InStr(sEquation, "(") Or InStr(sEquation, ")")
For N = 1 To Len(sEquation)
sChar = Mid$(sEquation, N, 1)
If sChar = "(" Then
lPos = N
ElseIf sChar = ")" Then
If lPos Then
' Bracket Pair Found - Validate it
If Not Validate(Mid$(sEquation, lPos + 1, N - lPos - 1), sError, False) Then Exit Function
sEquation = Left$(sEquation, lPos - 1) & sIndicator & Mid$(sEquation, N + 1)
lPos = 0
Exit For
Else
sError = "Missing Parentheses"
Exit Function
End If
End If
Next N
If lPos Then
sError = "Missing Parentheses"
Exit Function
End If
Loop
' Check Simple Equation Conditions
' Must go value, operator, value, operator,..., value.
For N = 1 To Len(sEquation)
If bValue Then
' This must be an comparison or calculation operator
sChar = Mid$(sEquation, N, 2)
If Left$(sChar, 1) = " " Then
' Do Nothing
ElseIf InStr(sComparison, " " & sChar & " ") Or InStr(sOperators, " " & sChar & " ") Then
bValue = Not bValue
N = N + 1
ElseIf InStr(sComparison, " " & Left$(sChar, 1) & " ") Or InStr(sOperators, " " & Left$(sChar, 1) & " ") Then
bValue = Not bValue
Else
Exit For
End If
Else
' This must be a value
sChar = Mid$(sEquation, N, 1)
If sChar = " " Then
' Do Nothing
ElseIf InStr(sAlgebra, UCase$(sChar)) Then
Do While InStr(sAlgebra, UCase$(Mid$(sEquation, N + 1, 1)))
N = N + 1
If LenB(Mid$(sEquation, N + 1, 1)) = 0 Then Exit Do
Loop
bValue = Not bValue
ElseIf InStr(sInteger, sChar) Then
Do While InStr(sInteger, UCase$(Mid$(sEquation, N + 1, 1)))
N = N + 1
If LenB(Mid$(sEquation, N + 1, 1)) = 0 Then Exit Do
Loop
bValue = Not bValue
ElseIf sChar = sIndicator Then
bValue = Not bValue
Else
Exit For
End If
End If
Next N
If Not N > Len(sEquation) Or Not bValue Then
sError = "Missing or Invalid " & IIf(bValue, "Operator", "Value")
Exit Function
End If
' Check whether main equation is a comparison
If IsTopLevel Then
arrComp = Split(sComparison, " ")
For N = 1 To UBound(arrComp) - 1
If InStr(sEquation, arrComp(N)) Then Exit For
Next N
If N > UBound(arrComp) - 1 Then
sError = "No Comparison Operator in Top-Level Equation"
Exit Function
End If
End If
Validate = True
End Function
it validates simple equations (such as x = y + 5) by checking they go: value, operator, value, operator, value and uses recursion to check all brackets. It also returns the nature of any error encountered.
The Function accepts an Equation and a Buffer string with which to return the error to. The function returns True if the equation is valid. False if it's not.
say we have (x / 100) = ((y && q) || (F + K)) which would be called:
VB Code:
MsgBox Validate("(x / 100) = ((y && q) || (F + K))", sErr)
the code above does:
Code:
Validate (x / 100) = ((y && q) || (F + K))
Looks for bracket pair: Finds (x / 100)
Validate x / 100
Looks for bracket pair: none
Checks equation order: correct
Returned: True
Replaces (x / 100) with ?
Validate ? = ((y && q) || (F + K))
Looks for bracket pair: Finds (y && q)
Validate y && q
Looks for bracket pair: none
Checks equation order: correct
Returned: True
Replaces (y && q) with ?
Validate ? = (?|| (F + K))
Looks for bracket pair: Finds (F + K)
Validate F + K
Looks for bracket pair: none
Checks equation order: correct
Returned: True
Replaces (F + K) with ?
Validate ? = (? || ?)
Looks for bracket pair: Finds (? || ?)
Validate ? || ?
Looks for bracket pair: none
Checks equation order: correct
Returned: True
Replaces (? || ?) with ?
Validate ? = ?
Looks for bracket pair: none
Checks equation order: correct
Checks Top Level Equation has comparison operator: True
Returned: True
Here is some example variations and outcomes:
VB Code:
Dim sTemp As String, sText As String, sEq As String, iPos As Integer
Randomize
sEq = "(x / 100) = ((y && q) || (F + K))"
For N = 0 To 10
Do
iPos = (Len(sEq) - 1) * Rnd + 1
Loop While Mid$(sEq, iPos, 1) = " "
sText = Left$(sEq, iPos - 1) & " " & Mid$(sEq, iPos + 1)
Debug.Print sText & " : " & IIf(Validate(sText, sTemp), True, sTemp)
sTemp = vbNullString
Next N
Code:
Results
-------
(x / 100) = ((y && q) || (F K)) : Missing Operator
( / 100) = ((y && q) || (F + K)) : Missing Value
(x / 1 0) = ((y && q) || (F + K)) : Missing Operator
(x / 100) = ((y && q) || (F K)) : Missing Operator
(x / 100) ((y && q) || (F + K)) : Missing Operator
(x / 100) = ((y && q) || F + K)) : Missing Parentheses
(x / 100) = ((y && q) || ( + K)) : Missing Value
(x / 10 ) = ((y && q) || (F + K)) : True
(x / 100) = ((y && q) || (F + K) : Missing Parentheses
(x / 100) = ((y && q) || (F + K ) : Missing Parentheses
(x / 100) = ((y & q) || (F + K)) : True
An equation like (X + Y) / (N + M) would return false because it contains no comparison operator (=, >, etc.) in the top-level equation. To remove this check simply call the function like so:
VB Code:
MsgBox Validate("(X + Y) / (N + M)", sErr, False)
Last edited by bushmobile; Mar 17th, 2006 at 08:00 PM.
-
Mar 16th, 2006, 02:27 PM
#15
Hyperactive Member
Re: need help!!!
sorry: bush, what is the " || "???
-
Mar 16th, 2006, 02:29 PM
#16
Re: need help!!!
I don't know!
It's just a random bit of syntax. I've put it as a comparison operator, but it might not be.
There may well be errors with the code, so let me know if you see any
-
Mar 16th, 2006, 07:23 PM
#17
Addicted Member
Re: need help!!!
|| - (or) operator in c lang
-
Mar 16th, 2006, 07:25 PM
#18
Re: need help!!!
Logical Or, to be more precise. In C and C-based languages.
-
Mar 16th, 2006, 07:26 PM
#19
Re: need help!!!
Aghh, suddenly this is a discussion about C syntax. Get me out of here
-
Mar 16th, 2006, 10:54 PM
#20
Thread Starter
New Member
Re: need help!!!
bush i will try it.If I encounter some error I'll just ask u about it...thats exactly I was lookin for, its somewhat the same as if condition syntax checker. Tnx a lot..
Kanbei I already try the code but It still have an error...however I wana thnk u for sharing it.I learn something from it.
oOYunaOo tnx sa email mo...I appreciate it.
Tnx guys!!
-
Mar 17th, 2006, 07:08 AM
#21
Re: need help!!!
If your questions been answered you should mark this thread resolved by using the 'Thread Tools' at the top of thread
-
Mar 17th, 2006, 04:56 PM
#22
Re: need help!!!
For what it's worth, I've now updated post #15 so that it also returns the reason that the equation has not been validated - Missing Parentheses, Missing Value etc.
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
|