Write a C++ program that reads string expressions from an input file \"prefix.tx
ID: 3826220 • Letter: W
Question
Write a C++ program that reads string expressions from an input file "prefix.txt" containing prefix expressions (one expression per input line). For each prefix expression read from the input file, the program should: (1) convert the expression to a fully parenthesized infix expression and (2) find the value of the expression. Use a stack to solve the problem. Assume the expressions contain only integer numbers and the *, /, +, - operators. Generate the results in an output file "results.txt" in the form of a table with 3 columns as shown in the example below. Example of an input file "prefix.txt": + 5 * 10 2/*32 - 74 The obtained "results.txt" output file for the above example of input file:Explanation / Answer
Here is the commented code for prefix evaluation and generating infix expression. Sample output is shown. Please don't forget to rate the answer if it helped. Thank you very much.
#include <iostream>
#include <fstream>
#include <sstream>
#include <stack>
#include <iomanip>
using namespace std;
void process(ifstream &infile, ofstream &outfile)
{
string line;
size_t idx,prev=string::npos;
string token;
string operators="+-/*";
stack<int> operands;
stack<string> prefix,infix;
string s1,s2;
int op1,op2,result;
istringstream strstream;
outfile<<"Prefix Expression Infix Expression Value"<<endl;
outfile<<"__________________________________________________________"<<endl;
//for each prefix , since we want to scan tokens from right to left, we make a string stream to extract the tokens
//and push them onto a prefix stack. This allows to see the last token first, effectively scanning from right
while(getline(infile,line))
{
strstream.clear();
strstream.str(line);
//get tokens and push them on prefix stack
while(strstream >> token)
{
prefix.push(token);
}
//now processing from top is equivalent to scanning from right of the prefix expression
while(!prefix.empty())
{
token = prefix.top();
prefix.pop();
//we maintain a stack to generate the infix expression, whenever operator is found, we pop 2 elements and surrornd the operands with
//operator by parenthesis and push it back to infix stack
if(token.size()== 1 && operators.find(token)!=string::npos) //its an operator
{
//numeric evaluation operands, 1st operand on top and 2nd one is below
op1=operands.top();
operands.pop();
op2=operands.top();
operands.pop();
//string tokens for forming infix
s1 = infix.top();
infix.pop();
s2 = infix.top();
infix.pop();
if(token=="+")
{
result=op1+op2;
infix.push("("+s1+"+"+s2+")");
}
else if(token=="-")
{
result=op1-op2;
infix.push("("+s1+"-"+s2+")");
}
else if(token=="/")
{
result=op1/op2;
infix.push("("+s1+"/"+s2+")");
}
else if(token=="*")
{
result=op1*op2;
infix.push("("+s1+"*"+s2+")");
}
operands.push(result);
}
else //its an operand
{
infix.push(token);
operands.push(atoi(token.c_str()));
}
}
//write output to file
outfile<<setw(30)<<left<<line<<setw(30)<<infix.top()<<setw(5)<<operands.top()<<endl;
infix.pop(); //remove the results to clear the stack for next iteration
operands.pop();
}
}
int main()
{
string filename;
cout<<"Enter filename: ";
cin>>filename;
ifstream infile(filename.c_str());
if(!infile.is_open())
{
cout<<"Error opening input file : "<<filename;
exit(1);
}
filename="out-"+filename;
ofstream outfile(filename.c_str());
if(!outfile.is_open())
{
cout<<"Error opening output file : "<<filename;
exit(1);
}
process(infile,outfile);
infile.close();
outfile.close();
}
output
Prefix Expression Infix Expression Value
__________________________________________________________
+ 5 * 10 2 (5+(10*2)) 25
/ * 3 2 - 7 4 ((3*2)/(7-4)) 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.