I am writing a program that turns infix expressions into postfix. And I have to either output to a file provide or output to the screen. The is the program.
#include "stack.hpp"
#include <iostream>
#include <fstream>
String Topostfix(std::istream&);
int main(int argc, char *argv[]){
std::ifstream in(argv[1]) ;
if(!in)
{std::cerr<<"Couldn't open "<<argv[2] << "\n" ;}
String b ;
if(argc !=3){
while(in>>b){
std::cout<<"postfix = "<<Topostfix(in)<<std::endl<<std::endl;
}
}
else{
std::ofstream outputfile(argv[2]) ;
while(in>>b){
outputfile<<"postfix = "<<Topostfix(in)<<std::endl<<std::endl;
}
}
in.close();
return 0;
}
//Takes the file for parameter then reads in and converts each line to postfix while printing out the infix
//Pre: valid input (fully parenthesized and spaces inbetween the characters.
//Post: Printed out the infix spaced and returns the postfix which was the tos.
String Topostfix(std::istream& in){
stack<String> result ;
String right,op,left ;
String cin ;
std::cout<<"infix = (" ;
while(cin != ';'){
if(cin ==')'){
right = result.pop() ;
op = result.pop() ;
left = result.pop() ;
result.push(left+" "+right+" "+op);
}
elseif(cin !='(')
result.push(cin) ;
std::cout<<cin<<" " ;
in>>cin;
}
outputfile<<std::endl;
return result.gettos() -> data ;
}
Now the problem is in the function Topostfix if there was an outputfile given I need to print to it and I was just wondering how to do this rather then just printing to the screen.