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
|
#include <iostream>
#include <cmath>
using namespace std;
void factorization(int a, int b, int c, int& u1, int& v1, bool& isFactorable);
int main() {
// Write your main here
int coeffofXsquared;
int coeffofX;
int constantTerm;
int u;
int v;
bool isPolynomialFactorable;
cout << "Enter the coefficient of x^2: ";
cin >> coeffofXsquared;
cout << endl;
cout << "Enter the coefficient of x: ";
cin >> coeffofX;
cout << endl;
cout << "Enter the constant term: ";
cin >> constantTerm;
cout << endl;
factorization(coeffofXsquared, coeffofX, constantTerm, u, v, isPolynomialFactorable);
if (isPolynomialFactorable)
{
if(coeffofXsquared > 0)
cout << coeffofXsquared << "x^2";
else if(coeffofXsquared < 0)
cout << "-" << abs(coeffofXsquared) << "x^2";
if (coeffofX > 0)
cout << "+" << coeffofX << "x";
else if (coeffofX < 0)
cout << "-" << abs(coeffofX) << "x";
if(constantTerm > 0)
cout << "+" << constantTerm;
else if (constantTerm < 0)
cout << "-" << abs(constantTerm);
cout << " = (x";
if (u > 0)
cout << "-" << u << ")(x";
else if (u < 0)
cout << "+" << u << ")(X";
if (v > 0)
cout << "-" << v << ")" << endl;
else if (v < 0)
cout << "+" << abs(v) << ")" << endl;
}
else
cout << "The polynomial is not factorable." << endl;
return 0;
}
void factorization(int a, int b, int c, int& u1, int& v1, bool& isFactorable){
double discriminant;
int temp;
isFactorable = true;
discriminant = b * b - 4 * c;
if (discriminant < 0)
isFactorable = false;
else
{
temp = static_cast<int>(sqrt(discriminant));
if (temp * temp != discriminant)
isFactorable = false;
else
{
if (((-b + temp) % 2 != 0) || ((-b - temp) % 2 != 0))
isFactorable = false;
else
{
u1 = (-b + temp) / 2;
v1 = (-b - temp) / 2;
}
}
}
}
|