/* StrExpr2 - Calculates a binary numerical expression in a string and returns the result as a number. One of the elements can be itself a binary expression, in which case it must be set in parentheses. Requires the UDO StrExpr1 DESCRIPTION Calculates a binary numerical expression in a string and returns the result as a number. One of the elements can be itself a binary expression, in which case it must be set in parentheses. No spaces are allowed. Supported math operations are +, -, *, /, ^, and %. A simple number string or a simple expression (like in StrExpr1) is also accepted and converted to a number. Requires the UDO StrExpr1. SYNTAX iNum StrExpr2 Str INITIALIZATION Str - Input string with a binary math expression. One of the elements can be itself a binary expression, in which case it must be set in parentheses. No spaces are allowed. Supported math operations are +, -, *, /, ^, and %. A simple number string or a simple expression (like in StrExpr1) is also accepted and converted to a number. iNum - result of the binary operation as number CREDITS joachim heintz 2012 */ opcode StrExpr2, i, S Sin xin ilen strlen Sin ;if a parenthesis can be found iparenth strindex Sin, "(" if iparenth > -1 then ;if in first half and no '-' preceeds if iparenth == 0 then ;then first element ends in ")" iprend strindex Sin, ")" S1 strsub Sin, 1, iprend ;convert this element into a number i1 StrExpr1 S1 ;append the rest and convert again S2 strsub Sin, iprend+2 Sep strsub Sin, iprend+1, iprend+2 Scoll sprintf "%f%s%s", i1, Sep, S2 ires StrExpr1 Scoll ;if in first half and '-' preceeds elseif iparenth == 1 then ;then first element ends in ")" iprend strindex Sin, ")" S1 strsub Sin, 2, iprend ;convert this element into a number i1 StrExpr1 S1 i1 = -i1 ;append the rest and convert again S2 strsub Sin, iprend+2 Sep strsub Sin, iprend+1, iprend+2 Scoll sprintf "%f%s%s", i1, Sep, S2 ires StrExpr1 Scoll ;if the parenthesis is in the second half else ;isolate first element and the conjunction S1 strsub Sin, 0, iparenth-1 Sep strsub Sin, iparenth-1, iparenth ;convert the second element S2 strsub Sin, iparenth+1, ilen-1 i2 StrExpr1 S2 ;if subtraction and i2 negative, convert to addition isepminus strcmp Sep, "-" if i2 < 0 && isepminus == 0 then i2 = -i2 Sep = "+" endif ;convert the whole Scoll sprintf "%s%s%f", S1, Sep, i2 ires StrExpr1 Scoll endif ;if no parenthesis, simply convert else ires StrExpr1 Sin endif xout ires endop