Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Need help with evaluating prefix exprs using queue (http://www.go4expert.com/forums/help-evaluating-prefix-exprs-using-queue-t8792/)

Puppka 13Feb2008 03:26

Need help with evaluating prefix exprs using queue
 
Hi guys,

Could you please take a look at the following code and tell me where I went wrong? :crazy: 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;
}



All times are GMT +5.5. The time now is 22:38.