当前位置:   article > 正文

02-线性结构2 一元多项式的乘法与加法运算

02-线性结构2 一元多项式的乘法与加法运算
  1. #pragma warning(disable:4996)
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. typedef struct Node *List;
  5. struct Node
  6. {
  7. int coef;//系数
  8. int expon;//指数
  9. List next;
  10. };
  11. List CreateList(int n);
  12. List Mul(List list1,List list2);
  13. List Add(List list1,List list2);
  14. void Print(List L);
  15. int main()
  16. {
  17. List list1, list2,mul,add;
  18. int number1, number2;
  19. scanf("%d",&number1);
  20. list1 = CreateList(number1);
  21. scanf("%d",&number2);
  22. list2 = CreateList(number2);
  23. mul = Mul(list1,list2);
  24. add = Add(list1,list2);
  25. Print(mul);
  26. printf("\n");
  27. Print(add);
  28. return 0;
  29. }
  30. List CreateList(int n)
  31. {
  32. List L,t,cycle;
  33. L = (List)malloc(sizeof(struct Node));
  34. L->next = NULL;
  35. t = L;
  36. for (int i=0;i<n;i++)
  37. {
  38. List temp;
  39. temp = (List)malloc(sizeof(struct Node));
  40. scanf("%d",&temp->coef);
  41. scanf("%d",&temp->expon);
  42. t->next = temp;
  43. t = temp;
  44. temp->next = NULL;
  45. }
  46. t->next = NULL;
  47. cycle = L;
  48. L = L->next;
  49. free(cycle);
  50. return L;
  51. }
  52. void Print(List L)
  53. {
  54. if (!L)
  55. {
  56. printf("0 0");
  57. }
  58. else
  59. {
  60. printf("%d %d", L->coef, L->expon);
  61. L = L->next;
  62. }
  63. while (L)
  64. {
  65. printf(" %d %d",L->coef,L->expon);
  66. L = L->next;
  67. }
  68. }
  69. List Mul(List list1,List list2)
  70. {
  71. List L,t,l1,l2;
  72. int flag;
  73. L = (List)malloc(sizeof(struct Node));
  74. L->next = NULL;
  75. flag = 0;
  76. l1 = list1;
  77. while (l1)
  78. {
  79. List t2,t3,cycle;
  80. t2= (List)malloc(sizeof(struct Node));
  81. t2->next = NULL;
  82. t3 = t2;
  83. t = L;
  84. l2 = list2;
  85. while (l2)
  86. {
  87. List temp;
  88. temp = (List)malloc(sizeof(struct Node));
  89. temp->next = NULL;
  90. temp->coef = l1->coef * l2->coef;
  91. temp->expon = l1->expon + l2->expon;
  92. t3->next = temp;
  93. t3 = temp;
  94. l2 = l2->next;
  95. }
  96. cycle = t2;
  97. t2 = t2->next;
  98. free(cycle);
  99. if (flag == 0)
  100. {
  101. flag = 1;
  102. cycle = t;
  103. t = t ->next;
  104. free(cycle);
  105. }
  106. L = Add(t, t2);
  107. l1 = l1->next;
  108. }
  109. return L;
  110. }
  111. List Add(List list1, List list2)
  112. {
  113. List L, t, cycle,l1,l2;
  114. L = (List)malloc(sizeof(struct Node));
  115. L->next = NULL;
  116. t = L;
  117. l1 = list1;
  118. l2 = list2;
  119. while (l1 && l2)
  120. {
  121. if (l1->expon>l2->expon)
  122. {
  123. List temp;
  124. temp = (List)malloc(sizeof(struct Node));
  125. temp->expon = l1->expon;
  126. temp->coef = l1->coef;
  127. if (temp->coef!=0)
  128. {
  129. t->next = temp;
  130. t = temp;
  131. }
  132. l1 = l1->next;
  133. temp->next = NULL;
  134. }
  135. else if (l1->expon < l2->expon)
  136. {
  137. List temp;
  138. temp = (List)malloc(sizeof(struct Node));
  139. temp->expon = l2->expon;
  140. temp->coef = l2->coef;
  141. if (temp->coef != 0)
  142. {
  143. t->next = temp;
  144. t = temp;
  145. }
  146. l2 = l2->next;
  147. temp->next = NULL;
  148. }
  149. else
  150. {
  151. List temp;
  152. temp = (List)malloc(sizeof(struct Node));
  153. temp->expon = l1->expon;
  154. temp->coef = l1->coef+l2->coef;
  155. if (temp->coef != 0)
  156. {
  157. t->next = temp;
  158. t = temp;
  159. }
  160. l1 = l1->next;
  161. l2 = l2->next;
  162. temp->next = NULL;
  163. }
  164. }
  165. while (l1)
  166. {
  167. List temp;
  168. temp = (List)malloc(sizeof(struct Node));
  169. temp->expon = l1->expon;
  170. temp->coef = l1->coef;
  171. if (temp->coef != 0)
  172. {
  173. t->next = temp;
  174. t = temp;
  175. }
  176. l1 = l1->next;
  177. temp->next = NULL;
  178. }
  179. while (l2)
  180. {
  181. List temp;
  182. temp = (List)malloc(sizeof(struct Node));
  183. temp->expon = l2->expon;
  184. temp->coef = l2->coef;
  185. if (temp->coef != 0)
  186. {
  187. t->next = temp;
  188. t = temp;
  189. }
  190. l2 = l2->next;
  191. temp->next=NULL;
  192. }
  193. cycle = L;
  194. L = L->next;
  195. free(cycle);
  196. return L;
  197. }

题源中国mooc

p.s:这个练习里变量还挺多的,大家多加注意。(来自一个debug,de了几个小时,最后发现是变量的区分后缀打错了人的自我感言。)

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

闽ICP备14008679号