• (C语言)一元多项式的乘法与加法运算


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

    输入格式:

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

    输出格式:

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

    输入样例:

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

    输出样例:

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

    代码: 

    1. # include
    2. # include
    3. struct Node {
    4. int coef;
    5. int expn;
    6. struct Node * next;
    7. };
    8. struct Node * createList(int n)
    9. {
    10. struct Node * L = (struct Node *)malloc(sizeof(struct Node));
    11. struct Node * p = L;
    12. L->next = NULL;
    13. while (n--)
    14. {
    15. int coef, expn;
    16. scanf("%d%d", &coef, &expn);
    17. p->next = (struct Node *)malloc(sizeof(struct Node));
    18. p = p->next;
    19. p->coef = coef;
    20. p->expn = expn;
    21. p->next = NULL;
    22. }
    23. return L;
    24. }
    25. void print(struct Node * L)
    26. {
    27. for (struct Node * p = L; p->next != NULL; p = p->next)
    28. printf("%d %d%s", p->next->coef, p->next->expn, p->next->next == NULL ? "\n" : " ");
    29. return;
    30. }
    31. // 把A加到B中
    32. void addAtoB(struct Node * A, struct Node * B)
    33. {
    34. struct Node *preb = B;
    35. while (A->next != NULL && preb->next != NULL)
    36. {
    37. if (A->next->expn > preb->next->expn)
    38. {
    39. // preb后面插入A->next节点
    40. struct Node * tmp = A->next;
    41. A->next = tmp->next;
    42. tmp->next = preb->next;
    43. preb->next = tmp;
    44. }
    45. else if (A->next->expn < preb->next->expn)
    46. {
    47. preb = preb->next;
    48. }
    49. else
    50. {
    51. if (A->next->coef + preb->next->coef == 0)
    52. {
    53. struct Node * tmp = A->next;
    54. A->next = tmp->next;
    55. free(tmp);
    56. tmp = preb->next;
    57. preb->next = tmp->next;
    58. free(tmp);
    59. }
    60. else
    61. {
    62. preb->next->coef += A->next->coef;
    63. struct Node * tmp = A->next;
    64. A->next = tmp->next;
    65. free(tmp);
    66. }
    67. }
    68. }
    69. if (A->next != NULL)
    70. {
    71. preb->next = A->next;
    72. A->next = NULL;
    73. }
    74. if (B->next == NULL)
    75. {
    76. B->next = (struct Node *)malloc(sizeof(struct Node));
    77. B->next->next = NULL;
    78. B->next->coef = B->next->expn = 0;
    79. }
    80. return;
    81. }
    82. struct Node * multi(struct Node * A, struct Node * B)
    83. {
    84. struct Node * L = (struct Node *)malloc(sizeof(struct Node));
    85. L->next = NULL;
    86. for (struct Node * pa = A->next; pa != NULL; pa = pa->next)
    87. {
    88. struct Node * prel = L;
    89. for (struct Node * pb = B->next; pb != NULL; pb = pb->next)
    90. {
    91. int newCoef = pa->coef * pb->coef;
    92. int newExpn = pa->expn + pb->expn;
    93. if (newCoef == 0) continue;
    94. struct Node * prel = L;
    95. if (prel->next == NULL)
    96. {
    97. int b = 1;
    98. }
    99. for (;prel->next != NULL && prel->next->expn > newExpn ; prel = prel->next)
    100. {
    101. int a = 1;
    102. }
    103. if (prel->next == NULL || prel->next->expn < newExpn)
    104. {
    105. struct Node * tmp = (struct Node *)malloc(sizeof(struct Node));
    106. tmp->coef = newCoef;
    107. tmp->expn = newExpn;
    108. tmp->next = prel->next;
    109. prel->next = tmp;
    110. }
    111. else
    112. {
    113. prel->next->coef += newCoef;
    114. if (prel->next->coef == 0)
    115. {
    116. struct Node * tmp = prel->next;
    117. prel->next = tmp->next;
    118. free(tmp);
    119. }
    120. }
    121. }
    122. }
    123. if (L->next == NULL)
    124. {
    125. L->next = (struct Node *)malloc(sizeof(struct Node));
    126. L->next->next = NULL;
    127. L->next->coef = L->next->expn = 0;
    128. }
    129. return L;
    130. }
    131. int main(void)
    132. {
    133. int la, lb;
    134. scanf("%d", &la);
    135. struct Node * A = createList(la);
    136. scanf("%d", &lb);
    137. struct Node * B = createList(lb);
    138. // 乘法
    139. struct Node * D = multi(A, B);
    140. print(D);
    141. // 加法
    142. addAtoB(A, B);
    143. print(B);
    144. return 0;
    145. }

  • 相关阅读:
    PCB(一):altium designer 环境安装配置
    《bug记录》在利用TCP协议创建【服务器-客户端交互程序】中出现的一些问题
    在keil MDK中定义非初始化(noini)变量
    [flume]组成|source|channel|事务|拦截器|选择器|处理器|监控器|数据丢失问题|如何保证数据的完整性
    九号公司半年报背后的估值分歧
    1.Linux端SVN的搭建及多项目权限配置
    Android 10+ wifi使用相关权限问题
    TCP和UDP的最完整的区别
    Day02-网页布局实战
    第七章第二节:B树和B+树
  • 原文地址:https://blog.csdn.net/weixin_45339670/article/details/134450344