2013-07-03 35 views
0

我在解析表達式一個代碼錯誤,它說解析樹的程序錯誤

「146 C:\開發-CPP \ ZC衝突的類型 'show_tree' 111 C:\開發-CPP \ ZC以前的隱性 'show_tree' 的聲明在這裏」

幫助,請...

#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"); 
    } 
} 
+0

每次我開始認爲升壓精神是簡單的任務過於複雜,這樣的事情讓我想起它差多少可能是沒有適當的庫支持。 (編輯:公平地說,這看起來像漂亮的代碼,而不是試圖把它放下來。對於我來說,通過解析器實現去實現_low-level_) – sehe

回答

5

前主聲明你show_tree功能。 結構樹1之前,即:

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

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

...

+0

我剛剛意識到'#define node struct tree1'是正確的, 謝謝 :) –