theme

" जीतने वाले छोड़ते नहीं और छोड़ने वाले जीतते नही "

Wednesday, July 6, 2011

programme to addition of two polynomial

#include
#include

struct node
{
    int coeff, exp;
    struct node *next;
}*startx, *starty, *startz, *newnode, *p, *x, *y, *z, *last;

main()
{
    int ch;
    clrscr();
    do
    {
        printf("\n\n");
        printf("\n1 insert in x");
        printf("\n2 insert in y");
        printf("\n3 poly add");
        printf("\n4 show list x");
        printf("\n5 show list y");
        printf("\n6 show list z");
        printf("\n7 exit");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1: add_x();
            break;
            case 2: add_y();
            break;
            case 3: poly_add();
            break;
            case 4: list_x();
            break;
            case 5: list_y();
            break;
            case 6: list_z();
            break;
            case 7:
            break;
            default:
            printf("you have inputted wrong value\n");
        }
    }
    while(ch!=7);
}

add_x()
{
    struct node *newnode;
    newnode=(struct node*)malloc(sizeof(struct node));
    newnode->next=NULL;
    printf("enter coeff=");
    scanf("%d", &newnode->coeff);
    printf("enter exp=");
    scanf("%d", &newnode->exp);

    if(x==NULL)
    {
        x=newnode;
        startx=newnode;
    }
    else
    {
        x->next=newnode;
        x=x->next;
    }
}


add_y()
{
    struct node *newnode;
    newnode=(struct node*)malloc(sizeof(struct node));
    newnode->next=NULL;
    printf("enter coeff=");
    scanf("%d", &newnode->coeff);
    printf("enter exp=");
    scanf("%d", &newnode->exp);

    if(y==NULL)
    {
        y=newnode;
        starty=newnode;
    }
    else
    {
        y->next=newnode;
        y=y->next;
    }
}


poly_add()
{
    struct node *newnode;
    newnode=(struct node*)malloc(sizeof(struct node));
    newnode->next=NULL;

    x=startx;
    y=starty;
    while(x!=NULL && y!=NULL)
    {
        if(x->expexp)
        {
            newnode->coeff=y->coeff;
            newnode->exp=y->exp;
            y=y->next;
        }
        else if(x->exp>y->exp)
        {
            newnode->coeff=x->coeff;
            newnode->exp=x->exp;
            x=x->next;
        }
        else if(x->exp==y->exp)
        {
            newnode->coeff=y->coeff+x->coeff;
            newnode->exp=x->exp;
            x=x->next;
            y=y->next;
        }

            if(startz==NULL)
            {
                startz=newnode;
                z=newnode;
            }
            else
            {
                z=z->next;
                z=newnode;

            }
    }
}

list_x()
{
    for(p=startx; p!=NULL; p=p->next)
    {
        printf("%d-%d\t", p->coeff,p->exp);
    }
}

list_y()
{
    for(p=starty; p!=NULL; p=p->next)
    {
        printf("%d-%d\t", p->coeff,p->exp);
    }
}

list_z()
{
    for(p=startz; p!=NULL; p=p->next)
    {
        printf("%d-%d\t", p->coeff,p->exp);
    }
}



































No comments:

Post a Comment