<-- Previous || Up || Next -->

Infix To Prefix Function
Math Symbolic Class

Public Function InfixToPrefix( _
    ByVal vExpr As Variant _
    ) As Variant

"Translate Infix Expression To Prefix Format"
Translate an expression from infix format (in which the operators come between the operands) into Prefix format (where the operators precede the operands and parentheses).

Examples:
    ? InfixToPrefix("34 + 45 * 91")
    +(34,*(45,91))
    ? InfixToPrefix("2 ^ 3 ^ 4 + 5")
    +(^(2,^(3,4)),5)
    ? InfixToPrefix("A * ( B + C ) + D")
    +(*(A,+(B,C)),D)
    ? InfixToPrefix("+ A") ' #4
    Error: Expected two operands.
    ? InfixToPrefix("A / B ^ C + D * E - A * C")
    -(+(/(A,^(B,C)),*(D,E)),*(A,C))
    ? InfixToPrefix("A + B")
    +(A,B)
    ? InfixToPrefix("A + 0 - B")
    -(+(A,0),B)
    ? InfixToPrefix("A * % ( D + C )") ' #8
    Error: Expected two operands.
    ? InfixToPrefix("A + B * % G * H") ' #9
    Error: Expected two operands.
    ? InfixToPrefix("% ( D + F ) * 2") ' #10
    Error: Expected two operands.
See also:
    InfixToPostfix Function
    InfixToBasic Function
    InfixToPostfixInComingPriority Function
Note: This function recognizes the following operators and functions (listed from highest precedence to lowest):
    ABS, ASIN, ATAN, COS, LN, SIGN, SIN, SQRT, TAN
    ^
    *, /
    \, MOD
    +, -
    (
The functions at the top of this list have the highest precedence and the open parentheses character has the lowest precedence (and functions/operators on the same line have the same precedence). This function assumes that all other alphanumeric words represent variables or numeric constants.
BIG Note: Although this function supports subtraction, it does not support the use of "-" as a unary minus operator. Rephrase any expressions like "-A" into "0-A".

vExpr: String containing the infix-format algebraic expression which will be converted to Prefix. Function returns Null if vExpr is Null or cannot be fixed up to a String.

Note: Function will return an error in the form of a string like "Error: Expected one operand." if the algebraic (infix) expression is ill-formed (as in examples #4, 8, 9, and 10). The possible errors include:
Error: Expected one operand. (Function has encountered a function call so it needs an operand but the operand stack is empty.)
Error: Expected two operands. (Function has encountered a binary operator so it needs two operands but the operand stack has less than two operands.)
Error: Expected end of expression. (Function has encountered the end of the input expression but there are still operands on the stack.)

Copyright 1996-1999 Entisoft
Entisoft Tools is a trademark of Entisoft.