Advertisement
M_A_Tabarani

Tutor 4

Nov 11th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. class Quadratic{
  7.  
  8. public:
  9.     Quadratic(){}
  10.     Quadratic(double, double, double);
  11.     double overload () const;
  12.     double root() const;
  13.     double range (double, double, double) const;
  14.  
  15. private:
  16.     double a,b,c;
  17.  
  18. friend istream& operator>>(istream&, Quadratic&);
  19. friend ostream& operator<<(ostream&, const Quadratic&);
  20. };
  21.  
  22.  Quadratic::Quadratic(double A, double B, double C)
  23. {
  24.     a=A;
  25.     b=B;
  26.     c=C;
  27. }
  28.  
  29. double Quadratic::overload() const
  30. {
  31.     if (a == 0){
  32.         cout<<"The function is not quadratic"<<endl;
  33.         return 1;}
  34.     else
  35.         return 0;
  36. }
  37.  
  38. double Quadratic::root() const
  39. {
  40.     double x1,x2,determinant;
  41.     determinant= b*b - 4*a*c;
  42.  
  43.     if(determinant>0)
  44.     {
  45.         x1=(-b + sqrt(determinant))/(2*a);
  46.         x2=(-b - sqrt(determinant))/(2*a);
  47.         cout<<"The function has two real roots."<<endl;
  48.         cout<<"x1= "<<x1<<endl;
  49.         cout<<"x2= "<<x2<<endl;
  50.  
  51.     }
  52.     else if(determinant==0)
  53.     {
  54.         cout<<"The function has one real root."<<endl;
  55.         x1=(-b + sqrt(determinant))/(2*a);
  56.         cout<<"x1=x2= "<<x1<<endl;
  57.     }
  58.     else
  59.     {
  60.         cout<<"The function has no real root."<<endl;
  61.     }
  62.     return 0;
  63. }
  64.  
  65. double Quadratic :: range (double left, double right, double inc) const
  66. {
  67.     double i, v;
  68.     for(i=left; i<=right;i+=inc)
  69.     {
  70.         v = a*pow(i,2) + b*i + c;
  71.         cout<<v<<" ";
  72.     }
  73.     return 0;
  74.  
  75. }
  76.  
  77. istream& operator>>(istream& is, Quadratic& val)
  78. {
  79.     is>>val.a>>val.b>>val.c;
  80.     return is;
  81. }
  82.  
  83. ostream& operator<<(ostream& os, const Quadratic& val)
  84. {
  85.     if(val.a<0)
  86.         os<<"f(x)="<<showpos<<val.a<<"x^2 "<<val.b<<"x "<<val.c<<noshowpos<<endl;
  87.     else
  88.         os<<"f(x)="<<val.a<<"x^2 "<<showpos<<val.b<<"x "<<val.c<<noshowpos<<endl;
  89.  
  90.     return os;
  91. }
  92.  
  93. int main()
  94. {
  95.     Quadratic coord;
  96.     double left,right,inc;
  97.  
  98.     cout<<"Insert the value for a, b and c. Separate by pressing space bar."<<endl;
  99.     cout<<"=> ";
  100.     cin>>coord;
  101.  
  102.     if (coord.overload() !=1)
  103.     {
  104.         cout<<coord<<endl<<endl;
  105.         cout<<"Input the range of x, starting with the left interval, right interval\nand increment to find the value of f(x) => ";
  106.         cin>>left>>right>>inc;
  107.  
  108.         cout<<endl<<"The f(x) for "<<left<<"<x<"<<right<<" by increment of "<<inc<<" are => ";
  109.         coord.range(left,right,inc);
  110.         cout<<endl<<endl;
  111.         coord.root();
  112.  
  113.     }
  114.     return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement