Results 1 to 3 of 3

Thread: i need a syntax for this program..please!

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    3

    i need a syntax for this program..please!

    Assume the following rules of precedence for expressions:
    Highest is addition (+)
    then subtraction (-)
    then multiplication (*)
    then division (/)

    Evaluate inputs such as:
    (a) 1+4*3-2/2 Output: 5*3-2/2 = 5*1/2 = 5/2 = 2.5
    (b) 5-2/2+1*2 Output: 5-2/3*2 = 3/3*2 = 3/6 = 0.5
    (c) 3-2+4-2+5 Output: 3-6-7 = -3 - 7 = -10

    Output up to 5 decimal places if long.

  2. #2
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: i need a syntax for this program..please!

    Always no parenthesis?

    If so then how about updating the expression with parenthesis to force precedence and make possible use of script control Eval() method rather than manually doing the calculations yourself:
    (a) 1+4*3-2/2 update due to addition --> (1+4)*3-2/2 update due to sub --> (1+4)*(3-2)/2 update due to mul --> ((1+4)*(3-2))/2
    (b) 5-2/2+1*2 --> 5-2/(2+1)*2 --> (5-2)/(2+1)*2 --> (5-2)/((2+1)*2)
    (c) 3-2+4-2+5 --> 3-(2+4)-(2+5) --> ((3-(2+4))-(2+5)), note left to right processing or wrapping in parenthesis for operators of same precedence

    To facilitate string processing and transfer to an array, you need to pre-process the expression.
    exp = replace(exp, "+", vbCrLf & "+" & vbCrLf)
    exp = replace(exp, "-", vbCrLf & "+" & vbCrLf)
    exp = replace(exp, "*", vbCrLf & "+" & vbCrLf)
    exp = replace(exp, "/", vbCrLf & "+" & vbCrLf)

    Now transfer to string array with exparr = Split(exp, vbCrLf). For 1+4*3-2/2 you'll get
    exparr(0) = "1"
    exparr(1) = "+"
    exparr(2) = "4"
    exparr(3) = "*"
    exparr(4) = "3"
    exparr(5) = "-"
    exparr(6) = "2"
    exparr(7) = "/"
    exparr(8) = "2"

    Then iterate through array and locate operators... update relevant array elements first for addition, then subtraction and so on. exparr(1) is addition so exparr(1) and adjacent elements are updated accordingly; the oprands are moved to the operator then wrapped in parenthesis.

    Addition pass
    exparr(0) = ""
    exparr(1) = "(1+4)"
    exparr(2) = ""
    exparr(3) = "*"
    exparr(4) = "3"
    exparr(5) = "-"
    exparr(6) = "2"
    exparr(7) = "/"
    exparr(8) = "2"

    Subtraction pass
    exparr(0) = ""
    exparr(1) = "(1+4)"
    exparr(2) = ""
    exparr(3) = "*"
    exparr(4) = ""
    exparr(5) = "(3-2)"
    exparr(6) = ""
    exparr(7) = "/"
    exparr(8) = "2"

    If adjacent is blank then retrieve nearest non-blank elements. Multiplication pass
    exparr(0) = ""
    exparr(1) = ""
    exparr(2) = ""
    exparr(3) = "((1+4)*(3-2))"
    exparr(4) = ""
    exparr(5) = ""
    exparr(6) = ""
    exparr(7) = "/"
    exparr(8) = "2"

    Then rebuild expression into string:
    newexp = Join(exparr, "")

    newexp will then contain "((1+4)*(3-2))/2". Then pass newexp to Eval() method of script control. You will need to add reference to this. Do a search for Eval() method for samples of use.

    If parenthesis will be supported in original expression then while processing operators and operands within existing parenthesis ( parenthesis you are inserting must come after existing ( and ) before existing ) when wrapping operands and operators... actually it might not matter if you'll end up with (( or )) but I mentioned it just in case not doing so will cause an error and you can consider it during debugging.

    Now that you have algorithm design, implementation should be easier.
    Last edited by leinad31; Mar 26th, 2008 at 02:49 AM.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: i need a syntax for this program..please!

    Please do not create duplicate threads.

    Based on your profile, you are using VB2005, which means you should be posting in our VB.Net forum. Your copy of this question is here.

    Thread closed.

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