You are to write a Java program that allows the user to type in a mathematical f
ID: 3828423 • Letter: Y
Question
You are to write a Java program that allows the user to type in a mathematical formula at the keyboard and produces the answer on the screen.
You are not allow to use JAVA STACK. IMPLEMENT OWN STACK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
The formula will be typed in INFIX notation and will include only positive integers for the numbers. The operators that are acceptable are the following: + (plus), - (minus), * (multiply), / (divide), and ^ (power). Parenthesis are also allowed.
You should allow the user to type in the equation of his choice and then display the answer on the screen. Display a real answer. (for example: 3 / 2 = 1.5, NOT 1)
ALSO DISPLAY THE POST-FIX EQUATION ON THE SCREEN.
The normal rules of mathematics apply (parenthesis have the highest precedence followed by the ^, followed by * and /, followed by - and +).
Do not allow the program to bomb and warn the user if the equation is wrong. For example: 2 + 43 / * 12 cannot be calculated because there are too many operators.
When I test the program, I will only type positive integers as input and you can assume the input equation is valid.
Hints: You should treat the equation as a queue of tokens and read the tokens from the queue one at a time. Convert the INFIX equation to a POSTFIX equation. Then resolve the POSTFIX equation.
Sample equations-> Postfix
12 +(2 – 4 ) /5 ->12 2 4 – 5 / +
43 + 3^4 * 2 / 34 – 9 ->43 3 4 ^ 2 * 34 / + 9 -
2 ^ 4+34 - 3 ->2 4 ^ 34 + 3 -
Here are 2 String methods that will come in handy: replaceAll and split. Example of program running:
Enter an equation: 12+ (6- 4 ) ^ (1+1) / 2
RPN: 12 6 4 – 1 1 + ^ 2 / +
Answer: 14
Explanation / Answer
import java.io.*;
class Stack { char a[]=new char[100]; int top=-1;
void push(char c)
{
try
{
a[++top]= c;
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println("Stack full , no room to push , size=100");
System.exit(0);
}
}
char pop()
{
return a[top--];
}
boolean isEmpty()
{
return (top==-1)?true:false;
}
char peek()
{
return a[top];
}
}
public class InfixToPostfix
{
static Stack operators = new Stack();
public static void main(String argv[]) throws IOException
{
String infix;
//create an input stream object
BufferedReader keyboard = new BufferedReader (new InputStreamReader(System.in));
//get input from user
System.out.print(" Enter the algebraic expression in infix: ");
infix = keyboard.readLine();
//output as postfix
System.out.println("The expression in postfix is:" + toPostfix(infix));
}
private static String toPostfix(String infix)
//converts an infix expression to postfix
{
char symbol;
String postfix = "";
for(int i=0;i<infix.length();++i)
//while there is input to be read
{
symbol = infix.charAt(i);
//if it's an operand, add it to the string
if (Character.isLetter(symbol))
postfix = postfix + symbol;
else if (symbol=='(')
//push (
{
operators.push(symbol);
}
else if (symbol==')')
//push everything back to (
{
while (operators.peek() != '(')
{
postfix = postfix + operators.pop();
}
operators.pop(); //remove '('
}
else
//print operators occurring before it that have greater precedence
{
while (!operators.isEmpty() && !(operators.peek()=='(') && prec(symbol) <= prec(operators.peek()))
postfix = postfix + operators.pop();
operators.push(symbol);
}
}
while (!operators.isEmpty())
postfix = postfix + operators.pop();
return postfix;
}
static int prec(char x)
{
if (x == '+' || x == '-')
return 1;
if (x == '*' || x == '/' || x == '%')
return 2;
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.