-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhww22.cpp
More file actions
194 lines (191 loc) · 6.17 KB
/
hww22.cpp
File metadata and controls
194 lines (191 loc) · 6.17 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
186
187
188
189
190
191
192
193
194
// hww22.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
// class polynomial
class polynomial
{
private:
struct node
{
float coeff;
int exp;
node *link;
}*p;
public:
polynomial();
void append(float c, int e);
void display();
void add(polynomial &l1, polynomial &l2);
~polynomial();
};
//default constructor
polynomial::polynomial()
{
p = NULL;
}
// function to add a polunomial
void polynomial::append(float c, int e)
{
node *temp = p;
if (temp == NULL)
{
temp = new node;
p = temp;
}
else
{
while (temp->link != NULL)
temp = temp->link;
temp->link = new node;
temp = temp->link;
}
temp->coeff = c;
temp->exp = e;
temp->link = NULL;
}
//function to display polynomial
void polynomial::display()
{
node *temp = p;
int f = 0;
cout << endl;
while (temp != NULL)
{
if (f != 0)
{
if (temp->coeff > 0)
cout << " + ";
else
cout << " ";
}
if (temp->exp != 0)
cout << temp->coeff << "x^" << temp->exp;
else
cout << temp->coeff;
temp = temp->link;
f = 1;
}
}
void polynomial::add(polynomial &l1, polynomial &l2)
{
node *z = NULL;
if (l1.p == NULL && l2.p == NULL)
return;
node *temp1, *temp2;
temp1 = l1.p;
temp2 = l2.p;
while (temp1 != NULL && temp2 != NULL)
{
if (p == NULL)
{
p = new node;
z = p;
}
else
{
z->link = new node;
z = z->link;
}
if (temp1->exp < temp2->exp)
{
z->coeff = temp2->coeff;
z->exp = temp2->exp;
temp2 = temp2->link;
}
else
{
if (temp1->exp > temp2->exp)
{
z->coeff = temp1->coeff;
z->exp = temp1->exp;
temp1 = temp1->link;
}
else
{
if (temp1->exp == temp2->exp)
{
z->coeff = temp1->coeff + temp2->coeff;
z->exp = temp1->exp;
temp1 = temp1->link;
temp2 = temp2->link;
}
}
}
}
while (temp1 != NULL)
{
if (p == NULL)
{
p = new node;
z = p;
}
else
{
z->link = new node;
z = z->link;
}
z->coeff = temp1->coeff;
z->exp = temp1->exp;
temp1 = temp1->link;
}
while (temp2 != NULL)
{
if (p == NULL)
{
p = new node;
z = p;
}
else
{
z->link = new node;
z = z->link;
}
z->coeff = temp2->coeff;
z->exp = temp2->exp;
temp2 = temp2->link;
}
z->link = NULL;
}
//destructor
polynomial :: ~polynomial()
{
node *q;
while (p != NULL)
{
q = p->link;
delete p;
p = q;
}
}
int main()
{
//enter details for first polynomial
polynomial p1;
p1.append(14, 5);
p1.append(15, 4);
p1.append(17, 2);
p1.append(18, 1);
p1.append(19, 0);
//display first polynomial
cout << "\nFirst polynomial:";
p1.display();
//enter details for second polynomial
polynomial p2;
p2.append(15, 6);
p2.append(5, 5);
p2.append(-35, 4);
p2.append(45, 3);
p2.append(65, 1);
//display second polynomial
cout << "\nSecond polynomial:";
p2.display();
polynomial p3;
// add two polynomials and display it
p3.add(p1, p2);
cout << "\nResultant polynomial: ";
p3.display();
return 0;
}