Jim
This post has been edited by jimblumberg: 12 November 2014 - 11:23 AM
New Topic/Question
This topic is locked




Posted 12 November 2014 - 11:22 AM
This post has been edited by jimblumberg: 12 November 2014 - 11:23 AM
Posted 12 November 2014 - 12:02 PM
Posted 12 November 2014 - 12:49 PM
Posted 12 November 2014 - 01:10 PM
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <stack>
using namespace std;
// function prototypes
ifstream& get_ifs(ifstream& ifs);
int main()
{
ifstream ifs;
char c; // input character
if (!get_ifs(ifs))
{ //File failed to open.
return(1);
}
while ((c = ifs.peek()) != EOF) // as long as there is input
{
stack< int > stk; // stack of ints
stk.push(c);// handle one expression
while ((c = ifs.get()) != '\n')
{
if (isspace(c))
{
putchar(c);
c++;
// handle whitespace character
}
else if (isdigit(c)) // is it a digit ?
{
ifs.putback(c); // put it back in the input
int number; // an incoming number
ifs >> number; // read the number
stk.push(number);
cout << number << ' ';
}
else // it must be an operator
{
int a;
int b;
switch (c)
{
case '+':return a + b;
case '-':return a - b;
case '*':return a*b;
case '/':return a / b;
default: return 0;
}
// handle operators +, -, *, and /
// flag invalid operators
}
}
cout << "result is " << stk.top() << '\n';
}
return 0;
}
ifstream& get_ifs(ifstream &ifs) // get input file stream
{
string filename; // input file name
cout << "name of file to read from? ";
cin >> filename;
ifs.open(filename.c_str());
if (!ifs) // cannot open file infilen
{
cerr << "cannot open input file '" << filename << "'\n";
}
return ifs; // return input file stream
}
Posted 12 November 2014 - 01:35 PM
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <stack>
using namespace std;
// function prototypes
ifstream& get_ifs(ifstream& ifs);
int main()
{
// Insert the following lines at the very beginning of main().
ofstream fout("MySpecialFile.txt");
if(!fout)
{
cerr << "Couldn't open the file for output.";
return(1);
}
cout << "SUCCESS" << endl;
return(0);
// ... The rest of your program follows.
ifstream ifs;
This post has been edited by jimblumberg: 12 November 2014 - 01:51 PM
Posted 12 November 2014 - 01:41 PM
Posted 12 November 2014 - 01:51 PM
Posted 12 November 2014 - 02:05 PM
This post has been edited by RFernandez: 12 November 2014 - 02:17 PM
Posted 12 November 2014 - 02:18 PM
Posted 12 November 2014 - 02:21 PM
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <stack>
using namespace std;
// function prototypes
ifstream& get_ifs(ifstream& ifs);
int main()
{
ifstream ifs;
char c; // input character
if (!get_ifs(ifs))
{ //File failed to open.
return(1);
}
while ((c = ifs.peek()) != EOF) // as long as there is input
{
stack< int > stk; // stack of ints
stk.push(c);// handle one expression
while ((c = ifs.get()) != '\n')
{
if (isspace(c))
{
putchar(c);
c++;
// handle whitespace character
}
else if (isdigit(c)) // is it a digit ?
{
ifs.putback(c); // put it back in the input
int number; // an incoming number
ifs >> number; // read the number
stk.push(number);
cout << number << ' ';
}
else // it must be an operator
{
int a;
int b;
switch (c)
{
case '+':return a + b;
case '-':return a - b;
case '*':return a*b;
case '/':return a / b;
default: return 0;
}
// handle operators +, -, *, and /
// flag invalid operators
}
}
cout << "result is " << stk.top() << '\n';
}
return 0;
}
ifstream& get_ifs(ifstream &ifs) // get input file stream
{
string filename; // input file name
cout << "name of file to read from? ";
cin >> filename;
ifs.open(filename.c_str());
if (!ifs) // cannot open file infilen
{
cerr << "cannot open input file '" << filename << "'\n";
}
return ifs; // return input file stream
}
Posted 12 November 2014 - 02:35 PM
name of file to read from? postfix.txt 5 6
Posted 12 November 2014 - 02:38 PM
Posted 12 November 2014 - 02:43 PM
Posted 12 November 2014 - 02:49 PM
