Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #define MAX(a,b) ((a>b)?a:b)
  3. #define MAX_DEGREE 50
  4.  
  5. typedef struct {
  6. int degree;
  7. float coef[MAX_DEGREE + 1];
  8. }polynomial;
  9.  
  10. void printPoly(polynomial poly);
  11. void zeroP(polynomial *poly);
  12. int isZeroP(polynomial poly);
  13. float coef(polynomial poly, int exp);
  14. void addTerm(polynomial *poly, float val, int exp);
  15. void delTerm(polynomial *poly, int exp);
  16.  
  17. polynomial multTerm(polynomial A, float val, int exp);
  18.  
  19.  
  20.  
  21. void main()
  22. {
  23. polynomial A;
  24. polynomial C;
  25. zeroP(&A);
  26. addTerm(&A, 4, 3);
  27. addTerm(&A, 3, 2);
  28. addTerm(&A, 5, 0);
  29.  
  30. zeroP(&C);
  31. C = multTerm(A, 2.0, 3);
  32.  
  33. /* 결과 출력 */
  34. printf("\n A(x)="); printPoly(A); // 다항식 A 출력
  35. printf("\n C(x)="); printPoly(C); // 다항식 C 출력
  36.  
  37. }
  38.  
  39. void printPoly(polynomial poly)
  40. {
  41. int i;
  42.  
  43. for (i = 0; i <= MAX_DEGREE; i++)
  44. if (poly.coef[i] != 0)
  45. printf("%3.0fx^%d", poly.coef[i], MAX_DEGREE - i);
  46. printf("\n");
  47. }
  48.  
  49. void zeroP(polynomial *poly)
  50. {
  51. int i;
  52. poly->degree = 0;
  53. for (i = 0; i<MAX_DEGREE + 1; i++) {
  54. poly->coef[i] = 0;
  55. }
  56. }
  57.  
  58. int isZeroP(polynomial poly)
  59. {
  60. if (poly.degree == 0 && poly.coef[MAX_DEGREE] == 0)
  61. return 1;
  62. else
  63. return 0;
  64. }
  65.  
  66. float coef(polynomial poly, int exp)
  67. {
  68. return poly.coef[MAX_DEGREE - exp];
  69. }
  70.  
  71. void addTerm(polynomial *poly, float val, int exp)
  72. {
  73. poly->coef[MAX_DEGREE - exp] = val;
  74. if (exp > (poly->degree))
  75. poly->degree = exp;
  76. }
  77.  
  78. void delTerm(polynomial *poly, int exp)
  79. {
  80. poly->coef[MAX_DEGREE - exp] = 0;
  81. if (poly->degree>0)
  82. poly->degree--;
  83. }
  84.  
  85. polynomial multTerm(polynomial A, float val, int exp)
  86. {
  87. int i;
  88. polynomial C;
  89. zeroP(&C);
  90. i = A.degree;
  91. while(i>=0)
  92. {
  93. printf("i = %d\n", i);
  94. addTerm(&C, coef(A, A.degree) * val, A.degree + exp);
  95. delTerm(&A, A.degree);
  96. i--;
  97. }
  98.  
  99. return C;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement