শুক্রবার, ৮ এপ্রিল, ২০১৬

Quadratic Equation Solver : Lab Assignment


  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
#include <iostream>
#include <math.h>
#define sqr(x) (x*x)
using namespace std;
class QuadraticEquation{
private:
    double a;
    double b;
    double c;
    double root1;
    double root2;
    double imaginaryRootReal;
    double imaginaryRootImg;
    bool isImaginaryroot;


public:
    QuadraticEquation();
    void setCoefficient(double a,double b,double c);
    void solve();
    bool isImaginary();
    double getRoot1();
    double getRoot2();
    double ImagReal();
    double ImagImg();


};
QuadraticEquation::QuadraticEquation()
{
    cout<<"Quadratic Equation is represented in the form aX^2 + bX + c = 0 ."<<endl;
    isImaginaryroot = false;

}
void QuadraticEquation::setCoefficient(double a,double b,double c)
{
    this->a  =a;
    this->b = b;
    this-> c = c;
}
void QuadraticEquation::solve()
{
double DETERMINANT;
DETERMINANT = sqr(b) - 4*a*c;
if(DETERMINANT<0)
{
    isImaginaryroot = true;
    DETERMINANT*=-1;
    imaginaryRootReal = -b/(2*a) ;
    imaginaryRootImg = sqrt(DETERMINANT)/(2*a);
    return;

}

root1 = (b + sqrt(DETERMINANT) )/(2*a);
root2 = (b - sqrt(DETERMINANT) )/(2*a);
}
bool QuadraticEquation::isImaginary()
{
    if(isImaginaryroot)
        return true;
    return false;
}
double QuadraticEquation::getRoot1()
{
    return root1;
}
double QuadraticEquation::getRoot2()
{
    return root2;
}
double QuadraticEquation::ImagReal()
{
    return imaginaryRootReal;
}
double QuadraticEquation::ImagImg()
{
    return  imaginaryRootImg;
}
int main()
{
    QuadraticEquation quadEqn;
    double a,b,c;
    cin>>a>>b>>c;

    quadEqn.setCoefficient(a,b,c);
    quadEqn.solve();
   if(quadEqn.isImaginary())
    {
        cout<<"The imaginary roots are "<<quadEqn.ImagReal()<<" + "<<quadEqn.ImagImg()<<"i"<<endl;
        cout<<"and "<<quadEqn.ImagReal()<<" - "<<quadEqn.ImagImg()<<"i"<<endl;

    }
    else
    {
        cout<<"The real roots are "<<quadEqn.getRoot1()<<endl;
        cout<<" and "<<quadEqn.getRoot2()<<endl;
    }

    return 0;
}

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন