-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 4.cpp
More file actions
110 lines (108 loc) · 2.08 KB
/
Assignment 4.cpp
File metadata and controls
110 lines (108 loc) · 2.08 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
//============================================================================
// Name : Quadratic.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <math.h>
using namespace std;
class Quadratic
{
private: int a,b,c;
public:
Quadratic(int a1=0, int b1=0, int c1=0)
{
a=a1;
b=b1;
c=c1;
}
Quadratic operator + (Quadratic const &obj)
{
Quadratic res;
res.a=a+obj.a;
res.b=b+obj.b;
res.c=c+obj.c;
return res;
}
void eval()
{
int m,f;
cout<<"Enter the value of x : ";
cin>>f;
m=a*f*f+b*f+c;
cout<<m;
}
void root()
{
double m1,m2,t;
t=b*b-4*a*c;
if(t>=0)
{
m1=(-b+sqrt(t))/2*a;
m2=(-b-sqrt(t))/2*a;
cout<<"roots : "<<endl<<m1<<endl<<m2<<endl;
}
else
cout<<"imaginary roots"<<endl;
}
friend ostream &operator << (ostream &o, Quadratic &c)
{
o<<c.a<<"x^2+"<<c.b<<"x+"<<c.c;
return o;
}
friend istream &operator >> (istream &o, Quadratic &c)
{
cout<<"Enter a : ";
o>>c.a;
cout<<"Enter b : ";
o>>c.b;
cout<<"Enter c : ";
o>>c.c;
}
};
int main() {
int o,f;
char e;
Quadratic p1,p2,p3;
cout<<"Enter polynomial 1 :"<<endl;
cin>>p1;
cout<<"Polynomial 1 : "<<p1<<endl;
cout<<"Enter polynomial 2 :"<<endl;
cin>>p2;
cout<<"Polynomial 2 : "<<p2<<endl;
do
{
cout<<"Which operation would you like to perform?\n1.Addition\n2.Finding the value of the polynomials for any x\n3.Finding the roots of the polynomials\n";
cin>>o;
switch(o)
{
case 1 :
p3=p1+p2;
cout<<"Addition is : "<<p3<<endl;
break;
case 2 :
cout<<"The value of polynomial 1 is : ";
p1.eval();
cout<<endl;
cout<<"The value of polynomial 2 is : ";
p2.eval();
cout<<endl;
break;
case 3 :
cout<<"Polynomial 1 has ";
p1.root();
cout<<"Polynomial 2 has ";
p2.root();
break;
default : cout<<"Please enter a valid choice"<<endl;
}
cout<<"Would you like to perform another operation?"<<endl;
cin>>e;
}
while(e=='Y');
if(e=='N')
cout<<"Thank you for using the program!";
return 0;
}