Now I've got a new problem, the queue has a character array, and its getting tricky to get the digits in and out, without being treated as something other than digits.
If I use - 4 2 + 7 as input, it works fine, gets a result of 9.
However, if I try to put in extra numbers/operands, the program starts to churn out random letters and numbers (if it doesn't go into an infinite loop).
This is my function for evaluating the prefix equations. Any help would be greatly appreciated!
Code:
int preEval(char buffer[])
{
int i, result, length, num1, num2;
char opr, opr1, temp1, temp2, token[2], item;
QUEUE q;
initQueue(&q);
for (i = 0; i < strlen(buffer); i++)
{
enQueue(&q, buffer[i]);
}
do
{
length = q.rear - q.front;
if((!isdigit(q.data[q.front])) && (isdigit(q.data[q.front+1])) && (isdigit(q.data[q.front+2])))
{
deQueue(&q, &opr);
deQueue(&q, &temp1);
token[0] = temp1;
token[1] = '\0';
num1 = atoi(token);
deQueue(&q, &temp2);
token[0] = temp2;
token[1] = '\0';
num2 = atoi(token);
if (opr == '+')
result = num1 + num2;
else if (opr == '-')
result = num1 - num2;
else if (opr == '*')
result = num1 * num2;
else if (opr == '/')
result = num1 / num2;
else if (opr == '%')
result = num1 % num2;
//printf("Result: %d\n", result);
result = result + '0';
enQueue(&q, result);
}
else
{
deQueue(&q, &opr1);
enQueue(&q, opr1);
}
}
while(length > 1);
//printf("num1: %d\n", num1);
//printf("num2: %d\n", num2);
while(deQueue(&q, &item))
{
printf("%c\n", item);
}
return(0);
}