-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment-4.cpp
More file actions
119 lines (113 loc) · 2.24 KB
/
Copy pathassignment-4.cpp
File metadata and controls
119 lines (113 loc) · 2.24 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
//author:Tejas joshi
//date: 03/08/2017
#include<iostream>
#include<math.h>
using namespace std;
class polynomial
{
float a,b,c;
public:
polynomial(){}
polynomial(float x,float y,float z)
{
a=x;
b=y;
c=z;
}
friend istream & operator>>(istream &i,polynomial &x);
friend ostream & operator<<(ostream &o,polynomial &x);
polynomial operator+(polynomial x)
{
polynomial p;
p.a=a+x.a;
p.b=b+x.b;
p.c=c+x.c;
return p;
}
polynomial operator-(polynomial x)
{
polynomial p;
p.a=a-x.a;
p.b=b-x.b;
p.c=c-x.c;
return p;
}
void root()
{
int y,z;
if((b*b-4*a*c)>=0)
{
y=-b/(2*a) + sqrt(b*b-4*a*c)/(2*a);
z=-b/(2*a) - sqrt(b*b-4*a*c)/(2*a);
cout<<"one root is:"<<y<<endl<<"second root is:"<<z<<endl;
}
else if((b*b-4*a*c)<0)
{
cout<<"as discriminent of this polynomial is negeative, roots are imaginary.\n";
cout<<"one root is:"<<-b/(2*a)<<" + i"<<sqrt(-(b*b-4*a*c))/(2*a)<<endl;
cout<<"second root is:"<<-b/(2*a)<<" - i"<<sqrt(-(b*b-4*a*c))/(2*a)<<endl;
}
}
void eval(int num)
{
cout<<num*num*a+num*b+c<<endl;
}
};
istream & operator>>(istream &i,polynomial &x)
{
cin>>x.a>>x.b>>x.c;
return i;
}
ostream & operator<<(ostream &o,polynomial &x)
{
cout<<x.a<<"x^2+"<<x.b<<"x+"<<x.c;
return o;
}
int main()
{
int ch;
polynomial A(0.0,0.0,0.0),B(0.0,0.0,0.0),C,D;
cout<<"enter first polynomial's coefficients.\n";
cin>>A;
cout<<"you entered polynomial:"<<A<<endl;
cout<<"enter second polynomial's coefficients.\n";
cin>>B;
cout<<"you entered polynomial:"<<B<<endl;
cout<<"enter your choice\n1. Addition\n2. Subtraction\n3. Evaluation\n4. Roots\n";
cin>>ch;
switch (ch)
{
case 1:{
cout<<"addition of these polynomials is:\n";
C=A+B;
cout<<C<<endl;
}
break;
case 2:{
cout<<"Subtraction is:\n";
D=A-B;
cout<<D<<endl;
}
break;
case 3:{
int num;
cout<<"enter number to evaluate the expression.\n";
cin>>num;
cout<<"envaluation of first polynomial:";
A.eval(num);
cout<<"envaluation of second polynomial:";
B.eval(num);
}
break;
case 4:{
cout<<"roots of first polynomial are:\n";
A.root();
cout<<"roots of second polynomial are:\n";
B.root();
}
break;
default : cout<<"invalid choice.\n";
break;
}
return 0;
}