输入两个一元多项式(指数从高到低),自定义项数,系数和指数,用单链表方法顺序输出求乘积后的多项式的各项系数和指数。
稍等,帮你写一个
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int coef;
int exp;
struct node* next;
} Node;
Node* create_node(int coef, int exp)
{
Node* new_node = (Node*)malloc(sizeof(Node));
new_node->coef = coef;
new_node->exp = exp;
new_node->next = NULL;
return new_node;
}
Node* create_poly(int n)
{
Node* head = NULL;
Node* tail = NULL;
int coef, exp;
for (int i = 0; i < n; i++)
{
printf("请输入第%d项的系数和指数:", i + 1);
scanf("%d%d", &coef, &exp);
if (coef != 0)
{
Node* new_node = create_node(coef, exp);
if (head == NULL)
{
head = new_node;
tail = new_node;
}
else
{
tail->next = new_node;
tail = new_node;
}
}
}
return head;
}
void print_poly(Node* head)
{
Node* p = head;
if (p == NULL)
{
printf("0\n");
}
else
{
printf("%dx^%d", p->coef, p->exp);
p = p->next;
while (p != NULL)
{
printf(" + %dx^%d", p->coef, p->exp);
p = p->next;
}
printf("\n");
}
}
Node* poly_mult(Node* poly1, Node* poly2)
{
Node* result = NULL;
Node* p1 = poly1;
while (p1 != NULL)
{
Node* p2 = poly2;
while (p2 != NULL)
{
int coef = p1->coef * p2->coef;
int exp = p1->exp + p2->exp;
Node* q = result;
while (q != NULL && q->exp > exp)
{
q = q->next;
}
if (q == NULL)
{
Node* new_node = create_node(coef, exp);
new_node->next = result;
result = new_node;
}
else if (q->exp == exp)
{
q->coef += coef;
}
else
{
Node* new_node = create_node(coef, exp);
new_node->next = q->next;
q->next = new_node;
}
p2 = p2->next;
}
p1 = p1->next;
}
return result;
}
int main()
{
int n1, n2;
printf("请输入第一个多项式的项数:");
scanf("%d", &n1);
Node* poly1 = create_poly(n1);
printf("第一个多项式为:");
print_poly(poly1);
printf("请输入第二个多项式的项数:");
scanf("%d", &n2);
Node* poly2 = create_poly(n2);
printf("第二个多项式为:");
print_poly(poly2);
Node* result = poly_mult(poly1, poly2);
printf("乘积为:");
print_poly(result);
return 0;
}
可直接运行
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct Node;
typedef struct Node *PNode;
struct Node
{
int zhishu;
int xishu;
PNode next;
};
typedef struct Node *LinkList;
LinkList createNullList_link(void)//创建空链表
{
LinkList llist = (LinkList)malloc(sizeof(struct Node));
if(llist != NULL) llist -> next = NULL;
else
{
printf("out of space!\n");
}
return llist;
}
PNode locate_link(LinkList llist,int zhishu)//求某元素的存储位置
{
PNode p;
if(llist == NULL) return NULL;
p = llist->next;
while(p != NULL && p->zhishu!=zhishu)p=p->next;
return p;
}
int insertPost_link(LinkList llist,PNode p,int zhishu,int xishu)//在p节点后添加新节点
{
PNode q = (PNode)malloc(sizeof(struct Node));
if(q==NULL) {
printf("out of space.");
return 0;
}
else
{
q->zhishu=zhishu;
q->xishu=xishu;
q->next=p->next;
p->next=q;
return 1;
}
}
int main()
{
LinkList a1 = createNullList_link();
insertPost_link(a1,a1,1,4);
PNode p1 = a1->next;
insertPost_link(a1, p1, 0, 5);
LinkList a2 = createNullList_link();
insertPost_link(a2, a2, 1, 6);
PNode p2 = a2->next;
insertPost_link(a2, p2, 0, 3);
//以上设置好了两个一元多项式
PNode p11 = locate_link(a1,1);
PNode p21 = locate_link(a2, 1);
PNode p10 = locate_link(a1, 0);
PNode p20 = locate_link(a2, 0);
int xishu1 = p11->xishu + p21->xishu;
int xishu0 = p10->xishu+p20->xishu;
printf("the sum is %d x + %d",xishu1,xishu0);
return 0;
}