Before you start doin' the postfix calculations with the stack I would make sure you can read the file in and assign all the variables properly?
Example
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <fstream>
#include <limits>
using namespace std;
bool myCheck ( string );
bool isNumber ( char ch );
int getIndex ( string ch );
int main()
{
ifstream read ( "c:\\temp\\575_prog2_data.txt" );
string crap;
int vals[26] = {0};
int placeHolder = 0;
while ( read >> crap )
{
if ( ( myCheck ( crap ) == false )
&& ( crap.length() > 1 ) )
{
cout << "Postfix Expression maybe?:" << crap << endl;
}
else if ( ( myCheck ( crap ) == false )
&& ( crap.length() == 1 ) )
{
cout << "Var:" << crap << endl;
placeHolder = getIndex ( crap );
}
else
{
cout << "integer:" << crap << endl;
istringstream myStream ( crap );
int j;
myStream >> j;
vals[placeHolder] = j;
}
}
read.close();
string letterz = "abcdefghijklmnopqrstuvwxyz";
cout << "\n";
for ( int i = 0; i < 26; i++ )
{
cout << letterz[i] << "=" << vals[i] << " ";
}
cin.get();
}
bool myCheck ( string t )
{
int length = t.length();
int counter = 0;
for ( int i = 0; i < length; i++ )
{
if ( isNumber ( t[i] ) == true )
{
counter++;
}
}
if ( counter == length )
return true;
else
return false;
}
bool isNumber ( char ch )
{
if ( ( ch >= '0' ) && ( ch <= '9' ) )
return true;
else
return false;
}
int getIndex ( string ch )
{
string letterz = "abcdefghijklmnopqrstuvwxyz";
int t = letterz.length();
for ( int i = 0; i < t; i++ )
{
if ( ch == letterz.substr ( i, 1 ) )
{
return i;
}
}
}
My output
Var:a
integer:4
Var:d
integer:9
Var:z
integer:17
Var:y
integer:0
Var:x
integer:2
Postfix Expression maybe?:aD+
Postfix Expression maybe?:ad+zy-x/*
Postfix Expression maybe?:xz*yd+ad-*+
Postfix Expression maybe?:ab+
Postfix Expression maybe?:ad*yz*+ad*yz*-*
Postfix Expression maybe?:adz*+yx/-
Postfix Expression maybe?:zy/
Postfix Expression maybe?:adx^+
a=4 b=0 c=0 d=9 e=0 f=0 g=0 h=0 i=0 j=0 k=0 l=0 m=0 n=0 o=0 p=0 q=0 r=0 s=0 t=0
u=0 v=0 w=0 x=2 y=0 z=17
Last edited by iamthwee; Oct 3rd, 2007 at 5:27 pm.
Reputation Points: 1536
Solved Threads: 431
Posting Expert
Offline 5,865 posts
since Aug 2005