Thread: polish notation calc problem

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    21

    polish notation calc problem

    Hey there. I have been having trouble compiling this Ritchie program on polish notation after i tried adding a mod case to it. it says I am missing a semicolon but I dont understand why. Also, for some reason, when I try to get the input to have a negative sign first or something, it says error. I have been using unix, so maybe that's why?

    PHP Code:
    #include <stdio.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 atof(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(atof(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(int pop() % int op2);
                else
                    
    printf("Error, Cannot mod by zero.\n");
                break;
            case 
    '\n':
                
    printf("%.8f\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 atof(char s[]) {    /* converts string s to type double */

        
    double valpower;
        
    int isign;

        for (
    0isspace(s[i]); i++) /* crunches out whitespace */
            
    ;
        
    sign = (s[i] == '-') ? -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.0isdigit(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.0isdigit(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 ic;

        while ((
    s[0] = getch()) == ' ' || == '\t'/* crunches out whitespace */
            
    ;
        
    s[1] = '\0';
        if (!
    isdigit(c) && != '.')
            return 
    c;        /* it's not a number */
        
    0;
        if (
    isdigit(c))     /* gets integer part */
            
    while (isdigit(s[++i] = getch()))
                ;
        if ( 
    == '.')      /* gets fraction part */
            
    while (isdigit(s[++i] = getch()))
                ;
        
    s[i] = '0';         /* terminates string */
        
    if (!= 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 */ 
    If anyone would be willing to pop this in a compiler and tell me what the heck is going wrong i'd appreciate it. Thanks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    case '%':
                op2 = pop();
                if (op2 != 0.0)
                    push(int pop() % int op2);
    Are you trying to type cast those or what? If so, wrap a set of parenthesis around each int that you have in that line. If not, remove each int from that line.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    I wasn't sure if I had to or not so I left it, but when I did, the same errors came up. Yes I was trying to cast them because if I didn't then a different error saying that illegal type for the case came up.

    I put (int) like you said. Is there any reason for my compiler to report back that there is a semicolon missing? and that else is unrecognizable?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It compiles just fine if you typecast that correctly.
    Code:
    push((int) pop() % (int) op2);
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    Thank you for your help! You are by far the most helpful person on this board! if i am not mistaken you have helped me before and I really do appreciate it

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by deedlit
    Thank you for your help!
    No prob.
    Quote Originally Posted by deedlit
    You are by far the most helpful person on this board!
    *chuckle* If you say so.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Go quzah. Once again he saves the day

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Big Oh Notation problem
    By vaibhav in forum C++ Programming
    Replies: 4
    Last Post: 04-01-2006, 09:02 AM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Major Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 01:06 PM