两个一元多项式相加

程序运行结果如下:
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;"># include<stdio.h></span>
# include<stdlib.h>
struct node
{
	int exp;
	float coef;
	struct node *next; /*指向结构体指针*/ 
};
typedef struct node ListNode;
ListNode *createpoly() //创建多项式链表 
{
  ListNode *h=NULL,*p,*q=NULL;
  int e;
  float c;
  printf("请输入系数和指数:");
  scanf("%f,%d",&c,&e);
  while(e!=0||c!=0)
  {
  	p=(ListNode*)malloc(sizeof(ListNode));
  	p->coef=c;
  	p->exp=e;
  	p->next=NULL;
  	if(h==NULL)
  	h=p;
  	else
  	q->next=p;
  	q=p;
  	printf("请输入系数和指数:");
  	scanf("%f,%d",&c,&e);
	  }	
	  return h;
}
void disppoly(ListNode *h)
/*输出多项式*/
{
	ListNode *p;
	p=h;
	while(p!=NULL)
	{
		if(p->exp==0)
		printf("%.2f",p->coef);
		else
		printf("%fx^%d",p->coef,p->exp);
		p=p->next;
		if(p!=NULL)
		printf("+");
	}
	printf("\n");
 } 
 ListNode *addpoly(ListNode *h1,ListNode *h2)
 /*将两个多项式相加*/
 {
 	ListNode *p,*r=NULL,*s1,*s2,*s=NULL;
 	float c;
 	int e;
 	s1=h1;
 	s2=h2;
 	while(s1!=NULL&&s2!=NULL)
 	{
 		if(s1->exp==s2->exp)
 		{
 			c=s1->coef+s2->coef;
 			e=s1->exp;
 			s1=s1->next;
 			s2=s2->next;
		 }
		 else if(s1->exp>s2->exp)
		 {
		 	c=s1->coef;
		 	e=s1->exp;
		 	s1=s1->next;
		 }
		 else
		 {c=s2->coef;
		 e=s2->exp;
		 s2=s2->next;
		 }
		 if(c!=0)
		 {
		 	p=(ListNode*)malloc(sizeof(ListNode));
		 	p->coef=c;
		 	p->exp=e;p->next=NULL;
		 	if(s==NULL)
		 	s=p;
		 	else
		 	r->next=p;
		 	r=p;
		 }
	 }
	 while(s1!=NULL)
	 {
	 	c=s1->coef;
	 	e=s1->exp;
	 	s1=s1->next;
	 	if(c!=0)
	 	{
	 		p=(ListNode*)malloc(sizeof(ListNode));
		 	p->coef=c;
		 	p->exp=e;p->next=NULL;
		 	if(s==NULL)
		 	s=p;
		 	else
		 	r->next=p;
		 	r=p;
		 }
	 }
	 while(s2!=NULL)
	 {
	 	c=s2->coef;
	 	e=s2->exp;
	 	s2=s2->next;
	 	if(c!=0)
	 	{
	 		p=(ListNode*)malloc(sizeof(ListNode));
		 	p->coef=c;
		 	p->exp=e;p->next=NULL;
		 	if(s==NULL)
		 	s=p;
		 	else
		 	r->next=p;
		 	r=p;
		 }
	 }
	 return s;
  } 
void deletepoly(ListNode *h)
//释放多项式所占的内存单元 
{
	ListNode *p,*r=h;
	while(r!=NULL)
	{
		p=r->next;
		free(r);
		r=p;
	}
}
int main()
{
	ListNode *head1,*head2,*head;
	printf("创建第一个多项式:\n"); 
	head1=createpoly();
	printf("创建第二个多项式:\n");
	head2=createpoly();
	printf("将两个多项式相加:\n");
	head=addpoly(head1,head2);
	disppoly(head);
	deletepoly(head);
}
  </span>

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个关于多项式相加的编程问题。以下是一个可能的实现: ```python class Node: def __init__(self, coef, exp, next=None): self.coef = coef self.exp = exp self.next = next class Polynomial: def __init__(self): self.head = None def add_term(self, coef, exp): if coef == 0: return if self.head is None: self.head = Node(coef, exp) return if exp > self.head.exp: self.head = Node(coef, exp, self.head) return if exp == self.head.exp: self.head.coef += coef if self.head.coef == 0: self.head = self.head.next return curr = self.head while curr.next is not None and curr.next.exp > exp: curr = curr.next if curr.next is not None and curr.next.exp == exp: curr.next.coef += coef if curr.next.coef == 0: curr.next = curr.next.next return curr.next = Node(coef, exp, curr.next) def __add__(self, other): result = Polynomial() curr1, curr2 = self.head, other.head while curr1 is not None and curr2 is not None: if curr1.exp > curr2.exp: result.add_term(curr1.coef, curr1.exp) curr1 = curr1.next elif curr1.exp < curr2.exp: result.add_term(curr2.coef, curr2.exp) curr2 = curr2.next else: result.add_term(curr1.coef + curr2.coef, curr1.exp) curr1, curr2 = curr1.next, curr2.next while curr1 is not None: result.add_term(curr1.coef, curr1.exp) curr1 = curr1.next while curr2 is not None: result.add_term(curr2.coef, curr2.exp) curr2 = curr2.next return result def __str__(self): terms = [] curr = self.head while curr is not None: if curr.exp == 0: terms.append(str(curr.coef)) elif curr.exp == 1: terms.append(f"{curr.coef}x") else: terms.append(f"{curr.coef}x^{curr.exp}") curr = curr.next if not terms: return "0" return " + ".join(terms) p1 = Polynomial() p1.add_term(2, 3) p1.add_term(3, 2) p1.add_term(4, 1) p1.add_term(5, 0) p2 = Polynomial() p2.add_term(3, 4) p2.add_term(2, 2) p2.add_term(1, 1) p2.add_term(6, 0) p3 = p1 + p2 print(p1) print(p2) print(p3) ``` 这个实现使用了单链表来存储多项式的每一项,其中每个节点包含了系数和指数。`add_term` 方法用于向多项式中添加一项,它会按照指数从大到小的顺序插入节点。`__add__` 方法用于实现多项式相加,它会遍历两个多项式的节点,按照指数从大到小的顺序将相同指数的项相加,并将结果存储在一个新的多项式中。`__str__` 方法用于将多项式转换为字符串,它会遍历多项式的节点,将每一项转换为字符串后拼接起来。 在上面的代码中,我们创建了两个多项式 `p1` 和 `p2`,分别为 $2x^3 + 3x^2 + 4x + 5$ 和 $3x^4 + 2x^2 + x + 6$。然后我们将它们相加得到了一个新的多项式 `p3`,它为 $3x^4 + 2x^3 + 5x^2 + 5x + 11$。最后我们分别输出了 `p1`、`p2` 和 `p3` 的字符串表示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值