
题目:用C语言编程多项式计算
要求:(1)输入并建立多项式;(2)输出多项式;(3)两个多项式相加,建立并输出和多项式;(4)两个多项式相减,建立并输出差多项式。...
要求:(1)输入并建立多项式;
(2)输出多项式;
(3)两个多项式相加,建立并输出和多项式;
(4)两个多项式相减,建立并输出差多项式。 展开
(2)输出多项式;
(3)两个多项式相加,建立并输出和多项式;
(4)两个多项式相减,建立并输出差多项式。 展开
2个回答
展开全部
#include <stdio.h>
#include <stdlib.h>
//结构体节点
struct polynode
{
int coef;
int exp;
struct polynode * link;
};
typedef struct polynode *polypointer;//重命名
//插入一个节点,并排序
polypointer insert( int e,int c,polypointer head )
{
polypointer p,pr,k;
p = (polypointer)malloc(sizeof(struct polynode)) ;
pr=head;
p->link=NULL;
p->coef=c;
p->exp=e;
if (head==NULL) //链表为空
{
head=p;
}
else
{
while (pr!=NULL&&pr->exp>=e) //降序排列
{
if (pr->exp==e)
{
pr->coef=pr->coef+c;
return head;
}
k=pr;
pr=pr->link;
}
if (pr==head)
{
p->link=head;
head=p;
return head;
}
if (pr==NULL)
{
k->link=p;
}
else
{
p->link=k->link;
k->link=p;
}
}
return head;
}
//打印链表
void display( polypointer head )
{
polypointer p;
p=head;
do
{
printf("%dx(exp)%d+",p->coef,p->exp);
p=p->link;
}
while (p!=NULL);
}
int main()
{
polypointer first=(polypointer)malloc(sizeof(struct polynode));
polypointer second=(polypointer)malloc(sizeof(struct polynode));
int c;
int e;
int i;
first=NULL;
second=NULL;
printf(" 多项式加法运算小程序\n\n");
printf("请输入第一个多项式。(输入0,0结束)\n\n");
printf("请输入第1项。\n系数:\n");
scanf("%d",&c);
printf("请输入第1项。\n指数:\n");
scanf("%d",&e);
for (i=0;c!=0;i++)//将第一个方程存入链表
{
first=insert(e,c,first);
printf("请输入第一个多项式。(输入0,0结束)\n\n");
printf("请输入第%d项。\n系数:\n",i+2);
scanf("%d",&c);
printf("请输入第%d项。\n指数:\n",i+2);
scanf("%d",&e);
}
printf("第一个多项式");
display(first);
printf("\n\n请输入第二个多项式。(输入0,0结束)\n\n");
printf("请输入第1项。\n系数:\n");
scanf("%d",&c);
printf("请输入第1项。\n指数:\n");
scanf("%d",&e);
for (i=0;c!=0;i++)//将第二个方程存入链表
{
second=insert(e,c,second);
printf("请输入第一个多项式。(输入0,0结束)\n\n");
printf("请输入第%d项。\n系数:\n",i+2);
scanf("%d",&c);
printf("请输入第%d项。\n指数:\n",i+2);
scanf("%d",&e);
}
printf("第二个多项式");
display(second);
printf("\n\n\n\n");
printf("多项式和");
while(second!=NULL)//将第二个链表并入第一个链表
{
first=insert(second->exp,second->coef,first);
second=second->link;
}
display(first);//打印输出
return 0;
}
#include <stdlib.h>
//结构体节点
struct polynode
{
int coef;
int exp;
struct polynode * link;
};
typedef struct polynode *polypointer;//重命名
//插入一个节点,并排序
polypointer insert( int e,int c,polypointer head )
{
polypointer p,pr,k;
p = (polypointer)malloc(sizeof(struct polynode)) ;
pr=head;
p->link=NULL;
p->coef=c;
p->exp=e;
if (head==NULL) //链表为空
{
head=p;
}
else
{
while (pr!=NULL&&pr->exp>=e) //降序排列
{
if (pr->exp==e)
{
pr->coef=pr->coef+c;
return head;
}
k=pr;
pr=pr->link;
}
if (pr==head)
{
p->link=head;
head=p;
return head;
}
if (pr==NULL)
{
k->link=p;
}
else
{
p->link=k->link;
k->link=p;
}
}
return head;
}
//打印链表
void display( polypointer head )
{
polypointer p;
p=head;
do
{
printf("%dx(exp)%d+",p->coef,p->exp);
p=p->link;
}
while (p!=NULL);
}
int main()
{
polypointer first=(polypointer)malloc(sizeof(struct polynode));
polypointer second=(polypointer)malloc(sizeof(struct polynode));
int c;
int e;
int i;
first=NULL;
second=NULL;
printf(" 多项式加法运算小程序\n\n");
printf("请输入第一个多项式。(输入0,0结束)\n\n");
printf("请输入第1项。\n系数:\n");
scanf("%d",&c);
printf("请输入第1项。\n指数:\n");
scanf("%d",&e);
for (i=0;c!=0;i++)//将第一个方程存入链表
{
first=insert(e,c,first);
printf("请输入第一个多项式。(输入0,0结束)\n\n");
printf("请输入第%d项。\n系数:\n",i+2);
scanf("%d",&c);
printf("请输入第%d项。\n指数:\n",i+2);
scanf("%d",&e);
}
printf("第一个多项式");
display(first);
printf("\n\n请输入第二个多项式。(输入0,0结束)\n\n");
printf("请输入第1项。\n系数:\n");
scanf("%d",&c);
printf("请输入第1项。\n指数:\n");
scanf("%d",&e);
for (i=0;c!=0;i++)//将第二个方程存入链表
{
second=insert(e,c,second);
printf("请输入第一个多项式。(输入0,0结束)\n\n");
printf("请输入第%d项。\n系数:\n",i+2);
scanf("%d",&c);
printf("请输入第%d项。\n指数:\n",i+2);
scanf("%d",&e);
}
printf("第二个多项式");
display(second);
printf("\n\n\n\n");
printf("多项式和");
while(second!=NULL)//将第二个链表并入第一个链表
{
first=insert(second->exp,second->coef,first);
second=second->link;
}
display(first);//打印输出
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询