View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12795 Members
Join this group to post and comment.
upgrade
upgrade • Feb 27, 2008

C function for addition of two single variable polynomials

can any1 tel me wat may be c code for writing function to add 2 single variable polynomials of length say n&m?
upgrade
upgrade • Feb 27, 2008
c code

can anyone giv me c code for addition of two single variable polynomials.
mahul
mahul • Feb 27, 2008
please be a little more elaborate if u can, like how u represent and store ur polynomials, the arguments u would like to pass to ur function and such stuff. By what u have posted, there was very little that i could comprehend.
upgrade
upgrade • Feb 28, 2008
i want to accept any length two polynomials like say 6x^3+5x^2
and 4x^2+5x+7
now i hav to take powers and coefficients of x from user.i hav done that.
but now i need to add these polynomials.
friendster7
friendster7 • Mar 1, 2008
mynode *polynomial_add(mynode *h1, mynode *h2, mynode *h3)
{
mynode *p1, *p2;
int x1, x2, y1, y2, cf1, cf2, cf;

p1 = h1->next;

while(p1!=h1)
{
x1 = p1->px;
y1 = p1->py;
cf1 = p1->cf;

// Search for this term in the second polynomial

p2 = h2->next;

while(p2 != h2)
{
x2 = p2->px;
y2 = p2->py;
cf2 = p2->cf;

if(x1 == x2 && y1 == y2)break;

p2 = p2->next;

}


if(p2 != h2)
{
// We found something in the second polynomial.

cf = cf1 + cf2;
p2->flag = 1;

if(cf!=0){h3=addNode(cf,x1,y1,h3);}
}
else
{
h3=addNode(cf,x1,y1,h3);
}

p1 = p1->next;

}//while


// Add the remaining elements of the second polynomail to the result

while(p2 != h2)
{
if(p2 -> flag ==0)
{
h3=addNode(p2->cf, p2->px, p2->py, h3);
}
p2=p2->next;
}

return(h3);
}
this will help u i guess..this can add two polynomials
I just joined the site and would like to contribute in here
Code in PASCAL using linked list
{Program to perform polynomial addition using linked list}
PROGRAM polyaddll(input,output);
TYPE nodeptr=^node;
     node=RECORD
     coef:integer;
     exp:integer;
     link:nodeptr;
     END;
VAR one,two,res:nodeptr;
    k:integer;
PROCEDURE print(n:nodeptr);
VAR p:nodeptr;
BEGIN
     p:=n;
     WHILE (p<>NIL) DO
     BEGIN
          write(p^.coef,' X^ ',p^.exp);
          IF (p^.link<>NIL) THEN
             write(' + ');
          p:=p^.link;
     END;
     writeln;
END;
PROCEDURE create(VAR n:nodeptr);
VAR p,q:nodeptr;
    c:char;
BEGIN
     c:='y';
     WHILE (c<>'n') DO
     BEGIN
          new(p);
          write('Enter the coefficient of the term : ');
          readln(k);
          p^.coef:=k;
          write('Enter the exponent of the term : ');
          readln(k);
          p^.exp:=k;
          p^.link:=NIL;
          write('Do you want to add any more terms (Y/N) : ');
          readln(c);
          IF (n=NIL) THEN
          BEGIN
               n:=p;
               q:=p;
          END
          ELSE
          BEGIN
               q^.link:=p;
               q:=p;
          END;
     END;
END;
PROCEDURE sum(VAR a,b,c:nodeptr);
VAR p,q,r,s:nodeptr;
BEGIN
     p:=a;
     q:=b;
     WHILE ((p<>NIL) AND (q<>NIL)) DO
     BEGIN
          new(r);
          r^.link:=NIL;
          IF (p^.exp>q^.exp) THEN
          BEGIN
               r^.coef:=p^.coef;
               r^.exp:=p^.exp;
               p:=p^.link;
          END
          ELSE
              IF (q^.exp>p^.exp) THEN
              BEGIN
                   r^.coef:=q^.coef;
                   r^.exp:=q^.exp;
                   q:=q^.link;
              END
              ELSE
              BEGIN
                   r^.coef:=p^.coef+q^.coef;
                   r^.exp:=p^.exp;
                   p:=p^.link;
                   q:=q^.link;
              END;
          IF (c=NIL) THEN
          BEGIN
               c:=r;
               s:=r;
          END
          ELSE
          BEGIN
               s^.link:=r;
               s:=r;
          END;
     END;
     WHILE (p<>NIL) DO
     BEGIN
          new(r);
          r^.coef:=p^.coef;
          r^.exp:=p^.exp;
          r^.link:=NIL;
          p:=p^.link;
          IF (c=NIL) THEN
          BEGIN
               c:=r;
               s:=r;
          END
          ELSE
          BEGIN
               s^.link:=r;
               s:=r;
          END;
     END;
     WHILE (q<>NIL) DO
     BEGIN
          new(r);
          r^.coef:=q^.coef;
          r^.exp:=q^.exp;
          r^.link:=NIL;
          q:=q^.link;
          IF (c=NIL) THEN
          BEGIN
               c:=r;
               s:=r;
          END
          ELSE
          BEGIN
               s^.link:=r;
               s:=r;
          END;
     END;
END;
BEGIN
     one:=NIL;
     two:=NIL;
     res:=NIL;
     writeln('Enter the first polynomial : ');
     create(one);
     writeln('Enter the second polynomial : ');
     create(two);
     writeln('The first polynomial is  : ');
     print(one);
     writeln('The second polynomial is : ');
     print(two);
     sum(one,two,res);
     writeln('The reult of addition is : ');
     print(res);
     readln;
END.
We Rock!!!!!!!
Hi friendster7,

I did not tried running your code but here are some of my view:
1. Since you are initilizing p1 & p2 with next of h1 & h2 and then comparing for (p1 != h1) and (p2 != h2), the conditions will not meet.
2. Code for addNode is required.

Here is my sample code for the given problem statement.

#include "stdio.h"

typedef struct node {
  int cof;
  int pow;
  struct node *next;
} nodet;

nodet* createnewnode(int cof, int pow) {
  nodet* newnode = (nodet*)malloc(sizeof(nodet));
  newnode->cof = cof;
  newnode->pow = pow;
  newnode->next = NULL;
  return newnode;
}

void deletepoly(nodet* head) {
  nodet* temp = NULL;
  while(head != NULL) {
    temp = head;
    head = head->next;
    free(temp);
  }
}

void displaypoly(nodet* head) {
  if(head == NULL) {
    printf("Empty polynomial\n");
  } else {
    do {
      if(head->cof > 0) {
        printf("+");
      }
      if((head->cof == 1) && (head->pow == 0)) {
        printf("1");
      } else {
        if(head->cof != 1) {
          printf("%d", head->cof);
        }
        if(head->pow != 0) {
          printf("x");
          if(head->pow != 1) {
            printf("^%d", head->pow);
          }
         printf(" ");
        }
      }
      head = head->next;
    } while(head != NULL);
    printf("\n");
  }
}

nodet* addpoly(nodet* poly1, nodet* poly2) {
  nodet* newnode = NULL;
  nodet* retnode = NULL;
  nodet* retnodelast = NULL;
  nodet* temp = NULL;
  nodet* temp1 = poly1;
  nodet* temp2 = poly2;
  int cof = 0;
  int pow = 0;

  while((temp1 != NULL) && (temp2 != NULL)) {
    if(temp1->pow > temp2->pow) {
      cof = temp1->cof;
      pow = temp1->pow;
      temp1 = temp1->next;
    } else if(temp1->pow == temp2->pow) {
      cof = temp1->cof + temp2->cof;
      pow = temp1->pow;
      temp1 = temp1->next;
      temp2 = temp2->next;
    } else {
      cof = temp2->cof;
      pow = temp2->pow;
      temp2 = temp2->next;
    }
    if(cof != 0) {
      newnode = createnewnode(cof, pow);
      if(retnode == NULL) {
        retnode = newnode;
        retnodelast = retnode;
      } else {
        retnodelast->next = newnode;
        retnodelast = retnodelast->next;
      }
    }
  }

  if(temp1 != NULL) {
    temp = temp1;
  } else {
    temp = temp2;
  }

  while(temp != NULL) {
    newnode = createnewnode(temp->cof, temp->pow);
    if(retnode == NULL) {
      retnode = newnode;
      retnodelast = retnode;
    } else {
      retnodelast->next = newnode;
      retnode = retnode->next;
    }
    temp = temp->next;    
  }

  return retnode;
}

int main() {
  nodet *poly1 = NULL;
  nodet *poly2 = NULL;
  nodet *polysum = NULL;
  nodet* newnode = NULL;
  nodet* temp = NULL;
  int order_poly1 = 0;
  int order_poly2 = 0;
  int cof = 0;
  int i = 0;

  //read polynomial 1
  scanf("%d", &order_poly1);
  if(order_poly1 < 0) {
    printf("Polynomial of invalid order\n");
    return 0;
  }

  for(i = order_poly1; i >= 0; i--) {
    scanf("%d", &cof);
    if(cof != 0) {
      newnode = createnewnode(cof, i);
      if(poly1 == NULL) {
        poly1 = newnode;
        temp = poly1;
      } else {
        temp->next = newnode;
        temp = temp->next;
      }
    }
  }

  //read polynomial 2
  scanf("%d", &order_poly2);
  if(order_poly2 < 0) {
    printf("Polynomial of invalid order\n");
    deletepoly(poly1);
    return 0;
  }

  for(i = order_poly2; i >= 0; i--) {
    scanf("%d", &cof);
    if(cof != 0) {
      newnode = createnewnode(cof, i);
      if(poly2 == NULL) {
        poly2 = newnode;
        temp = poly2;
      } else {
        temp->next = newnode;
        temp = temp->next;
      }
    }
  }

  //display polynomial 1
  printf("Polynomial 1:\n");
  displaypoly(poly1);

  //display polynomial 2
  printf("Polynomial 2:\n");
  displaypoly(poly2);

  //add polynomials & display resultant polynomial
  polysum = addpoly(poly1, poly2);
  printf("Result Polynomial:\n");
  displaypoly(polysum);

  //delete polynomials
  deletepoly(poly1);
  deletepoly(poly2);
  deletepoly(polysum);

  return 0;
}
Compile the file with above code and run as:
a.exe < input.txt > output.txt [on windows]
a.out < input.txt > output.txt [on linux]

Here,
a.exe or a.out are the executables generated after compilation

input.txt is the file containing input, e.g., if the polynomials are
P1: 4x^2 + x + 1
P2: 5x^3 -4x^2 - 1
Then input.txt will be
2
4
1
1
3
5
-4
0
-1
2
4
1
1

2 represent order of polynomial (4x^2 + x + 1) and then each number represents coefficients
3
5
-4
0
-1

3 represent order of polynomial (5x^3 -4x^2 - 1) and then each number represents coefficients
output.txt is the output file containing result, for given example output.txt will contain
Polynomial 1:
+4x^2 +x +1
Polynomial 2:
+5x^3 -4x^2 -1
Result Polynomial:
+5x^3 +x
-Pradeep

Share this content on your social channels -