开发者

using make command infix to postfix conversion

this is my 1.c content

#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;
}

and this is my 2.h content

#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);

and my main.c content is

#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;

                        开发者_如何学C    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);
}

i am doing this using MAKE in linux

the code is

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

but iin coming the error coming

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 trying to fix it up. but not able to resolve


You are defining a global variable tos in a header file, which you include both from 1.c and main.c. So you end up with two global variables with the same name. The linker does not like that. As a traditional-unix extension, the linker may cope with such a situation if the multiply-defined variable is not explicitly initialized, but your code initializes the variable.

I recommend reading the K&R book which you will find in any decent university library.


@Thomas gave you a good explanation of your problem. The global variable tos declaration is in the header file 2.h which is included twice via #include in 1.c and main.c.

If you want to share the variable tos you should declaration it in 1.c or main.c, and modify 2.h to declare it as an extern, e.g.:

1.c:

int tos = 0;

2.h:

extern int tos;

Then you can access tos from main.c, but the variable is only defined once.

Sidebar: Just a suggestion for your own benefit, and for the benefit of sharing future questions in StackOverflow, try to minimize the source code, to the bare necessaries of generating the error. To the point where the program is trivial, because then the bug will be easier to isolate (for you and everyone else), and clearer for the reader, what they should focus on. As well, @Thomas suggestion of The C Programming Language is strongly encouraged as an excellent recommendation for any C programmer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜