Question

In: Computer Science

Write a program to convert a text-file containing expressions (one per line) into post-fix expressions outputted...

Write a program to convert a text-file containing expressions (one per line) into post-fix expressions outputted to a file of your choice using a stack with one space between operators and variables ( one letter variables) and/or constants (one digit constants).

Solutions

Expert Solution

A program in Java programmig language to convert infix expression into post-fix expression.

import java.util.*;
import java.io.*;
  
class Conversion
{
static String infixToPostfix(String expInfix) //method to create infix to postfix
{
String expPostfix = new String("");
Stack <Character> stack = new Stack<>();
for(int i = 0; i < expInfix.length(); ++i)
{
char ch = expInfix.charAt(i);
if (ch == ' ')
{
continue;
}
if (Character.isLetterOrDigit(ch))
expPostfix += ch;
else if (ch == '(')
stack.push(ch);
else if (ch == ')')
{
while (!stack.isEmpty() && stack.peek() != '(')
expPostfix += stack.pop();
  
if (!stack.isEmpty() && stack.peek() != '(')
return "The expresiion is Invalid";   
else
stack.pop();
}
else
{
while (!stack.isEmpty() && Prec(ch) <= Prec(stack.peek()))
{
if(stack.peek() == '(')
return "The expression is invalid";
expPostfix += stack.pop();
}
stack.push(ch);
}

}
while (!stack.isEmpty())
{
if(stack.peek() == '(')
return "The expression is invalid";
expPostfix += stack.pop();
}
return expPostfix;
}
  
static int Prec(char ch)
{
switch (ch)
{
case '-':
case '+':
return 1;

case '/':
case '*':
return 2;

case '%':
return 3;
}
return -1;
}
}
  
public class Test //test class
{
   public static void main(String[] args)
   {
   try
   {
Conversion con = new Conversion();
String exp = "(A + B) * C";
String inputExp = "";
  
FileReader fr=new FileReader("D:\\input.txt"); //read file
int i;
while((i=fr.read())!=-1)
inputExp = inputExp + (char)i;
fr.close();

FileWriter fw=new FileWriter("D:\\output.txt"); //write file
//fw.write(con.infixToPostfix(exp));
fw.write(con.infixToPostfix(inputExp));
fw.close();
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("\nFile created successfully.");
   }
}

OUTPUT:

File created successfully.

Content of output.txt file:

AB+C*


Related Solutions

Write a program to convert a text-file containing expressions (one per line) into post-fix expressions outputted...
Write a program to convert a text-file containing expressions (one per line) into post-fix expressions outputted to a file of your choice using a stack with one space between operators and variables (one letter variables) and/or constants (one digit constants). IN PYTHON please
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
Write a Python program that reads a file, input by the user, containing one word/token per...
Write a Python program that reads a file, input by the user, containing one word/token per line with an empty line between sentences. The program prints out the longest word found in the file along with its length.
create a file with 1000 alphanumeric ones written one per line. Write a program in python...
create a file with 1000 alphanumeric ones written one per line. Write a program in python that randomly selects 100 of them, holds the index of each alphanumeric, adds the hash of the previous 100 and tries to find a random alphanumeric so that if it adds it to the list of indicators the SHA256 of the whole to have 10 zeros at the end. You start with the original Hash: 1111..111 my solution so far: import random import string...
Part I The input to the program will be a text file containing the information for...
Part I The input to the program will be a text file containing the information for a tolerance table. An example follows using the values from the first lecture on tolerance analysis. These values will be stored in a text file. The data is comma delimited, which means that each data field is separated by a comma. If the first word is ‘PART’ the following values are the nominal size, +/- impact, tolerance, and fixed/variable. If the first word is...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Write a program that takes in a line of text as input, and outputs that line...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeH IN C++ PLEASE!
2. Write a program which reads in the text file generated in part one and writes...
2. Write a program which reads in the text file generated in part one and writes out the same data as a comma-separated values (CSV) file for further processing. The file should have a header line which identifies which column contains which values and should look something like this: Time, Potentiometer, Temperature, Light, Switch0, Switch1, Switch2, Switch3 That header line should be followed by detail lines containing the measurements and should look something like this (matching the above Arduino output):...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT