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);
}