当前位置:   article > 正文

02-线性结构1 一元多项式的乘法与加法运算_采用线性结构,设计一个混合运算

采用线性结构,设计一个混合运算
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct node{
  4. int coefficient;
  5. int exponent;
  6. struct node * next;
  7. } PolyNode, *Polynomial;
  8. Polynomial ReadPoly();
  9. void Attach(int c, int e, Polynomial * Rear);
  10. Polynomial MultPoly(Polynomial P1, Polynomial P2);
  11. Polynomial AddPoly(Polynomial P1, Polynomial P2);
  12. void PrintPoly(Polynomial P);
  13. int main(int argc, char const *argv[])
  14. {
  15. Polynomial Poly1, Poly2, PolySum, PolyMul;
  16. Poly1 = ReadPoly();
  17. Poly2 = ReadPoly();
  18. PolyMul = MultPoly(Poly1, Poly2);
  19. PrintPoly(PolyMul);
  20. PolySum = AddPoly(Poly1, Poly2);
  21. PrintPoly(PolySum);
  22. return 0;
  23. }
  24. Polynomial ReadPoly()
  25. {
  26. Polynomial P, Rear, temp;
  27. P = (PolyNode*)malloc(sizeof(PolyNode));
  28. P->next = NULL;
  29. Rear = P;
  30. int N, c, e;
  31. scanf("%d", &N);
  32. while(N--){
  33. scanf("%d %d", &c, &e);
  34. Attach(c, e, &Rear);
  35. }
  36. temp = P;
  37. P = P->next;
  38. free(temp);
  39. return P;
  40. }
  41. void Attach(int c, int e, Polynomial * pRear)
  42. {
  43. Polynomial P;
  44. P = (PolyNode*)malloc(sizeof(PolyNode));
  45. P->coefficient = c;
  46. P->exponent = e;
  47. P->next = NULL;
  48. (*pRear)->next = P;
  49. *pRear = P;
  50. }
  51. Polynomial MultPoly(Polynomial P1, Polynomial P2)
  52. {
  53. Polynomial P, temp1, temp2, Rear, temp;
  54. int c, e;
  55. if(!P1 || !P2)
  56. return NULL;
  57. temp1 = P1;
  58. temp2 = P2;
  59. P = (PolyNode*)malloc(sizeof(PolyNode));
  60. P->next = NULL;
  61. Rear = P;
  62. while(temp2){
  63. c = temp1->coefficient * temp2->coefficient;
  64. e = temp1->exponent + temp2->exponent;
  65. if(c != 0){
  66. Attach(c, e, &Rear);
  67. temp2 = temp2->next;
  68. }
  69. }
  70. temp1 = temp1->next;
  71. while(temp1){
  72. temp2 = P2, Rear = P;
  73. while(temp2){
  74. c = temp1->coefficient * temp2->coefficient;
  75. e = temp1->exponent + temp2->exponent;
  76. if(c != 0){
  77. while(Rear->next && Rear->next->exponent > e)
  78. Rear = Rear->next;
  79. if(Rear->next && Rear->next->exponent == e){
  80. if(Rear->next->coefficient + c)
  81. Rear->next->coefficient += c;
  82. else{
  83. temp = Rear->next;
  84. Rear->next = temp->next;
  85. free(temp);
  86. }
  87. }
  88. else{
  89. temp = (PolyNode*)malloc(sizeof(PolyNode));
  90. temp->coefficient = c;
  91. temp->exponent = e;
  92. temp->next = Rear->next;
  93. Rear->next = temp;
  94. Rear = Rear->next;
  95. }
  96. temp2 = temp2->next;
  97. }
  98. }
  99. temp1 = temp1->next;
  100. }
  101. temp = P;
  102. P = P->next;
  103. free(temp);
  104. return P;
  105. }
  106. Polynomial AddPoly(Polynomial P1, Polynomial P2)
  107. {
  108. Polynomial P, temp1, temp2, Rear, temp;
  109. if(!P1 && !P2){
  110. if(!P1)
  111. return P2;
  112. else
  113. return P1;
  114. }
  115. P = (PolyNode*)malloc(sizeof(PolyNode));
  116. P->next = NULL;
  117. Rear = P;
  118. temp1 = P1;
  119. temp2 = P2;
  120. while(temp1 && temp2){
  121. if(temp1->exponent > temp2->exponent){
  122. if(temp1->coefficient){
  123. Attach(temp1->coefficient, temp1->exponent, &Rear);
  124. }
  125. temp1 = temp1->next;
  126. }
  127. else if(temp1->exponent == temp2->exponent){
  128. if(temp1->coefficient + temp2->coefficient){
  129. Attach(temp1->coefficient + temp2->coefficient, temp1->exponent, &Rear);
  130. }
  131. temp1 = temp1->next;
  132. temp2 = temp2->next;
  133. }
  134. else{
  135. if(temp2->coefficient){
  136. Attach(temp2->coefficient, temp2->exponent, &Rear);
  137. }
  138. temp2 = temp2->next;
  139. }
  140. }
  141. while(temp1){
  142. Attach(temp1->coefficient, temp1->exponent, &Rear);
  143. temp1 = temp1->next;
  144. }
  145. while(temp2){
  146. Attach(temp2->coefficient, temp2->exponent, &Rear);
  147. temp2 = temp2->next;
  148. }
  149. temp = P;
  150. P = P->next;
  151. free(temp);
  152. return P;
  153. }
  154. void PrintPoly(Polynomial P)
  155. {
  156. int flag = 0;
  157. if(!P){
  158. printf("0 0");
  159. }
  160. while(P){
  161. if (!flag)
  162. flag = 1;
  163. else
  164. printf(" ");
  165. printf("%d %d", P->coefficient, P->exponent);
  166. P = P->next;
  167. }
  168. printf("\n");
  169. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/342857
推荐阅读
相关标签
  

闽ICP备14008679号