Close
Login to Your Account
Faadooengineers




Results 1 to 2 of 2

Thread: Need help in C code

  1. #1
    Fuchcha FaaDoO Engineer
    Join Date
    Dec 2011
    Posts
    1

    Gender: : Male

    Branch: : Information Technology Engineering

    City : Surathkal

    Send Private Message
    Send Friend Request

    Need help in C code

    Hello everyone,

    This code is for converting infix notation to postfix notation. It does not give any error on compiling but shows segmentation fault instead of the output. Please help.


    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    struct exp{
    char t;
    struct exp *next;
    };
    typedef struct exp node;
    node *top=NULL;
    void create(node**);
    void push(node**,char);
    char pop(node**);
    int check(char);
    void outfix(char[] ,node*);


    main()
    {
    int i;
    char a[40],b[40];
    node *top=NULL;
    printf("Enter expression\n");
    gets(a);
    create(&top);

    outfix(a,top);


    }


    int check(char c)
    {
    switch(c)
    {
    case '+': return 1;
    break;
    case '-': return 1;
    break;
    case '*': return 2;
    break;
    case '/': return 2;
    break;
    case '(': break;
    case ')': break;
    default: return 3;

    }
    }

    void outfix(char c[],node *top)
    {
    static int i=0;
    char b[40];
    char k;


    for(k=0;c[k]!='\0';k++)
    {
    if(check(c[k])==3)
    {
    b[i]=c[k];

    i++;
    }
    else if(c[k]==')')
    {
    while(top->t!='(')
    {
    b[i]=pop(&top);
    i++;
    }
    pop(&top);
    }
    else if(check(c[k])>check((top)->t) )
    push(&top,c[k]);

    else
    while((check(c[k]))<=check((top)->t))
    {
    b[i]=pop(&top);
    i++;

    }

    }
    while(top!=NULL)
    {
    b[i]=pop(&top);
    i++;
    }

    b[i]='\0';
    printf("%s\n",b);




    }

    void create(node **top)
    {
    *top=NULL;
    }







    void push(node **top, char c)
    {
    node *new;
    new = (node *) malloc(sizeof(node));


    new->t = c;
    new->next = *top;
    *top = new;
    }



    char pop (node **top)
    {
    char m;
    node *p;
    p=*top;
    m = (*top)->t;
    *top = (*top)->next ;
    free (p);
    return m;
    }


  2. #2
    Fuchcha FaaDoO Engineer
    Join Date
    Sep 2011
    Posts
    8

    Gender: : Female

    Branch: : Aeronautical Engineering

    City : Gurgaon

    Send Private Message
    Send Friend Request

    Re: Need help in C code

    my rating to this code is 5

Tags for this Thread