Write a program in java to translate infix mathematical expression into postfix
ID: 3556275 • Letter: W
Question
Write a program in java to translate infix mathematical expression into postfix expression and a program to evaluate the posfix expression. There should be three separate progams. use stack data abstraction and class implementation. The program has three verions. The first version uses array implementation, the second version uses linked lsit, and the third one uses double linked list stack implementation in the abstraction. First read and output data using std. then use the files. Use the stds only for the first version.
Please explain how you did each version and write lots of comments (to expalin what you do in the code). The expression can have the following; +,-,*,/,(,), and the program has to beable to do more than just single digits convertion and evaluation.
Thank you
Explanation / Answer
import java.lang.*;
import java.util.*;
import java.io.*;
class InfixConverter
{
public static String ReadString()
{
try
{
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
return reader.readLine();
}
catch (Exception e)
{
e.printStackTrace();
return "";
}
}
public static int 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 InfixToPostfixConvert(String infixBuffer)
{
int priority = 0;
String postfixBuffer = "";
Stack s1 = new Stack();
for (int i = 0; i < infixBuffer.length(); i++)
{
char ch = infixBuffer.charAt(i);
if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
// check the precedence
if (s1.size() <= 0)
s1.push(ch);
else
{
Character chTop = (Character) s1.peek();
if (chTop == '*' || chTop == '/')
priority = 1;
else
priority = 0;
if (priority == 1)
{
if (ch == '+' || ch == '-')
{
postfixBuffer += s1.pop();
i--;
}
else
{ // Same
postfixBuffer += s1.pop();
i--;
}
}
else
{
if (ch == '+' || ch == '-')
{
postfixBuffer += s1.pop();
s1.push(ch);
}
else
s1.push(ch);
}
}
}
else
{
postfixBuffer += ch;
}
}
int len = s1.size();
for (int j = 0; j < len; j++)
postfixBuffer += s1.pop();
return postfixBuffer;
}
public static void main(String[] args)
{
String infixBuffer = "";
String postfixBuffer = "";
if(args.length == 1)
{
infixBuffer = args[0];
postfixBuffer = InfixToPostfixConvert(infixBuffer);
System.out.println("InFix : " + infixBuffer);
System.out.println("PostFix: " + postfixBuffer);
System.out.println();
}
else
{
infixBuffer = "a+b*c";
postfixBuffer = InfixToPostfixConvert(infixBuffer);
System.out.println("InFix : " + infixBuffer);
System.out.println("PostFix: " + postfixBuffer);
System.out.println();
infixBuffer = "a+b*c/d-e";
postfixBuffer = InfixToPostfixConvert(infixBuffer);
System.out.println("InFix : " + infixBuffer);
System.out.println("PostFix: " + postfixBuffer);
System.out.println();
infixBuffer = "a+b*c/d-e+f*h/i+j-k";
postfixBuffer = InfixToPostfixConvert(infixBuffer);
System.out.println("InFix : " + infixBuffer);
System.out.println("PostFix: " + postfixBuffer);
System.out.println();
}
}
}
2nd
import java.io.*;
class Stack
{
private int[] a;
private int top,m;
public Stack(int max)
{
m=max;
a=new int[m];
top=-1;
}
public void push(int key)
{
a[++top]=key;
}
public int pop()
{
return(a[top--]);
}
}
class Evaluation{
public int calculate(String s)
{
int n,r=0;
n=s.length();
Stack a=new Stack(n);
for(int i=0;i<n;i++)
{
char ch=s.charAt(i);
if(ch>='0'&&ch<='9')
a.push((int)(ch-'0'));
else
{
int x=a.pop();
int y=a.pop();
switch(ch)
{
case '+':r=x+y;
break;
case '-':r=y-x;
break;
case '*':r=x*y;
break;
case '/':r=y/x;
break;
default:r=0;
}
a.push(r);
}
}
r=a.pop();
return(r);
}
}
class PostfixEvaluation
{
public static void main(String[] args)throws IOException
{
String input;
while(true)
{
System.out.println("Enter the postfix expresion");
input=getString();
if(input.equals(""))
break;
Evaluation e=new Evaluation();
System.out.println("Result:- "+e.calculate(input));
}
}
public static String getString()throws IOException
{
DataInputStream inp=new DataInputStream(System.in);
String s=inp.readLine();
return s;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.