-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuadraticPolynomial.cpp
More file actions
91 lines (88 loc) · 1.73 KB
/
QuadraticPolynomial.cpp
File metadata and controls
91 lines (88 loc) · 1.73 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
#include <iostream>
#include <cmath>
using namespace std;
class Quadratic
{
int a;
int b;
int c;
public:
Quadratic()
{
a=0;
b=0;
c=0;
}
Quadratic operator+(Quadratic x)
{
Quadratic temp;
temp.a = (a)+(x.a);
temp.b = (b)+(x.b);
temp.c = (c)+(x.c);
return temp;
}
friend ostream &operator<<(ostream &out, Quadratic &y)
{
out<<y.a<<"x^2 + "<<y.b<<"x + "<<y.c<<endl;
return out;
}
friend istream &operator>>(istream &in, Quadratic &y)
{
in>>y.a>>y.b>>y.c;
return in;
}
void eval(int v)
{
int ans = (a*v*v)+(b*v)+(c);
cout<<"The value of the entered expression when x = "<<v<<" is "<<ans<<endl;
}
void solution()
{
float ans1, ans2;
ans1 = ((-b) + sqrt(pow(b,2)-(4*a*c)))/(2*a);
ans2 = ((-b) - sqrt(pow(b,2)-(4*a*c)))/(2*a);
cout<<"The solutions are: "<<ans1<<" and "<<ans2<<endl;
}
};
int main()
{
Quadratic a, b, c;
int choice;
char ans;
do
{
cout<<"\n************* MENU ************\n";
cout<<"\n\t1.Addition\n\t2.Evaluate for x\n\t3.Solutions";
cout<<"\n\nEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the values of a, b and c in the polynomial."<<endl;
cin>>a;
cin>>b;
c=a+b;
cout<<"\n\nAddition is: "<<c;
break;
case 2:
cout<<"Enter the values of a, b and c in the polynomial."<<endl;
cin>>a;
int v;
cout<<"Enter the value of x: ";
cin>>v;
a.eval(v);
break;
case 3:
cout<<"Enter the values of a, b and c in the polynomial."<<endl;
cin>>a;
a.solution();
break;
default:
cout<<"\nWrong choice";
}
cout<<"\nDo you want to continue?(y/n): ";
cin>>ans;
}
while(ans=='y' || ans=='Y');
return 0;
}