Could you please take a look at the following code and tell me where I went wrong?
I'm trying to write a program that evaluates a prefix expression using a queue. Below is the function that takes two chars and an operator and performs calculation. The problem here is that when add(divide, multiply, or subtract) two integers, I get a nice int result, but the I have to convert that int back into char in order to store it in a queue. What a mess! I think I'm doing something stupid here, but please take a look!Code:
char calculate(char d1, char d2, char op) {
int num1, num2, answer;
num1 = d1 - '0';
printf("Num1 is %d\n", num1);
num2 = d2 - '0';
printf("Num2 is %d\n", num2);
char result;
switch (op){
case '+': {
answer = num1 + num2;
break;
}
case '-': {
answer = num1 - num2;
break;
}
case '*': {
answer = num1 * num2;
break;
}
case '/': {
answer = num1 / num2;
break;
}
}
printf("Answer is %d", answer); //here it prints a nice answer
result = answer + '0';
printf("Result is %c\n", result); //and here I get a mess!
return result;
}
