2010-02-26 101 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程序员的优秀建议。