Repository navigation
Expand file tree
/
Copy pathpolynomial.cpp
More file actions
153 lines (136 loc) · 3.15 KB
/
Copy pathpolynomial.cpp
File metadata and controls
153 lines (136 loc) · 3.15 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
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
const unsigned short int maxpower = 21;
// too store polynomails where degree <= 20
struct Polynomial
{
int degree;
int coefficients[maxpower];
};
Polynomial createPolynomial(int degree)
{
Polynomial r;
r.degree=degree;
for(int i=0; i<=degree; i++){
r.coefficients[i]=0;
}
return r;
}
void printPolynomial(const Polynomial &p)
{
bool first_term=true;
for(int i=0; i<=p.degree; i++){
if(p.coefficients[i]!=0){
int power=p.degree-i;
if(!first_term && p.coefficients[i]>0){
cout<<"+";
}
if(power>1){
cout<<p.coefficients[i];
cout<<"x^"<<power<<" ";
}
else if(power==1){
cout<<p.coefficients[i];
cout<<"x"<<" ";
}
else{
cout<<p.coefficients[i]<<" ";
}
first_term=false;
}
}
if(first_term){
cout<<"0";
}
return;
}
Polynomial inputPolynomial(int degree)
{
Polynomial t;
t.degree=degree;
for(int i=0; i<=degree; i++){
cin>>t.coefficients[i];
}
return t;
}
void updatePolynomial(Polynomial &p)
{
for(int i=0; i<=p.degree; i++){
cin>>p.coefficients[i];
}
return ;
}
Polynomial addPolynomials(const Polynomial &p1, const Polynomial &p2)
{
int maxdegree;
if (p1.degree > p2.degree){
maxdegree = p1.degree;
}
else{
maxdegree = p2.degree;
}
int c1,c2;
Polynomial t;
t.degree=maxdegree;
for(int i=0; i<=maxdegree; i++){
if(i<=p1.degree){
c1 = p1.coefficients[i];
}
else{
c1=0;
}
if(i<=p2.degree){
c2 = p2.coefficients[i];
}
else{
c2=0;
}
t.coefficients[i]=c1+c2;
}
return t;
}
unsigned int value(const Polynomial &m, int varValue)
{
int power;
int v,value;
v=0;
for( int i=0 ; i<=m.degree; i++){
power=m.degree-i;
value=m.coefficients[i]*(pow(varValue,power));
v+=value;
}
return v;
}
int main()
{
cout << "Enter 3 coefficients for Polynomial of degree 2" << endl;
Polynomial m1 = inputPolynomial(2);
cout << "Your Polynomial is:" << endl;
printPolynomial(m1);
cout<<endl;
cout << "Again enter 3 coefficients for same Polynomial of degree 2" << endl;
updatePolynomial(m1);
cout << "Your Polynomial is:" << endl;
printPolynomial(m1);
cout<<endl;
int v = value(m1, 2);
cout << "The polynomial for x = 2 is evaluated as:" << endl;
cout << v << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << "Enter 4 coefficients for a degree 3 polynomial" << endl;
Polynomial a = inputPolynomial(3);
cout << "Your Polynomial is:" << endl;
printPolynomial(a);
cout << "Now enter 3 coefficients for another degree 2 polynomial" << endl;
Polynomial b = inputPolynomial(2);
cout << "Your Polynomial is:" << endl;
printPolynomial(b);
cout<<endl;
Polynomial c; // after this line total garbage
c = addPolynomials(a, b);
cout << "Sum of above two polynomials is" << endl;
printPolynomial(c);
return 0;
}