【问题标题】:RPN Calculator in CC语言中的RPN计算器
【发布时间】:2016-07-22 06:00:45
【问题描述】:

有人可以解释为什么我在 myString 上使用 strok() 后总是乱码吗?我的输出图片在底部。我尝试将 myString 中的每个元素初始化为 NULL 字符,但我的代码仍然不起作用:(

预期输出:

我的输出:

int main()
{
    int i;
    char myString[60];
    char *token;
    float result;
    float x;
    float y;
    struct Stack myStack;
    StackInit(&myStack);


    BOARD_Init();

    printf("\nWelcome to my RPN calculator.\n");
    printf("Enter floats and + - / * in RPN format: \n >");

    for (i = 0; i < sizeof (myString); i++) {
        myString[i] = '\0';
    }

    fgets(myString, sizeof (myString), stdin);



    token = strtok(myString, " ");
    while (token != NULL) {
        printf("%f\n", atof(token));
        token = strtok(NULL, " ");

        if (atof(token) != 0) {
            StackPush(&myStack, atof(token));
        } else if (*token == '+') { // if token is addition
            StackPop(&myStack, &y);
            StackPop(&myStack, &x);
            result = x + y;
            StackPush(&myStack, result);

        } else if (*token == '-') { // if token is subtraction
            StackPop(&myStack, &y);
            StackPop(&myStack, &x);
            result = x - y;
            StackPush(&myStack, result);
        } else if (*token == '*') { // if token is multiplication
            StackPop(&myStack, &y);
            StackPop(&myStack, &x);
            result = x * y;
            StackPush(&myStack, result);
        } else if (*token == '/') { // if token is division
            StackPop(&myStack, &y);
            StackPop(&myStack, &x);
            result = x / y;
            StackPush(&myStack, result);
        }

    }




while (1);

}

【问题讨论】:

  • 请不要张贴文字输出的图片。将其作为文本粘贴到问题本身中。这使得其他人可以更轻松地复制它以供 cmets/questions 中参考。
  • 另外,请发帖minimal reproducible example。我们不能评论我们看不到的代码。完整的代码还允许我们自己运行它并查看/调试程序。
  • while (token != NULL) { printf("%f\n", atof(token)); token = strtok(NULL, " "); : 第一个令牌被丢弃。
  • 是否删除了一些相关代码?看不到这段代码如何产生上述输出。
  • @JohnSnow - 您发布的代码无法生成您发布的输出。你应该解决这个问题。 BLUEPIXY 指出的问题与您的原始代码有关。在该代码中,您在循环内调用strtok,然后再使用在循环外获得的token 的值。换句话说:token = strtok(NULL, " "); 应该是循环内的最后一行。

标签: c calculator rpn


【解决方案1】:

正如@BLUEPIXY 在评论中指出的那样,您的代码在这里有问题:

token = strtok(myString, " ");   // Here you get the first sub string
while (token != NULL) {
    printf("%f\n", atof(token));
    token = strtok(NULL, " ");   // Here you get the second sub string
                                 // and throw away the first substring
                                 // So all the code below is never executed
                                 // for the first substring.
                                 // In other words: You never push the first
                                 // float to the stack

    if (atof(token) != 0) {
        StackPush(&myStack, atof(token));

你应该做的是将token = strtok(NULL, " ");移动到循环的末尾:

while (token != NULL) {
    printf("%f\n", atof(token));
    // Not here............. token = strtok(NULL, " ");

    if (atof(token) != 0) {
        StackPush(&myStack, atof(token));
    } else if (*token == '+') { // if token is addition
    //...
    //...
    }

    token = strtok(NULL, " ");    // but here
}

【讨论】:

  • 我添加了一个 printf ("%f", myStack);在令牌之前 = strtok(NULL, " ");我的结果是打印了三遍,你知道为什么吗?
  • @JohnSnow - printf ("%f", myStack); ?这应该给你一个很大的警告!您不能使用 %f 打印 struct - 这仅适用于浮点数和双精度数。您应该始终打开所有警告。对于gcc,请始终使用gcc -Wall ...
  • 它确实给了我一个错误但是我将如何只打印出我堆栈中的最后一个东西?我会返回结果吗? ?
  • @JohnSnow - 我无法确定,因为我没有堆栈代码。但也许你可以这样做:float t = StackPop(..); printf("%f", t); StackPush(.., t) ; 即弹出元素,打印它,然后再次推回它。无论如何 - 我认为你应该为那部分提出一个新问题。
猜你喜欢
  • 1970-01-01
  • 2012-05-14
  • 2017-01-12
  • 1970-01-01
  • 2013-12-03
  • 1970-01-01
  • 2014-05-04
  • 2013-10-18
  • 2016-01-22
相关资源
最近更新 更多