c - 解析树程序错误

标签 c

我在解析表达式的代码中有一个错误,它说

"146 C:\Dev-Cpp\z.c conflicting types for 'show_tree' 111 C:\Dev-Cpp\z.c previous implicit declaration of 'show_tree' was here"

请帮忙...

#include<stdio.h>
#include<stdlib.h>

int getOperatorPosition(char);

#define node struct tree1

int matrix[5][5]=
{
    {1,0,0,1,1},
    {1,1,0,1,1},
    {0,0,0,2,3},
    {1,1,3,1,1},
    {0,0,0,3,2}
};
int tos=-1;
void matrix_value(void);
//node create_node(char,*node);void show_tree( node *);
int isOperator(char);

struct tree1
{
    char data;
    node  *lptr;
    node  *rptr;
}
*first;


struct opr
{
    char op_name;
    node *t;
}
oprate[50];

char cur_op[5]= {'+','*','(',')','['};
char stack_op[5]= {'+','*','(',')',']'};

int main()
{
    char exp[10];
    int ssm=0,row=0,col=0;
    node *temp;
    //    clrscr();
    printf("Enter Exp : ");
    scanf("%s",exp);
    matrix_value();
    while(exp[ssm] != '\0')
    {
        if(ssm==0)
        {
            tos++;
            oprate[tos].op_name = exp[tos];
        }
        else
        {
            if(isOperator(exp[ssm]) == -1)
            {
                oprate[tos].t = (node*) malloc(sizeof(node));
                oprate[tos].t->data = exp[ssm];
                oprate[tos].t->lptr = '\0';
                oprate[tos].t->rptr = '\0';
            }
            else
            {
                row = getOperatorPosition(oprate[tos].op_name);
                col = getOperatorPosition(exp[ssm]);
                if(matrix[row][col] == 0)
                {
                    tos++;
                    oprate[tos].op_name = exp[ssm];
                }
                else if(matrix[row][col] == 1)
                {
                    temp = (node*) malloc(sizeof(node));
                    temp->data = oprate[tos].op_name;
                    temp->lptr = (oprate[tos-1].t);
                    temp->rptr = (oprate[tos].t);
                    tos--;
                    oprate[tos].t = temp;
                    ssm--;
                }
                else if(matrix[row][col] == 2)
                {
                    //temp = (node*) malloc (sizeof(node));
                    temp = oprate[tos].t;
                    tos--;
                    oprate[tos].t = temp;
                }
                else if(matrix[row][col] == 3)
                {
                    printf("\nExpression is Invalid...\n");
                    printf("%c  %c can not occur simultaneously\n",oprate[tos].op_name,exp[ssm]);
                    break;
                }
            }
        }
        ssm++;
    }
    printf("show tree \n\n\n");
    show_tree(oprate[tos].t);
    printf("Over");
}
int isOperator(char c)
{
    int i=0;
    for(i=0; i<5; i++)
    {
        if(c==cur_op[i] || c==stack_op[i])
        {
            break;
        }
    }
    if(i==5)
    {
        return (-1);
    }
    else
    {
        return i;
    }
}

int getOperatorPosition(char c)
{
    int i;
    for(i=0; i<5; i++)
    {
        if(c==cur_op[i] || c==stack_op[i])
        {
            break;
        }
    }
    return i;
}

void show_tree(node *start)
{
    if(start->lptr != NULL)
    {
        show_tree(start->lptr);
    }
    if(start->rptr != NULL)
    {
        show_tree(start->rptr);
    }
    printf("%c \n",start->data);
}

void matrix_value(void)
{
    int i,j;
    printf("OPERATOR PRECEDENCE MATRIX\n");
    printf("==========================\n");
    for(i=0; i<5; i++)
    {
        printf("%c ",stack_op[i]);
    }
    printf("\n");
    for(i=0; i<5; i++)
    {
        printf("%c  ",cur_op[i]);
        for(j=0; j<5; j++)
        {
            if(matrix[i][j] == 0)
            {
                printf("< ");
            }
            else if(matrix[i][j] == 1)
            {
                printf("> ");
            }
            else if(matrix[i][j] == 2)
            {
                printf("= ");
            }
            else if(matrix[i][j] == 3)
            {
                printf("  ");
            }
        }
        printf("\n");
    }
}

最佳答案

在 main 之前声明您的 show_tree 函数。 即在 struct tree1 之前:

int isOperator(char );
void show_tree(node *start);

 struct tree1
  {
   char data;
   node  *lptr;
   node  *rptr;
  }
 *first;

...

关于c - 解析树程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17456340/

相关文章:

c - 简单递归题

mysql - 如何执行 MYSQL REPLACE 将多个或单个空格替换为 | (管道)

c - 如何创建用户定义的头文件?希望我能在 C/C++ 中得到它

python - C/Python 中的国际数据加密算法(IDEA)

c - SPIN_LOCK_UNLOCKED 的 Linux 3.16 等价物是什么?

c - 获取一个字符串并在c中提取2个子字符串

c - makefile 中是否忽略了扩展名?

c - 使用 SWIG 将指向结构的指针从 TCL 传递到 C 函数

c - 为什么第一个代码中没有使用 "&"运算符?

C:线程之间的文件锁定