简体   繁体   English

评估后缀表达式时不需要的符号结果

[英]Unwanted symbol result when evaluating postfix expression

I'm trying to make a C program to evaluate postfix expressions and when doing so an unwanted symbol is being printed on the screen for the input 45+. 我正在尝试制作一个C程序来评估后缀表达式,并且这样做时,在输入45+的屏幕上打印了不需要的符号。 PS Please tell me the mistake (except of that gets() I am studying right now how to use fgets()) 附言:请告诉我这个错误(除了我正在研究的get()之外,现在如何使用fgets())

// to Evaluate a postfix expression
#include<stdio.h>
#include<conio.h>
int is_operator(char);
void answer();
char stack[100];
int top =-1;
void push(char);
char pop();
void main()
{
char postfix[100],item;
int i=0;
clrscr();
printf("Enter Postfix Expression");
gets(postfix);
while(postfix[i]!='\0')
    {
    item=postfix[i];
    if(is_operator(item)==2)
        {
        push(item);
        }
    if(is_operator(item)==1)
        {
        char op;
        int n1,n2,n3;
        op=item;
        n1=pop();
        n2=pop();
        switch(op)
        {
            case '+':
            n3=n1+n2;
            case '-':
            n3=n1-n2;
            case '*':
            n3=n1*n2;
            case '/':
            n3=n1/n2;
        }
        push(n3);
        }
    i++;
    }//end while
answer();
getch();
}
void push(char c)
{
top++;
stack[top]=c;
}
char pop()
{
char c;
c=stack[top];
top--;
return(c);
}
int is_operator(char i)
{
char ch=i;
if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
    {
    return(1);
    }
else
    {
    return(2);
    }
}
void answer()
{
char ans;
ans=stack[top];
printf("Answere is %c",ans);
}

There are a lot of mistakes in your code.Try to properly type cast. 您的代码中有很多错误。请尝试正确键入cast。

Go through comments to understand the mistakes. 通过评论来了解错误。

Go through this for understanding character pointer and arrays. 通过这篇文章了解字符指针和数组。

// to Evaluate a postfix expression //评估后缀表达式

#include<stdio.h>



int is_operator(char);
void answer();
int stack[100];//Use integer array since operands are integer
int top =-1;
void push(int);//Arguments changed to integer type since the stack is integer 
int pop(); //Return type to integer
void main()
{
char* postfix;//Use character pointer for iterating through loop smoothly
 int item;
int i=0;

printf("Enter Postfix Expression");
gets(postfix);
char c;

while(*postfix!='\0')
    {
    c=*postfix;
    if(is_operator(c)==2)
        {
        push((c-'0')); //Converting char to int before pushing it into the stack
        }
    if(is_operator(c)==1)
        {


        char op;
        int n1,n2,n3;
        op=*postfix;
        n1=pop();
        n2=pop();
        switch(op)
        {
            case '+':
            n3=n1+n2;
            break;
            case '-':
            n3=n1-n2;
            break;
            case '*':
            n3=n1*n2;
            break;
            case '/':
            n3=n1/n2;
            break;
        }
        push(n3);
        }
    postfix++;
    }//end while
answer();

}
void push(int c)
{
top++;
stack[top]=c;
}
int pop()
{
int c;
c=stack[top];
top--;
return(c);
}
int is_operator(char i)
{
char ch=i;
if(ch=='+'||ch=='-'||ch=='*'||ch=='/')
    {
    return(1);
    }
else
    {
    return(2);
    }
}
void answer()
{
char ans;
ans=stack[top];
printf("Answere is %d",ans);
}  

I hope it is helpful.... 希望对您有所帮助。

The problems I see with your code are: your switch() is missing break statements on the individual case clauses (and a default case might be nice too); 我在您的代码中看到的问题是:您的switch()在单个case子句上缺少break语句( default情况下也可能很好); when you push your non-operators (aka single digit numbers) on the stack, you push them as character codes rather then converting them to numbers so the math doesn't make sense; 当您将非运算符(也称为一位数字)压入堆栈时,您将它们作为字符代码压入而不是将它们转换为数字,这样数学就没有意义了; you're not properly handling the order of non-communitive operations like subtraction and division (use Unix dc command as a comparison tool); 您没有正确处理非相减运算的顺序,例如减法和除法(使用Unix dc命令作为比较工具); finally, don't reinvent booleans. 最后,不要重塑布尔值。 Below is a rework of your code with the above changes and some style adjustments: 以下是对代码的重做,其中包含上述更改和一些样式调整:

// Evaluate a postfix expression

#include <ctype.h>
#include <stdio.h>
#include <stdbool.h>

char stack[100];
int top = -1;

void push(char);
char pop(void);
bool is_operator(char);
void answer(void);

void push(char c)
{
    stack[++top] = c;
}

char pop()
{
    return stack[top--];
}

bool is_operator(char op)
{
    return (op == '+' || op == '-' || op == '*' || op == '/');
}

void answer()
{
    printf("Answer is %d\n", stack[top]);
}

int main()
{
    char item, postfix[100];
    int i = 0;

    printf("Enter Postfix Expression: ");

    gets(postfix);

    while ((item = postfix[i++]) != '\0')
    {
        if (is_operator(item))
        {
            char n1 = pop();
            char n2 = pop();
            char n3 = 0;

            switch (item)
            {
                case '+':
                    n3 = n1 + n2;
                    break;
                case '-':
                    n3 = n2 - n1;
                    break;
                case '*':
                    n3 = n1 * n2;
                    break;
                case '/':
                    n3 = n2 / n1;
                    break;
            }

            push(n3);
        } else if (isdigit(item)) {
            push(item - '0');
        }
    } // end while
    answer();

    return 0;
}

Example (note this evaluator only operates on single digit numbers): 示例(请注意,此评估程序仅对一位数字起作用):

> ./a.out
Enter Postfix Expression: 6 4 - 7 * 1 +
Answer is 15
> dc
6 4 - 7 * 1 + p
15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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