Question: Please write notes Details for this code import java.lang.*; import java.util.*; import java.io.*; class ArithmeticExpressionEvaluator { public static String EvaluateReading() { try { InputStreamReader Input
Please write notes Details for this code
import java.lang.*;
import java.util.*;
import java.io.*;
class ArithmeticExpressionEvaluator
{
public static String EvaluateReading()
{
try
{
InputStreamReader Input = new InputStreamReader(System.in);
BufferedReader Reader = new BufferedReader(Input);
return Reader.readLine();
}
catch (Exception e)
{
e.printStackTrace();
return "";
}
}
public static int Evaluate_ReadInteger()
{
try
{
InputStreamReader Input = new InputStreamReader(System.in);
BufferedReader Reader = new BufferedReader(Input);
return Integer.parseInt(Reader.readLine());
}
catch (Exception e)
{
e.printStackTrace();
return 0;
}
}
public static String InfixToPostfix(String infixBuffer)
{
int Pri = 0;
String PostfixBuffer = "";
Stack S1 = new Stack();
for (int i = 0; i < infixBuffer.length(); i++)
{
char c = infixBuffer.charAt(i);
if (c == '+' || c == '-' || c == '*' || c == '/')
{
if (S1.size() <= 0)
S1.push(c);
else
{
Character CharTop = (Character) S1.peek();
if (CharTop == '*' || CharTop == '/')
Pri = 1;
else
Pri = 0;
if (Pri == 1)
{
if (c == '+' || c == '-')
{
PostfixBuffer += S1.pop();
i--;
}
else
{ // Same
PostfixBuffer += S1.pop();
i--;
}
}
else
{
if (c == '+' || c == '-')
{
PostfixBuffer += S1.pop();
S1.push(c);
}
else
S1.push(c);
}
}
}
else
{
PostfixBuffer += c;
}
}
int Len = S1.size();
for (int j = 0; j < Len; j++)
PostfixBuffer += S1.pop();
return PostfixBuffer;
}
public static void main(String[] args)
{
String Infix_Buffer = "";
String Postfix_Buffer = "";
if(args.length == 1)
{
Infix_Buffer = args[0];
Postfix_Buffer = InfixToPostfix(Infix_Buffer);
System.out.println("The expression for a InFix :\t" + Infix_Buffer);
System.out.println("The expression for a PostFix:\t" + Postfix_Buffer);
System.out.println();
}
else
{
Infix_Buffer = "A+B*C";
Postfix_Buffer = InfixToPostfix(Infix_Buffer);
System.out.println("The expression for a InFix :\t" + Infix_Buffer);
System.out.println("The expression for a PostFix:\t" + Postfix_Buffer);
System.out.println();
System.out.println("The final result:\t" + Postfix_Buffer);
System.out.println();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
