Thread: Overflow in Stack Counter

  1. #31
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Or! if you REALLY wanted to be creative (which I am all about)

    Example:
    Code:
    typedef float (*operation)(float, float);
    
    float add(float a, float b) { return a + b; }
    float sub(float a, float b) { return a - b; }
    float mul(float a, float b) { return a * b; }
    float div(float a, float b) { return a / b; }
    
    operation isoperator(int c)
    {
      switch(c)
      {
        case '+': return add;
        case '-': return sub;
        case '*': return mul;
        case '/': return div;
        default: return 0;
      }
    }
    
    /* Then in your for loop */
    for(i = 0; i < strlen(input); ++i)
    {
      operation op = isoperator(input[i]);
    
      if(op)
        push((&stk), (*op)(pop(&stk),pop(&stk)));
      else if(isdigit(input[i]))
        push(&stk, (input[i] - '0'));
    }
    Viola.

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I might probably have written it along the lines of this:
    Code:
    typedef float (operation_ptr)(float, float);
    
    float add(float a, float b) { return a + b; }
    float sub(float a, float b) { return a - b; }
    float mul(float a, float b) { return a * b; }
    float div(float a, float b) { return a / b; }
    
    operation_ptr* get_op_function(int c)
    {
        switch(c)
        {
            case '+': return &add;
            case '-': return &sub;
            case '*': return &mul;
            case '/': return &div;
            default: return NULL;
        }
    }
    
    /* Then in your for loop */
    for(i = 0; i < strlen(input); i++)
    {
        operation_ptr* pOp = get_op_function(input[i]);
    
        if (pOp)
            push(&stk, pOp( pop(&stk),pop(&stk) ));
        else if ( isdigit(input[i]) )
            push( &stk, (input[i] - '0') );
    }
    Especially, the name is_operator is misleading (since it returns a function pointer) and the syntax for invoking the function pointer is a little more verbose than necessary.
    But I fear we are at a level that is beyond the understanding of the OP...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #33
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well he opted not to use it at all. I was just trying to copy and paste and spew forth my two cents. Where is the love?

  4. #34
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I love your code
    But it just had a few flaws I wanted to correct...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #35
    Registered User
    Join Date
    Sep 2008
    Posts
    21
    Quote Originally Posted by Elysia View Post
    I might probably have written it along the lines of this:
    Code:
    typedef float (operation_ptr)(float, float);
    
    float add(float a, float b) { return a + b; }
    float sub(float a, float b) { return a - b; }
    float mul(float a, float b) { return a * b; }
    float div(float a, float b) { return a / b; }
    
    operation_ptr* get_op_function(int c)
    {
        switch(c)
        {
            case '+': return &add;
            case '-': return &sub;
            case '*': return &mul;
            case '/': return &div;
            default: return NULL;
        }
    }
    
    /* Then in your for loop */
    for(i = 0; i < strlen(input); i++)
    {
        operation_ptr* pOp = get_op_function(input[i]);
    
        if (pOp)
            push(&stk, pOp( pop(&stk),pop(&stk) ));
        else if ( isdigit(input[i]) )
            push( &stk, (input[i] - '0') );
    }
    Especially, the name is_operator is misleading (since it returns a function pointer) and the syntax for invoking the function pointer is a little more verbose than necessary.
    But I fear we are at a level that is beyond the understanding of the OP...
    I'm learning...

    The compilation is throwing me a syntax error within the calculate function:

    Code:
    float calculate(char *postfix)
    {
       char *operation;
       stack_type stk;
       int operand1;
       int operand2;
       float sum;
       stk.top = -1;
       operation = &postfix[0];
    
       while(1)
       {
          if (strcmp(operation, "exit\n") == 0)
          {
             printf("Time to quit...goodbye!\n");
             exit(0);
          }
    
          else if {                          <-------------------------------- "expected '(' before '{' token"
               switch(*operation)
               {
               case '0':
               case '1':
               case '2':
               case '3':
               case '4':
               case '5':
               case '6':
               case '7':
               case '8':
               case '9':
                  push(&stk, (*operation - 48));
                          break;
               }
               }
          else if
          {                                                <-------------------------------- "expected '(' before '{' token"
             operand1 = pop(&stk);
             operand2 = pop(&stk);
    
    
                switch(*operation)
                {
                case '+':
                   sum = operand1 + operand2;
                break;
                case '-':
                   sum = operand1 - operand2;
                break;
                case '/':
                   sum = operand1 / operand2;
                break;
                case '*':
                   sum = operand1 * operand2;
                break;
                case '%':
                   sum = operand1 % operand2;
                break;
                default:
                   printf("Epic Fail...invalid input.\n");
                return 0;
                }
             push(&stk, sum);
    
          operation++;
       }
       sum = pop(&stk);
       }
       return sum;
    }
    I checked my parenthetic matching and I do not appear to be missing one. Am I syntactically incorrectly declaring a variable?

  6. #36
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mlsrar View Post
    I'm learning...

    The compilation is throwing me a syntax error within the calculate function:


    I checked my parenthetic matching and I do not appear to be missing one. Am I syntactically incorrectly declaring a variable?
    Of course you're missing one, you just happen to be missing the ) as well later on. Once you say "if" you need to say if "what". If you just mean else, then just say else, not else if.

  7. #37
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Heh... logic errors are fun.

    Should I go to the store
    Or go to
    Or go to
    Or

    Or go where? Same type of thing dude. And thank you for the corrections Elysia, I actually do appreciate it since 99.95&#37; of the time I just write code in a response and never compile it or anything. "You're a genius programming master! THANKS THANKS THANKS" (I figured I may as well combine all your fan fare)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM