C一元多项式加减乘法

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#include
#define NULL 0

typedef struct node1{ //定义结构体类型
float coef; //系数
int index; //指数
struct node1 *next; //结构体指针
}node1;
void windows()//用户选择界面
{
printf("************************************\n");
printf(" 两个一元多项式的相加/相减,相乘:\n");
printf("************************************\n");
printf("请选择操作:\n");
printf("0.退出\n");
printf("1.两个一元多项式相加\n");
printf("2.两个一元多项式相减\n");
printf("3.两个一元多项式相乘\n");
printf("(选择后请先输入第一个多项式而后输入第二个多项式)\n");
}
node1 *creat1()
{
int i,m;
node1 *head,*p,*q;
head=(struct node1 *)malloc(sizeof(struct node1));
head->next=NULL;
q=head;
printf("请输入多项式的项数\n");
scanf("%d",&m);
for(i=1;i<=m;i++)
{
p=(struct node1 *)malloc(sizeof (struct node1));
printf("请输入第%d项的系数和指数【(条件必须是指数有小到大)例如系数,指数】\n",i);
scanf("%f,%d",&p->coef,&p->index);
p->next=NULL;
q->next=p;
q=p;
}
return(head);
}
void output1(node1 *head)
{
node1 *p;
p=head->next;
printf("多项式为\n");
for(;p!=NULL;p=p->next)
printf("%fX^%d+",p->coef,p->index);
printf("\n");
}
com(int a,int b)
{
if(areturn -1;
else if(a==b)
return 0;
else return 1;
}
node1 *insert1(node1 *q1,node1 *new1,int m) //比较形插入,减法加法合用,m=1是为加法,m=2时为减法。
{
float add1,sub1;
int f=0,a,b;
node1 *pt,*m1,*m2;
m2=q1;
m1=q1->next;
while (f==0&&m1!=NULL)
{
a=new1->index;
b=m1->index;
switch (com(a,b))
{
case -1:
{q1->next=new1;
new1->next=m1;
q1=q1->next;
f=1;
if(m==2)
new1->coef=-new1->coef;
break;}
case 0:
{add1=m1->coef+new1->coef;
sub1=m1->coef-new1->coef;
f=1;
if ((add1==0&&m==1)||(sub1==0&&m==2))
{ q1->next=m1->next;
pt=m1;
free(pt);
break;
}
else if(m==1)
{ m1->coef=add1;
break;
}
else
m1->coef=sub1;
pt=new1;
free(pt);
break;
}
case 1:
{
q1=q1->next;
m1=m1->next;
if(m1==NULL)
{
q1->next=new1;
if(m==2)
new1->coef=-new1->coef;
}
}
}
}
printf("救命啊");
return m2;
}
node1 *mix(int m) //减法和加法合用函数,m=1是加法算法,m=2时减法算法。
{
node1 *q2,*q1,*new1,*h1,*m1,*n1;
q1=(struct node1 *)malloc(sizeof (struct node1));
q1=creat1();
output1(q1);
q2=(struct node1 *)malloc(sizeof (struct node1));
q2=creat1();
output1(q2);
h1=(struct node1 *)malloc(sizeof (struct node1));
m1=q1;
n1=q2->next;
while (n1!=NULL)
{
new1=(struct node1 *)malloc(sizeof (struct node1));

new1->coef=n1->coef;
new1->index=n1->index;
new1->next=NULL;
m1=insert1(m1,new1,m);
n1=n1->next;
}
return m1;
}
node1 *mul()
{
node1 *q2,*q1,*m1,*n1,*f1,*f2;
q1=(struct node1 *)malloc(sizeof (struct node1));
q1=creat1();
q2=(struct node1 *)malloc(sizeof (struct node1));
q2=creat1();
n1=(struct node1 *)malloc(sizeof (struct node1));
f1=q1->next;
f2=q2->next;
while (f2!=NULL)
{ while(f1!=NULL)
{
m1=(struct node1 *)malloc(sizeof (struct node1));
printf("f");
m1->coef=(f1->coef*f2->coef);
m1->index=(f1->index+f2->index);
m1->next=NULL;
if(f1==q1->next&&f2==q2->next)
{ f1=f1->next;
n1->next=m1;
continue;
}
printf("wo cao");
n1=insert1(n1,m1,1);
f1=f1->next;
}
f1=q1->next;
f2=f2->next;
}
return n1;
}
void main()
{
int m,f=0;
node1 *h1;
h1=(struct node1 *)malloc(sizeof (struct node1));
while(f==0)
{
windows();
scanf("%d",&m);
if(m<0||m>3)
{ printf("输入错误请重新输入\n");
continue;
}
switch (m)
{
case 0:f=1;break;
case 1: h1=mix(m);
output1(h1);
break;
case 2:h1=mix(m);
output1(h1);
break;
case 3:h1=mul();
output1(h1);
break;
}
}
}

相关文档
最新文档