c++鍊錶實現一元多項式相加**如下:
(輸入多項式按次數從高到低排列)
#include
#include
using namespace std;
intmain()
;//定義節點型別,coe表示x前的係數,order表示x的次數
int coe,order;
int i;
int nodenuma;
//多項式a的項數
printf
("請輸入多項式a的項數:\n");
scanf
("%d"
,&nodenuma)
;printf
("請輸入多項式a:\n");
struct node *heada=0,
*pa=0,
*qa=0;
scanf
("%d %d"
,&coe,
&order)
; heada=
newstruct node;
heada-
>coe=coe;
heada-
>order=order;
heada-
>link=0;
qa=heada;
for(i=
1;i)//掃入a
printf
("\n您輸入的多項式a為:\n");
printf
("%dx^%d"
,heada-
>coe,heada-
>order)
; pa=heada-
>link;
while
(pa!=0)
//輸出所輸入的多項式a
printf
("\n\n");
int nodenumb;
//多項式b的項數
printf
("請輸入多項式b的項數:\n");
scanf
("%d"
,&nodenumb)
;printf
("請輸入多項式b:\n");
struct node *headb=0,
*pb=0,
*qb=0;
scanf
("%d %d"
,&coe,
&order)
; headb=
newstruct node;
headb-
>coe=coe;
headb-
>order=order;
headb-
>link=0;
qb=headb;
for(i=
1;i)//掃入b
printf
("\n您輸入的多項式b為:\n");
printf
("%dx^%d"
,headb-
>coe,headb-
>order)
; pb=headb-
>link;
while
(pb!=0)
//輸出所輸入的多項式b
printf
("\n\n");
struct node *p=0,
*q=0
; p=heada;
q=headb;
struct node *headc=0,
*r=0
,*s=0;
headc=
newstruct node;
while
(p->order==q-
>order&&p-
>coe+q-
>coe==0)
//當多項式a、b最高項次數相同且係數和為0直接捨去
if(p-
>order==q-
>order)
else
if(p-
>order>q-
>order)
else
//此處生成輸出的頭結點
while
(p!=
0&&q!=0)
//當多項式a、b未掃入c剩下的最高項次數相同且係數和為0直接捨去
if(p-
>order==q-
>order)
else
if(p-
>order>q-
>order)
else}if
(p==
0&&q!=0)
s->link=q;
else
if(p!=
0&&q==0)
s->link=p;
printf
("\n相加結果為:\n");
printf
("%dx^%d"
,headc-
>coe,headc-
>order)
; r=headc-
>link;
while
(r!=0)
printf
("\n");
while
(heada!=0)
while
(headb!=0)
while
(headc!=0)
//釋放節點空間
一元多項式相加 鍊錶
問題描述 設計乙個一元稀疏多項式簡單計算器。基本要求 一元稀疏多項式簡單計算器的基本功能是 輸入並建立多項式 輸出多項式,輸出形式為整數序列 n,c1,e 1,c2,e 2,c n,en,其中n是多項式的項數,ci,e i分別是第i項的係數和指數,序列按指數降序排列 多項式a和b相加,建立多項式a ...
鍊錶實現一元多項式的相加
如下 include include struct polynomial 結構體的定義 void print polynomial l printf n int getlength polynomial l return i void newlist polynomial l l head retu...
用鍊錶實現一元稀疏多項式的相加
由於一元多項式包含係數項和指數項,其係數為float型,指數項為int型,往往將其兩項組合成為乙個結構元素型別,將多項式看成是乙個有序表,則多項式定義中的各個操作均可利用有序表操作來完成。建立多項式演算法 操作結果 輸入m項的係數和指數,建立一元多項式p 銷毀多項式演算法 初始條件 一元多項式p已存...