#include <stdio.h>
#include<malloc.h>
#define NULL 0
typedef struct PolynNode { /*某一项*/
float coef; /*系数*/
int expn; /*指数*/
} ElemType;
typedef struct LNode {
ElemType data;
struct LNode *next;
}LNode,*LinkList;
typedef LinkList Polyn;
Polyn init_polyn(Polyn head,int n)
{ /*创建有n项的多项式,n应大于0,但不作检查*/
/*指数e1<e2<e3...,但不作检查,间隔地输入系数与指数,即c1 e1 ...*/
Polyn p,q = head;
float coef;
int expn,count;
p = (Polyn)malloc(sizeof(LNode));
printf("Please input n pairs of coef and expn.\n");
for(count = 0;count < n;count++)
{q->next = p;
q = p;
scanf("%f%d",&coef,&expn);
p->data.coef = coef;
p->data.expn = expn;
p = (Polyn)malloc(sizeof(LNode));
}
q->next = NULL;/*尾*/
return head;
}
Polyn print_polyn(Polyn head)
{/*输出*/
Polyn p=head->next;
while(p != NULL)
{
printf("%f",p->data.coef);
printf("X^%d",p->data.expn);
p=p->next;
if(p==NULL) break;
if(p->data.coef>0) printf("+");
}
printf("\n");
return head;
}
Polyn add_polyn(Polyn ha,Polyn hb)
{/*多项式相加*/
Polyn p1 = ha,p2 = ha,q1 = hb,q2 = hb;
p1 = p1->next;
q1 = q1->next;
while( (p1 != NULL) && (q1 != NULL)) {
if(p1->data.expn < q1->data.expn) { /*p1指数<q1指数*/
p2 = p1;
p1 = p1->next;
}
else {
if(p1->data.expn == q1->data.expn) { /*q1指数==p1指数*/
if(!(p1->data.coef + q1->data.coef))/*系数之和为0*/
{
p1 = p1->next;
free(p2->next);
p2->next = p1;
q1 = q1->next;
free(q2->next);
q2->next =q1;
}
else { /*系数之和不为0*/
p1->data.coef += q1->data.coef;
p2 = p1;
p1 = p1->next;
q1 = q1->next;
free(q2->next);
q2->next =q1;
}
}
else { /*q1指数<p1指数*/
p2->next = q1;
q1 = q1->next;
q2->next->next=p1;
q2->next = q1;
p2=p2->next;
}
}
}
if(p1 == NULL) { /*p1结束了,q1未结束*/
p2->next = q1;
}
printf("\n");
return ha;
}
Polyn minus_polyn(Polyn ha,Polyn hb)
{ Polyn p1 = ha,p2 = ha,q1 = hb,q2 = hb;
p1 = p1->next;
q1 = q1->next;
while(q1!=NULL)
{q1->data.coef=0-q1->data.coef;
q1=q1->next;}
q1 = hb;
q1 = q1->next;
while( (p1 != NULL) && (q1 != NULL)) {
if(p1->data.expn < q1->data.expn) { /*p1指数<q1指数*/
p2 = p1;
p1 = p1->next;
}
else {
if(p1->data.expn == q1->data.expn) { /*q1指数==p1指数*/
if(!(p1->data.coef + q1->data.coef)) {/*系数之和为0*/
p1 = p1->next;
free(p2->next);
p2->next = p1;
q1 = q1->next;
free(q2->next);
q2->next =q1;
}
else { /*系数之和不为0*/
p1->data.coef += q1->data.coef;
p2 = p1;
p1 = p1->next;
q1 = q1->next;
free(q2->next);
q2->next =q1;
}
}
else { /*q1指数<p1指数*/
p2->next = q1;
q1 = q1->next;
q2->next->next=p1;
q2->next = q1;
p2=p2->next;
}
}
}
if(p1 == NULL) { /*p1结束了,q1未结束*/ p2->next = q1;
}
printf("\n");
return ha;
}
void main()
{
Polyn init_polyn(Polyn head,int n);
Polyn print_polyn(Polyn head);
Polyn add_polyn(Polyn ha,Polyn hb);
Polyn minus_polyn(Polyn ha,Polyn hb);
LNode polyn1,polyn2;
Polyn ha = &polyn1,hb = &polyn2;
int n,a;
printf("输1为加法,输2为减法,0为退出");
while(a){
scanf("%d",&a);
switch(a)//选择操作加、减
{case 1:
printf("输入要第一项的项数,按升幂输入多项式的系数和指数\n");
scanf("%d",&n);
init_polyn(ha,n);
print_polyn(ha);
printf("输入要相加的项数.\n");
scanf("%d",&n);
init_polyn(hb,n);
print_polyn(hb);
add_polyn(ha,hb);
print_polyn(ha);
printf("\n");break;
case 2:
printf("输入要第一项的项数,按升幂输入多项式的系数和指数\n");
scanf("%d",&n);
init_polyn(ha,n);
print_polyn(ha);
printf("输入要相减的项数\n");
scanf("%d",&n);
init_polyn(hb,n);
print_polyn(hb);
minus_polyn(ha,hb);
print_polyn(ha);
printf("\n");break;
}
}//while