Thread: RPN incorrect output

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    RPN incorrect output

    Sorry about that, the title is incorrect. I fixed the conflicting type. Hi everyone, the following code compiles correctly, but does not give the results it should. i.e. 30 5 - 7* should give me 175. Not even coming close can anyone help?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* This program will read Polish notation into the system and then translate it properly. */
    
    #define MAXOP 100
    #define NUMBER '0'   /* flag, signals that a number was found */
    
    double blah(char s[]);
    int getop(char[]);
    void push(double);
    double pop(void);
    
    main() {
    
        int type;
        double op2;     /* placeholder for certain cases to calculate correctly */
        char s[MAXOP];
    
    
        printf("Enter postfix or ^D (control + D) to quit:\n");
        while ((type = getop(s)) != EOF) {
            switch (type) {
            case NUMBER:
                push(blah(s));   /* calls atof to translate to floating first */
                break;
            case '+':
                push(pop() + pop());
                break;
            case '*':
                push(pop() * pop());
                break;
            case '-':
                op2 = pop();
                push(pop() - op2);
                break;
            case '/':
                op2 = pop();
                if (op2 != 0.0)
                    push(pop() / op2);
                else
                    printf("Error. Cannot divide by zero.\n");
                break;
            case '%':
                op2 = pop();
                if (op2 != 0.0)
                    push((double)((int) pop() % (int) op2));
                else
                    printf("Error, Cannot mod by zero.\n");
                break;
            case '\n':
                printf("%.2f\n", pop());
                printf("Enter postfix or ^D to quit:\n");
                break;
            default:
                printf("Error. Enter postfix or ^D to quit:\n");
                break;
            }
        }
        return 0;
    } /* end of main */
    
    
    
    double blah(char s[]) {    /* converts string s to type double */
    
        double val, power;
        int i, sign;
    
        for (i = 0; isspace(s[i]); i++) /* crunches out whitespace */
            ;
        sign = (s[i] == '-') ? -1 : 1;   /* checks for minus sign, options for first character */
        if (s[i] == '+' || s[i] == '-')   /* checks if there is a sign at all, if so bumps over it */
            i++;
        for (val = 0.0; isdigit(s[i]); i++)
            val = 10.0 * val + (s[i] - '0');  /* subtracts out the 30 to account for ascii */
        if (s[i] == '.')
            i++;
        for (power = 1.0; isdigit(s[i]); i++) {
            val = 10.0 * val + (s[i] - '0');
            power *= 10.0;
        }
        return (sign * val / power);   /* returns expression */
    } /* end of atof */
    
    
    
    int getch(void);
    void ungetch(int);
    
    int getop(char s[]) {   /* gets next operator or operand */
    
        int i, c;
    
        while ((s[0] = c = getch()) == ' ' || c == '\t') /* crunches out whitespace */
            ;
        s[1] = '\0';
        if (!isdigit(c) && c != '.')
            return c;        /* it's not a number */
        i = 0;
        if (isdigit(c))     /* gets integer part */
            while (isdigit(s[++i] = c = getch()))
                ;
        if ( c == '.')      /* gets fraction part */
            while (isdigit(s[++i] = c = getch()))
                ;
        s[i] = '0';         /* terminates string */
        if (c != EOF)
            ungetch(c);     /* read too far, need to put back */
        return NUMBER;
    } /* end of getop */
    
    
    #define BUFSIZE 100
    
    char buf[BUFSIZE];
    int bufp = 0;        /* indicates next free position in buffer */
    
    int getch(void) {     /* gets a character */
    
        return (bufp > 0) ? buf[--bufp] : getchar(); /* checks if something is there, gives options */
    } /* end of getch */
    
    
    void ungetch(int c) {    /* pushes character back on input if necessary */
    
        if (bufp >= BUFSIZE)
            printf("Overflow, no space left.\n");
        else
            buf[bufp++] = c;   /* otherwise puts in array, bumps pointer to next available position */
    } /* end of ungetch */
    
    
    
    #define MAXVAL 100
    
    int sp = 0;
    double val[MAXVAL];
    
    void push(double f) {  /* pushes f onto stack */
    
        if (sp < MAXVAL)
            val[sp++] = f;  /* puts f on stack if room */
        else
            printf("Error. Stack is full, can't push.\n");
    } /* end of push */
    
    double pop(void) { /* pops values from stack */
    
        if (sp > 0)
            return val[--sp];
        else {
            printf("Error. Stack is empty.\n");
            return 0.0;
        }
    } /* end of pop */
    Last edited by csharp100; 10-14-2010 at 11:24 AM. Reason: Wrong title

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Isn't it from K&R? May be you don't copy correctly?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Troubleshooting is a critical skill, so learn it well. In this case, check each of the case statements operations, for accuracy, one by one.

    You never want to try and troubleshoot a whole program. Verify each function as you code it up, for accuracy, as well as syntax. When you have a lot of code, lots of functions, and errors, it's a quick ride on the "Dante's Express", to the lower (and hotter) realms.

    That's how I would have to debug your program, and i will decline that honor, so you can practice your new troubleshooting methodology skills! Nobody knows your program better than you do.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by Bayint Naung View Post
    Isn't it from K&R? May be you don't copy correctly?
    Have no clue if it is or not. Not the text I use.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    What is the purpose of this for loop in your "blah" function.
    Code:
        
     for (val = 0.0; isdigit(s[i]); i++)
            val = 10.0 * val + (s[i] - '0');  /* subtracts out the 30 to account for ascii */
    You are also missing an include file (ctype.h) for isspace().

    Jim

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    Troubleshooting is a critical skill, so learn it well. In this case, check each of the case statements operations, for accuracy, one by one.

    You never want to try and troubleshoot a whole program. Verify each function as you code it up, for accuracy, as well as syntax. When you have a lot of code, lots of functions, and errors, it's a quick ride on the "Dante's Express", to the lower (and hotter) realms.
    Agreement. Troubleshooting skills are actually more important than programming skills. C isn't a big language and it's not all that hard to learn (at least the basics). What takes real skill is figuring out what went wrong and, believe me, things do go wrong.

    In my own experience, I spend about 25% of my time writing code, about 50% testing code and the rest is spent scratching my head over why it's not doing what I expected.

    I once wrote a 200 line function that decoded and translated Midi files. The thing worked first time... I nearly fainted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: conflicting types for 'sbp_pos_lookup_1'
    By gerger in forum C Programming
    Replies: 7
    Last Post: 10-05-2010, 11:44 AM
  2. conflicting types for getline() fix
    By saqib_ in forum C Programming
    Replies: 4
    Last Post: 04-05-2010, 04:04 AM
  3. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  4. error: conflicting types for 'basename'
    By samf in forum C Programming
    Replies: 3
    Last Post: 09-20-2007, 09:22 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM