Guest User

AddPoly.C

a guest
Sep 3rd, 2013
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4.  
  5. // This is my implementation to add and multiply
  6. // two polynomials using linked list
  7. // So far, it just inputs and displays the polynomial
  8. struct term {
  9.     int exp;
  10.     int coef;
  11.     struct term *next;
  12. };
  13.  
  14. struct term * addTerm(struct term * polynomial,int exp,int coef){ // adds a term to polynomial
  15.     if(polynomial == NULL ){
  16.         polynomial = (struct term *) malloc(sizeof(struct term));
  17.         polynomial->exp = exp;
  18.         polynomial->coef = coef;
  19.         polynomial->next = NULL;
  20.     }else{
  21.         struct term *newTerm = (struct term *)malloc(sizeof(struct term));
  22.         newTerm->exp = exp;
  23.         newTerm->coef = coef;
  24.         newTerm->next = NULL;
  25.        
  26.         struct term *temp = polynomial;
  27.         while(temp->next != NULL){
  28.             temp = temp->next;
  29.         }
  30.         temp->next = newTerm;
  31.     }
  32.     return polynomial;
  33. }
  34.  
  35. void display(struct term * polynomial){ // displays the polynomial
  36.     struct term *p = polynomial;
  37.     if(p != NULL){
  38.         while(p != NULL){
  39.             printf("+ %dx%d ",p->coef,p->exp);
  40.             p = p->next;
  41.         }
  42.     }
  43.     p = NULL;
  44. }
  45.  
  46.  struct term * addPolynomials(struct term * poly1, struct term * poly2){
  47.     struct term * polynomial3 = (struct term *) malloc(sizeof(struct term)); // final answer
  48.    
  49.     struct term * poly1Copy = poly1; // keeping copies
  50.     struct term * poly2Copy = poly2; // in case we need to restart passes
  51.    
  52.    
  53.     while(poly1 != NULL && poly2 != NULL){
  54.         if(poly1->exp == poly2->exp){
  55.             polynomial3 = addTerm(polynomial3,poly1->exp,(poly1->coef + poly2 -> coef)); // add the term to final answer
  56.            
  57.             poly1 = poly1->next; // go to next term
  58.             poly2 = poly2->next; // go to next term
  59.            
  60.         }
  61.        
  62.         if(poly1->exp > poly2->exp){
  63.             poly1 = poly1->next;
  64.         }
  65.        
  66.        
  67.         if(poly2->exp > poly1->exp ){
  68.             poly2 = poly2->next;
  69.         }
  70.        
  71.     }
  72.     return polynomial3;
  73. }
  74.  
  75. void main(){ // run it
  76.     int i = 0;
  77.     int coef = 0;
  78.     int exp = 0;
  79.    
  80.     struct term * polynomial1 = NULL;
  81.     struct term * polynomial2 = NULL;
  82.    
  83.     printf("Entering polynomial 1: \n");
  84.     while(i++ < 3){
  85.         printf("Enter CoEfficient and Exponent for Term %d ",i);
  86.         scanf("%d %d",&coef,&exp);
  87.         polynomial1 = addTerm(polynomial1,exp,coef);
  88.     }
  89.     i = 0;
  90.     display(polynomial1); // show it
  91.    
  92.     printf("\n\nEntering polynomial 2: \n");
  93.     while(i++ < 4){
  94.         printf("Enter CoEfficient and Exponent for Term %d ",i);
  95.         scanf("%d %d",&coef,&exp);
  96.         polynomial2 = addTerm(polynomial1,exp,coef);
  97.     }
  98.     display(polynomial2); // show it
  99.    
  100.     printf("\n\nDisplaying Addition \n");
  101.     display(addPolynomials(polynomial1,polynomial2));
  102.    
  103.     getch();
  104. }
Advertisement
Add Comment
Please, Sign In to add comment