首页 > 解决方案 > 将 INFIX 表达式转换为等效的 POSTFIX 表达式

问题描述

我在 Turbo C++ 代码的第 28 行收到“调用‘push’时参数 1 中的类型不匹配”错误,请帮助我,因为我必须提交我的项目,我知道 Turbo C++ 是一个非常古老的编译器但这就是我们大学老师的建议,所以我对此无能为力

''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''' '''''

#include <stdio.h>
#include <conio.h>

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

void in2post(char[]);
void push(int);
int pop();
int prec(char);
int cal(char[]);

void main()
{
    char in[100], post[100];
    clrscr();
    printf("Enter an infix expression:  ");
    gets(in);
    in2post(in);
    getch();
}

void in2post(char in_exp[])
{
    int x = 0, y = 0, z, result = 0;
    char a, c, post_exp[100];
    char t;
    push("\0");
    t = in_exp[x];
    while (t != '\0')
    {
        if (isalnum(t))
        {
            post_exp[y] = t;
            y++;
        }
        else if (t == '(')
        {
            push('(');
        }
        else if (t == ')')
        {
            while (stack[top] != '(')
            {
                c = pop();
                post_exp[y] = c;
                y++;
            }
            c = pop();
        }
        else
        {
            while (prec(stack[top]) >= prec(t))
            {
                c = pop();
                post_exp[y] = c;
                y++;
            }
            push(t);
        }
        x++;
        t = in_exp[x];
    }

    while (top != -1)
    {
        c = pop();
        post_exp[y] = c;
        y++;
    }
    printf("\nThe equivalent postfix expression is: ");

    for (z = 0; z < y; z++)
        printf("%c", post_exp[z]);
    printf("\n\nDo you want to evaluate the postfix expression? (Y/N):  ");
    scanf("%c", &a);

    if (a == 'y' || a == 'y')
    {
        result = cal(post_exp);
        printf("\nResult = % d\n", result);
        getch();
    }
    else if (a == 'n' || a == 'N')
    {
        exit(0);
    }
}

int cal(char post[])
{
    int m, n, x, y, j = 0, len;
    len = strlen(post);
    while (j < len)
    {
        if (isdigit(post[j]))
        {
            x = post[j] - '0';
            push(x);
        }
        else
        {
            m = pop();
            n = pop();

            switch (post[j])
            {
            case '+':
                x = n + m;
                break;
            case '-':
                x = n - m;
                break;
            case '*':
                x = n * m;
                break;
            case '/':
                x = n / m;
                break;
            }
            push(x);

''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''' '''''

标签: cexpressionpostfix-mtainfix-notation

解决方案


推荐阅读