Advertisement
Guest User

Untitled

a guest
May 30th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. /* polynomial.c source file for task in exercise 10*/
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #include "polynomial.h"
  7. Node_ptr constructorTerm(int coef, int exp, Node_ptr tmp);
  8. void printPolynomial(Polynomial p, int bild);
  9. Polynomial constructPolynomial(int ce[], int size);
  10. Polynomial addPolynomials( Polynomial p1, Polynomial p2);
  11.  
  12.  
  13. int main ( ){
  14. Polynomial p1;
  15. Polynomial p2;
  16. Polynomial p3; // variable refers to header node
  17. Node_ptr term; // variable refers to a term (node)
  18. Node_ptr tmp;
  19. int ce[100];
  20. int ce2[100];
  21. // constructorTerm(5,14,tmp);
  22. int size = 0;
  23. int size2 = 0;
  24. int i, temp;
  25. printf("Antal siffror i polynomial nr 1: ");
  26. scanf("%d",&size);
  27. for(i = 0; i < size; i++){
  28. printf("\ncoef: ");
  29. scanf("%d",&ce[i*2]);
  30. printf("\nexp: ");
  31. scanf("%d",&ce[i*2+1]);
  32.  
  33. }
  34. printf("Antal siffror i polynomial nr 2: ");
  35. scanf("%d",&size2);
  36. for(i = 0; i < size2; i++){
  37. printf("\ncoef: ");
  38. scanf("%d",&ce2[i*2]);
  39. printf("\nexp: ");
  40. scanf("%d",&ce2[i*2+1]);
  41. }
  42.  
  43.  
  44. int bild = 1;
  45. p1 = constructPolynomial(ce, size);
  46. printPolynomial(p1,bild);
  47.  
  48. bild++;
  49. p2 = constructPolynomial(ce2, size2);
  50. printPolynomial(p2,bild);
  51. int count = 0;
  52. p3 = addPolynomials(p1,p2);
  53. printf("The final polynomial: \n");
  54. p3 = p3 -> next;
  55.  
  56. while(p3 != NULL){
  57. if(count >= 1){
  58. printf("+ %dx^%d ", p3->coef, p3->exp);
  59. p3 = p3 -> next;
  60. count++;
  61. }
  62. else{
  63. printf(" %dx^%d ", p3->coef, p3->exp);
  64. count++;
  65. p3 = p3 -> next;
  66. }
  67. }
  68. }
  69.  
  70. Node_ptr constructorTerm(int coef,int exp, Node_ptr tmp){
  71. Node_ptr hello = (Node_ptr)malloc ( sizeof (struct node) );
  72. (*hello).coef = coef;
  73. (*hello).exp = exp;
  74. (*hello).next = tmp->next;
  75. return hello;
  76. }
  77. printPolynomial(Polynomial p, int bild){
  78. int count = 0;
  79. if(bild == 1)
  80. printf("1st polynomial:\n");
  81. else printf("2nd polynomial:\n");
  82. while(p != NULL){
  83. if(count >= 1){
  84. printf("+ %dx^%d ", p->coef, p->exp);
  85. p = p -> next;
  86. count++;
  87. }else{
  88. printf(" %dx^%d ", p->coef, p->exp);
  89. p = p -> next;
  90. count++;
  91. }
  92. }
  93. printf("\n");
  94. }
  95. Polynomial constructPolynomial(int ce[], int size){
  96. int i = 1;
  97. Node_ptr temporary = (Node_ptr)malloc(sizeof(struct node));
  98.  
  99. Polynomial sup = (Node_ptr)malloc(sizeof(struct node));
  100.  
  101. sup-> coef = ce[0]; sup ->exp = ce[1]; sup -> next = NULL;
  102.  
  103. for (i= 1; i< size; i++){
  104. temporary = constructorTerm(ce[i*2],ce[(i*2+1)],sup);
  105. temporary -> next = sup;
  106. sup = temporary;
  107. }
  108.  
  109. return sup;
  110. }
  111. /*Polynomial addPolynomials(Polynomial p1, Polynomial p2){
  112. Polynomial p3 = (Node_ptr)malloc(sizeof(struct node));
  113. Node_ptr tmp = p3;
  114. p3 -> next = NULL;
  115. while(p1 !=NULL && p2 != NULL){
  116. if(p1->exp > p2->exp){
  117. tmp -> next = constructorTerm(p1->coef,p1->exp,tmp);
  118. tmp = tmp -> next;
  119. p1 = p1->next;
  120. }
  121. else if(p2->exp > p1->exp){
  122. tmp -> next = constructorTerm(p2->coef,p2->exp,tmp);
  123. tmp = tmp -> next;
  124. p2 = p2->next;
  125. }
  126. else if(p2->exp == p1 ->exp){
  127. int bingo = p2->coef + p1->coef;
  128. tmp -> next = constructorTerm(bingo,p1->exp,tmp);
  129. tmp = tmp -> next;
  130. p2 = p2 ->next;
  131. p1 = p1 -> next;
  132. }
  133. }
  134. while(p1 != NULL || p2 != NULL){
  135. if(p1 == NULL && p2 == NULL)
  136. break;
  137. else{
  138. if(p1 == NULL && p2 != NULL){
  139. tmp -> next = constructorTerm(p2->coef,p2->exp,tmp);
  140. tmp = tmp -> next;
  141. p2 = p2->next;
  142. }
  143. else if(p2 == NULL && p1 != NULL){
  144. tmp -> next = constructorTerm(p1->coef,p1->exp,tmp);
  145. tmp = tmp -> next;
  146. p1 = p1->next;
  147. }
  148. }
  149. }
  150. return p3;
  151. }
  152. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement