1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help with evaluating prefix exprs using queue

Discussion in 'C' started by Puppka, Feb 12, 2008.

  1. Puppka

    Puppka New Member

    Joined:
    Feb 12, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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;
    }
     

Share This Page