baumgc
April 9th, 2008, 03:49 PM
The title should have been "Stack Postfix Calculator/Infix Converter." Sorry for any confusion this creates.
So I'm making a stack based post fix calculator. It's pretty basic, but I'm having some problems. My stack header is fine..., it's a template that has been proven to work in many other programs I've made, so that isn't the problem.
I am reading the file where lines are something like this:
5 4 +
11 2 -
3 3 *
18 2 /
etc.
My problem is trying to make it work for numbers that are larger than one digit (thus all the loops). Also, for whatever reason, my program captures spaces, and misc text within the files. I figured that since I used my or's (0 || 1 || 2 || 3 || 4 etc., or * || + || - || / etc.), it would ignore everything else, but it doesn't.
The help I've got elsewhere (from friends, various programming professionals, etc.) says that the logic behind my code makes sense, and that it should work, but they don't know why it doesn't.
Here is my code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include "Stack.h"
using namespace std;
//Global Stuff
ifstream infile;
ofstream outfile;
int main(int argc, char *argv[])
{
Stack<string> ss;
char file[45];
string line;
string k;
cout << "What is the file name, including extension (ex ex1.txt): ";
cin >> file;
infile.open(file);
if (!infile)
{
cout << "The file does not exist." << endl;
exit(0);
}
else
{
cout << "Opening file..";
while(!infile.eof())
{
getline(infile,line);
string hold = "";
string operators = "";
for(int i = 0; i < line.length(); i++)
{
for (;i < line.length(); i++)
{
if (line[i] == '0' || '1'|| '2' || '3' || '4' || '5' || '6' || '7' || '8' || '9')
hold = hold + line[i];
cout << hold;
}
ss.push(hold);
for (; i < line.length(); i++)
{
if (line[i] == '+' || '-' || '*' || '/')
operators = hold + line[i];
}
ss.push(operators);
}
}
}
//This is for test purposes to check what's in my stack
while (!ss.isEmpty())
{
if (!ss.pop(k))
cout << "Failed to pop!!" << endl;
else
cout << "Popped: " << k << endl;
}
}
If you -really- want my stack.h, I could include it. It's an array based stack for ease. I know linked stacks are better, but I've got this one and it works, so I might as well use it. Heck, I can always come back and change it later.
Thanks for your assistance.
PS: Once I get this working, then I'l start popping, re-arranging stuff in infix form, and finally calculation.
So I'm making a stack based post fix calculator. It's pretty basic, but I'm having some problems. My stack header is fine..., it's a template that has been proven to work in many other programs I've made, so that isn't the problem.
I am reading the file where lines are something like this:
5 4 +
11 2 -
3 3 *
18 2 /
etc.
My problem is trying to make it work for numbers that are larger than one digit (thus all the loops). Also, for whatever reason, my program captures spaces, and misc text within the files. I figured that since I used my or's (0 || 1 || 2 || 3 || 4 etc., or * || + || - || / etc.), it would ignore everything else, but it doesn't.
The help I've got elsewhere (from friends, various programming professionals, etc.) says that the logic behind my code makes sense, and that it should work, but they don't know why it doesn't.
Here is my code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include "Stack.h"
using namespace std;
//Global Stuff
ifstream infile;
ofstream outfile;
int main(int argc, char *argv[])
{
Stack<string> ss;
char file[45];
string line;
string k;
cout << "What is the file name, including extension (ex ex1.txt): ";
cin >> file;
infile.open(file);
if (!infile)
{
cout << "The file does not exist." << endl;
exit(0);
}
else
{
cout << "Opening file..";
while(!infile.eof())
{
getline(infile,line);
string hold = "";
string operators = "";
for(int i = 0; i < line.length(); i++)
{
for (;i < line.length(); i++)
{
if (line[i] == '0' || '1'|| '2' || '3' || '4' || '5' || '6' || '7' || '8' || '9')
hold = hold + line[i];
cout << hold;
}
ss.push(hold);
for (; i < line.length(); i++)
{
if (line[i] == '+' || '-' || '*' || '/')
operators = hold + line[i];
}
ss.push(operators);
}
}
}
//This is for test purposes to check what's in my stack
while (!ss.isEmpty())
{
if (!ss.pop(k))
cout << "Failed to pop!!" << endl;
else
cout << "Popped: " << k << endl;
}
}
If you -really- want my stack.h, I could include it. It's an array based stack for ease. I know linked stacks are better, but I've got this one and it works, so I might as well use it. Heck, I can always come back and change it later.
Thanks for your assistance.
PS: Once I get this working, then I'l start popping, re-arranging stuff in infix form, and finally calculation.