-->
当前位置:首页 > 题库 > 正文内容

函数题:实现一元多项式的乘法运算(张瑞霞-数据结构与算法-清华大学出版社-课后习题2-9-1)

Luz4年前 (2021-10-11)题库1212
实现一元多项式的乘法运算,要求用户可以任意输入无序数据。

### 函数接口定义:
c++
在这里描述函数接口。其中pa和pb是两个多项式
void multiply_poly_expression(Pnode pa, Pnode pb);



### 裁判测试程序样例:
c++
#include <stdio.h>
#include <stdlib.h>

typedef struct tagnode
{
float coef;
int exp;
struct tagnode *next;
} * Pnode;

int list_len(Pnode temp); //求链表长度;
void sort_list(Pnode temp); //进行多项式排序;
Pnode initialize_head_node(void); //构造一个单链表头结点;
void create_poly_expression(Pnode temp); //构造一个多项式;
void multiply_poly_expression(Pnode pa, Pnode pb); //实现两个多项式乘法;
void print_poly_expression(Pnode temp); //打印多项式的结果;
void copy_poly_expression(Pnode pa, Pnode pb); //复制多项式以便进行其它操作;
void destroy_linkedlist(Pnode temp); //销毁动态分配的内存;

int main(void)
{
Pnode mylist1 = initialize_head_node();
Pnode mylist2 = initialize_head_node();
Pnode mylist3 = initialize_head_node();
Pnode mylist4 = initialize_head_node();
Pnode mylist5 = initialize_head_node();

create_poly_expression(mylist1); //构造一元多项式;
sort_list(mylist1); //排序;
create_poly_expression(mylist2); //构造一元多项式;
sort_list(mylist2); //排序;
copy_poly_expression(mylist1, mylist3); //复制一元多项式;
copy_poly_expression(mylist2, mylist4); //复制一元多项式;
copy_poly_expression(mylist1, mylist5); //复制一元多项式;
multiply_poly_expression(mylist3, mylist4); //实现乘法运算;
print_poly_expression(mylist3); //打印乘法结果;
destroy_linkedlist(mylist3);

return 0;
}

int list_len(Pnode temp)
{
int i = 0;
Pnode pos = temp->next;

while (pos != NULL)
{
++i;
pos = pos->next;
}
return i;
}

void sort_list(Pnode temp) //对多项式进行递增指数排序;
{
Pnode p, q;
int i, j, t;
int len = list_len(temp);

for (i = 0, p = temp->next; i < len - 1; i++, p = p->next)
{
for (j = i + 1, q = p->next; j < len; j++, q = q->next)
{
if (q->exp < p->exp)
{
t = p->exp;
p->exp = q->exp;
q->exp = t;
t = p->coef;
p->coef = q->coef;
q->coef = t;
}
}
}
return;
}

Pnode initialize_head_node(void)
{
Pnode new_node = (Pnode)malloc(sizeof(struct tagnode));
if (NULL == new_node)
{
exit(EXIT_FAILURE);
}
new_node->next = NULL;
return new_node;
}

void create_poly_expression(Pnode temp)
{
int my_exp;
float my_coef;
Pnode tail = temp;


while (scanf("%f,%d", &my_coef, &my_exp) && (my_coef != 0.0f || my_exp != 0)) //严格依照题目要求输入,系数和指数之间加个逗号;
{
Pnode new_node = (Pnode)malloc(sizeof(struct tagnode));
if (NULL == new_node)
{
exit(EXIT_FAILURE);
}
new_node->exp = my_exp;
new_node->coef = my_coef;
new_node->next = NULL;
tail->next = new_node;
tail = new_node;
}
return;
}

void multiply_poly_expression(Pnode pa, Pnode pb)
{
/* 请在这里填写答案 */
}

void print_poly_expression(Pnode temp)
{
Pnode pos = temp->next;

while (pos != NULL)
{
printf("%g,%d ", pos->coef, pos->exp); //依次打印系数和指数;
pos = pos->next;
}
putchar('\n');
return;
}

void copy_poly_expression(Pnode pa, Pnode pb) //进行多项式的复制;
{
Pnode temp = pa->next;
Pnode new_node;
Pnode tail = pb;

while (temp != NULL)
{
new_node = (Pnode)malloc(sizeof(struct tagnode));
if (NULL == new_node)
{
exit(EXIT_FAILURE);
}
new_node->exp = temp->exp;
new_node->coef = temp->coef;
new_node->next = NULL;
tail->next = new_node;
tail = new_node;
temp = temp->next;
}
return;
}

void destroy_linkedlist(Pnode temp)
{
Pnode p, q;
p = temp;

while (p != NULL) //从头结点开始销毁;
{
q = p->next;
free(p);
p = q;
}
return;
}



### 输入样例:

在这里给出一组输入。例如:

in
2,1,1,6,8,15,5,0,0,0
3,6,4,8,-2,1,0,0


### 输出样例:

在这里给出相应的输出。例如:

out
5,0,-4,1,3,6,4,8,8,15







答案:若无答案欢迎评论

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。