-
Notifications
You must be signed in to change notification settings - Fork 0
/
52.polynomial_representation.cpp
99 lines (87 loc) · 1.94 KB
/
52.polynomial_representation.cpp
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
/***
* p(x) = 3x^3 + 3x + 4
* each x has two things - coefficient and exponent;
* we can maintain a struct likewise
*
*/
#include<iostream>
using namespace std;
struct term{
int c,e;
};
class poly{
int num;
term* terms;
public:
poly(int num){
this->num = num;
terms = new term[num];
}
poly(){
cout<<"enter num pf terms: ";
cin>>num;
terms = new term[num];
}
void create();
void display();
int evaluate(int x);
~poly(){
delete []terms;
}
poly operator+(poly &p2);
};
void poly::create(){
cout<<"Enter "<< num<<" pairs of coefficient and exponent:\n";
for(int i=0;i<num;i++)
cin>>terms[i].c>>terms[i].e;
}
void poly::display(){
for(int i=0;i<num;i++){
if(terms[i].e==0)
cout<<terms[i].c;
else
cout<<terms[i].c<<"x^"<<terms[i].e;
if(i!=num-1)
cout<<" + ";
}
}
int poly::evaluate(int x){
int sum = 0;
for(int i=0;i<num;i++){
sum += terms[i].c*(pow(x,terms[i].e));
}
return sum;
}
poly poly::operator+(poly &p2){
int i,j,k;
i=j=k=0;
poly result(num + p2.num);
while(i<num && j<p2.num){
if(terms[i].e>p2.terms[j].e)
result.terms[k++] = terms[i++];
else if(terms[i].e<p2.terms[j].e)
result.terms[k++] = terms[j++];
else{
//equal addition
result.terms[k] = terms[i++];
result.terms[k++].c += p2.terms[j++].c;
}
}
result.num = k;
return result;
}
int main(){
poly p1(5);
poly p2(3);
p1.create();
p2.create();
cout<<endl<<"p1(x): ";
p1.display();
cout<<endl<<"p2(x): ";
p2.display();
poly p3 = p1 + p2;
cout<<"\np1(x) + p2(x) = ";
p3.display();
cout<<"\nfor x = 1: "<<p1.evaluate(1);
cout<<"\nfor x = 2: "<<p1.evaluate(2);
}