Preface :
Electronic calculators illustrate one of the primary application of a stack. The user enters a mathematical expression by pressing a sequence of numbers (operands) and operators, followed by the '=' key. The calculator computes and displays the result. The process assumes that the user enters the expression in a specific expression format. For instance, an integer expression such as :
-8 + (4*12 + 5%2)/3
contains operands (8,4,12,5,2,3), a unary operator (-), binary operators (+,%,*,/), and parentheses that create subexpressions. The operators are termed unary and binary because they require one or two operands respectively. We say the expression is in infix format because each binary operator appears between its operands and each unary operator precedes its operand. Infix is the most common format for writing expressions and is the expression format of choice for most programming languages and calculators.
Some calculators allow an alternative postfix format, where an operator comes after its operands. The format is also called RPN or Reverse Polish Notation. The term Polish refers to the fact that the notation was developed by the Polish mathematician. The infix expression "a+b" has the equivalent postfix form "ab+". With postfix format, an operator appears in the expression as soon as its two operands are available. To understand how this rule applies, consider the translation of the following infix expression to posfix :
1. Infix: a+ b*c > Postfix: abc*+
2. Infix: (a+b)*c > Postfix: ab+c*
3. Infix: (a*b+c)/d + e > Postfix: ab*c+d/e+
We can summarize the conversion from infix to postfix with two simple rules :
Postfix Evaluation :
With an expression in postfix format, we can use a simple algorithm to evaluate the result. The algorithm scans each term of the expression from left to right and uses a stack to hold the operands. If a term is an operand, push it onto the stack. If the term is a binary operator, we can evaluate its result because its two operands already reside on the stack in the top two positions. To carry out the evaluation of the operator, pop the stack twice to retrieve the operands, evaluate the expression, and then push the result back onto the stack. After processing all of the terms in the expression, there will be a single value on the top of the stack, which is the result. Consider the expression "4 3 5 * +". Evaluating it requires five steps :
The PostfixEval Class :
We can design a class, PostfixEval, which takes a postfix expression and evaluates it. We assume the operands are single-digit nonnegative integers. This simplifies the process of distinguishing between an operator and an operand. The class handles the standard binary integer operators +, -, *, / and %. In addition, the class evaluates expression contains the binary exponentiation operator ^, which takes two operands, a and b, and computes a^b.
The PostfixEval class contains a default constructor that creates an instance without a specified postfix expression to evaluation. The expression is introduced by the method setPostfixExp(), which takes a string argument containing the postfix expression. The access method getPostfixExp() enables a programmer to retrieve the current expression. The key method, evaluate(), attempts to compute the value of the postfix expression. If successful, it returns the value of the expression. If the expression contains an error, the method throws an ArithmeticException. If desired, the calling block can catch the exception and output the error message that is created when the evaluation process identifies an error. A series of private methods, compute(), getOperand() and isOperator() are used by evaluate() to identify operators and to compute the result for an arithmetic operation :
Program 14.3 : Evaluating a Postfix Expression
The program evaluates a postfix expression. After prompting, the program reads an expression from the keyboard and assign it to the PostfixEval objectpostfixExp using the method setPostfixExp(). Enclose the call to evaluate() in a try block in case an error occurs :
Electronic calculators illustrate one of the primary application of a stack. The user enters a mathematical expression by pressing a sequence of numbers (operands) and operators, followed by the '=' key. The calculator computes and displays the result. The process assumes that the user enters the expression in a specific expression format. For instance, an integer expression such as :
-8 + (4*12 + 5%2)/3
contains operands (8,4,12,5,2,3), a unary operator (-), binary operators (+,%,*,/), and parentheses that create subexpressions. The operators are termed unary and binary because they require one or two operands respectively. We say the expression is in infix format because each binary operator appears between its operands and each unary operator precedes its operand. Infix is the most common format for writing expressions and is the expression format of choice for most programming languages and calculators.
Some calculators allow an alternative postfix format, where an operator comes after its operands. The format is also called RPN or Reverse Polish Notation. The term Polish refers to the fact that the notation was developed by the Polish mathematician. The infix expression "a+b" has the equivalent postfix form "ab+". With postfix format, an operator appears in the expression as soon as its two operands are available. To understand how this rule applies, consider the translation of the following infix expression to posfix :
1. Infix: a+ b*c > Postfix: abc*+
2. Infix: (a+b)*c > Postfix: ab+c*
3. Infix: (a*b+c)/d + e > Postfix: ab*c+d/e+
We can summarize the conversion from infix to postfix with two simple rules :
Postfix Evaluation :
With an expression in postfix format, we can use a simple algorithm to evaluate the result. The algorithm scans each term of the expression from left to right and uses a stack to hold the operands. If a term is an operand, push it onto the stack. If the term is a binary operator, we can evaluate its result because its two operands already reside on the stack in the top two positions. To carry out the evaluation of the operator, pop the stack twice to retrieve the operands, evaluate the expression, and then push the result back onto the stack. After processing all of the terms in the expression, there will be a single value on the top of the stack, which is the result. Consider the expression "4 3 5 * +". Evaluating it requires five steps :
The PostfixEval Class :
We can design a class, PostfixEval, which takes a postfix expression and evaluates it. We assume the operands are single-digit nonnegative integers. This simplifies the process of distinguishing between an operator and an operand. The class handles the standard binary integer operators +, -, *, / and %. In addition, the class evaluates expression contains the binary exponentiation operator ^, which takes two operands, a and b, and computes a^b.
The PostfixEval class contains a default constructor that creates an instance without a specified postfix expression to evaluation. The expression is introduced by the method setPostfixExp(), which takes a string argument containing the postfix expression. The access method getPostfixExp() enables a programmer to retrieve the current expression. The key method, evaluate(), attempts to compute the value of the postfix expression. If successful, it returns the value of the expression. If the expression contains an error, the method throws an ArithmeticException. If desired, the calling block can catch the exception and output the error message that is created when the evaluation process identifies an error. A series of private methods, compute(), getOperand() and isOperator() are used by evaluate() to identify operators and to compute the result for an arithmetic operation :
Program 14.3 : Evaluating a Postfix Expression
The program evaluates a postfix expression. After prompting, the program reads an expression from the keyboard and assign it to the PostfixEval objectpostfixExp using the method setPostfixExp(). Enclose the call to evaluate() in a try block in case an error occurs :
This message was edited 3 times. Last update was at 09/10/2010 14:37:59
沒有留言:
張貼留言