c - 函数不评估用户输入

标签 c function linked-list stack evaluation

我有这个计算后修复表达式的代码,最初,该代码没有用户输入,但在主函数中已经定义了表达式,如下所示:

 int main() 
{ 
    char exp[] = "100 200 + 2 / 5 * 7 +"; 
    printf ("%d", evaluation(exp)); 
    return 0; 
} 

它会显示答案

但是,当我将代码更改为

int main()
{
    char exp[100];
    printf("Enter an expression:");
    scanf("%s", exp);
    printf ("%d", evaluation(exp));
    return 0;
}

它不起作用,每次运行它都会给我一个随机数

我真的不明白为什么会发生这种情况。如何让函数评估用户输入?

非常感谢一些帮助。谢谢。

完整代码如下:

/*---------------POSTFIX EVALUATION-----------------------------*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

struct posteval
{
    int head1;
    unsigned size;
    int* expression;
};


struct posteval* newstack(int size)
{
    struct posteval* posteval2=(struct posteval*)malloc(sizeof(struct posteval));

    if(!posteval2)
    {
        return 0;
    }

    posteval2->expression=(int*)malloc(posteval2->size * sizeof(int));
    posteval2->head1= -1;
    posteval2->size = size;

    if(!posteval2->expression)
    {
        return 0;
    }

    return posteval2;

};

// a function to push into the stack for postfix evaluation
void postpush(struct posteval* posteval2, int x)
{
    posteval2->expression[++posteval2->head1] = x ;
}

// a function to pop into the stack for postfix evaluation
int postpop(struct posteval* posteval2)
{
    if(!nothing(posteval2))
    {
        return posteval2->expression[posteval2->head1--];
    }
    return 'E';
}

//a function that checks if the stack has nothing in it
int nothing(struct posteval* posteval2)
{
    return posteval2->head1 == -1;
}

//function that evaluates the postfix expressions
int evaluation(char* exp)
{
    int i;
    int operand;
    int explength=strlen(exp);
    int num1, num2;
    struct posteval* posteval2=newstack(explength);

    if (!posteval2)
    {
        return -1;
    }

    for(i=0; exp[i]; ++i)
    {
        if (exp[i]==' ')
        {
            continue;
        }
        else if (isdigit(exp[i]))
        {
            operand=0;
            while(isdigit(exp[i]))
            {
                operand= operand*10 + (int)(exp[i]-'0');
                i++;
            }
            i--;

            postpush(posteval2, operand);

        }
        else {
            num1 = postpop(posteval2);
            num2 = postpop(posteval2);

            if(exp[i]=='/')
            {
                postpush(posteval2, num2/num1);
            }
            else if(exp[i]=='+')
            {
                postpush(posteval2, num2 + num1);
            }
            else if (exp[i]=='*')
            {
                postpush(posteval2, num2 * num1);
            }
            else if (exp[i]=='-')
            {
                postpush(posteval2, num2 - num1);
            }

        }
    }
    return postpop(posteval2);
}

    int main()
{
    char exp[100];
    printf("Enter an expression:");
    scanf("%c", exp);
    printf ("%d", evaluation(exp));
    return 0;
}
}

最佳答案

按照Yunnosch的建议,我编辑了我的答案,所以我在你的代码中看到你使用了scanf(“%c”, exp);,在评论中协作者对你说这是不正确,我同意他们的观点,您需要读取一行直到输入中出现换行符,为此您可以使用 scanf(“[^\n]”, exp);这种扫描格式用于读取输入中的每个字符,直到找到括号内的字符之一,然后您将得到您期望的字符串,例如 “100 200 + 2/5 * 7 +”,而不是 “100 200 + 2/5 * 7 +\n”,这与您的预期不同

关于c - 函数不评估用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59311748/

相关文章:

无法遍历C链表

c - 如何使用 libelf 为我自己的编译器生成 ELF 文件?

java - 数组/链表 : performance depends on the *direction* of traversal?

c++ - 除非存在特定的 printf 语句,否则 fwrite 不会将缓冲区写入标准输出

r - 如何检查 R 函数的输入参数是否存在

c - 如何在 C 中打印函数指针的值?

php - 通过表单中的链接自动填充网站标题

javascript - 两个索引之间的反向链表

c++ - 如何将值存储在内存中的特定位置?

c# - 单声道嵌入,从 C 调用 C# 泛型方法