#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    21
    Rep Power
    0

    Please help Stack Valiadation


    hi....i know how to do "pop" and "push" for my stack program. But i can't get my validation right. I hope someone can help me on this. Thank you ^_^

    This is what i want to do:

    int valid(char k[], int p)
    check the array containing o characters.
    return true if array has only spaces, digits, or operators
    otherwise return false
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,612
    Rep Power
    4247
    Are you looking for something like this:
    Code:
    #include <ctype.h>
    int valid(char k[], int p) {
        register int i;
        char c;
    
        if (p == 0)
           return 0;
    
        for (i = 0; i < p; i++) {
            c = k[i];
            if ((!isdigit(c)) && (c != ' ') && (c != '+') 
                  && (c != '-') && .... rest of operators ... )
                return 0;
        }
        return 1; 
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    21
    Rep Power
    0
    Scorpions4ever...thanks for helping. Can you or anyone tell me about the different "return 1" and "return -1"?
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,612
    Rep Power
    4247
    >> Can you or anyone tell me about the different "return 1" and "return -1"?

    It entirely depends on the function(s) you are calling. For instance, I used a library of functions a long time ago, that would return 1 on success and negative numbers on failure. The value of the negative number would indicate what the specific cause of failure was. So you could write code like this:
    Code:
    int result;
    
    result = some_func();
    if (result > 0) {
       /* Do something useful
    } else {
       /* Determine the cause of failure from the return value */
       switch (result) {
          case -1:
           printf ("OS Error: Could not  open blah");
           break;
          case -2:
            printf("Phase of moon error");
            break;
          ...
       }
    }
    Then again, these return values depended entirely upon the author of those functions. You can invent your own return values for your functions. Traditionally though, most functions in C usually return a number to indicate success and 0 to indicate failure (though there are significant exceptions like strcmp() in which the meaning of the return values are reversed!). Other functions may return positive numbers to indicate success codes and negative numbers to indicate error codes.

    Hope this helps!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    21
    Rep Power
    0
    thanks for explaning to me. It's very helpful
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Posts
    21
    Rep Power
    0
    Scorpions4ever...or anyone...I just inserted that code wiht my program. But i got all the errors and warnings popping up.:confused:

    it's getting frustrated. I hope someone can help me to check my code. If i have your email, i'll send it to you. Because this code it's too long to put it here.

    Thank you

IMN logo majestic logo threadwatch logo seochat tools logo