简体   繁体   中英

Evaluating a PostFix Expression in C (New Issue)

My previous question can be found here (just want to provide assurance to this most helpful community that I'm not trying to spam questions):

Evaluating a postfix Expression in C

My problem involves evaluating postfix expressions. Say I have a postfix expression such as:

3 2 1 2 + ^ ^

I'm trying to store the values (all user inputted as a string of characters) in a stack and through the use of other functions, I intend on evaluating it with the final result being the only remaining element in the stack to be popped out and presented. After a couple hours of fiddling with the debugger in Code Blocks, I've discovered that the function is not storing anything in the stack. Furthermore, when I use my functions to check if the character being pointed to is an operand, despite being one, it ignores the appropriate actions it should take. Here is the code I'm using with the isOperand function included as well:

bool isOperand(char *str)
{
    /** For value 3, str seems to be the entire character string
    being "3 2 1 2 + ^ ^" **/
    return isdigit(str) != 0;
}

int evaluatePostfix(char *postfixStr)
{
    stack * s;
    int x, y, z;

    stackInit(&s);

    while(postfixStr != NULL) {
        /** For the first value 3, it SHOULD be an operand and
        proceed to push it on the stack. But it just skips
        this condition. **/
        if(isOperand(postfixStr)) {
            stackPush(&s, postfixStr);
        }

        if(isOperator(postfixStr) == 1) {
            y = atoi(stackPop(&s));
            x = atoi(stackPop(&s));
            char *str = malloc(10 * sizeof(char));
            sprintf(str, "%d", applyOperator(x, y, postfixStr));
            stackPush(&s, str);
        }
        ++postfixStr;
    }

    z = stackPop(s);
    stackDestroy(&s);
    return z;
}

I don't know how to send it just the one value at the pointer apparently. And in the case it were double or even triple digits, how could I tell the program to recognize that? Again, thank you for your time, I certainly appreciate all the help people provide.

****EDIT/SOLUTION FOUND****

Well, since I'm not permitted to answer my own question yet being a "new user", I will instead edit the original post so that perhaps it will be of use to someone else in the future.

It would seem tokenization is the appropriate situation here. By using

strtok( string , delimiter )

the function breaks a string into smaller strings separated by what seems to be null characters in-between the spaces. I'm still not 100% sure if it's because I used a delimiter of a string space (aka " "), but regardless.

Ending the loop with

token = strtok(NULL, " ");

instead of incrementing the pointer by 1 works well enough because it seems to be taking the original string and directly modifying it when it pushes the smaller string portions into the stack by removing it entirely from the original string. While not being good practice (as I should instead create a copy of the original string to preserve the integrity of the input I feel and modify the copy instead), I believe it will work for the scope of this project. I just wanted to share my findings in case it may be of use to anyone in the future. Also, if there's some things in my description of use that need to be corrected, by all means, I would love to know.

You're calling isdigit on a pointer,

return isdigit(str) != 0;

That should be called on the pointee,

return isdigit(*str) != 0; // or, equivalently: return isdigit(*str);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM