样例">样例">样例">样例">样例">样例">样例">输入样例:4 3 4 -5 2_设计函数分别求两个一元多项式的乘积与和。 输入格式: 输入分">
当前位置:   article > 正文

02-线性结构2 一元多项式的乘法与加法运算 (20 分)_设计函数分别求两个一元多项式的乘积与和。 输入格式: 输入分2行,每行分别先给出

设计函数分别求两个一元多项式的乘积与和。 输入格式: 输入分2行,每行分别先给出

题目要求:设计函数分别求两个一元多项式的乘积与和。

输入格式:输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数)。数字间以空格分隔。

输出格式:输出分2行,分别以指数递降方式输出乘积多项式以及和多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。零多项式应输出0 0

输入样例:

4 3 4 -5 2  6 1  -2 0
3 5 20  -7 4  3 1

输出样例:

15 24 -25 22 30 21 -10 20 -21 8 35 6 -33 5 14 4 -15 3 18 2 -6 1
5 20 -4 4 -5 2 9 1 -2 0

思路:加法的实现比较简单,设置两个指针分别指向两个多项式链表第一项,哪个多项式当前项系数大,就把那一项加入到结果多项式中,然后令加入的那一项所属的多项式的指针移向下一项;如果指数相同,就使系数相加,然后加入到结果多项式中,然后令两个多项式的指针都移向下一项。乘法比较复杂,我采取的方法是用其中一多项式的每一项去分别乘另一多项式的每一项,将每次相乘结果加入到结果多项式中,但是注意:每次插入都要满足指数递减的顺序,即要插入到不比自己指数大的最小指数项前面,如果发现有指数相同的项,就直接使系数相加即可。输出的时候要注意,由于链表中存在系数为0的项,按题目要求应当去除,所以不应输出,当多项式每一项都为0时,应直接输出“0 0”,这要进行讨论。

代码:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct polynomial* PolyN;
  4. typedef struct polynomial {
  5. int coef, expon;
  6. PolyN next;
  7. }Polynomial;
  8. PolyN readPolynomial();
  9. PolyN makeSum(PolyN, PolyN);
  10. PolyN makeProduct(PolyN, PolyN);
  11. PolyN insert(PolyN, PolyN);
  12. void printPolynomial(PolyN);
  13. int main()
  14. {
  15. PolyN p1, p2, prod, sum;
  16. p1 = readPolynomial();
  17. p2 = readPolynomial();
  18. prod = makeProduct(p1, p2);
  19. sum = makeSum(p1, p2);
  20. printPolynomial(prod);
  21. printf("\n");
  22. printPolynomial(sum);
  23. return 0;
  24. }
  25. PolyN readPolynomial()
  26. {
  27. int num, coef, expon; scanf("%d", &num);
  28. PolyN head = 0, tail = 0, node = 0;
  29. while (num--)
  30. {
  31. node = (PolyN)malloc(sizeof(Polynomial));
  32. scanf("%d %d", &node->coef, &node->expon);
  33. if (!head)
  34. head = node;
  35. if (tail)
  36. tail->next = node;
  37. tail = node;
  38. tail->next = 0;
  39. }
  40. return head;
  41. }
  42. PolyN makeSum(PolyN p1, PolyN p2)
  43. {
  44. if (!p1 && !p2) return 0; //如果两多项式都为零多项式直接返回零多项式
  45. else if ((p1 && !p2) || (!p1 && p2)) return p1 ? p1 : p2; //如果其中一个多项式为零多项式则直接返回非零多项式即可
  46. PolyN ptr1 = p1, ptr2 = p2, sum = 0, * ptr = 0; //ptr是指向sum的next的指针,所以是二级指针
  47. while (ptr1 && ptr2)
  48. {
  49. if (!sum) {
  50. sum = (PolyN)malloc(sizeof(Polynomial));
  51. ptr = &sum;
  52. }
  53. else
  54. *ptr = (PolyN)malloc(sizeof(Polynomial));
  55. if (ptr1->expon > ptr2->expon)
  56. {
  57. (*ptr)->coef = ptr1->coef; (*ptr)->expon = ptr1->expon;
  58. ptr = &(*ptr)->next;
  59. ptr1 = ptr1->next;
  60. }
  61. else if (ptr1->expon < ptr2->expon)
  62. {
  63. (*ptr)->coef = ptr2->coef; (*ptr)->expon = ptr2->expon;
  64. ptr = &(*ptr)->next;
  65. ptr2 = ptr2->next;
  66. }
  67. else
  68. {
  69. (*ptr)->coef = ptr1->coef + ptr2->coef; (*ptr)->expon = ptr1->expon;
  70. ptr = &(*ptr)->next;
  71. ptr1 = ptr1->next;
  72. ptr2 = ptr2->next;
  73. }
  74. }
  75. ptr1 = ptr1 ? ptr1 : ptr2;
  76. *ptr = NULL;
  77. while (ptr1)
  78. {
  79. *ptr = (PolyN)malloc(sizeof(Polynomial));
  80. (*ptr)->coef = ptr1->coef;
  81. (*ptr)->expon = ptr1->expon;
  82. ptr1 = ptr1->next;
  83. (*ptr)->next = NULL;
  84. }
  85. return sum;
  86. }
  87. PolyN makeProduct(PolyN p1, PolyN p2)
  88. {
  89. if (!p1 || !p2) return 0;
  90. PolyN ptr1 = p1, ptr2 = p2, prod = 0;
  91. while (ptr1)
  92. {
  93. while (ptr2)
  94. {
  95. Polynomial node; //建立临时节点储存结果
  96. node.coef = ptr1->coef * ptr2->coef;
  97. node.expon = ptr1->expon + ptr2->expon;
  98. prod = insert(prod, &node); //插入
  99. ptr2 = ptr2->next;
  100. }
  101. ptr1 = ptr1->next;
  102. ptr2 = p2;
  103. }
  104. return prod;
  105. }
  106. PolyN insert(PolyN prod, PolyN next)
  107. {
  108. if (!prod) { //如果结果为空,直接插入即可
  109. prod = (PolyN)malloc(sizeof(Polynomial));
  110. prod->coef = next->coef;
  111. prod->expon = next->expon;
  112. prod->next = 0;
  113. }
  114. else {
  115. PolyN pre, cur = prod;
  116. for (; cur && cur->expon > next->expon; pre = cur, cur = cur->next); //寻找插入点
  117. if (!cur) { //如果发现应当插入到表末
  118. pre->next = (PolyN)malloc(sizeof(Polynomial));
  119. pre->next->coef = next->coef;
  120. pre->next->expon = next->expon;
  121. pre->next->next = NULL;
  122. }
  123. else if (cur->expon == next->expon) //发现指数相同项直接系数相加
  124. cur->coef += next->coef;
  125. else { //插入到两节点之间
  126. pre->next = (PolyN)malloc(sizeof(Polynomial));
  127. pre->next->coef = next->coef;
  128. pre->next->expon = next->expon;
  129. pre->next->next = cur;
  130. }
  131. }
  132. return prod;
  133. }
  134. void printPolynomial(PolyN p)
  135. {
  136. PolyN read = p; int flag = 0, cnt = 0;
  137. while (read) //先遍历一遍,看是否所有项均为0
  138. {
  139. if (read->coef != 0) {
  140. flag = 1;
  141. break;
  142. }
  143. read = read->next;
  144. }
  145. read = p;
  146. while (read && flag) //存在非零项则开始输出
  147. {
  148. if (!cnt && read->coef != 0) { //为了处理行末空格的问题,先输出第一个系数非零项
  149. printf("%d %d", read->coef, read->expon);
  150. cnt++;
  151. }
  152. else if (cnt && read->coef != 0) { //其后的系数非零项先输出空格再输出数据
  153. printf(" %d %d", read->coef, read->expon);
  154. }
  155. read = read->next;
  156. }
  157. if (!flag) //如果是零多项式直接输出"0 0"
  158. printf("0 0");
  159. }

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

闽ICP备14008679号