跪求编程高手解决 链表多项式运算程序
请根据程序框架编写函数creatpoly,polyadd,polymulti,ployout,ClearList,InserNode,实现多项式的建立求和求积显示释放插入...
请根据程序框架编写函数creatpoly,polyadd,polymulti,ployout,ClearList,InserNode,实现多项式的建立 求和 求积 显示 释放 插入排序等功能。
程序框架
#include"stdafx.h"
#include"stdio.h"
#include"stdlib.h"
#include"math.h"
typedef struct mulpoly
{
double coef
double exp
struct mulpoly*next;
}MULPOLY;//多项式链式结构
MULPOLY*creatpoly();//创建多项式
void InserNODE(MULPOLY*head,MULPOLY*s);//排序插入,从小到大,且合并同类项,乘法若运用它,显得很方便
void polyout(MULPOLY*head,char*s);//输出显示多项式
MULPOLY*polyadd(MULPOLY*ha,MULPOLY*hb);//多项式求和
MULPOLY*polymulti(MULPOLY*ha,MULPOLY*hb);//多项式求积
void ClearList(MULPOLY*head);//释放清空链表
int main(int argc,char*argv[])
{
MULPOLY*ha*hb*hc*hd;
ha=creatpoly();
ployout(ha,"输入整理结果:\n);
hb=creatpoly();
ployout(hb,"输入整理结果:\n);
hc=polyadd(ha,hb);
ployout(hc,"加法整理结果:\n);
hd=polymulti(ha,hb);
ployout(hd,"乘法整理结果:\n);
ClearList(ha);
ClearList(hb);
ClearList(hc);
ClearList(hd);
return 0;
} 展开
程序框架
#include"stdafx.h"
#include"stdio.h"
#include"stdlib.h"
#include"math.h"
typedef struct mulpoly
{
double coef
double exp
struct mulpoly*next;
}MULPOLY;//多项式链式结构
MULPOLY*creatpoly();//创建多项式
void InserNODE(MULPOLY*head,MULPOLY*s);//排序插入,从小到大,且合并同类项,乘法若运用它,显得很方便
void polyout(MULPOLY*head,char*s);//输出显示多项式
MULPOLY*polyadd(MULPOLY*ha,MULPOLY*hb);//多项式求和
MULPOLY*polymulti(MULPOLY*ha,MULPOLY*hb);//多项式求积
void ClearList(MULPOLY*head);//释放清空链表
int main(int argc,char*argv[])
{
MULPOLY*ha*hb*hc*hd;
ha=creatpoly();
ployout(ha,"输入整理结果:\n);
hb=creatpoly();
ployout(hb,"输入整理结果:\n);
hc=polyadd(ha,hb);
ployout(hc,"加法整理结果:\n);
hd=polymulti(ha,hb);
ployout(hd,"乘法整理结果:\n);
ClearList(ha);
ClearList(hb);
ClearList(hc);
ClearList(hd);
return 0;
} 展开
1个回答
展开全部
这是我亲自写的,没有按照你定义的结构体和函数名来写,已经实现创建,相加,相乘,输出了,插入的话你自己完成吧
输入方法:
1 2 1 1 0 0
1 2 1 1 0 0
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct polynomial
{
int coef,ex;
struct polynomial *next;
}POLY;
POLY *create_poly(void);
POLY *add_poly(POLY *,POLY *);
POLY *mul_poly(POLY *,POLY *);
void print_poly(POLY *);
int main(void)
{
POLY *heada=NULL,*headb=NULL,*headsum=NULL,*headmul=NULL;
printf("create the first multinomial:\n");
heada=create_poly();
printf("create the second multinomial:\n");
headb=create_poly();
printf("The first multinomial:\n");
print_poly(heada);
printf("The second multinomial:\n");
print_poly(headb);
headsum=add_poly(heada,headb);
printf("The sum multinomial:");
print_poly(headsum);
headmul=mul_poly(heada, headb);
printf("The mul multinomial:");
print_poly(headmul);
return 0;
}
//创建
POLY *create_poly()
{
int iA=0,iE=0;
POLY *head=NULL,*s=NULL,*r=NULL;
head=(POLY *)malloc(sizeof(POLY));
r=head;
printf("please input coef and exponent:\n");
scanf("%d%d",&iA,&iE);
while(iA != 0)
{
s=(POLY *)malloc(sizeof(POLY));
s->coef=iA,s->ex=iE;
r->next=s;
r=s;
scanf("%d%d",&iA,&iE);
}
r->next=NULL;
return head;
}
//相加
POLY *add_poly(POLY *p1, POLY *p2) //多项式相加
{
POLY *head, *tmp, *s;
int value;
p1=p1->next;
p2=p2->next;
head=tmp=(POLY*)malloc(sizeof(POLY));
head->next=NULL;
while(p1 && p2)
{
if(p1->ex == p2->ex)
{
value=p1->coef+p2->coef;
if(value != 0)
{
s=(POLY *)malloc(sizeof(POLY));
s->coef=value;
s->ex=p1->ex;
s->next=NULL;
}
p1=p1->next;
p2=p2->next;
}
else
if(p1->ex < p2->ex)
{
s=(POLY *)malloc(sizeof(POLY));
s->coef=p1->coef;
s->ex=p1->ex;
s->next=NULL;
p1=p1->next;
}
else
{
s=(POLY *)malloc(sizeof(POLY));
s->coef=p2->coef;
s->ex=p2->ex;
s->next=NULL;
p2=p2->next;
}
if(head->next==NULL)
{
head->next=s;
tmp=s;
}
else
{
tmp->next=s;
tmp=s;
}
}
tmp->next=p1?p1:p2;
return head;
}
//相乘
POLY *mul_poly(POLY *p1, POLY *p2)
{
POLY *head;
POLY *t,*q,*s,*r;
head=(POLY *)malloc(sizeof(POLY));
head->next=NULL;
r=(POLY *)malloc(sizeof(POLY));
r->next=NULL;
for(t=p1->next;t;t=t->next)
{
for(q=p2->next;q;q=q->next)
{
s=(POLY *)malloc(sizeof(POLY));
r->next=s;
s->coef=q->coef * t->coef;
s->ex=q->ex + t->ex;
s->next=NULL;
head=add_poly(r,head);
}
}
return head;
}
//输出
void print_poly(POLY *head)
{
POLY *p=NULL;
p=head->next;
if(p == NULL)
printf("The multinomial is NULL.\n");
else
{
do
{
if(p->coef>=0)
printf("+%dx^%d",p->coef,p->ex);
else
printf("%dx^%d",p->coef,p->ex);
p=p->next;
}while(p != NULL);
printf("\n");
}
}
输入方法:
1 2 1 1 0 0
1 2 1 1 0 0
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct polynomial
{
int coef,ex;
struct polynomial *next;
}POLY;
POLY *create_poly(void);
POLY *add_poly(POLY *,POLY *);
POLY *mul_poly(POLY *,POLY *);
void print_poly(POLY *);
int main(void)
{
POLY *heada=NULL,*headb=NULL,*headsum=NULL,*headmul=NULL;
printf("create the first multinomial:\n");
heada=create_poly();
printf("create the second multinomial:\n");
headb=create_poly();
printf("The first multinomial:\n");
print_poly(heada);
printf("The second multinomial:\n");
print_poly(headb);
headsum=add_poly(heada,headb);
printf("The sum multinomial:");
print_poly(headsum);
headmul=mul_poly(heada, headb);
printf("The mul multinomial:");
print_poly(headmul);
return 0;
}
//创建
POLY *create_poly()
{
int iA=0,iE=0;
POLY *head=NULL,*s=NULL,*r=NULL;
head=(POLY *)malloc(sizeof(POLY));
r=head;
printf("please input coef and exponent:\n");
scanf("%d%d",&iA,&iE);
while(iA != 0)
{
s=(POLY *)malloc(sizeof(POLY));
s->coef=iA,s->ex=iE;
r->next=s;
r=s;
scanf("%d%d",&iA,&iE);
}
r->next=NULL;
return head;
}
//相加
POLY *add_poly(POLY *p1, POLY *p2) //多项式相加
{
POLY *head, *tmp, *s;
int value;
p1=p1->next;
p2=p2->next;
head=tmp=(POLY*)malloc(sizeof(POLY));
head->next=NULL;
while(p1 && p2)
{
if(p1->ex == p2->ex)
{
value=p1->coef+p2->coef;
if(value != 0)
{
s=(POLY *)malloc(sizeof(POLY));
s->coef=value;
s->ex=p1->ex;
s->next=NULL;
}
p1=p1->next;
p2=p2->next;
}
else
if(p1->ex < p2->ex)
{
s=(POLY *)malloc(sizeof(POLY));
s->coef=p1->coef;
s->ex=p1->ex;
s->next=NULL;
p1=p1->next;
}
else
{
s=(POLY *)malloc(sizeof(POLY));
s->coef=p2->coef;
s->ex=p2->ex;
s->next=NULL;
p2=p2->next;
}
if(head->next==NULL)
{
head->next=s;
tmp=s;
}
else
{
tmp->next=s;
tmp=s;
}
}
tmp->next=p1?p1:p2;
return head;
}
//相乘
POLY *mul_poly(POLY *p1, POLY *p2)
{
POLY *head;
POLY *t,*q,*s,*r;
head=(POLY *)malloc(sizeof(POLY));
head->next=NULL;
r=(POLY *)malloc(sizeof(POLY));
r->next=NULL;
for(t=p1->next;t;t=t->next)
{
for(q=p2->next;q;q=q->next)
{
s=(POLY *)malloc(sizeof(POLY));
r->next=s;
s->coef=q->coef * t->coef;
s->ex=q->ex + t->ex;
s->next=NULL;
head=add_poly(r,head);
}
}
return head;
}
//输出
void print_poly(POLY *head)
{
POLY *p=NULL;
p=head->next;
if(p == NULL)
printf("The multinomial is NULL.\n");
else
{
do
{
if(p->coef>=0)
printf("+%dx^%d",p->coef,p->ex);
else
printf("%dx^%d",p->coef,p->ex);
p=p->next;
}while(p != NULL);
printf("\n");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询