#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
usingnamespace std;
int egsit = false;
int a, b, c, A, B, C, A2, ans1, ans2;
char title[] = "Factor Trinomials";
char formula[] = "ax^2 + bx + c";
void centerstring(char* string)
{
int l = strlen(string);
int pos = (int)((80 - l) / 2);
for (int i = 0; i < pos; i++)
cout << " ";
cout << string;
}
int gcd(int a, int b)
{
while(1)
{
c = a % b;
if (c == 0)
return b;
a = b;
b = c;
}
}
int main()
{
while (egsit == false)
{
centerstring(title);
cout << "\n";
centerstring(formula);
cout << "\nA = ";
cin >> A;
cout << "B = ";
cin >> B;
cout << "C = ";
cin >> C;
ans1 = B + (sqrt( (B)^2 - (4 *A * C)));
ans2 = B - (sqrt( (B)^2 - (4 *A * C)));
a *= 2;
gcd(A, ans1);
A /= b;
ans1 /= b;
gcd(A, ans2);
A2 /= b;
ans2 /= b;
cout << "Answer is:\n(" << A << "x";
if (ans1 < 0)
{
ans1 /= -1;
cout << " - ";
}http://www.google.com/
else
cout << " + ";
cout << ans1 << ")(" << A2 << "x";
if (ans2 < 0)
{
ans2 /= -1;
cout << " - ";
}
else
cout << " + ";
cout << ans2 << ")";
do
{
cout << "\n\nFactor another? [y/n] ";
cin >> egsit;
if (egsit == 'y' | egsit == 'Y')
egsit = false;
elseif (egsit == 'n' | egsit == 'N')
egsit = true;
else
egsit = 2;
} while (egsit == 2);
cout << "\x1b[2J\x1b[H" << flush;
}
return 0;
}
When I compile it outputs:
1 2 3
temp.cpp:48: warning: converting to ‘int’ from ‘double’
temp.cpp:49: warning: converting to ‘int’ from ‘double’
Compilation finished successfully.
Which is weird because ans1 and ans2 are declared as int's. And If I use the example trinomial on the website, ans1 should equal 20 and ans2 should equal -24.
When I run the example trinomial on the website, I input A, B and C exactly as said and it outputs:
-- The logical OR operator is ||. | is the bitwise OR operator.
-- ^ is the bitwise XOR operator.
-- sqrt functions accepts float point numbers not ints. Thats why you are getting warnings.