-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolynom.cpp
More file actions
185 lines (173 loc) · 3.2 KB
/
Copy pathpolynom.cpp
File metadata and controls
185 lines (173 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct PolyType{
int Coef;
int Pwr;
struct PolyType *Next;
}POLYNODE;
class Polynom{
private:
POLYNODE *Head;
public:
Polynom();
POLYNODE *MakePolyNode(int Coef,int Pwr);
void InsertPolyNode(int Coef,int Pwr);
void DisplayPolyNode();
POLYNODE *getHead();
Polynom AddPoly(Polynom P,Polynom Q);
};
main()
{
int ele,ch,pwr,n1,n2;
Polynom p;
Polynom q;
Polynom r;
while(1){
cout<<"\nPOLYNOMIAL NUMBERS MENU\n";
cout<<"1.Enter"<<endl;
cout<<"2.Add Polynomial"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice\n";
cin>>ch;
switch(ch){
case 1: cout<<"Enter number of term for first polynomial ";
cin>>n1;
for(int i=0;i<n1;i++){
cout<<"Enter the element: ";
cin>>ele;
cout<<"Enter the Power: ";
cin>>pwr;
p.InsertPolyNode(ele,pwr);
}
cout<<"Enter number of term for second polynomial ";
cin>>n2;
for(int i=0;i<n2;i++){
cout<<"Enter the element: ";
cin>>ele;
cout<<"Enter the Power: ";
cin>>pwr;
q.InsertPolyNode(ele,pwr);
}
break;
case 2: r=p.AddPoly(p,q);
break;
case 3: cout<<"Expresion 1:\n";
p.DisplayPolyNode();
cout<<"Expresion 2:\n";
q.DisplayPolyNode();
cout<<"Result:\n";
r.DisplayPolyNode();
break;
case 4: exit(0);
}
}
}
//----------------Polynom()-----------------
Polynom::Polynom()
{
Head=NULL;
}
//----------------MakePolyNode()-----------------
POLYNODE *Polynom::MakePolyNode(int Coef,int Pwr)
{
POLYNODE *Node;
Node=(POLYNODE *)malloc(sizeof(POLYNODE));
Node->Coef=Coef;
Node->Pwr=Pwr;
Node->Next=NULL;
return Node;
}
//----------------InsertPolyNode()-----------------
void Polynom::InsertPolyNode(int Coef,int Pwr)
{
POLYNODE *Prev,*Curr,*p;
p=MakePolyNode(Coef,Pwr);
Curr=Head;
if(Head==NULL){
Head=p;
return;
}
if(p->Pwr > Curr->Pwr){
p->Next=Curr;
Head=p;
return;
}else{
while(Curr && p->Pwr < Curr->Pwr){
Prev=Curr;
Curr=Curr->Next;
}
p->Next=Curr;
Prev->Next=p;
}
}
//----------------DisplayPolyNode()-----------------
void Polynom::DisplayPolyNode()
{
if(Head==NULL){
cout<<"Expresion is empty";
}else{
POLYNODE *Curr;
Curr=Head;
while(Curr!=NULL){
if(Curr->Pwr==0){
cout<<Curr->Coef;
Curr=Curr->Next;
}else{
cout<<Curr->Coef;
cout<<"X^"<<Curr->Pwr;
Curr=Curr->Next;
if(Curr==NULL || Curr->Coef<0)
cout<<"";
else
cout<<"+";
}
}
cout<<endl;
}
}
POLYNODE *Polynom::getHead()
{
return Head;
}
Polynom Polynom::AddPoly(Polynom P,Polynom Q)
{
POLYNODE *A,*B;
Polynom R;
int Coef,Pwr;
A=P.getHead();
B=Q.getHead();
while(A&&B){
if(A->Pwr==B->Pwr){
Coef=A->Coef + B->Coef;
Pwr=A->Pwr;
R.InsertPolyNode(Coef,Pwr);
A=A->Next;
B=B->Next;
}else if(A->Pwr>B->Pwr){
Coef=A->Coef;
Pwr=A->Pwr;
R.InsertPolyNode(Coef,Pwr);
A=A->Next;
}else{
Coef=B->Coef;
Pwr=B->Pwr;
R.InsertPolyNode(Coef,Pwr);
B=B->Next;
}
}
while(A){
Coef=A->Coef;
Pwr=A->Pwr;
R.InsertPolyNode(Coef,Pwr);
A=A->Next;
}
while(B){
Coef=B->Coef;
Pwr=B->Pwr;
R.InsertPolyNode(Coef,Pwr);
B=B->Next;
}
return R;
}