For anyone who comes across this thread in the future with some questions about evaluating a string as an expression, here is what you must do:
1. Convert from infix to postfix, which is a way of getting the expression into a more readable form for the computer.
E.G. for 2 + 2 --postfix is-- 2 2 +
for 3 - 2 * 5 --postfix is-- 3 2 5 * -
for ( 1 + 3 ) * 4 --postfix is-- 1 3 + 4 *
Conversion can be accomplished with a stack( stk ) and a
string( postfix ). The algorithm follows:
I. Push a '(' onto stk (the stack)
II. Add a ')' to the end of the infix input string.
III. While stk is not empty
If current character in infix string is a digit
copy it to postfix
If current char is '(' push it onto stk
If current char is an operator ( +, *, etc. )
*pop operators of equal or higher precedence, and put them
in postfix
*push the operator onto stk
If current char is ')'
*pop all operators (adding them to postfix) until
'(' is encountered
*pop and ignore ')'
So for 3 + 2 * 4:
at beginning:
postfix = empty
stk = (
infix = 3 + 2 * 4 )
loop 1:
postfix = 3
stk = (
infix = + 2 * 4 )
loop 2:
postfix = 3
stk = ( +
infix = 2 * 4 )
loop 3:
postfix = 3 2
stk = ( +
infix = * 4 )
loop 4:
postfix = 3 2
stk = ( + *
infix = 4 )
loop 5:
postfix = 3 2 4
stk = ( + *
infix = )
loop 6:
first:
postfix = 3 2 4 *
stk = ( +
infix = )
then:
postfix = 3 2 4 * +
stk = (
infix = )
then:
'(' and ')' cancel each other out.
I'll explain how postfix may be calculated a little later when I have time. Following is the source code for my postfix converter. Of note is that the current implementation only handles single digit integers.
-----------------Header File--------------------------------------
//this header contains functions to convert a string to postfix
//problem 15.12 p 830 Deitel & Deitel
#ifndef POSTFIXCONVERTER_H
#define POSTFIXCONVERTER_H
#include<string>
using std::string;
#include<stack>
namespace PostfixConverter
{
string pfConvert( string );
bool isOperator( char );
}
#endif
------------------Source File------------------------------------
//function definitions for PostfixConverter namespace
#include "PostfixConverter.h"
//see d & d p 830 for explaination
string PostfixConverter:

fConvert( string infix )
{
const int precedence( char, char );
std::stack< char > signStack;
string postfix = "";
int i = 0;
infix += ')'; //add parenthesis around
signStack.push( '(' ); //the entire equation
while( !signStack.empty() )
{
char current = infix.at( i ); //the current character in infix being evaluated
if( current == ' ' )
;//do nothing
else if( isdigit( current ) )
postfix += current;
else if( current == '(' )
signStack.push( current );
else if( isOperator( current ) )
{
//while there are operators on the stack with equal or higher precedence than current
while( isOperator( signStack.top() ) && precedence( signStack.top(), current ) != -1 )
{
//add the operator to postfix, and pop it
postfix += signStack.top();
signStack.pop();
}
signStack.push( current );
}
else if( current == ')' )
{
//pop until a left parenthesis is found (adding char to postfix)
while( signStack.top() != '(' )
{
postfix += signStack.top();
signStack.pop();
}
//now pop the left parenthesis (discarding it)
signStack.pop();
}
++i;
}
return postfix;
}
bool PostfixConverter::isOperator( char c )
{
return ( c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '^' );
}
const int precedence( char op1, char op2 ) //PostfixConverter::
{
const int pVal( char );
if( pVal( op1 ) > pVal( op2 ) )
return 1;
else if ( pVal( op1 ) == pVal( op2 ) )
return 0;
else
return -1;
}
const int pVal( char op ) //PostfixConverter::
{
int val;
switch( op )
{
case '^':
val = 3;
break;
case '*':
case '/':
case '%':
val = 2;
break;
case '+':
case '-':
val = 1;
break;
default:
val = -1;
}
return val;
}