【问题描述】
设计一个一元稀疏多项式简单计算器。
【基本要求】
一元稀疏多项式简单计算器的基本功能是:
(1)输入并建立多项式;
(2)输出多项式,输出形式为整数序列:n,c1,e1,c2,e2,…,cn,en,其中n是多项式的项数,ci和ei分别是第i项的系数和指数,序列按指数降序排列;
(3)实现多项式相加:C=A+B;
(4)实现多项式相减:C=A-B。
ps.用带表头结点的单链表存储多项式,多项式的项数存放在头结点。
//异地相加减
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct PolyNode{
float coef;
int expn;
struct PolyNode *next;
}PolyNode;
typedef PolyNode* PolyLinkList;
void CreatPolyn(PolyLinkList *L, int n);
void DestroyPloyn(PolyLinkList *L);
void AddPolyn(PolyLinkList La, PolyLinkList Lb, PolyLinkList *Lc);
void SubtractPolyn(PolyLinkList La, PolyLinkList Lb, PolyLinkList *Lc);
void PrintPolyn(PolyLinkList L);
//1.创建一元多项式,n为项数
void CreatPolyn(PolyLinkList *L, int n){
PolyNode *newNode, *r;
float x;
int y,i;
*L = (PolyNode *)malloc(sizeof(PolyNode));
if(!(*L)){
printf("分配内存失败!\n");
exit(OVERFLOW);
}
(*L)->next = NULL; //初始化,建立一个头结点
r = *L; //尾指针,始终指向当前链表的尾结点
printf("请升幂输入各项:");
for(i = 0; i < n; i++){
scanf("%f%d", &x, &y);
newNode = (PolyNode *)malloc(sizeof(PolyNode));
if(!newNode) exit(OVERFLOW);
newNode->coef = x;
newNode->expn = y;
r->next = newNode;
r = newNode;
}
r->next = NULL;
}
//2.销毁多项式
void DestroyPloyn(PolyLinkList *L){
PolyNode *pre = *L, *p = pre->next;
while(p!=NULL){
free(pre);
pre = p;
p = pre->next;
}
free(pre);
*L = NULL;
}
//3.多项式相加,即Lc = La + Lb
void AddPolyn(PolyLinkList La, PolyLinkList Lb, PolyLinkList *Lc){
PolyNode *pa, *pb, *pc, *s;
pc = *Lc = (PolyNode *)malloc(sizeof(PolyNode));
pc->next = NULL;
pa = La->next;
pb = Lb->next;
while(pa!=NULL&&pb!=NULL){
if(pa->expn < pb->expn){
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = pa->coef;
s->expn = pa->expn;
s->next = NULL;
pc->next = s;
pc = s;
pa = pa->next;
}
else if(pa->expn > pb->expn){
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = pb->coef;
s->expn = pb->expn;
s->next = NULL;
pc->next = s;
pc = s;
pb = pb->next;
}
else{
float x = pa->coef + pb->coef;
if(abs(x)<=1.0e-6){
pa = pa->next;
pb = pb->next;
}
else{
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = x;
s->expn = pb->expn;
s->next = NULL;
pc->next = s;
pc = s;
pa = pa->next;
pb = pb->next;
}
}
}
while(pa!=NULL){ //pb==NULL时 将a的剩余部分连到c后面
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = pa->coef;
s->expn = pa->expn;
s->next = NULL;
pc->next = s;
pc = s;
pa = pa->next;
}
while(pb!=NULL){
s = (PolyNode *)malloc(sizeof(PolyNode));
s->coef = pb->coef;
s->expn = pb->expn;
s->next = NULL;
pc->next = s;
pc = s;
pb = pb->next;
}
}
//4.多项式相减,即Lc = La - Lb 利用Lb取反实现
void SubtractPolyn(PolyLinkList La, PolyLinkList Lb, PolyLinkList *Lc){
PolyNode *p = Lb->next;
while(p!=NULL){
p->coef = -p->coef;//Lb系数逐项取反
p = p->next;
}
AddPolyn(La, Lb, Lc);
}
//5.输出多项式
void PrintPolyn(PolyLinkList L){
if(L->next==NULL){
printf("0");
return;
}
PolyNode *p = L->next;
int i = 0;
while(p!=NULL){
if(i==0){ //第一项单独处理
if(p->expn==0) //指数等于0,系数原样输出
printf("%.1f", p->coef);
else if(p->expn==1){ //指数等于1
if(p->coef==1) //系数为1
printf("x");
else if(p->coef==-1) //系数为-1
printf("-x");
else
printf("%.1fx", p->coef);
}
else{ //其他指数
if(p->coef==1) //系数为1
printf("x^%d", p->expn);
else if(p->coef==-1) //系数为-1
printf("-x^%d", p->expn);
else
printf("%.1fx^%d", p->coef, p->expn);
}
}
else{ //其余项
if(p->expn==1){ //指数等于1
if(p->coef==1) //系数为1
printf(" + x");
else if(p->coef==-1) //系数为-1
printf(" - x");
else
if(p->coef>0) printf(" + %.1fx", p->coef);
else printf(" - %.1fx", -p->coef);
}
else{ //其他指数
if(p->coef==1) //系数为1
printf(" + x^%d", p->expn);
else if(p->coef==-1) //系数为-1
printf(" - x^%d", p->expn);
else
if(p->coef>0) printf(" + %.1fx^%d", p->coef, p->expn);
else printf(" - %.1fx^%d", -p->coef, p->expn);
}
}
p = p->next;
i++;
}
printf("\n\n");
}
int main(){
PolyLinkList La, Lb, Lc;
int n, m;
printf("----------一元多项式的计算-----------\n\n");
printf("多项式若为ax+bx^2,请输入a 1 b 2(系数和幂次都要输)\n");
printf("请输入第一个多项式的项数:");
scanf("%d", &n);
CreatPolyn(&La, n);
printf("第一个多项式为:");
PrintPolyn(La);
printf("请输入第二个多项式的项数:");
scanf("%d", &m);
CreatPolyn(&Lb, m);
printf("第二个多项式为:");
PrintPolyn(Lb);
AddPolyn(La, Lb, &Lc);
printf("两者相加:");
PrintPolyn(Lc);
SubtractPolyn(La, Lb, &Lc);
printf("两者相减:");
PrintPolyn(Lc);
return 0;
}