apc_002.cpp
Problem Statement
apc_002.cpp
Write a C++ program to add two polynomial equations and display the resultant polynomial.
Source Code
cpp
#include<iostream>
using namespace std;
typedef struct Node {
double coeff;
int expo;
Node *next;
} node;
class mains {
private:
node *head1 = nullptr, *head2 = nullptr, *rear = nullptr;
public:
void input() {
int n1;
cout << "Enter the degree of first polynomial equation: ";
cin >> n1;
for(int i = 0; i <= n1; i++) {
node *newNode = new node;
cout << "Enter the coefficient for n^" << i << ": ";
cin >> newNode -> coeff;
newNode -> expo = i;
newNode -> next = nullptr;
if(head1 == nullptr) {
head1 = newNode;
rear = newNode;
} else {
rear -> next = newNode;
rear = newNode;
}
}
cout << "You entered: ";
display(head1);
cout << endl;
int n2;
cout << "\nEnter the degree of second polynomial equation: ";
cin >> n2;
for(int i = 0; i <= n2; i++) {
node *newNode = new node;
cout << "Enter the coefficient for n^" << i << ": ";
cin >> newNode -> coeff;
newNode -> expo = i;
newNode -> next = nullptr;
if(head2 == nullptr) {
head2 = newNode;
rear = newNode;
} else {
rear -> next = newNode;
rear = newNode;
}
}
cout << "You entered: ";
display(head2);
cout << endl;
}
void display(node *head) {
node *temp = head;
bool first = true;
bool second = true;
while(temp != nullptr) {
if(first) {
cout << temp -> coeff;
first = false;
temp = temp -> next;
}
if(second) {
if(temp -> coeff >= 0)
cout << " + " << temp -> coeff << "x";
else
cout << " - " << (-temp -> coeff) << "x";
second = false;
} else {
if(temp -> coeff >= 0)
cout << " + " << temp -> coeff << "x^" << temp -> expo;
else
cout << " - " << (-temp -> coeff) << "x^" << temp -> expo;
}
temp = temp -> next;
}
}
void calc() {
node *head = nullptr, *temp1 = head1, *temp2 = head2, *rear = nullptr;
while(temp1 != nullptr && temp2 != nullptr) {
node *newNode = new node;
newNode -> coeff = (temp1 -> coeff + temp2 -> coeff);
newNode -> expo = temp1 -> expo;
newNode -> next = nullptr;
if(head == nullptr) {
head = newNode;
rear = newNode;
} else {
rear -> next = newNode;
rear = newNode;
}
temp1 = temp1 -> next;
temp2 = temp2 -> next;
}
while(temp1 != nullptr) {
node *newNode = new node;
newNode -> coeff = temp1 -> coeff;
newNode -> expo = temp1 -> expo;
newNode -> next = nullptr;
rear -> next = newNode;
rear = newNode;
temp1 = temp1 -> next;
}
while(temp2 != nullptr) {
node *newNode = new node;
newNode -> coeff = temp2 -> coeff;
newNode -> expo = temp2 -> expo;
newNode -> next = nullptr;
rear -> next = newNode;
rear = newNode;
temp2 = temp2 -> next;
}
cout << "\nSum: ";
display(head);
freeList(head);
}
void freeList(node *head) {
node *temp;
while(head != nullptr) {
temp = head;
head = head -> next;
delete temp;
}
}
void doAll() {
input();
calc();
freeList(head1);
freeList(head2);
}
};
int main() {
mains obj;
obj.doAll();
return 0;
}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
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
● Author - Amit Dutta · Updated - 01 Aug 2026