Program running results:
Realization principle
Polynomial addition:
(1) If the exponent of cpa is less than the exponent of cpb, insert the pointer pointed to by cpa into the polynomial chain and move the pointer backward;
(2) if the exponent of cpa> the exponent of cpb, then move the pointer pointed to by cpb Insert and polynomial chain, move the pointer backward;
(3) If the exponent of cpa = the exponent of cpb, if the exponent of cpa + the exponent of cpb = 0, then delete the corresponding node, move the pointer backward, and release the memory space ;
! (4) + cpb index or index of cpa = 0, then modify the exponential term with the coefficient value, rearward movement of the pointer;
(5) if pa list is not empty, the node remaining links pa;
(6) if If the pb linked list is not empty, link the remaining nodes of pb.
Polynomial multiplication:
(1) Polynomial multiplication can be regarded as the multiplication of monomials and polynomials, and then addition;
(2) The loop can be used to multiply each term of the multiplied polynomial and the multiplier polynomial in turn;
(3) Each time in the loop Accumulate partial products;
(4) The polynomial multiplication is completed at the end of the loop.
Program realization:
- Create a polynomial, m is the number of terms of the polynomial, when m is 0, the function only initializes the head node
void CreatePolyn(polynomial **p, int m){
int i, data;
int flag;
polynomial *cp, *temp;
(*p) = (polynomial *)malloc(sizeof(polynomial));
(*p)->coef = 0.0;
(*p)->expn = -1;
(*p)->next = NULL;
for(i = 1; i <= m; ++i){
cp = *p;
flag = 0; // 标志多项式中是否已经存在相同指数的多项式
temp = (polynomial *)malloc(sizeof(polynomial));
// 接收所输入的多项式的各项,默认各项指数互异
scanf("%f %d", &(temp->coef), &(temp->expn));
while(cp->next && temp->expn > cp->next->expn){
cp = cp->next;
}
if(cp->next && temp->expn == cp->next->expn){
continue;// 如果已经存在相同指数的多项式,忽略该项
}
temp->next = cp->next;
cp->next = temp;
}
}
- Print polynomials, consider the format of a variety of special items, and print them in the simplest form
void PrintPolyn(polynomial *p){
polynomial *temp = p->next;
while(temp){
if(temp->coef == 1 && temp->expn == 0) // 1x^0 = 1
printf("1");
else if(temp->coef == 1 && temp->expn == 1) // 1x^1 = x
printf("x", temp->coef);
else{
if(temp->expn == 0) // nx^0 = n
printf("%.0f", temp->coef);
else if(temp->expn == 1) // nx^1 = nx
printf("%.0fx", temp->coef);
else{
if(temp->coef == 1){
if(temp->expn < 0) // 1x^(-m) = x^(-m)
printf("x^(%d)", temp->expn);
else // 1x^m = x^m
printf("x^%d", temp->expn);
}
else{
// nx^(-m) = nx^(-m)
if(temp->expn < 0)
printf("%.0fx^(%d)", temp->coef, temp->expn);
else // nx^m = nx^m
printf("%.0fx^%d", temp->coef, temp->expn);
}
}
}
temp = temp->next;
if(temp)
printf(" + ");
}
}
- Copy the polynomial in pb to pa
void CopyPolyn(polynomial **pa, polynomial *pb){
CreatePolyn(pa, 0);
polynomial *temp, *cpa;
cpa = *pa;
pb = pb->next; // 移动指针指向第一个节点
while(pb){
temp = (polynomial *)malloc(sizeof(polynomial));
temp->coef = pb->coef;
temp->expn = pb->expn;
temp->next = NULL;
cpa->next = temp;
cpa = temp;
pb = pb->next;
}
}
- Polynomial addition, the result is saved in pa
void AddPolyn(polynomial **pa, polynomial **pb){
polynomial *cpa, *cpb, *temp, *ccpa, *p = (*pa);
cpa = (*pa)->next;
cpb = (*pb)->next;
while(cpa && cpb){
if(cpa->expn < cpb->expn){
// cpa的指数<cpb的指数,则将cpa所指向的插入和多项式链中,向后移动指针
p->next = cpa;
p = cpa;
cpa = cpa->next;
} else if(cpa->expn > cpb->expn) {
// cpa的指数>cpb的指数,则将cpb所指向的插入和多项式链中,向后移动指针
p->next = cpb;
p = cpb;
cpb = cpb->next;
} else {
// cpa的指数=cpb的指数
if(cpa->coef + cpb->coef == 0){
// cpa的指数+cpb的指数=0,则删除相应结点,向后移动指针,并释放内存空间
temp = *pa;
while(temp->next != cpa)
temp = temp->next;
temp->next = cpa->next;
ccpa = cpa;
cpa = cpa->next;
cpb = cpb->next;
free(ccpa);
} else {
// cpa的指数+cpb的指数!=0,则修改同指数项的系数值,向后移动指针
cpa->coef += cpb->coef;
p->next = cpa;
p = cpa;
cpa = cpa->next;
cpb = cpb->next;
}
}
}
if(cpa)
p->next = cpa; // pa链表非空,链接pa剩余结点
else
p->next = cpb; // pb链表非空,链接pb剩余结点
free(*pb);
}
- pa is a polynomial, pb is a monomial, multiply each item in pa in turn, and save the result in pa
void MultiplyOperate(polynomial *pa, polynomial *pb){
pa = pa->next;
while(pa){
pa->coef *= pb->coef;
pa->expn += pb->expn;
pa = pa->next;
}
}
- Polynomial multiplication, the result is stored in pa
void MultiplyPolyn(polynomial **pa, polynomial **pb){
polynomial *cpa, *ccpa, *res;
cpa = *pa; // 保存着原pa的内容
CreatePolyn(pa, 0); // 从新初始化pa为头结点
(*pb) = (*pb)->next; // 依次从pb中提取单项式
while(*pb){
CopyPolyn(&ccpa, cpa);
MultiplyOperate(ccpa, *pb); // 将所提取的单项式,依次与pa中的每一项相乘
AddPolyn(pa, &ccpa); // 将部分积累加
(*pb) = (*pb)->next; // 提取下一个单项式
}
}
- Main function
#include <stdio.h>
#include <stdlib.h>
/* 单链表实现多项式相加和相乘 */
typedef struct polynomial{
float coef; // 项的系数
int expn; // 项的指数
struct polynomial *next;
}polynomial;
int main(){
polynomial *pa, *pb, *cpa, *cpb;
int ma, mb;
printf("请输入多项式A的项数:");
scanf("%d", &ma);
printf("请输入多项式A的各项系数和指数:");
CreatePolyn(&pa, ma);
CopyPolyn(&cpa, pa);
printf("A = ");
PrintPolyn(pa);
printf("\n请输入多项式B的项数:");
scanf("%d", &mb);
printf("请输入多项式B的各项系数和指数:");
CreatePolyn(&pb, mb);
CopyPolyn(&cpb, pb);
printf("B = ");
PrintPolyn(pb);
AddPolyn(&cpa, &cpb);
MultiplyPolyn(&pa, &pb);
printf("\n\nA + B = ");
PrintPolyn(cpa);
printf("\nA * B = ");
PrintPolyn(pa);
return 0;
}