Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- class Quadratic{
- public:
- Quadratic(){}
- Quadratic(double, double, double);
- double overload () const;
- double root() const;
- double range (double, double, double) const;
- private:
- double a,b,c;
- friend istream& operator>>(istream&, Quadratic&);
- friend ostream& operator<<(ostream&, const Quadratic&);
- };
- Quadratic::Quadratic(double A, double B, double C)
- {
- a=A;
- b=B;
- c=C;
- }
- double Quadratic::overload() const
- {
- if (a == 0){
- cout<<"The function is not quadratic"<<endl;
- return 1;}
- else
- return 0;
- }
- double Quadratic::root() const
- {
- double x1,x2,determinant;
- determinant= b*b - 4*a*c;
- if(determinant>0)
- {
- x1=(-b + sqrt(determinant))/(2*a);
- x2=(-b - sqrt(determinant))/(2*a);
- cout<<"The function has two real roots."<<endl;
- cout<<"x1= "<<x1<<endl;
- cout<<"x2= "<<x2<<endl;
- }
- else if(determinant==0)
- {
- cout<<"The function has one real root."<<endl;
- x1=(-b + sqrt(determinant))/(2*a);
- cout<<"x1=x2= "<<x1<<endl;
- }
- else
- {
- cout<<"The function has no real root."<<endl;
- }
- return 0;
- }
- double Quadratic :: range (double left, double right, double inc) const
- {
- double i, v;
- for(i=left; i<=right;i+=inc)
- {
- v = a*pow(i,2) + b*i + c;
- cout<<v<<" ";
- }
- return 0;
- }
- istream& operator>>(istream& is, Quadratic& val)
- {
- is>>val.a>>val.b>>val.c;
- return is;
- }
- ostream& operator<<(ostream& os, const Quadratic& val)
- {
- if(val.a<0)
- os<<"f(x)="<<showpos<<val.a<<"x^2 "<<val.b<<"x "<<val.c<<noshowpos<<endl;
- else
- os<<"f(x)="<<val.a<<"x^2 "<<showpos<<val.b<<"x "<<val.c<<noshowpos<<endl;
- return os;
- }
- int main()
- {
- Quadratic coord;
- double left,right,inc;
- cout<<"Insert the value for a, b and c. Separate by pressing space bar."<<endl;
- cout<<"=> ";
- cin>>coord;
- if (coord.overload() !=1)
- {
- cout<<coord<<endl<<endl;
- cout<<"Input the range of x, starting with the left interval, right interval\nand increment to find the value of f(x) => ";
- cin>>left>>right>>inc;
- cout<<endl<<"The f(x) for "<<left<<"<x<"<<right<<" by increment of "<<inc<<" are => ";
- coord.range(left,right,inc);
- cout<<endl<<endl;
- coord.root();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement