Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java eclipse program(with comments) to convert a text-le containing expr

ID: 3751333 • Letter: W

Question

Write a java eclipse program(with comments) to convert a text-le containing expressions (one per line) into post-x expressions outputted to any le of your choice(name what file you used) using a stack with one space between operators and variables (one letter variables) and/or constants (one digit constants)

For example use the following expressions in a text file:

( a + b ) * ( c - d ) / e + f ^ g
( a + b ) / ( c - d )
a + b / c - d
(a + b) ^ c / ( d - e )
a / ( b * c ) / ( d * e )
a / b * c / * e

Explanation / Answer

//Save the program to IToF.java file and put input.txt into same directory

//final output is saved into output.txt

//IToF.java

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Arrays;

public class IToF {

String[] in=null,post=null,stack=null;

int top=0;

//constructor

public IToF(String[] infix) {

// copy infix to in and add ')' to end

in=new String[infix.length+1];

for(int i=0;i<infix.length;i++)

in[i]=infix[i];

in[in.length-1]=")";

post=new String[infix.length];

stack=new String[infix.length+2];

top=-1;

}

//stack push

void push(String x) {

top=top+1;

stack[top]=x;

}

//stack pop

String pop() {

String x;

x=stack[top];

top=top-1;

return x;

}

//check a character is operator

boolean isop(String x) {

if(x.equals("+")||x.equals("-")||x.equals("*")||x.equals("/")||x.equals("^"))

return true;

else

return false;

}

// return the precedence of operator

int pre(String x) {

int res=0;

if(x.equals("+")||x.equals("-"))

res=1;

else if(x.equals("*")||x.equals("/")||x.equals("^"))

res=2;

return res;

}

//convert infix to postfix

String[] in_to_post() {

int i=0,j=0;

String x="";

  

push("(");

while(top!=-1) {

if(isop(in[i])) {

x=pop();

while(isop(x) && (pre(in[i])<pre(x))) {

post[j]=x;

j=j+1;

x=pop();

}

push(x);

push(in[i]);

}

else if(in[i].equals("(")) {

push("(");

}

else if(in[i].equals(")")) {

x=pop();

while(!x.equals("(")) {

post[j]=x;

j=j+1;

x=pop();

}

}

else {

post[j]=in[i];

j=j+1;

}

i++;

}

//return the postfix element

return Arrays.copyOf(post, j);

}

public static void main(String[] args) throws IOException {

//Read from input.txt

FileReader fd=null;

FileWriter fw=null;

try {

fd=new FileReader(new File("input.txt"));

fw=new FileWriter(new File("output.txt"));

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if(fd!=null) {

BufferedReader br=new BufferedReader(fd);

String line;

while((line=br.readLine())!=null) {

String[] in=line.split(" ");

IToF ob=new IToF(in);

String[] post=ob.in_to_post();

if(fw!=null) {

System.out.print("Hit");

for(int i=0;i<post.length;i++)

fw.write(post[i]+" ");

fw.write(" ");

}

}

}

fw.close();

fd.close();

}

}

//input.txt

( a + b ) * ( c - d ) / e + f ^ g
( a + b ) / ( c - d )
a + b / c - d
( a + b ) ^ c / ( d - e )
a / ( b * c ) / ( d * e )
a / b * c / * e

//Output.txt

a b + c d - e / * f g ^ +
a b + c d - /
a b c / d - +
a b + c d e - / ^
a b c * d e * / /
a b c e * / * /

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote