Question

Write in C++: Write a program to convert a text-file containing expressions (one per line) into...

Write in C++:

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).

Homework Answers

Answer #1

//------------ main.cpp --------
#include<bits/stdc++.h>
#include<fstream>
using namespace std;

//function that returns the precedence as an integer.
int getPrecendence(char c)
{
switch(c)
{
case '+':
return 1;
break;
case '-':
return 1;
break;
case '*':
return 2;
break;
case '/':
return 2;
break;
case '^':
return 3;
break;
default:
return -1;
break;
}
}
//function that returns the type of given char
//like variable or contant or operator or ( or ) or something else.
int getType(char ch)
{
if((ch >='a' && ch <='z') || (ch >='A' && ch <='Z'))
{
return 0;
}
else if(ch >='0' && ch <='9')
{
return 0;
}
else if(ch == '(')
{
return 1;
}
else if(ch == ')')
{
return 2;
}
if(getPrecendence(ch) != -1)
{
return 3;
}
else
{
return -1;
}
}
//function that takes a string in infix notation
//and returns the postfix expression for it.
bool convertInfixToPost(string infix,string &res)
{
string postfix = "";
stack<char> st;
  
int len = infix.length();
  
st.push('$');
res ="";
int charType;
char ch;
for(int i =0;i<len;i++)
{
charType = getType(infix[i]);
if(charType == 0)
{
res += infix[i];
}
else if(charType == 1)
{
st.push('(');
}
else if(charType == 2)
{
while(st.top() != '$' && st.top() !='(')
{
ch = st.top();
st.pop();
res += ch;
}
if(st.top() == '(')
{
ch = st.top();
st.pop();
}
}
else if(charType == 3)
{
while(st.top() != '$' && getPrecendence(infix[i]) <= getPrecendence(st.top()))
{
ch = st.top();
st.pop();
res += ch;
}
st.push(infix[i]);
}
else
{
cout<<"\nInvalid character in expression: "<<infix[i]<<endl;
cout<<"Invalid Expression. Excluding this expression: "<<infix<<endl;
return false;
}
}
while(st.top() != '$')
{
ch = st.top();
st.pop();
res += ch;
}
return true;
}
//function that loads the input file and write the valid expressions
// to output file.
void loadAndWrite(string inpFile,string outFile)
{
ifstream inp(inpFile.c_str());
ofstream out(outFile.c_str());
if(inp.fail())
{
cout<<"\nUnable to open file: "<<inpFile<<endl;
return;
}
  
string line;
string post="";
cout<<"\n";
while(getline(inp,line))
{
if(convertInfixToPost(line,post))
{
cout<<"Equivalent Postfix expression for expression: "<<line<<" is: "<<post<<endl;
out << post<<"\n";
}
}
inp.close();
out.close();
}
int main()
{
//load expressions from inp.txt and write to out.txt.
   loadAndWrite("inp.txt","out.txt");
   return 0;
}
//----------- SAMPLE OUTPUT ---------

/*

//------- INP.TXT -----

a+b+c
a/b^c
a-b+c*d
1+2^3/6

//------------- OUT.TXT ------

ab+c+
abc^/
ab-cd*+
123^6/+

*/

//PLEASE LIKE THE ANSWER AND COMMENT IF YOU HAVE ANY DOUBTS.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program in C++ to convert a text-file containing expressions (one per line) into post-fix...
Write a program in C++ 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). parentheses data 8*a+b-c+d/e a+b*c+d/e+f ∧ g a+b/4-d a-b ∧ c/d+e a/b*c/d*e
C# Reading from Files Write a program to open a text file containing information about buildings....
C# Reading from Files Write a program to open a text file containing information about buildings. The program must display all the buildings in the file and then find the lowest cost building based on the cost per square foot. Format for one building: <building name> <size> sqft $<cost> Example: Allgood Hall 35000 sqft $8,250,099.75 Create the text file and add three or more of your favorite buildings.
C# Reading from Files Write a program to open a text file containing information about buildings....
C# Reading from Files Write a program to open a text file containing information about buildings. The program must display all the buildings in the file and then find the lowest cost building based on the cost per square foot. Format for one building: <building name> <size> sqft $<cost> Example: Allgood Hall 35000 sqft $8,250,099.75 Create the text file and add three or more of your favorite buildings.
SOLUTION IN C## Write a program that reads every line in a text file, line by...
SOLUTION IN C## Write a program that reads every line in a text file, line by line. Obtain the name of the existing file and the name of the new (copy) of the file by prompting the user for both of these on the console. Make sure that the original file exists and can be read. If not, display a warning message and allow the user to either abort the program or enter a new file name. If the name...
Write a Java program that reads words from a text file and displays all the words...
Write a Java program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. 1. You must use one of following Concrete class to store data from the input.txt file. Vector, Stack, ArrayList, LinkedList 2. To sort those words, you should use one of existing interface methods available in Collection or List class.
Attached is a text file containing lots of randomly generated numbers in no particular order. Write...
Attached is a text file containing lots of randomly generated numbers in no particular order. Write a program that can open that file, sort the numbers from least to greatest, and display them in sorted order on the screen. The name of the text file is; numbers_fall2020.txt Please solve using C++ Thanks!
Project File Processing. Write a program that will read in from input file one line at...
Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces,...
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full...
WRITE A C++ PROGRAM TO IMPLEMENT THE CONCEPT OF INDEX (Create index in text file) full code
Assignment 3 Write a C/C++ windows system program to move a file to a new location...
Assignment 3 Write a C/C++ windows system program to move a file to a new location using the MoveFile function. Write a C/C++ windows system program to delete a file using the DeleteFile function. Upload a ZIP folder containing your source codes.
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...