bu*******@hotmail.com (Henry Jordon) wrote in message news:<aa**************************@posting.google. com>...
void evaluates(char *postfix)
'postfix' should have type 'const char *'. Also, 'evaluates' (why
"s"?) should either return the answer (probably as a double, but not
necessarily), or it should take a 'result' parameter and return a
status code: 0 for success, and a non-zero value otherwise.
{
int position;
char number1, number2, number3=0;
char symbol, answer;
stack number_stack;
Some consider it bad practice to use 'stack' since the STL defines a
'std::stack' type. If you import the 'std' namespace (at some point),
you might run into problems. It's better to use 'Stack' or 'my_Stack'
or some namespace-qualified stack type. Also, since you are
*evaluating* the expression, I would expect 'number_stack' to be a
stack of 'int' values, and similarly, your temporary variables should
have type 'int' as well.
for(position=0; postfix[position] !='\0'; position++)
'position' is local to this loop. Therefore, you may declare it inside
the 'for' statement. However, since 'postfix' is a null-terminated
string, a common idiom is to increment and dereference 'postfix':
while(char symbol = *postfix)
Likewise, 'number1', 'number2', and 'number3' are local to the for
loop.
{
symbol=postfix[position];
[rearranged]
if(symbol=='0'
|| symbol=='1'
|| symbol=='2'
|| symbol=='3'
|| symbol=='4'
|| symbol=='5'
|| symbol=='6'
|| symbol=='7'
|| symbol=='8'
|| symbol=='9')
More concisely expressed as
if(std::strchr("0123456789", symbol) != 0) {
number_stack.push(symbol);
}
else
{
number2=number_stack.pop();
number1=number_stack.pop();
if(symbol=='+')
{
number3=number1+number2;
}
else if(symbol=='-')
{
number3=number1-number2;
}
else if(symbol=='*')
{
number3=number1*number2;
}
else if(symbol=='/')
{
number3=number1/number2;
What if 'number2' is 0? Do you want to assert? }
else if(symbol=='^')
{
number3=number2*number2;
}
number_stack.push(number3);
You don't really need 'number3' since you can just 'push' the result
back onto the stack in every operator branch.
}
}
answer=number_stack.pop();
The error comes here cannot conver char to const char*. I want to
convert the answer into an integer value, not ASCII values
cout<<"The answer evaluates to: "<<atoi(answer)<<endl;
}
Printing the result to standard output makes this function very
difficult to programmatically verify. How are you planning to test
this function? If you returned the result in a pararmeter (and
returned a status code from the function), you can easily test each
operand branch as well as correct execution for invalid expressions.
For example, assuming the function
int evaluate(const char *postfix, double *result);
// Evalute the specified 'postfix' mathematical expression
// and store the result in the specified 'result' parameter.
// Return 0 on success, and a non-zero value otherwise. In
// particular, if 'postfix' is an invalid or incomplete
// expression, a non-zero value is returned, and the value
// of 'result' is indeterminate. The behavior is undefined
// unless 'postfix' is a null-terminated string and 'result'
// is a valid pointer.
and a working (tested) function
std::string infix2postfix(const char *infix);
You can test (portions of) 'evaluate' as follows:
// Basic Evaluation Test
const struct {
const char *d_infix; // valid infix expression
double d_result; // result of 'infix' evaluation
} DATA[] =
{
// infix result
//------------------ -----------------
{ "0.0", 0.0 },
{ "1.0", 1.0 },
{ "12.0", 12.0 },
{ "123.0", 123.0 },
{ "0.1", 0.1 },
{ "0.12", 0.12 },
{ "0.123", 0.123 },
{ "1.0 + 2.0", 1.0 + 2.0 },
{ "3.0 - 4.0", 3.0 - 4.0 },
{ "5.0 * 6.0", 5.0 * 6.0 },
{ "7.0 / 8.0", 7.0 / 8.0 },
{ "1.0 + 2.0 - 3.0", 1.0 + 2.0 - 3.0 },
{ "1.0 + 2.0 * 3.0", 1.0 + 2.0 * 3.0 },
{ "1.0 + 2.0 / 3.0", 1.0 + 2.0 / 3.0 },
{ "1.0 - 2.0 * 3.0", 1.0 - 2.0 * 3.0 },
{ "1.0 - 2.0 / 3.0", 1.0 - 2.0 / 3.0 },
{ "1.0 * 2.0 / 3.0", 1.0 * 2.0 / 3.0 },
{ "1.0 * 2.0 + 3.0", 1.0 * 2.0 + 3.0 },
{ "1.0 / 2.0 - 3.0", 1.0 / 2.0 - 3.0 },
};
enum { DATA_SIZE = sizeof DATA / sizeof *DATA };
for(int i=0; i < DATA_SIZE; ++i){
std::string postfix = infix2postfix(DATA[i].d_infix);
const double RESULT = DATA[i].d_result;
double result;
int rc = evaluate(postfix.c_str(), &result);
if(veryVerbose){
std::cout << '\t'
<< "i = " << i << ' '
<< "rc = " << rc << ' '
<< "infix = " << infix << ' '
<< "RESULT = " << result << ' '
<< "result = " << result
<< std::endl;
}
assert(0 == rc && RESULT == result);
}
HTH, /david