评价在C后缀防爆pression [英] Evaluating a postfix Expression in C

查看:201
本文介绍了评价在C后缀防爆pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个评估后缀算术EX pression的程序。该程序发送一个字符串到我的功能 evaluatePostfix ,其中进行识别操作数和运算符,并拿出一个整数解。我按下扫描的角色,因为它是确定的,当然需要评估的时候做适当的弹出功能操作栈此计划。现在虽然,我在与程序挂在这似乎是一个无限循环的问题。我想我真的不知道如何告诉函数进行到下一个字符字符串中有否评估第一个字符后。另一个要注意的是,用户将一个空间在其间每个操作数和运算符。这里是我的功能:

I'm trying to write a program that evaluates a postfix arithmetic expression. The program sends a character string to my function evaluatePostfix, which proceeds to identify operands and operators and come up with an integer solution. I am manipulating stacks in this program by pushing the scanned character as it is identified and of course doing the appropriate pop functions when needing to evaluate. Right now though, I'm having a problem with the program hanging in what appears to be an infinite loop. I guess I'm not really sure how to tell the function to proceed to the next character in the string after it has evaluated the first character. Another thing to note is that the user puts a space in-between each operand and operator. Here is my function:

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

    stackInit(&s);

    do {
        if(isOperand(postfixStr) == 1) {
            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);
        }

    } while (postfixStr != NULL);
    return stackPop(s);
}

我知道,操纵栈的功能是正确的,因为它们是由我的老师提供。可能有人也许给我一个线索,我缺少的是什么吗?

I know the functions that manipulate the stack are correct as they were provided by my instructor. Could someone perhaps give me a clue as to what I'm missing?

推荐答案

您可以修改,而条件而(++ postfixStr!= NULL)来递增 postfixStr 指针指向下一个字符。

You could change the while condition to while (++postfixStr != NULL) to increment the pointer to the next character in postfixStr.

这增量是使用preFIX符号( ++ VAR VS VAR ++ ),这样做的下一个字符是比较 NULL 。我不熟悉你所使用的堆栈功能的行为,但我会建议修改做{...}而(++ postfixStr!= NULL); 循环到而(postfixStr!= NULL){...} 循环,增加 postfixStr 在虽然循环的块的结束。

This increment is done using the prefix notation (++var vs var++) so that the next character is compared to NULL. I'm not familiar with the behavior of the stack functions you're using, but I would recommend changing the do { ... } while (++postfixStr != NULL); loop to a while (postfixStr != NULL) { ... } loop, and increment postfixStr at the end of that while loop's block.

做最安全的做法是一个字符串长度参数添加到您的函数:

The safest thing to do is add a string length parameter to your function:

int evaluatePostfix(char *postfixStr, int strLength)

您会然后使用一个循环,从指数 0 索引字符串的开头明确步骤strLength - 1 ,这将安全处理空和非空值终止字符串。

You would then use a loop that explicitly steps from the beginning of the string at index 0 to index strLength - 1, which would safely handle empty and non-NULL-terminated strings.

这篇关于评价在C后缀防爆pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆