Results 1 to 20 of 20

Thread: I need help making a simple algorythm to load an expression like 100/5+(10+(2*4))

  1. #1

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457

    Smile I need help making a simple algorythm to load an expression like 100/5+(10+(2*4))

    Hi, I'm working on a scripting language (in VB) and it worked fine until I tried to add expressions (like 100/5+(10+(2*4)) )

    Can anyone help me with this? My idea would be to seperate it into "steps", and then execute them backwards, like this:

    (1+3)-((4+5)-6)

    would become

    Code:
    ( + )-(     - )
     1 3   ( + ) 6
            4 5
    Does anyone have any idea? Fox, Keda, any of the other gurus around?...
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  2. #2
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Try finding each bracket pair, then making their expressions into a UDT. This UDT could have something like a priority, with #1 being the most nested bracket. Then you could do BEDMAS in these expressions, using whatever math you already have.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  3. #3
    Zaei
    Guest
    Liek you said, you will have to separate each expression into steps. If you already support something like "10 + 2 - 3", you should be fine. First, loop through your string. At each open paren, increment a counter, and save it's location in an array. At the first close paren, find the location of the last open paren, pull that part of the string out, and find it's value. Save this value. At the next closed paren, use the saved value as a replacement for it's expression. Repeat.

    Code:
    (1+3)-((4+5)-6);
    
    The loop will look like:
    open paren, inc counter
    1
    +
    3
    close paren, dec counter, exec expression, 4
    -
    open paren, inc counter
    open paren, inc counter
    4
    +
    6
    close paren, dec counter, exec expression, 10
    -
    6
    close paren, dec counter, exec expression, 4
    semicolon, finish exec, 4 - 4 = 0
    Z.

  4. #4
    jim mcnamara
    Guest
    FWIW - I posted in C/C++ forum a C interpreter. It has a descending parser. Take a look at how parsers work -- you can implement a simple mathematical processor using one in either VB or C. Or you can just steal the math code part.

    http://www.vbforums.com/showthread.p...ht=interpreter

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I did a parser in C++ just a few weeks ago for homework. I bet you don't want to learn C++ now Jotaf so I could instead introduce you to something even more usefull for a vb programmer: Vbscript.
    Vbscript can evaluate most string expressions containing both mathematical operators as well as many vb functions. you can look up msdn for more details. That's usefull ofcourse but vbscript is rather more usefull as a scripting language since it's "object oriented" and can work with COM objects you pass to it as well as execute compiled blocks of code at runtime. It might take several months to get a scripting language for similar functionality and with vbscript you have everything just made up for you.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  6. #6

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Keda: I already knew about VB-Script, thanks for the tip anyway, but the whole idea was to make a scripting language WITHOUT VB-Script or anything like that

    Jim: Thanks, I'll look at your interpreter and maybe I'll get some ideas

    Zaei: That was kinda what I was trying to do, but couldn't get some working code to seperate the parts of the expression. Thanks

    Sas: I tried UDTs but the code got too complicated and still didn't work, so I decided to start it all over again. Btw, what's "BEDMAS"?
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    That's the whole idea? May I ask why?
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Americans may know it by PPMDAS -- it's the order of operations.

    Brackets
    Exponents
    Division
    Multiplication
    Addition
    Subtraction
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  9. #9

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Sas: well, I'm not american ( ) but I see what you mean, it's how you solve equations. Except that my teacher didn't have to give us something like that so we would remember the correct order (she just gave us the list and we learned to use it)

    Keda: Why would you hack into the Pentagon if you thought you had the skills for that? I'm doing this because I don't have anything else to do; also to prove myself that I can make a powerful scripting engine run at an acceptable speed in a VB game For the same reasones why we make free programs, games and DLLs sometimes. If you're a programmer, you should already know that

    Jim: Thanks for the code, it's a bit too complicated for me (sorry ) but I think it would still be useful if you packed it up in a DLL for use in games

    Anyway, I finally managed to code what I wanted. Open up a new VB project, create a textbox and a command button in the form, and dump this code into it:

    VB Code:
    1. Option Base 1
    2. Option Explicit
    3.  
    4. Private Sub Command1_Click()
    5.     Dim i As Long, Str As String, TempStr As String
    6.     Dim Stack() As Long, NumStack As Long, Total As Long
    7.     Dim k As Long
    8.    
    9.     Str = Text1.Text
    10.    
    11.     For i = 1 To Len(Str)
    12.         If i > Len(Str) Then Exit For
    13.        
    14.         TempStr = Mid(Str, i, 1)
    15.        
    16.         If TempStr = "(" Then
    17.             NumStack = NumStack + 1
    18.             ReDim Preserve Stack(NumStack)
    19.            
    20.             Stack(NumStack) = i
    21.         ElseIf TempStr = ")" Then
    22.             Total = ExecExpr2(Mid(Str, Stack(NumStack) + 1, i - Stack(NumStack) - 1))
    23.            
    24.             Str = Left(Str, Stack(NumStack) - 1) & Total & Right(Str, Len(Str) - i)
    25.            
    26.             i = Stack(NumStack)
    27.            
    28.             NumStack = NumStack - 1
    29.             If NumStack > 0 Then ReDim Preserve Stack(NumStack)
    30.         End If
    31.     Next i
    32.    
    33.     Total = ExecExpr2(Str, True)
    34.    
    35.     Text1.Text = Total
    36. End Sub
    37.  
    38. Private Function ExecExpr2(Str As String) As Long
    39.     Dim j As Long, TempStr As String
    40.     Dim Total As Long, Stack As String, Op As String * 1
    41.     Dim k As Long
    42.    
    43.     For j = 1 To Len(Str)
    44.         TempStr = Mid(Str, j, 1)
    45.        
    46.         If TempStr = "+" Or TempStr = "-" Or TempStr = "*" Or TempStr = "/" Then
    47.             If Op = Chr(0) Then
    48.                 Op = TempStr
    49.                 Total = Stack
    50.                 Stack = ""
    51.             Else
    52.                 If Op = "+" Then
    53.                     Total = Total + Stack
    54.                 ElseIf Op = "-" Then
    55.                     Total = Total - Stack
    56.                 ElseIf Op = "*" Then
    57.                     Total = Total * Stack
    58.                 ElseIf Op = "/" Then
    59.                     Total = Total / Stack
    60.                 End If
    61.                
    62.                 Stack = ""
    63.                 Op = TempStr
    64.             End If
    65.         ElseIf TempStr <> " " Then
    66.             Stack = Stack & TempStr
    67.         End If
    68.     Next j
    69.    
    70.     If Op = "+" Then
    71.         Total = Total + Stack
    72.     ElseIf Op = "-" Then
    73.         Total = Total - Stack
    74.     ElseIf Op = "*" Then
    75.         Total = Total * Stack
    76.     ElseIf Op = "/" Then
    77.         Total = Total / Stack
    78.     End If
    79.    
    80.     ExecExpr2 = Total
    81. End Function

    That's it! Type a simple expression into it and press the button; the textbox now has the result.

    It's not commented yet, but if you step trough it with F8 it really helps

    There's only one problem, it doesn't deal well with negative numbers well (like -10*3 or 200/-4 ). Could you please help me on this one? If you're interested, I can comment the code... basically I followed Zaei's steps, with some suggestions from Sas (except for the UDTs ).
    My idea is to load the minus signs differently, so instead of 1 - 2 it would be like 1 + (-2). What do you think?
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  10. #10
    Zaei
    Guest
    For negative numbers, preprocess your string. Find any place with a minus sign. If it is followed and preceded by a number, do nothing. if it followed, but not preceded by a number, insert a 0 before the minus sign, and exec as normal. You may want to add parens around the 0-num, so it works right always.

  11. #11
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    Listen to Zaei; he's already got a scripting language under his belt =)
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    I tell you what Jotaf, If you absolutely don't have anything else to do, and want to test your skills. Learn C++ immediately and you will thank me some day.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  13. #13
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by Zaei
    For negative numbers, preprocess your string. Find any place with a minus sign. If it is followed and preceded by a number, do nothing. if it followed, but not preceded by a number, insert a 0 before the minus sign, and exec as normal. You may want to add parens around the 0-num, so it works right always.
    What are you guys? Amateurs?
    (-x) is the unary negate operator, which should not be mistaken for the binary (x-y) substract operator.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  14. #14

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Keda: I don't wanna learn C++ because I'm learning Asm
    (So I can speed up my VB apps)

    Zaei: Thanks! My problem was that the number before the minus sign was nothing (when it was like 10*-4, I'd get the number between the * and the -, which is nothing), so all I have to do is set it to 0

    Is anyone here interested in this scripting language, once it's ready? Its syntax is kinda like C++/Java, but it's highly customizable and it's easy to add your own functions
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  15. #15

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Hum it's giving me different results than the calculator
    It's probably because it doesn't make multiplications and divisions before additions and subtractions.

    The best method would probably be to put brackets around them, but I'd have to do it BEFORE I start working on the expression - because it's built so it only loops trough the expression ONCE (it's a scripting engine, it must be fast ), so changing the order of the operations in the middle of it wouldn't be very good

    So I'll have to make another loop trough it to put brackets in the right places... but this isn't as simple as it seems, can someone please help me? Even the smallest idea would do
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  16. #16
    Zaei
    Guest
    If you want fast, write a compiled psudo-ASM scripting language. The one i am working with(that i wrote), in a few tests performed better then VB =).

    Z.

  17. #17
    Zaei
    Guest
    kedaman, from my tests, 0-x is -x. 0-3 = -3, 0--3 = 3... works for me.

    Z.

  18. #18
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Jotaf, do as you wish
    Zaei, did your language compile blocks of code like vb script does?

    0-x will be a waste of preprocessing time, especially as you have to count in all predecessing situations like a*-b and a^-(b+c) to add extra parentesis.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  19. #19
    Zaei
    Guest
    It compiles the entire script into bytecode. The only real downside is that it's psudo asm:
    Code:
    mov aa 10
    add aa 10
    shl aa 1
    push aa
    int 0
    call hello
    end
    
    function hello
    mov aa 10
    push aa
    int 10
    ret
    And that you can only use variable names aa..zz, but that was a speed consideration =).

    Z.

  20. #20

    Thread Starter
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Zaei: How do you think my scripting language works? Except that it converts the blocks into asm-like code (but it's not REAL asm, just "jump here", "set var to this", etc)

    And I'm gonna assign an index to each var so it'll be even faster

    Kedaman: It's not a waste of preprocessing time, actually all I had to do was If TempStr="" Then TempStr="0"
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

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