一元多项式的加减以及求导
生活随笔
收集整理的這篇文章主要介紹了
一元多项式的加减以及求导
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?采用鏈式存儲結構,將兩個線性鏈表表示的一元多項式相加減,求導并輸出。
#include<stdio.h>
?#include<stdlib.h>?typedef struct lnode
?{
? int coef; ?//系數?
? int index; //指數
struct lnode *next;?
?}node;
?node *Create() ? ? ?//頭插法建立一元多項式
?{
? node *p1,*p2,*head;
? int x,y,i = 1;
? head = (node *)malloc(sizeof(node));
? head->next = NULL;
? p1 = head;
? p2 = head;
? for(i=1; ; i++)
? {
? p2 = (node *)malloc(sizeof(node));
? printf("輸入第%d項的系數,指數:",i);
? scanf("%d%d",&x,&y);
? if(x==0 && y==0) ? ? ? ? ? ?//輸入0 0時表示輸入完成?
? {
? break;?
}
if(x == 0)
{
continue;
}
? p2->coef = x;
? p2->index = y;
? p2->next = p1->next;
? p1->next = p2;
}
return head;
?}
?void Sort (node *head) ? ? ? ? ? ? //將多項式中相同指數的項合并?
?{
? node *p,*q,*p1;
? for(p = head->next;p->next!=NULL;p = p->next)
? {
? for(q = p;q->next!=NULL;q=q->next)
? {
? if(p->index == q->next->index)
? {
? p->coef = p->coef + q->next->coef; ? ?//相同指數的項合并?
? q->next->coef = 0;
? q->next->index = 0;
}
}
}
p = head;
q = p;
while(q->next!=NULL)
{
p1 = q;
q = q->next;
if(q->coef==0 && q->index==0)
{
p1->next = q->next;
free(q);
q = p1;
}
}
?}
?int Print(node *head) ? ? ? ? ?//打印函數?
?{
? node *p;
? int x,y;
? if(head->next == NULL)
? return 0;
else
? p = head->next;
? while(p->next != NULL)
? {
if(p->index == 0)
{
printf("%d+",p->coef);
}
else if(p->next->coef<0)
{
printf("%dx^%d",p->coef,p->index);
}
else
{
? printf("%dx^%d+",p->coef,p->index);
? }
p = p->next;
}
if(p->index == 0)
{
printf("%d\n",p->coef);
}
else
{
? printf("%dx^%d\n",p->coef,p->index);
? }
return 1;
?}
?void Plus(node *head1,node *head2) ? ? ? ? ?//多項式相加?
?{
? node *p,*q,*p1;
? p = head1;
? q = head2;
? for(p = head1->next;p!=NULL;p = p->next)
? {
? for(q = head2->next;q!=NULL;q = q->next)
? {
? if(p->index == q->index) ? ? ? // //相同指數的項合并?
? {
? p->coef = p->coef + q->coef;
? q->coef = 0;
? q->index = 0;
}
}
}
for(q = head2->next;q!=NULL;q = q->next)
{
if(q->coef!=0 && q->index!=0) ? ? ? ? //將第二個多項式中與第一個多項式指數不同的項 添加入第一個多項式?
{
p1 = (node *)malloc(sizeof(node));
p1->coef = q->coef;
p1->index = q->index;
p1->next = head1->next;
head1->next = p1;
}
}
Print(head1);
?}
?void Minus(node *head1,node *head2) ? ? ? ? // 多項式相加(同相減)?
?{
? node *p,*q,*p1;
? p = head1;
? q = head2;
? for(p = head1->next;p!=NULL;p = p->next)
? {
? for(q = head2->next;q!=NULL;q = q->next)
? {
? if(p->index == q->index)
? {
? p->coef = p->coef - q->coef;
? q->coef = 0;
? q->index = 0;
}
}
}
for(q = head2->next;q!=NULL;q = q->next)
{
if(q->coef!=0 && q->index!=0)
{
p1 = (node *)malloc(sizeof(node));
p1->coef = 0-q->coef;
p1->index = q->index;
p1->next = head1->next;
head1->next = p1;
}
}
Print(head1);
?}
?void derivative(node *head) ? ?//求多項式的導數?
?{
? node *p;
? p = head->next;
? while(p!=NULL)
? {
? p->coef = p->coef*p->index;
? p->index = p->index-1;
? p = p->next;
}
printf("多項式的導數為:");
Print(head);
?}
?int main()
?{
? node *head1,*head2,*head;
? int x;
? printf("輸入第一個多項式的系數,指數(輸入0 0時結束輸入):\n");
? head1 = Create();
? Sort(head1);
Print(head1);
printf("輸入第二個多項式的系數,指數(輸入0 0時結束輸入):\n");
? head2 = Create();
? Sort(head2);
Print(head2);
printf("選擇運算:");
scanf("%d",&x);
switch(x)
{
case 1:derivative(head1);break;
case 2:derivative(head2);break;
case 3:Plus(head1,head2);break;
case 4: Minus(head1,head2);break;
}
?}
總結
以上是生活随笔為你收集整理的一元多项式的加减以及求导的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于信道和链路
- 下一篇: Leetcode--5. 最长回文子串(