write a program name calc.java that evaluates an infix expression entered by the
ID: 3630529 • Letter: W
Question
write a program name calc.java that evaluates an infix expression entered by the user. The expression may contain the following tokens:(1) Integer constants (a series of decimal digits).
(2) x (representing a value to be supplied later).
(4) Binary operators (+, -, *, / and %).
(5) Parentheses
additional requirements for this program:
(1) You must use stack objects during the translation from infix to postfix.
(2) Operators must have the correct precedence and associativity. Binary operators * , / and % takes precedence over binary + and -. And processing is always done from left to right.
If the infix expression contains an error of any kind, the program must display the message Error in expression (with an optional explanation) and then terminate.
Explanation / Answer
//characterstack class
class CharacterStack extends StackComposition
{
public char stackTop()
{
char temp = popChar();
pushChar( temp );
return temp;
}
public char popChar()
{
return ((Character)super.pop() ).charValue();
}
public void pushChar( char c )
{
super.push( new Character( c ) );
}
} // end class CharacterStack
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.