中缀和前缀 [英] infix and prefix

查看:124
本文介绍了中缀和前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的论坛,

我的代码有问题,输出不适用。我在我的智慧结束,我需要你明智的大师的建议。很多人



Dear forums,
i have problems with my code, where the output does not apppear. I am at my wits end, i need your advise from the wise gurus out ther. Thx a bunch

#include
#include
#include

#define MAX 10
#define EMPTY -1

struct stack
{
    char data[MAX];
    int top;
};

int isempty(struct stack *s)
{
    return (s->top == EMPTY) ? 1 : 0;
}

void emptystack(struct stack* s)
{
    s->top=EMPTY;
}

void push(struct stack* s,int item)
{
    if(s->top == (MAX-1))
    {
        printf("\nSTACK FULL");
    }
    else
    {
        ++s->top;
        s->data[s->top]=item;
    }
}

char pop(struct stack* s)
{
    char ret=(char)EMPTY;
    if(!isempty(s))
    {
        ret= s->data[s->top];
        --s->top;
    }
    return ret;
}

void display(struct stack s)
{
    while(s.top != EMPTY)
    {
        printf("\n%d",s.data[s.top]);
        s.top--;
    }
}

int isoperator(char e)
{
    if(e == ''+'' || e == ''-'' || e == ''*'' || e == ''/'' || e == ''%'')
        return 1;
    else
        return 0;
}


int priority(char e)
{
    int pri = 0;

    if(e == ''*'' || e == ''/'' || e ==''%'')
        pri = 2;
    else
    {
        if(e == ''+'' || e == ''-'')
        pri = 1;
    }
    return pri;
}

void infix2postfix(char* infix, char * postfix, int insertspace)
{
    char *i,*p;
    struct stack X;
    char n1;
    emptystack(&X);
    i = &infix[0];
    p = &postfix[0];

    while(*i)
    {
        while(*i == '' '' || *i == ''\t'')
        {
        i++;
        }

        if( isdigit(*i) || isalpha(*i) )
        {
            while( isdigit(*i) || isalpha(*i))
            {
                *p = *i;
                p++;
                i++;
            }
            /*SPACE CODE*/
            if(insertspace)
            {
                *p = '' '';
                p++;
            }
            /*END SPACE CODE*/
        }

        if( *i == ''('' )
        {
            push(&X,*i);
            i++;
        }

        if( *i == '')'')
        {
            n1 = pop(&X);
            while( n1 != ''('' )
            {
                *p = n1;
                p++;
                /*SPACE CODE*/
                if(insertspace)
                {
                    *p = '' '';
                    p++;
                }
                /*END SPACE CODE*/
                n1 = pop(&X);
            }
            i++;
        }

        if( isoperator(*i) )
        {
            if(isempty(&X))
                push(&X,*i);
            else
            {
                n1 = pop(&X);
                while(priority(n1) >= priority(*i))
                {
                    *p = n1;
                    p++;
                    /*SPACE CODE*/
                    if(insertspace)
                    {
                        *p = '' '';
                        p++;
                    }
                    /*END SPACE CODE*/
                    n1 = pop(&X);
                }
                push(&X,n1);
                push(&X,*i);
            }
            i++;
        }
    }
    while(!isempty(&X))
    {
        n1 = pop(&X);
        *p = n1;
        p++;
        /*SPACE CODE*/
        if(insertspace)
        {
            *p = '' '';
            p++;
        }
        /*END SPACE CODE*/
    }
    *p = ''\0'';
}


int evaluate(char eva)
{
    char *p;
    stack stack;
    int op1,op2,result;

    isempty(&stack);
    p = &eva;

    while(*p != ''\0'')
    {
        /* removes tabs and spaces */
        while(*p == '' '' || *p == ''\t'')
        {
            p++;
        }
        /* if is digit */
        if(isdigit(*p))
        {
            push(&stack,*p - 48);
        }
        else
        {
            /* it is an operator */
            op1 = pop(&stack);
            op2 = pop(&stack);

            switch(*p)
            {
                case ''+'':
                result = op2 + op1;
                break;

                case ''-'':
                result = op2 - op1;
                break;

                case ''/'':
                result = op2 / op1;
                break;

                case ''*'':
                result = op2 * op1;
                break;


                default:
                printf("\nInvalid Operator");
                return 0;
            }
            push(&stack,result);
        }
        p++;
    }
    result = pop(&stack);
    return result;
}

int main()
{
    char in[50],post[50];

    strcpy(&post[0],"");
    printf("Enter Infix Expression : ");
    fflush(stdin);
    gets(in);
    infix2postfix(&in[0],&post[0],1);
    printf("Postfix Expression is : %s\n",&post[0]);
    printf("Equal: %d\n\n",evaluate(&post));

    return 0;
} 

推荐答案

尝试以下代码(请注意,我的修复程序远非优雅......)。 br />
问题(您可以使用调试器轻松发现它们):

  • 您调用 isempty evaluate 函数的开头,而不是 emptystack
  • 你忘了检查字符串终止符在评估函数中跳过空格和制表符之后。
Try the following code (be aware, my fixes are far from being elegant...).
The problems (you could easily discover them with a debugger):
  • You called isempty instead of emptystack at the begin of evaluate function.
  • You forgot to check for string terminator after skipping spaces and tabulators in the evaluate function.
#define MAX 10
#define EMPTY -1

struct stack
{
    char data[MAX];
    int top;
};

int isempty(struct stack *s)
{
    return (s->top == EMPTY) ? 1 : 0;
}

void emptystack(struct stack* s)
{
    s->top=EMPTY;
}

void push(struct stack* s,int item)
{
    if(s->top == (MAX-1))
    {
        printf("\nSTACK FULL");
    }
    else
    {
        ++s->top;
        s->data[s->top]=item;
    }
}

char pop(struct stack* s)
{
    char ret=(char)EMPTY;
    if(!isempty(s))
    {
        ret= s->data[s->top];
        --s->top;
    }
    return ret;
}

void display(struct stack s)
{
    while(s.top != EMPTY)
    {
        printf("\n%d",s.data[s.top]);
        s.top--;
    }
}

int isoperator(char e)
{
    if(e == '+' || e == '-' || e == '*' || e == '/' || e == '%')
        return 1;
    else
        return 0;
}


int priority(char e)
{
    int pri = 0;

    if(e == '*' || e == '/' || e =='%')
        pri = 2;
    else
    {
        if(e == '+' || e == '-')
        pri = 1;
    }
    return pri;
}

void infix2postfix(char* infix, char * postfix, int insertspace)
{
    char *i,*p;
    struct stack X;
    char n1;
    emptystack(&X);
    i = infix;
    p = postfix;

    while(*i)
    {
        while(*i == ' ' || *i == '\t')
        {
        i++;
        }

        if( isdigit(*i) || isalpha(*i) )
        {
            while( isdigit(*i) || isalpha(*i))
            {
                *p = *i;
                p++;
                i++;
            }
            /*SPACE CODE*/
            if(insertspace)
            {
                *p = ' ';
                p++;
            }
            /*END SPACE CODE*/
        }

        if( *i == '(' )
        {
            push(&X,*i);
            i++;
        }

        if( *i == ')')
        {
            n1 = pop(&X);
            while( n1 != '(' )
            {
                *p = n1;
                p++;
                /*SPACE CODE*/
                if(insertspace)
                {
                    *p = ' ';
                    p++;
                }
                /*END SPACE CODE*/
                n1 = pop(&X);
            }
            i++;
        }

        if( isoperator(*i) )
        {
            if(isempty(&X))
                push(&X,*i);
            else
            {
                n1 = pop(&X);
                while(priority(n1) >= priority(*i))
                {
                    *p = n1;
                    p++;
                    /*SPACE CODE*/
                    if(insertspace)
                    {
                        *p = ' ';
                        p++;
                    }
                    /*END SPACE CODE*/
                    n1 = pop(&X);
                }
                push(&X,n1);
                push(&X,*i);
            }
            i++;
        }
    }
    while(!isempty(&X))
    {
        n1 = pop(&X);
        *p = n1;
        p++;
        /*SPACE CODE*/
        if(insertspace)
        {
            *p = ' ';
            p++;
        }
        /*END SPACE CODE*/
    }
    *p = '\0';
}


int evaluate(char *eva)
{
    char *p;
    stack stack;
    int op1,op2,result;

    emptystack(&stack);
    p = eva;

    while(*p != '\0')
    {
        /* removes tabs and spaces */
        while(*p == ' ' || *p == '\t')
        {
            p++;
        }
        if (*p == '\0') break;
        /* if is digit */
        if(isdigit(*p))
        {
            push(&stack,*p - 48);
        }
        else
        {
            /* it is an operator */
            op1 = pop(&stack);
            op2 = pop(&stack);

            switch(*p)
            {
                case '+':
                result = op2 + op1;
                break;

                case '-':
                result = op2 - op1;
                break;

                case '/':
                result = op2 / op1;
                break;

                case '*':
                result = op2 * op1;
                break;


                default:
                printf("\nInvalid Operator");
                return 0;
            }
            push(&stack,result);
        }
        p++;
    }
    result = pop(&stack);
    return result;
}

int main()
{
    char in[50],post[50];

    strcpy(post,"");
    printf("Enter Infix Expression : ");
    fflush(stdin);
    gets(in);
    infix2postfix(in,post,1);
    printf("Postfix Expression is : %s\n",post);
    printf("Equal: %d\n\n",evaluate(post));
    return 0;
}


非常感谢你的CPallini,你帮我很多了=)
Thank you so much CPallini, you helped me a lot =)


例如你的代码

3 + 12 * 2/2

后缀表达式为:3 12 2 * 2 / +

等于:3









3等于是错误

等于必须15
for example your code
3+12*2/2
postfix expression is : 3 12 2 * 2 / +
equal : 3




3 for equal is wrong
equal must 15


这篇关于中缀和前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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