amol0010
10-23-2009, 09:38 AM
Hi.
http://www.daniweb.com/code/snippet216831.html
The disclaimer is that I am not trying to pass the program as my own, just trying to think whether vectors were not used would there be a way to do it using regular strings ?
This is a link for an expression evaluator in C++.
I was wondering if this function:
void Equation::Next(string tmp)
{
vector <string> array;
int spaces = 0;
for ( int a = 0; a < tmp.length(); a++ )
{
if(tmp[a]==' ')
{
spaces++;
}
}
string token;
istringstream iss(tmp);
while ( getline(iss, token, ' ') )
{
array.push_back(token);
}
stack <string> my_stack;//initialise stack
vector <string> temp;
string ch;
for (int i = 0; i < spaces; i++)
{
string s;
s = array[i]; //make it easier to read
if ((s!="+")&&(s!="*")&&(s!="-")&&(s!="^")&&(s!="/"))
{
my_stack.push(s);
//push numbers onto the stack
}
else //i.e if it encounters an operator
{
my_stack.push(s);//push operator onto stack
for ( int i = 0; i < 3; i++ )
{
temp.push_back(my_stack.top());
my_stack.pop(); //erase from the stack
}
double z;
z = Eval(temp);
ostringstream outs;
outs << z; // Convert value into a string.
ch = outs.str();
my_stack.push(ch);
temp.clear();
}
}
cout << ch;
}
Is there a way to avoid using vectors in this function ?
push_back function is used on the vector here.
vector <string> array;
string token;
istringstream iss(tmp);
while ( getline(iss, token, ' ') )
{
array.push_back(token);
}
string array;
string token;
istringstream iss(tmp);
while ( getline(iss, token, ' ') )
{
array.push_back( (char) token);
}
I tried this code but it didn't work. Is this the right thing to do ? I am typecasting "token" as string here.
This is the error that shows up.
264 `struct std::string' used where a `char' was expected
What is going on here ?
http://www.daniweb.com/code/snippet216831.html
The disclaimer is that I am not trying to pass the program as my own, just trying to think whether vectors were not used would there be a way to do it using regular strings ?
This is a link for an expression evaluator in C++.
I was wondering if this function:
void Equation::Next(string tmp)
{
vector <string> array;
int spaces = 0;
for ( int a = 0; a < tmp.length(); a++ )
{
if(tmp[a]==' ')
{
spaces++;
}
}
string token;
istringstream iss(tmp);
while ( getline(iss, token, ' ') )
{
array.push_back(token);
}
stack <string> my_stack;//initialise stack
vector <string> temp;
string ch;
for (int i = 0; i < spaces; i++)
{
string s;
s = array[i]; //make it easier to read
if ((s!="+")&&(s!="*")&&(s!="-")&&(s!="^")&&(s!="/"))
{
my_stack.push(s);
//push numbers onto the stack
}
else //i.e if it encounters an operator
{
my_stack.push(s);//push operator onto stack
for ( int i = 0; i < 3; i++ )
{
temp.push_back(my_stack.top());
my_stack.pop(); //erase from the stack
}
double z;
z = Eval(temp);
ostringstream outs;
outs << z; // Convert value into a string.
ch = outs.str();
my_stack.push(ch);
temp.clear();
}
}
cout << ch;
}
Is there a way to avoid using vectors in this function ?
push_back function is used on the vector here.
vector <string> array;
string token;
istringstream iss(tmp);
while ( getline(iss, token, ' ') )
{
array.push_back(token);
}
string array;
string token;
istringstream iss(tmp);
while ( getline(iss, token, ' ') )
{
array.push_back( (char) token);
}
I tried this code but it didn't work. Is this the right thing to do ? I am typecasting "token" as string here.
This is the error that shows up.
264 `struct std::string' used where a `char' was expected
What is going on here ?