Advertisement
Guest User

struct Polynomial

a guest
Jan 13th, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct Polynomial {
  5. int deg;
  6. double* coeff;
  7. };
  8.  
  9. void findDeg(struct Polynomial* p) {
  10. for (int i = p->deg; i >= 0; i--) {
  11. if (p->coeff[i] != 0.0) {
  12. p->deg = i;
  13. return;
  14. }
  15. }
  16. p->deg = 0;
  17. }
  18.  
  19.  
  20. struct Polynomial* addPolynomials(struct Polynomial* a, struct Polynomial* b) {
  21. int newDeg = a->deg > b->deg ? a->deg : b->deg;
  22. struct Polynomial* n = malloc(sizeof(*n));
  23. n->coeff = malloc((newDeg + 1) * sizeof(*n->coeff));
  24.  
  25. for (int i = newDeg - abs(a->deg - b->deg); i >= 0; i--) {
  26. n->coeff[i] = a->coeff[i] + b->coeff[i];
  27. }
  28.  
  29. if (a->deg > b->deg) {
  30. for (int i = a->deg; i > a->deg - abs(a->deg - b->deg); i--) {
  31. n->coeff[i] = a->coeff[i];
  32. }
  33. } else {
  34. for (int i = b->deg; i > b->deg - abs(a->deg - b->deg); i--) {
  35. n->coeff[i] = b->coeff[i];
  36. }
  37. }
  38.  
  39. for (int i = newDeg; i >= 0; i--) {
  40. if (n->coeff[i] != 0.0) {
  41. n->deg = i;
  42. return n;
  43. }
  44. }
  45. n->deg = 0;
  46. return n;
  47. }
  48.  
  49. struct Polynomial* substractPolynomials(struct Polynomial* a, struct Polynomial* b) {
  50. int newDeg = a->deg > b->deg ? a->deg : b->deg;
  51. struct Polynomial* n = malloc(sizeof(*n));
  52. n->coeff = malloc((newDeg + 1) * sizeof(*n->coeff));
  53.  
  54. for (int i = newDeg - abs(a->deg - b->deg); i >= 0; i--) {
  55. n->coeff[i] = a->coeff[i] - b->coeff[i];
  56. }
  57.  
  58. if (a->deg > b->deg) {
  59. for (int i = a->deg; i > a->deg - abs(a->deg - b->deg); i--) {
  60. n->coeff[i] = a->coeff[i];
  61. }
  62. } else {
  63. for (int i = b->deg; i > b->deg - abs(a->deg - b->deg); i--) {
  64. n->coeff[i] = -b->coeff[i];
  65. }
  66. }
  67.  
  68. for (int i = newDeg; i >= 0; i--) {
  69. if (n->coeff[i] != 0.0) {
  70. n->deg = i;
  71. return n;
  72. }
  73. }
  74. n->deg = 0;
  75. return n;
  76. }
  77.  
  78. struct Polynomial* multiplyPolynomials(struct Polynomial* a, struct Polynomial* b) {
  79. struct Polynomial* n = malloc(sizeof(*n));
  80. n->deg = a->deg + b->deg;
  81. n->coeff = calloc((a->deg + b->deg + 1), sizeof(*n->coeff));
  82.  
  83. for (int i = 0; i < a->deg + 1; i++) {
  84. for (int j = 0; j < b->deg + 1; j++) {
  85. n->coeff[i + j] += a->coeff[i] * b->coeff[j];
  86. }
  87. }
  88.  
  89. return n;
  90. }
  91.  
  92. struct Polynomial* dividePolynomials(struct Polynomial* a, struct Polynomial* b, struct Polynomial* r) {
  93. struct Polynomial* q = malloc(sizeof(*q));
  94. if (a->deg < b->deg)
  95. {
  96. q->deg = 0;
  97. q->coeff = malloc((q->deg + 1) * sizeof(*q->coeff));
  98. q->coeff[0] = 0.0;
  99. r->deg = a->deg;
  100. for(int i=r->deg;i>=0;i--)
  101. r->coeff[i] = a->coeff[i];
  102. return q;
  103. }
  104. struct Polynomial* temp = malloc(sizeof(*temp));
  105.  
  106. temp->coeff = malloc((a->deg + 1) * sizeof(*temp->coeff));
  107. temp->deg = a->deg;
  108.  
  109. q->coeff = malloc((a->deg + 1) * sizeof(*q->coeff));
  110. q->deg = a->deg;
  111.  
  112. for (int i = 0; i < a->deg + 1; i++) {
  113. temp->coeff[i] = a->coeff[i];
  114. q->coeff[i] = 0.0;
  115. }
  116.  
  117. for (int i = a->deg - b->deg; i >= 0; i--) {
  118. q->coeff[i] = temp->coeff[b->deg + i] / b->coeff[b->deg];
  119. for (int j = b->deg + i - 1; j >= i; j--) {
  120. temp->coeff[j] -= q->coeff[i] * b->coeff[j - i];
  121. }
  122. }
  123.  
  124. for (int i = 0; i < b->deg; i++) {
  125. r->coeff[i] = temp->coeff[i];
  126. }
  127.  
  128. if (r->deg < a->deg) {
  129. for (int i = b->deg; i < r->deg + 1; i++) {
  130. r->coeff[i] = 0.0;
  131. }
  132. } else {
  133. for (int i = b->deg; i < a->deg + 1; i++) {
  134. r->coeff[i] = 0.0;
  135. }
  136. }
  137.  
  138. free(temp->coeff);
  139. free(temp);
  140.  
  141. findDeg(r);
  142. findDeg(q);
  143.  
  144. return q;
  145. }
  146.  
  147.  
  148. struct Polynomial *GCD(struct Polynomial* a, struct Polynomial* b)
  149. {
  150. struct Polynomial * temp = malloc(sizeof(*temp));
  151. temp->deg = a->deg;
  152. temp->coeff = malloc((temp->deg+1)*sizeof(*temp->coeff));
  153. while(b->deg != 0 || b->coeff[0] != 0.0) // Negujemy warunek b->deg == 0 && b->coeff[0] == 0.0 korzystajac z prawa De Morgana
  154. {
  155. for(int i=0;i <= a->deg;i++)
  156. {
  157. temp->coeff[i] = a->coeff[i];
  158. }
  159. free(a->coeff);
  160. a->deg = b->deg;
  161. a->coeff = malloc((a->deg+1)*sizeof(*a->coeff));
  162. for(int i = 0;i <= a->deg;i++)
  163. {
  164. a->coeff[i] = b->coeff[i];
  165. }
  166. dividePolynomials(temp,b,b);
  167. }
  168. findDeg(a);
  169. free(temp->coeff);
  170. free(temp);
  171. return a;
  172. }
  173.  
  174.  
  175. void printPolynomial(struct Polynomial* c) {
  176. if (c->deg == 0) return;
  177. for (int i = c->deg; i >= 0; i--) {
  178. if (c->coeff[i] < 0.0) {
  179. printf(" -%lfx^%d ", -c->coeff[i], i);
  180. } else if (c->coeff[i] > 0.0) {
  181. printf(" +%lfx^%d ", c->coeff[i], i);
  182. }
  183. }
  184. }
  185.  
  186. int main(int argc, char** argv) {
  187. struct Polynomial* a = malloc(sizeof(*a));
  188. struct Polynomial* b = malloc(sizeof(*b));
  189. struct Polynomial* c = malloc(sizeof(*c));
  190. struct Polynomial* r = malloc(sizeof(*r));
  191. a->deg = 3;
  192. r->deg = a->deg;
  193. r->coeff = malloc((a->deg + 1) * sizeof(*r->coeff));
  194. a->coeff = malloc((a->deg + 1) * sizeof(*a->coeff));
  195. a->coeff[0] = -12;
  196. a->coeff[1] = -8;
  197. a->coeff[2] = 1;
  198. a->coeff[3] = 1;
  199. //a->coeff[4] = 1;
  200.  
  201.  
  202.  
  203.  
  204. b->deg = 3;
  205. b->coeff = malloc((b->deg + 1) * sizeof(*b->coeff));
  206. b->coeff[0] = -4;
  207. b->coeff[1] = 0;
  208. b->coeff[2] = 3;
  209. b->coeff[3] = 1;
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. //c = dividePolynomials(a, b, b);
  217. c = GCD(a, b);
  218.  
  219. //printf("\n%d\n", c->deg);
  220.  
  221. printPolynomial(c);
  222. printf("\n");
  223. // printPolynomial(b);
  224. //for (int i = 0; i < b->deg; i++) printf("%lf", b->coeff[i]);
  225. printf("\n%d", c->deg);
  226. printf("\n%d", b->deg);
  227.  
  228. system("PAUSE");
  229. }
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement