2010-02-26 99 views
0

這是我1.C內容使用make命令中綴到後綴轉換

#include "2.h" 
#include<stdio.h> 
#include<string.h> 

void push(int ele) 
{ 
    stack[tos]=ele; 
    tos++; 
} 

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

void show() 
{ 
    int x=tos;  
    printf("--The Stack elements are....."); 
    while(x!=0) 
     printf("%c, ",stack[--x]); 
} 

//Function to get the precedence of an operator 
int prec(char symbol) 
{ 
    if(symbol== '(') 
     return 0; 

    if(symbol== ')') 
     return 0; 

    if(symbol=='+' || symbol=='-') 
     return 1; 

    if(symbol=='*' || symbol=='/') 
     return 2; 

    if(symbol=='^') 
     return 3; 

    return 0; 
} 

,這是我2.H內容

#define size 10 

char stack[size]; 
int tos=0,ele; 

void push(); 

char pop(); 

void show(); 

int isempty(); 

int isfull(); 

char infix[30], output[30]; 

int prec(char); 

和我的main.c內容

#include "2.h" 
#include<stdio.h> 

#include<string.h> 

int main() 
{ 
    int i=0,j=0,k=0,length; 

    char temp; 

    printf("\nEnter an infix expression:"); 

    scanf("%s",infix); 

    printf("\nThe infix expresson is %s",infix); 

    length=strlen(infix); 

    for(i=0;i<length;i++) 
    { 
     //Numbers are added to the out put QUE 

     if(infix[i]!='+' && infix[i]!='-' && infix[i]!='*' && infix[i]!='/' && 
       infix[i]!='^' && infix[i]!=')' && infix[i]!='(') 
     { 
      output[j++]=infix[i]; 

      printf("\nThe element added to Q is:%c",infix[i]); 

     } 
     //If an operator or a bracket is encountered... 
     else 
     { 
      if(tos==0) //If there are no elements in the stack, the operator is added to it 
      { 
       push(infix[i]); 

       printf("\nThe pushed element is:%c",infix[i]); 

      } 
      else 
      { 
       //Operators or pushed or poped based on the order of precedence 
       if(infix[i]!=')' && infix[i]!='(') 
       { 
        if(prec(infix[i]) <= prec(stack[tos-1])) 
        { 
         temp=pop(); 

         printf("\n the poped element is :%c",temp); 

         output[j++]=temp; 
         push(infix[i]); 
         printf("\n The pushed element is :%c",infix[i]); 

         show(); 
        } 
        else 
        { 
         push(infix[i]); 
         printf("\nThe pushed element is:%c",infix[i]); 

         show(); 
        } 
       } 
       else 
       { 
        if(infix[i]=='(') 
        { 
         push(infix[i]); 

         printf("\nThe pushed-- element is:%c",infix[i]); 
        } 

        if(infix[i]==')') 
        { 
         temp=pop(); 

         while(temp!='(') 
         { 
          output[j++]=temp; 

          printf("\nThe element added to Q is:%c",temp); 

          //temp=pop(); 
          printf("\n the poped element is :%c",temp); 
          temp=pop(); 
         } 
        } 
       } 
      } 
     } 
     printf("\nthe infix expression is: %s",output); 
    } 

    while(tos!=0) 
    { 
     output[j++]=pop(); 
    } 

    printf("the infix expression is: %s\n",output); 
} 

我正在做這個使用MAKE在Linux中

t他的代碼是

myapp: main.o 1.o 
    gcc -o myapp main.c 1.c 

main.o: main.c 2.h 
    gcc -c main.c 

1.o: 1.c 2.h 
    gcc -c 1.c 

但IIN即將到來

gcc -o myapp main.c 1.c 
/tmp/ccy0qyI1.o:(.bss+0x0): multiple definition of `tos' 
/tmp/ccQZzbOI.o:(.bss+0x0): first defined here 
collect2: ld returned 1 exit status 
make: *** [myapp] Error 1 

I M試圖修復它的錯誤。但無法解析

+0

看起來像一個作業問題,請標記爲這樣。 – aggietech 2010-02-26 14:47:51

+0

請格式化您的問題,以便代碼可讀。我嘗試過,但放棄了'main()'。您可以使用101/010按鈕來插入代碼。 – mouviciel 2010-02-26 14:50:47

回答

1

您正在定義一個頭文件中的全局變量tos,它包含在1.cmain.c中。所以你最終得到兩個同名的全局變量。鏈接器不喜歡那樣。作爲一個傳統的unix擴展,如果多重定義的變量沒有明確初始化,那麼鏈接器可能會處理這種情況,但是您的代碼會初始化該變量。

我建議您閱讀K&R book,您可以在任何體面的大學圖書館中找到它。

+0

感謝mctlyr。 – user216112 2010-02-27 01:44:52

1

@Thomas給你一個很好的解釋你的問題。全局變量tos聲明位於頭文件2.h中,該文件通過#include1.cmain.c中包含兩次。

如果你想份額變量tos你應該聲明它1.C或main.c中,並修改2.H將其申報爲extern,如:

1.C:

int tos = 0; 

·H:

extern int tos; 

然後你可以從main.c中訪問TOS,但是變量只被定義一次。

側欄:爲了您自己的利益,併爲了在StackOverflow中共享未來問題的好處,儘量減少源代碼,以避免生成錯誤。到這個程序是微不足道的,因爲那麼這個錯誤將更容易被隔離(對於你和其他人),並且對於讀者來說更清楚,他們應該關注什麼。同樣,@Thomas建議C程序設計語言強烈建議您作爲任何 C程序員的優秀建議。