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
|
#include <iostream>
using namespace std;
class quadratic
{
private:
int a;
int b;
int c;
public:
quadratic()
{
a=b=c=0;
}
quadratic(int A, int B, int C)
{
A=a;
B=b;
C=c;
}
int evaluate(int x)
{
int value = a*(x^2) + b*(x) + c;
return value;
}
void print()
{
cout << a << "x^2 + " << b << "x + " << c << endl;
}
int derivative(int X)
{
int deriv = 2*a*X + b;
return deriv;
}
int sum(double Parr[2], double Qarr[2])
{
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
if(i == j){
double s[] = Parr[i] + Qarr[j];
}
}
}
return s;
}
int delta ()
{
int Delta = b^2 - 4*a*c;
return Delta;
}
int realRoots(double &y, double &z)
{
int nbr_of_roots;
if(a != 0 && delta() >= 0){
nbr_of_roots = 2;
y = (- b + (delta()^(1/2))) / (2*a);
z = (- b - (delta()^(1/2))) / (2*a);
if(delta() == 0)
y = z = (- b + (delta()^(1/2)) ) / (2*a);
}
if(a != 0 && delta() < 0){
nbr_of_roots = 0;
}
if(a == 0 && b != 0) {
nbr_of_roots = 1;
y = -c/b;
}
if(a == 0 && b==0) {
if(c != 0)
nbr_of_roots = 0;
else nbr_of_roots = 999;
}
return nbr_of_roots;
}
};
int main()
{
quadratic p(1,5,3),q(1,2,1), r;
cout<<"p: ";p.print();
cout<<"q: ";q.print();
cout<<"r: ";r.print();
cout<<"p(2)="<<p.evaluate(2)<<endl;
r.sum(p,q);
cout<<"p+q: ";r.print();
cout<<"derivative of p for x=5: "<<p.derivative (5)<<endl;
double x1,x2;
int nbRoots = p.realRoots(x1,x2);
if (nbRoots ==2)
cout<<"roots of p : "<<x1<<","<<x2<<endl;
else if (nbRoots==1)
cout<<"root of p: "<<x1<<endl;
else if (nbRoots==0)
cout<<"No roots for p"<<endl;
else if (nbRoots=999)
cout<<"Infinetly many solutions for p"<<endl;
cout<<endl;
return 0;
}
|