当前位置:首页 » 编程语言 » c语言多项式的加法重复值怎么办
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言多项式的加法重复值怎么办

发布时间: 2022-12-06 01:32:54

A. 两个多项式相加运算(用c语言

#include<stdio.h>
#include<malloc.h>
#define Null 0
typedef struct Node
{
int coeff;
int expo;
Node *next;
}listNode,*list;

list createList()
{
list head;
head = (list)malloc(sizeof(listNode));
head = NULL;
printf("want to create a new node?y/n\n");
char ch;
fflush(stdin);
scanf("%c",&ch);
while(ch=='Y' || ch== 'y')
{
list p;
p = (list)malloc(sizeof(listNode));
printf("input coeff\n");
int coeff;
scanf("%d",&coeff);
p->coeff = coeff;
printf("input expo\n");
int expo;
scanf("%d",&expo);
p->expo = expo;
p->next = NULL;
//链表为空的时候,即添加首个元素
if(head == NULL)
{
head=p;//添加代码
}
else
{
list prev,curr;
curr = head;
prev = NULL;
//找到添加位置
while(curr!=NULL && curr->expo>p->expo)
{
prev=curr;
curr=curr->next;//添加代码
}
if(curr!=NULL && curr->expo == p->expo)
{
curr->coeff = curr->coeff + p->coeff;
printf("want to create a new node?y/n\n");
fflush(stdin);
scanf("%c",&ch);
if(ch=='Y' || ch== 'y')
continue;
else
return head;
}
//插入结点,结点非首
if(prev != NULL)
{
p->next=curr;
prev->next=p;
//添加代码
}
//插入结点,结点为首
else
{
p->next=curr;
head=p;
//添加代码
}

}
printf("want to create a new node?y/n\n");
fflush(stdin);
scanf("%c",&ch);
}
return head;
}

list add(list head1,list head2)
{
list head,newNode,ptr1,ptr2,ptr3;
ptr1 = head1;
ptr2 = head2;
head = NULL;
while(ptr1 != NULL && ptr2 != NULL)
{
newNode = (list)malloc(sizeof(listNode));
if(ptr1->expo > ptr2->expo)
{
newNode->coeff = ptr1->coeff;
newNode->expo = ptr1->expo;
newNode->next = NULL;
ptr1 = ptr1->next;
}
else if(ptr1->expo < ptr2->expo)
{
newNode->coeff = ptr2->coeff;
newNode->expo = ptr2->expo;
newNode->next = NULL;
ptr2 = ptr2->next;//添加代码
}
else
{
newNode->coeff = ptr1->coeff + ptr2->coeff;
newNode->expo = ptr1->expo;
newNode->next = NULL;
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
if(head==NULL)
{
head = newNode;
}
else
{
ptr3 = head;
//添加代码
while(ptr3->next != NULL)
ptr3 = ptr3->next;
ptr3->next = newNode;
}
}
while(ptr1 != NULL)
{
newNode = (list)malloc(sizeof(listNode));
newNode->coeff = ptr1->coeff;
newNode->expo = ptr1->expo;
newNode->next = NULL;
ptr3 = head;
if(ptr3 == NULL)
head = ptr3 = newNode;
else
{
while(ptr3->next != NULL)
ptr3 = ptr3->next;
ptr3->next = newNode;//添加代码
}
ptr1 = ptr1->next;
}
while(ptr2 != NULL)
{
newNode = (list)malloc(sizeof(listNode));
newNode->coeff = ptr2->coeff;
newNode->expo = ptr2->expo;
ptr3 = head;
if(ptr3 == NULL)
head = ptr3 = newNode;
else
{
while(ptr3->next != NULL)
ptr3 = ptr3->next;
ptr3->next = newNode;
}
ptr2 = ptr2->next;
}
return head;
}

void display(list head)
{
list ptr = head;
while(ptr != NULL)
{
if(ptr != head )
printf("+");
printf("%d",ptr->coeff);
printf("x^");
printf("%d",ptr->expo);
ptr = ptr->next;
}
printf("\n");
}

int main(int argc, char* argv[])
{
list head,head1,head2;
printf("input the first list\n");
head1 = createList();
display(head1);
printf("input the second list\n");
head2 = createList();
display(head2);
head = add(head1,head2);
display(head);
return 0;
}

B. C语言 多项式相加

/*为了一元多项式相加能够使用效率更高的算发,应该在多项式的排序操作中加入合并同类项,除非你可以保证输入不含有同类项*/
/*今天就这些吧,明天争取写出来,走了!*/
#include<stdio.h>
#include<stdlib.h>
typedef struct DXS{
float xs;
int zs;
struct DXS *next;
} multinomial;
//输入函数
multinomial *input(void)
{
multinomial *p,*h;
h=NULL;
printf("please input the xs and zs of the DXS:\n");
for(;;)
{
p=(multinomial *)malloc(sizeof(multinomial));
scanf("%f %d",&p->xs,&p->zs);
if(p->xs==0 && p->zs==0){free(p);break;}
p->next=h;
h=p;//从头部插入,没有问题
}
return(h);
}
//将多项式排序

multinomial *inorder(multinomial *h)
{
multinomial *s,*t,*m,*h3;
h3=h->next;//指向第二个结点
h->next=NULL;//拆下第一个结点,为什么?
while(h3!=NULL)
{
s=h3;
h3=h3->next;
t=h;
m=h;
while(s->zs<t->zs&&t!=NULL)
{
m=t;
t=t->next;
}
if(m=t)
{s->next=t;
h=s;
}
else
{
s->next=t;
m->next=s;
}
}
return(h);
}
//两个多项式相加
multinomial *add(multinomial *h1,multinomial *h2)
{
multinomial *u,*v,*w,*j;
w=h2;j=h2;
while(j!=NULL)
{
u=v=h1;w=j;
while(u->zs>w->zs&&u!=NULL)
{v=u;
u=u->next;
}
if(u->zs==w->zs) //指数相同,则系数相加
{
u->xs=u->xs+w->xs;
if(u->xs==0) //如果系数为0,则做删除操作
{
if(u=v) //删除表头
h1=h1->next;
}
else if(u->next==NULL) // 删除表尾
v=NULL;
else //删除表中
{
u=u->next;
}
}

else if(u=v) //待插入点在在表头
{
w->next=u;
h1->next=w;
}
else //待插入点不是在表头
{
w->next=u;
v->next=w;
}

j=j->next;
}
return(h1);
}

void print(multinomial *h1)
{
printf("f(x)=");
while(h1!=NULL)
{

printf("%f",h1->xs);
if(h1->zs!=0)
printf("x^%f",h1->xs);
else printf("%f",h1->xs);
h1=h1->next;
if(h1!=NULL&&h1->xs>0)
printf("+");
}
}

void main()
{multinomial *h1,*h2;
printf("输入第一个多项式的系数和指数(系数与指数之间用空格号隔开)");
h1=input();
printf("输入第二个多项式的系数和指数(系数与指数之间用空格号隔开");
h2=input();
h1=inorder(h1);
h2=inorder(h2);
h1=add(h1,h2);
print(h1);
}