Using pointers in Inverse polish calculator

This is a discussion on Using pointers in Inverse polish calculator within the C Programming forums, part of the General Programming Boards category; The code works well but I wish to represent the s[0], s[1], s[i], s[++i] using pointer notation and still have ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    35

    Using pointers in Inverse polish calculator

    The code works well but I wish to represent the s[0], s[1], s[i], s[++i] using pointer notation and still have the code working. eg input 4 5 + , output 9 which correct
    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <ctype.h>
    
    #define MAXOP 100 /* max size of operand or operator */
    #define NUMBER '0' /* signal that a number was found */
    #define MAXVAL 100 /* maximum depth of val stack */
    int getop(char []);
    void push(double);
    double pop(void);
    /* reverse Polish calculator */
    main()
          {
        int type;
        double op2;
        double a, b,q;
        char s[MAXOP];
    
         while ((type = getop(s)) != EOF) {
        switch (type) {
        case NUMBER: push(atof(s));
            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: zero divisor\n");
           break;
        case '\n': printf("\t%.8g\n", pop());
           break;
        default: printf("error: unknown command %s\n", s);
                break;
        }
      }
      //getchar();
    return 0;
    }
    
    int sp = 0; /* next free stack position */
    double val[MAXVAL]; /* value stack */
    /* push: push f onto value stack */
    void push(double f)
         {
         if (sp < MAXVAL)
          val[sp++] = f;
         else
         printf("error: stack full, canít push %g\n", f);
         }
    /* pop: pop and return top value from stack */
    double pop(void)
           {
           if (sp > 0)
           return val[--sp];
           else {
                 printf("error: stack empty\n");
           return 0.0;
                 }
            }
            
    void ungetch(int);
    
    int getop(char s[]){
     char *stp;/* I'm trying to use this pointer  for s[0], s[1], s[i], s[s++i] */ 
     stp = s;
             
           int i, c;
    while ((s[0] = c = getchar()) == ' ' || c == '\t'); 
         s[1] = '\0';
         if (!isdigit(c) && c != '.')
         return c; 
         i = 0;
          if (isdigit(c)) 
         while ((isdigit(s[++i] = c = getchar())));
         if (c == '.') 
         while ((isdigit(s[++i] = c = getchar())));
         s[i] = '\0';
                 
         if ( c != EOF)
         ungetch(c);
         return NUMBER;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    I've tried all you suggested about the pointer in this code but it didn't work. I guess it will be better if you can try same and see if it wil work and tell me where the problem is

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You were asked in the last topic to post your latest attempt.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    35

    This was what I did

    Quote Originally Posted by MacGyver View Post
    You were asked in the last topic to post your latest attempt.
    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <ctype.h>
    
    #define MAXOP 100 /* max size of operand or operator */
    #define NUMBER '0' /* signal that a number was found */
    #define MAXVAL 100 /* maximum depth of val stack */
    int getop(char []);
    void push(double);
    double pop(void);
    /* reverse Polish calculator */
    main()
          {
        int type;
        double op2;
        double a, b,q;
        char s[MAXOP];
    
         while ((type = getop(s)) != EOF) {
        switch (type) {
        case NUMBER: push(atof(s));
            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: zero divisor\n");
           break;
        case '\n': printf("\t%.8g\n", pop());
           break;
        default: printf("error: unknown command %s\n", s);
                break;
        }
      }
      //getchar();
    return 0;
    }
    
    int sp = 0; /* next free stack position */
    double val[MAXVAL]; /* value stack */
    /* push: push f onto value stack */
    void push(double f)
         {
         if (sp < MAXVAL)
          val[sp++] = f;
         else
         printf("error: stack full, canít push %g\n", f);
         }
    /* pop: pop and return top value from stack */
    double pop(void)
           {
           if (sp > 0)
           return val[--sp];
           else {
                 printf("error: stack empty\n");
           return 0.0;
                 }
            }
            
    void ungetch(int);
    
    int getop(char s[]){
     char *stp;/* I'm trying to use this pointer  for s[0], s[1], s[i], s[s++i] */ 
     stp = s;
             
           int i, c;
    while ((*(s) = c = getchar()) == ' ' || c == '\t'); 
         *(s+1) = '\0';
         if (!isdigit(*(s)) && c != '.')
         return *(s); 
         i = 0;
          if (isdigit(*(s))) 
         while ((isdigit(*(s+(++i)) = c = getchar())));
         if (*(s) == '.') 
         while ((isdigit(*(s+(++i)) = c = getchar())));
         *(s+i) = '\0';
                 
         if ( *(s) != EOF)
         ungetch(c);
         return NUMBER;
    }

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    35
    I've gotten it don't worry, thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  2. Reverse Polish Calculator
    By BlasterStar in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2003, 10:12 PM
  3. Java Calculator
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 11-12-2002, 07:39 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. c++ Reverse Polish Calculator Help
    By knight101 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2001, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21