Is there any one can help me in this assignment. Assignment: Using the java lang
ID: 3669215 • Letter: I
Question
Is there any one can help me in this assignment.
Assignment: Using the java languag. Consider the input file “p4in.txt”has the following contents
(Assuming that all expressions are correct): so using the valueStack ,uses ArrayStack and operatorStack
Here is the inputfile (p4in.txt)
2 + 3
(2+ 3) * 4
2 * 3 / (4 -5)
2 / 3 + (4 -5)
2 / 3 + c -d
2 ^ 3 ^ 4
(2 ^ 3) ^ 4
2 * (3 / 4 + 5)
(2 + 3) / (4 -5)
2 / (3 -4) * 5
2 - (3 / (4 - 5) * 6 + 0) ^ 1
(2 - 3 * 4) / (5 * 6 ^ 0) * 1 + 8
I need to produce the output file named “p4out.txt” similar to the following:
Here is the output sample p4out.txt.
Symbol Table
Variable Value
a 2
b 3
c 4
d 5
e 6
f 0
g 1
h 8
Input Infix Postfix Result
2 + 3 a+b ab+ 5
(2+ 3) * 4 (a+b)*c ab+c* 20
2 * 3 / (4 -5) all value Omitted downhere
2 / 3 + (4 -5)
2 / 3 + c -d
2 ^ 3 ^ 4
(2 ^ 3) ^ 4
2 * (3 / 4 + 5)
(2 + 3) / (4 -5)
2 / (3 -4) * 5
2 - (3 / (4 - 5) * 6 + 0) ^ 1
(2 - 3 * 4) / (5 * 6 ^ 0) * 1 + 8
Explanation / Answer
package expression;
import java.io.File;
import java.io.FileNotFoundException;
import static java.lang.Character.isSpace;
import java.util.Scanner;
import java.util.Stack;
public class Expression {
public static void main(String[] args) throws FileNotFoundException {
char operators[]={'+','-','*','/','^','(',')'};
char alphabets[]={'a','b','c','d','e','f','g','h','i','j'};
Stack valueStack=new Stack();
Stack ArrayStack=new Stack();
Stack operatorStack=new Stack();
File file=new File("p4in.txt");
Scanner scan=new Scanner(file);
while(scan.hasNext())
{
String line=scan.nextLine();
char tokens[]=line.toCharArray();
for(int i=0;i<tokens.length;i++)
{
if(!isCharacter(tokens[i],operators)&& !isSpace(tokens[i]))
{
int item=(int)tokens[i];
if(!inValStack(item,valueStack))
{
valueStack.push(item);
ArrayStack.push(alphabets[ArrayStack.size()]);
}
}
}
}
file=new File("p4in.txt");
scan=new Scanner(file);
File file1=new File("p4out.txt");
Scanner scan1=new Scanner(file);
while(scan.hasNext())
{
String line=scan.nextLine();
char tokens[]=line.toCharArray();
for(int i=0;i<tokens.length;i++)
{
if(!isCharacter(tokens[i],operators)&& !isSpace(tokens[i]))
{
int item=(int)tokens[i];
char key=getCharKey(item,ArrayStack);
}
}
}
System.out.println("");
}
private static boolean isCharacter(char token,char operators[]) {
boolean flag=false;
for(int k=0;k<operators.length;k++)
{
if(token==operators[k])
{
flag=true;
break;
}
}
return flag;
}
private static boolean inValStack(int item, Stack valueStack) {
boolean flag=false;
for(int i=0;i<valueStack.size();i++)
{
if(item==(int)valueStack.peek())
{
flag=true;
break;
}
}
return flag;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.