Results 1 to 21 of 21

Thread: ----->Look @ THis <------

  1. #1

    Thread Starter
    Lively Member Wah's Avatar
    Join Date
    Aug 2002
    Location
    colchester (Up the U's)
    Posts
    82

    ----->Look @ THis <------

    hi ,

    i've semi created a calulator that accepts more than one operator at a time by storing each pressed no. in an array .....yet wot i need is for this array to be accessed and each operator found needs to be used as an operator (as oppose to string values)

    Can anyone HELP

    Here is the project so far:
    Attached Files Attached Files
    Last edited by Wah; Oct 15th, 2002 at 12:37 PM.
    Up the U's, Up the U's. Up The Good Old White And Blues
    When We Get Together What a Game We'll See We're As Strong As The Old The Old Oak Tree
    All For One And One For All We're Football Good And Clean
    BULLSH*T
    You Can Hear The Crowd Sing "Oi We've Got A Team"
    Up the U's, Up the U's. Up The Uoooos's

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    The best bet for this is to use Reverse Polish Notation
    That way when the calulations get more complex
    you won't have to worry about precedence.
    A quick search in planet source code
    should throw up a few examples

  3. #3

    Thread Starter
    Lively Member Wah's Avatar
    Join Date
    Aug 2002
    Location
    colchester (Up the U's)
    Posts
    82
    thanks this really helps....... yet i need to knw how to search the array for operators to apply this
    Up the U's, Up the U's. Up The Good Old White And Blues
    When We Get Together What a Game We'll See We're As Strong As The Old The Old Oak Tree
    All For One And One For All We're Football Good And Clean
    BULLSH*T
    You Can Hear The Crowd Sing "Oi We've Got A Team"
    Up the U's, Up the U's. Up The Uoooos's

  4. #4
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    I've attached some code which should give you
    everything you need.
    Type in the Algebraic expression i.e. 3+4/2
    Then convert it.
    Then get the answer
    Make sure you convert the equation
    before trying to get an answer other wise it will just
    fall over
    Attached Files Attached Files

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    I have been doing something like that in a recent program that takes what amounts to written equations and computes them. I first tried it with strings, but that was slow and somewhat painful. I then tried it using objects. This turned out to be much cleaner, and somewhat faster.

    The technique I used was to put both numbers and operators into the same type of object. One field held value, another held an identifier, etc. This leaves the entire equation represented by an array of objects.

    I tranlsated this into a number by parsing through the objects, and putting the values into a temporary array based on operator precedence. The idea is that if a number is separated from a second number by either * or /, then do the calculation. If separated by +, then put the first number into the temporary array. If separated by -, then put the negative of the first number into the temporary array. Therefore, 5+6*3/9-3 would have 5,2,-3 in the temporary array. Sum the values in the temporary array to get the result. Parenthesis are handled using a recursive call to the translation function.

    Reverse polish notation might improve on this, though it can be highly non-intuitive for for some people.

    Hope this helps.

  6. #6

    Thread Starter
    Lively Member Wah's Avatar
    Join Date
    Aug 2002
    Location
    colchester (Up the U's)
    Posts
    82
    i don't see how this can help me with my project!!!!!!
    Up the U's, Up the U's. Up The Good Old White And Blues
    When We Get Together What a Game We'll See We're As Strong As The Old The Old Oak Tree
    All For One And One For All We're Football Good And Clean
    BULLSH*T
    You Can Hear The Crowd Sing "Oi We've Got A Team"
    Up the U's, Up the U's. Up The Uoooos's

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Your original post mentioned putting all of the numbers, and I assumed operators, in an array. I thought you were trying to figure out how to convert the information in the array into a result. This would require converting the operator from some string representation into an action. You would then have to deal with operator precedence.

    What I was talking about was a way to represent numbers and operators in an array such that you could parse through the array in a fashion that allowed you to obtain a result with the rules of operator precedence intact.

    If that's not the problem, then I misunderstood. However, I would point out that using strings will be slow. Even if it requires keeping the data in two different formats (a readable form like y=mx+b, and an object form), it may be faster.

  8. #8

    Thread Starter
    Lively Member Wah's Avatar
    Join Date
    Aug 2002
    Location
    colchester (Up the U's)
    Posts
    82
    u got the idea the first time ...yet the problem is coding this so that it works alongside my project
    Up the U's, Up the U's. Up The Good Old White And Blues
    When We Get Together What a Game We'll See We're As Strong As The Old The Old Oak Tree
    All For One And One For All We're Football Good And Clean
    BULLSH*T
    You Can Hear The Crowd Sing "Oi We've Got A Team"
    Up the U's, Up the U's. Up The Uoooos's

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    I guess I'm still not understanding something. It appears that you would only need to evaluate the equation at one given time. Most calculators will only evaluate when the user presses the = button.

    I am envisioning a bunch of buttons. As each button is pressed, it is evaluated to see if it is an operator, or a number. In either case, it gets added to the array. Then when the user presses the = button, the array is evaluated.

    The evaluation is based on the fact that the array will hold a number, followed by an operator, followed by another number. If you use objects to hold the numbers and operators, then one of the fields in the object could be Type. An operator just tells us how to combine number1 with number2. If the different operators are different types, then you can figure out how to combine number1 with number2 using a select case statement on the type.

  10. #10

    Thread Starter
    Lively Member Wah's Avatar
    Join Date
    Aug 2002
    Location
    colchester (Up the U's)
    Posts
    82
    download my project (see top of thread)

    it may make it easier to understand!!!
    Up the U's, Up the U's. Up The Good Old White And Blues
    When We Get Together What a Game We'll See We're As Strong As The Old The Old Oak Tree
    All For One And One For All We're Football Good And Clean
    BULLSH*T
    You Can Hear The Crowd Sing "Oi We've Got A Team"
    Up the U's, Up the U's. Up The Uoooos's

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Oops, didn't see the download. I'll look.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Ok, I looked at the download, though I had to use Notepad, which sucks.

    The code, as it currently stands, looks like it will build an equation character by character, then write the equation into a textbox when the user presses the EXE button.

    Would I be correct in assuming that what you want to do is write the evaluated equation?

    If that is the case, I would suggest that the first step is to go through the array turning the characters for the numbers into actual numbers.

    Code:
    dim x as long
    dim st1 as string
    dim d as double
    dim array2() as double
    dim array3() as string
    dim n as long
    
    for x=1 to ubound(iarray)
    
    if iarray(x)='some expression which includes only numeric and '.'.
    st1=st1 & iarray(x)
    
    else
    
    n=n+1
    redim preserve array2(n)
    array2(n)=val(st1)
    st1=""
    redim preserve array3(n)
    array3(n)=iarray(x)
    
    end if
    (first time using code formatting, hope it is readable)
    The idea is to turn the array of characters into two arrays. The first array holds numbers, while the second array holds operators.
    This is where the earlier posts come in. If you could put numbers or operators into objects, you could have one array.

    Am I getting closer?

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Well, son of a gun, I formatted it incorrectly. I guess it's still somewhat readable.

  14. #14

    Thread Starter
    Lively Member Wah's Avatar
    Join Date
    Aug 2002
    Location
    colchester (Up the U's)
    Posts
    82
    much closer .....now the numerics are sorted.. how do i work out the results from these?????
    Up the U's, Up the U's. Up The Good Old White And Blues
    When We Get Together What a Game We'll See We're As Strong As The Old The Old Oak Tree
    All For One And One For All We're Football Good And Clean
    BULLSH*T
    You Can Hear The Crowd Sing "Oi We've Got A Team"
    Up the U's, Up the U's. Up The Uoooos's

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    With the two arrays, note that array2(1) is the first number, and array3(1) is the first operator. Each number in array2 is separated by the operator in array3. Therefore, by reading array2(1), array3(1), array2(2), array3(2)...etc. You are reading the equation.

    Now make array4() as double.
    Ok, I'll try another formatting.

    [Visual Basic Code]
    dim array4() as double
    dim ubnd as long
    dim x as long
    dim n as long

    ubnd=ubound(array2)

    'Array2() will have one more value than array3().

    for x=1 to ubnd

    if x<ubnd
    if array3(x)="*" or array3(x)="/" then

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Hit the wrong flippin key. Let's try that again
    VB Code:
    1. dim array4() as double
    2. dim ubnd as long
    3. dim x as long
    4. dim n as long
    5. dim d as double
    6.  
    7. ubnd=ubound(array3)
    8.  
    9. 'Array2() will have one more value than array3(), but we're
    10. 'walking the operator array.
    11.  
    12. d=array2(1)
    13. for x=1 to ubnd
    14.  
    15. if array3(x)="+" then
    16. n=n+1
    17. redim preserve array4(n)
    18. array4(n)=d
    19. d=array2(x+1)
    20.  
    21. elseif array3(x)="-" then
    22. n=n+1
    23. redim preserve array4(n)
    24. array4(n)=d
    25. d=-array2(x+1)
    26.  
    27. elseif array3(x)="*" then
    28. d=d*array2(x+1)
    29.  
    30. elseif array3(x)="/" then
    31. d=d/array2(x+1)
    32.  
    33. end if
    34. next x
    35.  
    36. 'Something will remain in d, this must be added to array4()
    37. n=n+1
    38. redim preserve array4(n)
    39. array4(n)=d

    This should work fairly well, though I may have a typo in there, it was written on the fly.

    Basically, if a high level operator (* or /) is encountered, it is evaluated on the fly. d holds the partial equation. Whenever a + or a - is encountered, d is set aside into array4(), and a new value is put into d. Notice that the negative of the value is used if the operator is -. At the end of this loop, array4() holds some number of values (equal to the number of + and -, but that is irrelevant) with their signs correct. Add all the values in array4(), and you have the result of the equation.
    Last edited by Shaggy Hiker; Oct 15th, 2002 at 03:35 PM.

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Obviously, I haven't figured out how to format yet!

  18. #18
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Catalonia
    Posts
    397
    Originally posted by Shaggy Hiker
    Obviously, I haven't figured out how to format yet!
    to format your code use [ vbcode] and [ /vbcode] without spaces between brackets and text
    Josep Mª

  19. #19
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106
    Ok, how do you indent?

  20. #20
    Hyperactive Member
    Join Date
    Nov 2001
    Location
    Catalonia
    Posts
    397
    Originally posted by Shaggy Hiker
    Ok, how do you indent?
    You should indent previously.
    Normally I type code in VBasic and then I paste it. If you are using notepad then you should indent it in the notepad previously to paste it
    Josep Mª

  21. #21
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918
    If you just want to evaluate expressions, here's a nice easy way to do it in three lines of code: (this code was posted a few days ago by Serge)
    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Dim objSC As New MSScriptControl.ScriptControl    
    4.     objSC.Language = "vbscript"
    5.    
    6.     MsgBox objSC.Eval("3*2+6^1-6/69.2*(6+4^(-1))")
    7.  
    8. End Sub
    You need to add a reference to the Microsoft Script Control object library.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width