一元稀疏多项式加减运算算法
//一元稀疏多项式加减运算算法 //一元稀疏多项式加减运算算法
#include
#include
#include
#include
typedef struct node
{
int coef; //定义系数
int exp; //定义指数
struct node *next;
}node;
void interphase(void);
node *creat(void);
void display(node *);
node *add(void);
node *subtration(void);
void main()
{
int choice=0;
node *poly_a,*poly_c;
while (1)
{
system("cls");
interphase();
printf("输入你的选择:");
scanf("%d",&choice);
switch(choice)
{
case 1:
poly_a=creat();
printf("输入");
display(poly_a);
break;
case 2: //加法运算
poly_c=add();
printf("\n");
printf("结果是");
display(poly_c);
break;
case 3: //减法运算
poly_c=subtration();
printf("\n");
printf("结果是");
display(poly_c);
break;
case 0:
exit(1);
break;
default:
printf("输入错误!请重新选择");
getch();
}
}
}
void interphase(void) //显示目录
{
printf("\t\t\t\t欢迎使用本程序\n\n\n\n");
printf("\t\t\t1.输入一个一元稀疏多项式。\n\n");
printf("\t\t\t2.进行两个多项式加法运算\n\n");
printf("\t\t\t3.进行两个多项式减法运算\n\n");
printf("\t\t\t0.退出程序\n\n");
}
node *creat(void) //建立一个多项式
{
int m,n,i=0;
node *head,*r,*s;
head=(node *)malloc(sizeof(node));
r=head;
printf("\n输入系数(输入0退出):");
scanf("%d",&n);
printf("输入指数(按降序排列):");
scanf("%d",&m);
do
{
s=(node *)malloc(sizeof(node));
s->exp=m;
s->coef=n;
r->next=s;
s->next=NULL;
r=s;
printf("\n输入系数(输入0退出):");
scanf("%d",&n);
printf("输入指数(按降序排列):");
scanf("%d",&m);
i++;
}while(n);
head->coef=i;
return head;
}
void display(node *head) //显示多项式
{
node *r;
r=head->next;
printf("一元稀疏方程式为:%-5d",head->coef);
if (r != NULL)
{
printf("%dX^%d",r->coef,r->exp);
r=r->next;
}
while (r != NULL)
{
printf("%+dX^%d",r->coef,r->exp);
r=r->next;
}
getch();
}
node *add(void) //加法函数
{
int sum,i=0;
node *a,*b,*c,*s,*poly_a,*poly_b,*poly_c;
printf("\n输入第一个多项式");
poly_a=creat();
display(poly_a);
printf("\n\n输入第二个多项式");
poly_b=creat();
display(poly_b);
printf("\n");
a=poly_a->next;
b=poly_b->next;
poly_c=(node *)malloc(sizeof(node));
c=poly_c;
while (a != NULL && b != NULL)
{
if (a->exp == b->exp)
{
sum=a->coef+b->coef;
if (sum)
{
s=(node *)malloc(sizeof(node));
s->coef=sum;
s->exp=a->exp;
c->next=s;
s->next=NULL;
c=s;
a=a->next;
b=b->next;
poly_a->coef--;
}
else
{
a=a->next;
b=b->next;
}
}
else if(a->exp > b->exp)
{
s=(node *)malloc(sizeof(node));
s->coef=a->coef;
s->exp=a->exp;
c->next=s;
s->next=NULL;
c=s;
a=a->next;
}
else
{
s=(node *)malloc(sizeof(node));
s->coef=b->coef;
s->exp=b->exp;
c->next=s;
s->next=NULL;
c=s;
b=b->next;
}
}
if (b)
{
while (b != NULL)
{
s=(node *)malloc(sizeof(node));
s->coef=b->coef;
s->exp=b->exp;
c->next=s;
s->next=NULL;
c=s;
b=b->next;
}
}
else if (a)
{
while (a != NULL)
{
s=(node *)malloc(sizeof(node));
s->coef=a->coef;
s->exp=a->exp;
c->next=s;
s->next=NULL;
c=s;
a=a->next;
}
}
poly_c->coef=poly_a->coef+poly_b->coef;
return poly_c;
}
node *subtration(void) //减法函数
{
int sum,i=0;
node *a,*b,*c,*s,*poly_a,*poly_b,*poly_c;
printf("\n输入第一个多项式");
poly_a=creat();
display(poly_a);
printf("\n输入第二个多项式");
poly_b=creat();
display(poly_b);
printf("\n");
a=poly_a->next;
b=poly_b->next;
poly_c=(node *)malloc(sizeof(node));
c=poly_c;
while (a != NULL && b != NULL)
{
if (a->exp == b->exp)
{
sum=a->coef-b->coef;
if (sum)
{
s=(node *)malloc(sizeof(node));
s->coef=sum;
s->exp=a->exp;
c->next=s;
s->next=NULL;
c=s;
a=a->next;
b=b->next;
poly_a->coef--;
}
else
{
a=a->next;
b=b->next;
poly_a->coef -= 2;
}
}
else if(a->exp > b->exp)
{
s=(node *)malloc(sizeof(node));
s->coef=a->coef;
s->exp=a->exp;
c->next=s;
s->next=NULL;
c=s;
a=a->next;
}
else
{
s=(node *)malloc(sizeof(node));
s->coef=b->coef*(-1);
s->exp=b->exp;
c->next=s;
s->next=NULL;
c=s;
b=b->next;
}
}
if (b)
{
while (b != NULL)
{
s=(node *)malloc(sizeof(node));
s->coef=b->coef *(-1);
s->exp=b->exp;
c->next=s;
s->next=NULL;
c=s;
b=b->next;
}
}
else if (a)
{
while (a != NULL)
{
s=(node *)malloc(sizeof(node));
s->coef=a->coef;
s->exp=a->exp;
c->next=s;
s->next=NULL;
c=s;
a=a->next;
}
}
poly_c->coef=poly_a->coef+poly_b->coef;
return poly_c;
}