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

Need help with tha Java code for this Write a program that converts an infix (fu

ID: 3726941 • Letter: N

Question

Need help with tha Java code for this

Write a program that converts an infix (fully parenthesized) expression to a prefix expression. Like you know, the terms "in" and "pre" are according with the operator's position. The program will have to handle only the binary operators +,-, *, /, ^. parenthesis, letters and numbers. An example would be an expression like: (((A*B)+(2*(CA3))/(2*A)) The program must convert this expression (infix) to the prefix expression: /+*AB*2AC3*2A All expressions of the test cases are expressions with valid syntax. Input Read input from a file "in1.txt". The first line of input is an integer N (N

Explanation / Answer

As per your requirement the below one is solution please follow it

import java.io.*;
import java.util.Scanner;
import java.io.FileNotFoundException;// header file for exceptions
import java.util.*;
//stack for checking priority
class Stack
{
private char[] a;
private int top,m;
public Stack(int max)
{
m=max;
a=new char[m];
top=-1;
}
public void push(char key)
{
a[++top]=key;
}
public char pop()
{
return(a[top--]);
}
public char peek()
{
return(a[top]);
}
public boolean isEmpty()
{
return (top==-1);
}
}
// evaluates the expression before final conversion
class Evaluation
{
private Stack s;
private String input;
private String output="";
public Evaluation(String str)
{
input=str;
s=new Stack(str.length());
}
//priotiry of the operators
public String inToPre()
{
for(int i=input.length()-1;i>=0;i--)
{
char ch=input.charAt(i);
switch(ch)
{
case '+':
case '-':gotOperator(ch,1,')');
break;
case '*':
case '/':gotOperator(ch,2,')');
break;
case ')':s.push(ch);
break;
case '(':gotParenthesis(')');
break;
default:output=ch+output;
}
}
while(!s.isEmpty())
output=s.pop()+output;
return output;
}

private void gotOperator(char opThis,int prec1,char x)
{
while(!s.isEmpty())
{
char opTop=s.pop();
if(opTop==x)
{
s.push(opTop);
break;
}
else
{
int prec2;
if(opTop=='+'||opTop=='-')
prec2=1;
else
prec2=2;
if(prec2<prec1&&x=='(')
{
s.push(opTop);
break;
}
else if(prec2<=prec1&&x==')')
{
s.push(opTop);
break;
}
else
{
if(x==')')
output=opTop+output;
else
output=output+opTop;
}
}
}
s.push(opThis);
}
//dealing with parenthsis
private void gotParenthesis(char x)
{
while(!s.isEmpty())
{
char ch=s.pop();
if(ch==x)
break;
else
{
if(x==')')
output=ch+output;
else
output=output+ch;
}
}
}
}
//driver program
class Parser
{
public static void main(String args[]) throws FileNotFoundException
{
//the input file for procssing
File file =
new File("in1.txt"); // name of the file


String s,str, check="y";
int n;
Evaluation inf;
DataInputStream inp=new DataInputStream(System.in);
Scanner sc = new Scanner(file);
//loop works until end of file
while (sc.hasNextLine())

// input until end of file
{
str = sc.nextLine();
System.out.print("Input expression is: ");
System.out.println(str);
s = str;
inf=new Evaluation(s);
System.out.println("Evaluated Prefix expression:- "+inf.inToPre());   
//
}
}
}

Output of Code:

dps@machine:~/Documents/Chegg$ cat in1.txt
3
(A*2)
((((A*2)+c)-d)/2)
(((2*4)/)(a^b)/(2*c))

dps@machine:~/Documents/Chegg$ java Parser
Input expression is: 3
Evaluated Prefix expression:- 3
Input expression is: (A*2)
Evaluated Prefix expression:- *A2
Input expression is: ((((A*2)+c)-d)/2)
Evaluated Prefix expression:- /-+*A2cd2
Input expression is: (((2*4)/)(a^b)/(2*c))

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