上原题:
设计程序,实现对多项式的加,减,乘,除,微分和积分的运算。考虑分别用顺序分配的线性表和链接分配的线性表表示多项式的实现。估计各项运算的算法复杂性。
本来钱老师只要求用后一种的,结果我没看到,两种都编了,麻烦死。
别的都还好说,这多项式的除法真烦人。娘的,老子火很大。链接表的除法实在太烦了,我就直接把链表转化成数组然后调用数组的除法函数,这样只要一次遍历表就OK,感觉效率还蛮高的。就这么地吧。
数组线性表:
1
#ifndef LIST2
#define LIST3
using namespace std;4
#define N 10005

6
class ListType7


{8
friend ListType ListPlus(ListType L1,ListType L2); 9
friend ListType ListMinus(ListType L1,ListType L2);10
friend ListType ListMulti(ListType L1,ListType L2);11
friend ListType ListDevide(ListType L1,ListType L2);12
friend ListType ListDs(ListType L);13
friend ListType ListJf(ListType L);14
friend bool operator==(ListType,ListType);15
public:16
int Limit;17
int Length;18
ListType();19
//~ListType();20
String Print();21
void Insert(double Item,int Position);22
void Delete(int Position);23
void Clear();24

25
double Data[N];26
};27
void ListType::Insert(double Item,int Position)28


{29
Data[Position] += Item;30
}31
void ListType::Delete(int Position)32


{33
Data[Position] = 0;34
}35
void ListType::Clear()36


{37
for(int i = 0; i < Limit;i++)38

{39
Data[i]= 0;40
}41
}42
String ListType::Print()43


{44
String ss;45
String pos;46
String xs;47
String fh;48
for(int i = 0;i <= Length ; i++)49

{50
if(Data[i]!= 0)51

{52
if(Data[i]>0)fh = "+";53
else fh = "";54
pos = i;xs = Data[i];55
ss = ss + fh +xs+"x^"+pos;56
}57
}58
return ss;59
60
}61
ListType::ListType() //initial62


{63
Length = 0;64
Limit = N;65
for(int i =0;i< Limit; i++)66

{67
Data[i] = 0;68
}69
}70

71
ListType ListPlus(ListType L1,ListType L2)72


{73
ListType L3;74
int max;75
if(L1.Length > L2.Length)max = L1.Length;76
else max = L2.Length;77
L3.Length = max;78
for(int i = 0; i <= max; i++)79

{80
L3.Data[i] = L1.Data[i]+ L2.Data[i];81
}82
return L3;83
}84
ListType ListMinus(ListType L1,ListType L2)85


{86

87
ListType L3;88
int max;89
if(L1.Length > L2.Length)max = L1.Length;90
else max = L2.Length;91
L3.Length = max;92
for(int i = 0; i <= max; i++)93

{94
L3.Data[i] = L1.Data[i] - L2.Data[i];95
}96
while(L3.Length != 0 && L3.Data[L3.Length] == 0)97

{98
L3.Length--;99
}100
return L3;101
}102
ListType ListMulti(ListType L1,ListType L2)103


{104
ListType L3;105
int max;106
max = L1.Length + L2.Length;107
L3.Length = max;108
for(int i =0;i <= L1.Length; i++)109

{110
for(int j = 0;j <= L2.Length ; j++)111

{112
L3.Data[i+j] += L1.Data[i]*L2.Data[j];113
}114
}115
return L3;116
}117
ListType ListDevide(ListType L1,ListType L2)118


{119
ListType L3;120
ListType L4;121
while(1)122

{123
if(L1.Length >= L2.Length)124

{125
L4.Clear();126
L4.Length = L1.Length - L2.Length;127

128
L4.Data[L4.Length] = L1.Data[L1.Length]/L2.Data[L2.Length];129

130
L3 = ListPlus(L3,L4);131
L4 = ListMulti(L4,L2);132

133
L1 = ListMinus(L1,L4);134

135
if(L1.Length == 0)break;136
}137
else138

{139
ShowMessage("无法进行除法运算!");140
break;141
}142
}143
return L3;144
}145
ListType ListDs(ListType L)146


{147

148
ListType L3;149
for(int i =0 ;i < L.Length;i++)150

{151
L3.Data[i]= L.Data[i+1]*(i+1);152
}153
L3.Length = L.Length-1;154
return L3;155
}156
ListType ListJf(ListType L)157


{158
ListType L3;159
for(int i =0 ;i <= L.Length; i++)160

{161
L3.Data[i+1]= L.Data[i]/(i+1);162
}163
L3.Length = L.Length+1;164
return L3;165
}166
bool operator==(ListType L1,ListType L2)167


{168
if(L1.Length != L2.Length)return false;169
if(L1.Length == L2.Length)170

{171
for(int i =0 ;i <= L1.Length;i++)172

{173
if(L1.Data[i]!= L2.Data[i])return false;174
}175
}176
return true;177
}178

179
#endif180

181

链接表
Chain
1
#ifndef CHAIN2
#define CHAIN3
#include "List.h"4
struct Node5


{6
double Data;7
int pos;8
Node *Next;9
Node *Previous;10
};11

12
class Chain13


{14
friend Chain ChainPlus(Chain C1,Chain C2);15
friend Chain ChainMinus(Chain C1,Chain C2);16
friend Chain ChainMulti(Chain C1,Chain C2);17
friend Chain ChainDevide(Chain C1,Chain C2);18
friend Chain ChainDs(Chain C);19
friend Chain ChainJf(Chain C);20
friend bool operator==(Chain,Chain);21
public:22
Chain();23
//~Chain();24
void InsertBack(double Item,int pos ,Node *Position);25
void InsertFront(double Item,int pos,Node *Position);26
void DeleteNode(Node *Position);27
void PushBack(double Item,int Pos);28
void Clear();29
Node *Search(int order);30
String Print();31
Chain Mod();32
ListType Mody();33
private:34
Node *Head;35

36
};37
Chain::Chain()38


{39
Head = new Node;40
Head->Next = NULL;41
Head->Data = 0;42
Head->pos = 0;43
Head->Previous = NULL;44

45
}46
void Chain::InsertBack(double Item,int postemp,Node *Position)47


{48
Node *p;49
if(Position == NULL)ShowMessage("No pointed Node");50
else51

{52
p = new Node;53
p->Data = Item;54
p->pos = postemp;55
p->Previous = Position;56
p->Next = Position->Next;57
if(Position->Next != NULL)Position->Next->Previous = p;58
Position->Next = p;59
}60
}61
void Chain::InsertFront(double Item,int Pos,Node *Position)62


{63
Node *p;64
if(Position == NULL)ShowMessage("No pointed Node");65
else66

{67
p = new Node;68
p->Data = Item;69
p->Previous = Position->Previous;70
p->Next = Position;71
p->Previous->Next = p;72
Position->Previous = p;73
}74
}75
void Chain::PushBack(double Item,int Pos)76


{77
Node *p = Head;78
while(p->Next != NULL)p = p->Next;79
InsertBack(Item,Pos,p);80
}81
void Chain::DeleteNode(Node *Position)82


{83
if(Position == Head && Position == NULL)ShowMessage("No deletable Node");84
else if(Head->Next == NULL)ShowMessage("No deletable Node");85
else86

{87
Position->Previous->Next = Position->Next;88
if(Position->Next != NULL)Position->Next->Previous = Position->Previous;89
delete Position;90
}91
}92
void Chain::Clear()93


{94
Node *p = Head->Next ;Node * temp;95
while(p != NULL)96

{97
temp = p->Next;98
delete p;99
p =temp;100
}101
Head->Next = NULL;102
}103
String Chain::Print()104


{105
String ss;106
String poss;107
String xs;108
String fh;109
Node * p;110
for(p = Head; p->Next != NULL ; p = p->Next)111

{112
if(p->Data != 0)113

{114
if(p->Data>0)fh = "+";115
else fh = "";116
poss = p->pos;xs = p->Data;117
ss = ss + fh +xs+"x^"+poss;118
}119
}120
if(p->Data != 0)121

{122
if(p->Data>0)fh = "+";123
else fh = "";124
poss = p->pos;xs = p->Data;125
ss = ss + fh +xs+"x^"+poss;126
}127
return ss;128
}129
Chain Chain::Mod()130


{131
Node *p;132
p = Head;133
Chain C3;134

135
double ss[1000]; int Length = 0;136
for(int i =0;i<1000;i++)ss[i]=0;137
while(p != NULL)138

{139
ss[p->pos] += p->Data;140
if(p->pos >Length)Length = p->pos;141
p = p->Next;142
}143
for(int i =0;i <= Length; i ++)144

{145

if(ss[i]!=0)
{C3.PushBack(ss[i],i);}146
}147
return C3;148
}149
ListType Chain::Mody()150


{151
ListType L;L.Length = 0;152
Node *p; p = Head;153
while(p != NULL)154

{155
if(p->pos >L.Length)L.Length = p->pos;156
L.Data[p->pos] += p->Data;157
p = p->Next;158
}159
return L;160
}161
Chain ChainPlus(Chain C1,Chain C2)162


{163
Chain C3;164
Node *ptr;165
ptr = C1.Head;166

167
while(ptr->Next != NULL)ptr = ptr->Next;168
ptr->Next = C2.Head;169
C2.Head->Previous = ptr;170

171
C3 = C1.Mod();172
return C3;173
}174
Chain ChainMinus(Chain C1,Chain C2)175


{176
Chain C3;177
Node *ptr;178
ptr = C1.Head;179
Node *re;180
re = C2.Head;181
while(re->Next != NULL)182

{183
re->Data = 0 - re->Data ;184
re = re->Next;185
}186
re->Data = 0 - re->Data ;187
while(ptr->Next != NULL)ptr = ptr->Next;188
ptr->Next = C2.Head;189
C2.Head->Previous = ptr;190
C3 = C1.Mod();191
return C3;192
}193
Chain ChainMulti(Chain C1,Chain C2)194


{195
Chain C3;196
double dp;197
int ip;198
Node *ptr1;Node *ptr2;199
for(ptr1 = C1.Head;ptr1 != NULL;ptr1 = ptr1->Next )200

{201
for(ptr2 = C2.Head;ptr2 != NULL; ptr2 = ptr2->Next)202

{203
dp = (ptr1->Data)*(ptr2->Data);204
ip = ptr1->pos + ptr2->pos;205
C3.PushBack(dp,ip);206
}207
}208
C3= C3.Mod();209
return C3;210
}211
Chain ChainDevide(Chain C1,Chain C2)212


{213
ListType temp;214
temp = ListDevide(C1.Mody(),C2.Mody());215
Chain C3;216
for(int i = 0; i <= temp.Length;i++)217

{218
if(temp.Data[i] != 0)C3.PushBack(temp.Data[i],i);219
}220
return C3;221
}222
Chain ChainDs(Chain C)223


{224
Node *p = C.Head;225
while(p != NULL)226

{227
p->Data = (p->Data)*(p->pos);228
p->pos--;229
p = p->Next;230
}231
C = C.Mod();232
return C;233
}234
Chain ChainJf(Chain C)235


{236
Node *p = C.Head;237
while(p != NULL)238

{239
p->pos += 1;240
p->Data = (p->Data)/(p->pos);241
p=p->Next;242
}243
C = C.Mod();244
return C;245
}246

247
bool operator==(Chain C1,Chain C2)248


{249
Node *p1;Node *p2;250
p1 = C1.Head;p2 = C2.Head;251
while(p1 != NULL && p2 != NULL)252

{253
if(p1->Next == NULL &&p2->Next != NULL)return false;254
else if(p2->Next == NULL &&p1->Next != NULL)return false;255
else if(p1->Data != p2->Data)return false;256
else if(p1->pos != p2->pos)return false;257

258
p1 = p1->Next;259
p2 = p2->Next;260
}261
return true;262

263
}264

265
#endif266

浙公网安备 33010602011771号