Guest_imported
New member
- Jan 1, 1970
- 0
can someone check out what's wrong with this and provide insight on what to do about it? it's supposed to evaluate a postfix expression stored in exp[]. operands are pushed, then popped when an operator is encountered. the problem lies in passing the values of the operands because they're characters.
void parse()
{
int i, v1, v2, v3;
signed int result;
char c1, c2, c3;
char *x1, *x2, *x3;
i = top = 0;
for (;
{
if (!isOperator(exp))
pushStack(exp[i++]);
else
break;
}
top = i - 1;
//result = calculate(isp[top - 1], isp[top], exp);
c1=isp[top - 1]; c2=isp[top];
*x1=c1; *x2=c2;
v1=atoi(x1); v2=atoi(x2);
result = calculate(v1, v2, exp);
// pop stack without the output:
if (isp[top - 1] != '(' && isp[top - 1] != ')') {
exp[nIdx++] = isp[top - 1];
}
isp[--top] = '\0';
if (isp[0] != '\0') {
for (i++; exp; i++) {
if (isOperator(exp))
c3=exp[i-1];
*x3=c3;
v3=atoi(x3);
result = calculate(result, v3, exp);
//result = calculate(result, atoi((char *)exp[i - 1]), exp);
}
}
printf("\nResult: %d\n", result);
}
int calculate(int op1, char op2, char operand)
{
int result;
switch (operand) {
case '*' :
result = op1 * op2; break;
case '/' :
result = op1 / op2; break;
case '+' :
result = op1 + op2; break;
case '-' :
result = op1 - op2; break;
}
return (result);
}
void parse()
{
int i, v1, v2, v3;
signed int result;
char c1, c2, c3;
char *x1, *x2, *x3;
i = top = 0;
for (;
if (!isOperator(exp))
pushStack(exp[i++]);
else
break;
}
top = i - 1;
//result = calculate(isp[top - 1], isp[top], exp);
c1=isp[top - 1]; c2=isp[top];
*x1=c1; *x2=c2;
v1=atoi(x1); v2=atoi(x2);
result = calculate(v1, v2, exp);
// pop stack without the output:
if (isp[top - 1] != '(' && isp[top - 1] != ')') {
exp[nIdx++] = isp[top - 1];
}
isp[--top] = '\0';
if (isp[0] != '\0') {
for (i++; exp; i++) {
if (isOperator(exp))
c3=exp[i-1];
*x3=c3;
v3=atoi(x3);
result = calculate(result, v3, exp);
//result = calculate(result, atoi((char *)exp[i - 1]), exp);
}
}
printf("\nResult: %d\n", result);
}
int calculate(int op1, char op2, char operand)
{
int result;
switch (operand) {
case '*' :
result = op1 * op2; break;
case '/' :
result = op1 / op2; break;
case '+' :
result = op1 + op2; break;
case '-' :
result = op1 - op2; break;
}
return (result);
}