码迷,mamicode.com
首页 > 其他好文 > 详细

[DataStructure]多项式加法与乘法--B.链表存储(适用于零元系数多的多项式)

时间:2015-12-31 22:53:27      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

数据结构大作业……

发出来大家乐呵乐呵……

一、问题描述

给出N个多项式,求他们的和与积

二、解题报告

基本思想:加法和乘法都是把得数项直接链接在链表后面,最后统一做一个Merge&Sort工作即可。方便又快捷。

(1)建立存储结构

1 struct _Poly
2 {
3     int factor;//系数
4     int Index;//
5     struct _Poly* next;//下一节点
6 };
7 _Poly* poly[MAXTIMES+1];
8 int Sum[MAXTIMES+1]; //每一个多项式所含项数

(2)主程序架构

 1 int main()
 2 {    int n;
 3     cout<<"请输入要做运算的多项式数量"<<endl;
 4     cin>>n;
 5     for (int i=0;i<=n+1;++i)   Init_Poly(i);
 6 for (int i=1;i<=n;++i)     Input_Data(i);
 7 /*TaskMgr,任务判断*/                                                                                                                                                                                                                                                                                                   cout<<"请输入你要进行什么类型的运算?1为加法,2为乘法:"<<endl;;
 8     cin>>Sum[0]; 
 9     if (Sum[0]==1)
10 {    cout<<"f(x)="; 
11 poly[0]=poly[1];
12         for (int i=2;i<=n;++i)  Plus_Poly(poly[0], poly[i]);//加法
13         Output_Poly(poly[0]);///Output加法结果
14         cout<<""<<endl;
15     }
16     else if (Sum[0]==2)
17     {   cout<<"f(x)=";
18         poly[0]=poly[1];
19         for (int i=2;i<=n;++i)  Multiply_Poly(poly[0], poly[i]);//乘法
20         Output_Poly(poly[0]);///Output乘法结果
21         cout<<""<<endl;
22 }    return 0;
23 }

(3)初始化模块Init_Po  ly(int i);

1 void Init_Poly(int i)
2 {
3      poly[i]=NULL;
4      poly[i]=new _Poly;
5      poly[i]->next=NULL;
6 }

(4)插入结点Insert_Node(_Poly* Poly0,_Poly* node);

 1 int Insert_Node(_Poly* poly0, _Poly* node)
 2 {
 3     while( poly0->next!=NULL )
 4         poly0=poly0->next;///遍历至表尾
 5     poly0->next=new _Poly;
 6     poly0=poly0->next;
 7     poly0->factor=node->factor;
 8     poly0->Index=node->Index;
 9     poly0->next=NULL;
10     return 0;
11 } 

(5)删除结点Delete_Poly(_Poly** Poly0);

 1 int Delete_Poly(_Poly** poly0)
 2 {
 3     _Poly* tmp=NULL;
 4     while( NULL!=*poly0 )
 5     {
 6         tmp=*poly0;
 7         *poly0=(*poly0)->next;
 8         delete tmp;
 9     }
10     return 0;
11 }
12

(6)输入模块Input_Poly(int i);

1 P.S. cout<<"----------------读入方式说明---------------"<<endl;
2     cout<<"输入幂次和对应系数,常数项幂次为0"<<endl;
3     cout<<"如对f(x)=x^5+x^2-54x+65"<<endl;
4     cout<<"请输入:4"<<endl;
5     cout<<"       1 5"<<endl;
6     cout<<"       1 2"<<endl;
7     cout<<"       -54 1"<<endl;
8     cout<<"       65 0"<<endl;
 1 void Input_Data(int i)
 2 {
 3     _Poly data;
 4     cout<<"请输入第"<<i<<"个多项式项数:"<<endl;;
 5     cin>>Sum[i];
 6     cout<<"请分别输入多项式1各项的系数和幂数,中间用空格隔开:"<<endl;;
 7     for( int j=1; j<=Sum[i]; ++j)
 8     {
 9         cin>>data.factor>>data.Index;
10         Insert_Node(poly[i], &data);
11     }
12     cout<<"f"<<i<<"(x)=";
13     Output_Poly(poly[i]);
14 } 

(7)输出模块Output_Data(_Poly Poly0);

 1 int Output_Poly(_Poly* poly0)
 2 {
 3     if( poly0->next->Index!=0 )
 4     {
 5         if (poly0->next->factor!=1) cout<<poly0->next->factor;
 6         cout<<"x";
 7         if (poly0->next->Index!=1) cout<<^<<poly0->next->Index;
 8     }
 9     else
10         cout<<poly0->next->factor;
11     if (poly0->next->next!=NULL && poly0->next->next->factor>0) cout<<+;
12 poly0=poly0->next;
13     while( NULL!=poly0->next )
14     {
15         if( poly0->next->factor>0 )
16         {
17             if( poly0->next->Index==0 )
18                 cout<<poly0->next->factor;
19             else
20             {
21                 if (poly0->next->factor!=1) cout<<poly0->next->factor;
22                 cout<<"x";
23                 if (poly0->next->Index!=1) cout<<^<<poly0->next->Index;
24             }
25         }
26         else if( poly0->next->factor<0 )
27         {
28             if( poly0->next->Index==0 )
29             {
30                 if( poly0->next->factor!=0 )
31                     cout<<poly0->next->factor;
32             }
33             else
34              {
35                 if (poly0->next->factor!=1) cout<<poly0->next->factor;
36                 cout<<"x";
37                 if (poly0->next->Index!=1) cout<<^<<poly0->next->Index;
38              }
39         }
40         if (poly0->next->next!=NULL && poly0->next->next->factor>0) cout<<+;
41         poly0=poly0->next;
42     }
43     cout<<endl;
44     return 0;
45 }
46

(8)加法模块Plus_Poly(_Poly* poly1, _Poly* poly2);//////完了完了我大作业这句标题写错了

 1 ///预处理,先把两个链表相连,其他操作见后Merge_Poly();
 2 int Plus_Poly(_Poly* poly1, _Poly* poly2)
 3 {
 4     _Poly* head=poly1;
 5     while( poly1->next!=NULL )
 6         poly1=poly1->next;
 7     poly1->next=poly2->next;///将Poly2全部添加到Poly1后
 8     Merge_Poly(head);///合并同类项
 9     poly[0]=head;
10     return 0;
11 } 

(9)乘法模块Multiply_Poly(int Sum);

 1 ///将新乘出的元素加在链表后面,待进一步Merge&Sort进行运算
 2 int Multiply_Poly(_Poly* poly1, _Poly* poly2)
 3 {
 4     _Poly data;
 5     _Poly *poly0=NULL;
 6     _Poly* head1=poly1;
 7     _Poly* head2=poly2;
 8     poly0=new _Poly;
 9     poly0->next=NULL;
10     while( poly1->next!=NULL )
11     {
12         while( poly2->next!=NULL )
13         {
14             data.factor=(poly1->next->factor)*(poly2->next->factor);
15             data.Index=(poly1->next->Index)+(poly2->next->Index);
16             data.next=NULL;
17             Insert_Node(poly0, &data);///新求得元素插入
18             poly2=poly2->next;
19         }
20         poly2=head2;
21         poly1=(poly1)->next;
22     }//OutputPoly(pNew);
23     Merge_Poly(poly0);///合并同类项
24     poly[0]=poly0;
25     Delete_Poly(&head1);
26     Delete_Poly(&head2);
27     return 0;
28 } 

(10)合并同类项,并排序Merge_Poly(_Poly* Poly0);

本函数承接乘法和加法未完成事业,并为输出打下基础。

是本程序核心代码之一。

 1 int Merge_Poly(_Poly* poly0)
 2 {    int sorted=0;   ///标记可合并元素
 3     _Poly* node;
 4     _Poly* tmp;
 5     _Poly* const head=poly0;
 6     if( NULL==poly0 ) return -1;
 7     node=head->next;
 8     head->next=NULL;
 9     while( node!=NULL )
10     {
11         sorted=0;
12         poly0=head;
13         while( poly0->next!=NULL )
14         {
15             if( node->Index==poly0->next->Index )  ///如果是幂数相同,则合并系数
16             {
17                 poly0->next->factor+=node->factor;
18                 node=node->next;
19                 sorted=1;
20                 break;
21             }
22             else if((node->Index) > (poly0->next->Index)) ///如果幂数大,转移该项至前
23             {
24                 tmp=node;
25                 node=node->next;
26                 tmp->next=poly0->next;
27                 poly0->next=tmp;
28                 sorted=1;
29                 break;
30             }
31             poly0=poly0->next;
32         }/*while2*/
33         if( sorted==0 )///该项已无可合并项
34         {
35             tmp=node;
36             node=node->next;
37             poly0->next=tmp;
38             tmp->next=NULL;
39         }
40     }/*while*/
41     return 0;
42 }

(11)完整代码

  1 /*
  2    By
  3      Iris.Catch-22.S、`
  4      Dept. of Mathematics,
  5      School of Science,
  6      HIT
  7    November,2015
  8 */
  9 #include<iostream>
 10 #define MAXTIMES 110
 11 using namespace std;
 12 void CopyRight()
 13 {
 14    cout<<"------------By ICS,HIT,2015/11-------------"<<endl;
 15    cout<<"--------多项式加法、乘法计算(链表)---------"<<endl;
 16    cout<<"------------------Ver 0.9------------------"<<endl;
 17 
 18 }
 19 struct _Poly
 20 {
 21     int factor;//系数
 22     int Index;//
 23     struct _Poly* next;//下一节点
 24 };
 25 /*定义*/
 26 _Poly* poly[MAXTIMES+1];
 27 int Sum[MAXTIMES+1];
 28 
 29 /*初始化Poly1、Poly2*/
 30 void Init_Poly(int i)
 31 {
 32      poly[i]=NULL;
 33      poly[i]=new _Poly;
 34      poly[i]->next=NULL;
 35 }
 36 /*插入节点*/
 37 int Insert_Node(_Poly* poly0, _Poly* node)
 38 {
 39     while( poly0->next!=NULL )
 40         poly0=poly0->next;///遍历至表尾
 41     poly0->next=new _Poly;
 42     poly0=poly0->next;
 43     poly0->factor=node->factor;
 44     poly0->Index=node->Index;
 45     poly0->next=NULL;
 46     return 0;
 47 }
 48 /*删除链表*/
 49 int Delete_Poly(_Poly** poly0)
 50 {
 51     _Poly* tmp=NULL;
 52     while( NULL!=*poly0 )
 53     {
 54         tmp=*poly0;
 55         *poly0=(*poly0)->next;
 56         delete tmp;
 57     }
 58     return 0;
 59 }
 60 /*输出多项式*/
 61 int Output_Poly(_Poly* poly0)
 62 {
 63     if( poly0->next->Index!=0 )
 64     {
 65         if (poly0->next->factor!=1) cout<<poly0->next->factor;
 66         cout<<"x";
 67         if (poly0->next->Index!=1) cout<<^<<poly0->next->Index;
 68     }
 69     else
 70         cout<<poly0->next->factor;
 71     if (poly0->next->next!=NULL && poly0->next->next->factor>0) cout<<+;
 72     poly0=poly0->next;
 73     while( NULL!=poly0->next )
 74     {
 75         if( poly0->next->factor>0 )
 76         {
 77             if( poly0->next->Index==0 )
 78                 cout<<poly0->next->factor;
 79             else
 80             {
 81                 if (poly0->next->factor!=1) cout<<poly0->next->factor;
 82                 cout<<"x";
 83                 if (poly0->next->Index!=1) cout<<^<<poly0->next->Index;
 84             }
 85         }
 86         else if( poly0->next->factor<0 )
 87         {
 88             if( poly0->next->Index==0 )
 89             {
 90                 if( poly0->next->factor!=0 )
 91                     cout<<poly0->next->factor;
 92             }
 93             else
 94              {
 95                 if (poly0->next->factor!=1) cout<<poly0->next->factor;
 96                 cout<<"x";
 97                 if (poly0->next->Index!=1) cout<<^<<poly0->next->Index;
 98              }
 99         }
100         if (poly0->next->next!=NULL && poly0->next->next->factor>0) cout<<+;
101         poly0=poly0->next;
102     }
103     cout<<endl;
104     return 0;
105 }
106 void Input_Data(int i)
107 {
108     _Poly data;
109     cout<<"请输入第"<<i<<"个多项式项数:"<<endl;;
110     cin>>Sum[i];
111     cout<<"请分别输入多项式1各项的系数和幂数,中间用空格隔开:"<<endl;;
112     for( int j=1; j<=Sum[i]; ++j)
113     {
114         cin>>data.factor>>data.Index;
115         Insert_Node(poly[i], &data);
116     }
117     cout<<"f"<<i<<"(x)=";
118     Output_Poly(poly[i]);
119 }
120 /*系数合并,降幂排列*/
121 int Merge_Poly(_Poly* poly0)
122 {
123     int sorted=0;   ///标记可合并元素
124     _Poly* node;
125     _Poly* tmp;
126     _Poly* const head=poly0;
127     if( NULL==poly0 ) return -1;
128     node=head->next;
129     head->next=NULL;
130     while( node!=NULL )
131     {
132         sorted=0;
133         poly0=head;
134         while( poly0->next!=NULL )
135         {
136             if( node->Index==poly0->next->Index )  ///如果是幂数相同,则合并系数
137             {
138                 poly0->next->factor+=node->factor;
139                 node=node->next;
140                 sorted=1;
141                 break;
142             }
143             else if((node->Index) > (poly0->next->Index)) ///如果幂数大,转移该项至前
144             {
145                 tmp=node;
146                 node=node->next;
147                 tmp->next=poly0->next;
148                 poly0->next=tmp;
149                 sorted=1;
150                 break;
151             }
152             poly0=poly0->next;
153         }/*while2*/
154         if( sorted==0 )///该项已无可合并项
155         {
156             tmp=node;
157             node=node->next;
158             poly0->next=tmp;
159             tmp->next=NULL;
160         }
161     }/*while*/
162     return 0;
163 }
164 /*多项式加法*/
165 int Plus_Poly(_Poly* poly1, _Poly* poly2)
166 {
167     _Poly* head=poly1;
168     while( poly1->next!=NULL )
169         poly1=poly1->next;
170     poly1->next=poly2->next;///将Poly2全部添加到Poly1后
171     Merge_Poly(head);///合并同类项
172     poly[0]=head;
173     return 0;
174 }
175 /*多项式乘法*/
176 int Multiply_Poly(_Poly* poly1, _Poly* poly2)
177 {
178     _Poly data;
179     _Poly *poly0=NULL;
180     _Poly* head1=poly1;
181     _Poly* head2=poly2;
182     poly0=new _Poly;
183     poly0->next=NULL;
184     while( poly1->next!=NULL )
185     {
186         while( poly2->next!=NULL )
187         {
188             data.factor=(poly1->next->factor)*(poly2->next->factor);
189             data.Index=(poly1->next->Index)+(poly2->next->Index);
190             data.next=NULL;
191             Insert_Node(poly0, &data);///新求得元素插入
192             poly2=poly2->next;
193         }
194         poly2=head2;
195         poly1=(poly1)->next;
196     }
197     //OutputPoly(pNew);
198 
199     Merge_Poly(poly0);///合并同类项
200     poly[0]=poly0;
201     Delete_Poly(&head1);
202     Delete_Poly(&head2);
203     return 0;
204 }
205 int main()
206 {
207     CopyRight();
208     /*Init*/
209     int n;
210     cout<<"请输入要做运算的多项式数量"<<endl;
211     cin>>n;
212     for (int i=0;i<=n+1;++i)
213        Init_Poly(i);
214 
215     cout<<"----------------读入方式说明---------------"<<endl;
216     cout<<"输入幂次和对应系数,常数项幂次为0"<<endl;
217     cout<<"如对f(x)=x^5+x^2-54x+65"<<endl;
218     cout<<"请输入:4"<<endl;
219     cout<<"       1 5"<<endl;
220     cout<<"       1 2"<<endl;
221     cout<<"       -54 1"<<endl;
222     cout<<"       65 0"<<endl;
223     cout<<"-------------------------------------------"<<endl;
224     /*InputData*/
225     for (int i=1;i<=n;++i)
226         Input_Data(i);
227     /*TaskMgr*/
228     cout<<"请输入你要进行什么类型的运算?1为加法,2为乘法:"<<endl;;
229     cin>>Sum[0];
230     if (Sum[0]==1)
231     {
232         cout<<"f(x)=";
233         poly[0]=poly[1];
234         for (int i=2;i<=n;++i)
235            Plus_Poly(poly[0], poly[i]);
236         Output_Poly(poly[0]);///Output
237         cout<<""<<endl;
238     }
239     else if (Sum[0]==2)
240     {
241         cout<<"f(x)=";
242         poly[0]=poly[1];
243         for (int i=2;i<=n;++i)
244            Multiply_Poly(poly[0], poly[i]);
245         Output_Poly(poly[0]);///Output
246         cout<<""<<endl;
247     }
248     return 0;
249 }
250

半吊子水平的链表……见笑了、欢迎神犇帮忙指正

--------Done By Iris.Catch-22.S、`

[DataStructure]多项式加法与乘法--B.链表存储(适用于零元系数多的多项式)

标签:

原文地址:http://www.cnblogs.com/Catch-22/p/5092713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!