Java Reference
In-Depth Information
Using a Stack to Process Algebraic Expressions
5.5
In mathematics, an algebraic expression is composed of operands that are variables or constants
and operators, such as + and * . We will use the Java notation + , - , * , and / to indicate addition, sub-
traction, multiplication, and division. We will use ^ to indicate exponentiation, with the warning
that Java has no operator for exponentiation; in Java ^ is the exclusive-or operator.
Operators generally have two operands, and so are called binary operators . For example, the +
in a + b is a binary operator. The operators + and - can also be unary operators when they have
one operand. For example, the minus sign in - 5 is a unary operator.
When an algebraic expression has no parentheses, operations occur in a certain order. Expo-
nentiations occur first; they take precedence over the other operations. Next, multiplications and
divisions occur, and then additions and subtractions. For example, the expression
20 - 2 * 2 ^ 3
evaluates as 20 - 2 * 8, then as 20 - 16, and finally as 4.
But what happens when two or more adjacent operators have the same precedence? Exponenti-
ations, such as those in a ^ b ^ c , occur right to left. Thus, 2 ^ 2 ^ 3 means 2 ^ (2 ^ 3), or 2 8 , instead
of (2 ^ 2) ^ 3, which is 4 3 . Other operations occur left to right, such as the multiplication and divi-
sion in a * b / c or the addition and subtraction in a - b + c . Therefore, 8 - 4 + 2 means (8 - 4) + 2,
or 6, instead of 8 - (4 + 2), which is 2. Parentheses in an expression override the normal operator
precedence.
Ordinarily, we place a binary operator between its operands, as in a + b . An expression in this
familiar notation is called an infix expression . Other notations are possible. For example, you
could write a binary operator before its two operands. Thus, a + b becomes + a b . This expression is
called a prefix expression . Or you could write a binary operator after its two operands, so a + b
becomes a b + . This expression is a postfix expression . Although infix expressions are more famil-
iar to us, both prefix and postfix expressions are simpler to process because they do not use prece-
dence rules or parentheses. The precedence of an operator in either a prefix expression or a postfix
expression is implied by the order in which the operators and operands occur in the expression. We
will learn more about these types of expressions later in this chapter.
Our first example looks at ordinary infix expressions.
Note: Algebraic expressions
In an infix expression, each binary operator appears between its operands, as in a + b .
In a prefix expression, each binary operator appears before its operands, as in + a b .
In a postfix expression, each binary operator appears after its operands, as in a b + .
Note: The notation in a prefix expression is sometimes called Polish notation , because it
was invented by the Polish mathematician Jan Lukasiewicz in the 1920s. The notation in a
postfix expression is sometimes called reverse Polish notation .
 
 
Search WWH ::




Custom Search