Created
December 5, 2011 17:54
-
-
Save paulbooth/1434561 to your computer and use it in GitHub Desktop.
Factor a trinomial in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Paul Booth | |
//June 01, 2009 | |
//Programming 3 final program | |
//Trinomial factoring program | |
//inputs a trinomial, displays factored form | |
#include <iostream> | |
#include <windows.h> | |
using namespace std; | |
int bestfactor(int n); | |
void getInput(); | |
void displayOutput(); | |
void reduce (int &num1, int &num2); | |
void displayGraph(); | |
void setTextColor(int fg,int bg); | |
void cycleColors(); | |
int a,b,c,//trinomial parameters | |
term1,term2,coef1, coef2, //factored answer parameters | |
fgindex=2, bgindex=10;//color code numbers for (f)ore(g)round and (b)ack(g)round-will change | |
//graph settings2 | |
const int graphWidth=79, graphHeight=40; | |
const double errorRange=4;//thickness of graph | |
const char hit=char(219), //char of function | |
miss=' ';//char of background | |
const int graphfg=13,//color of funct. | |
graphbg=0;//color of background | |
void getInput(){//gets input | |
cycleColors(); | |
cout<<"trinomial of form Ax^2+Bx+C\n"; | |
cycleColors(); | |
cout << "enter in A (nonzero):" ; | |
cin>>a; | |
cycleColors(); | |
cout << "enter in B:" ; | |
cin>>b; | |
cycleColors(); | |
cout << "enter in C:" ; | |
cin>>c; | |
cycleColors(); | |
} | |
void displayOutput(){//displays answer | |
cycleColors(); | |
cout<<"("; | |
if (coef1!=1) | |
cout<<coef1; | |
cout<<"X"<<((term1>=0)?"+":"")<<term1<<")"; | |
cout<<"("; | |
if(coef2!=1) | |
cout<<coef2; | |
cout<<"X"<<((term2>=0)?"+":"")<<term2<<")"<<endl; | |
displayGraph(); | |
} | |
void displayGraph(){//displays graph centered at vertex | |
setTextColor(graphfg,graphbg); | |
for(int y=graphHeight/2;y>=-graphHeight/2;y--){ | |
for(int x=-b/(2*a)-graphWidth/2;x<=-b/(2*a)+graphWidth/2;x++){ | |
if(x==0&&y==0){ | |
cout<<char(197); | |
} | |
else if (x==0){ | |
cout<<char(179); | |
} | |
else if(y==0){ | |
cout<<char(196); | |
} | |
else | |
if (abs(x*x*a+b*x+c-y)>errorRange){ | |
cout<<miss; | |
} | |
else { | |
cout<<hit; | |
} | |
} | |
cout<<endl; | |
} | |
cycleColors(); | |
} | |
int main( void ) | |
{ | |
getInput(); | |
//input as ax^2+bx+c | |
//put answer in ((coef1)x+term1)((coef2)x+term2) form | |
coef2=coef1=a; | |
cout<<a<<endl; | |
term1=bestfactor(a*c);//first factor | |
cout<<term1<<endl; | |
if(term1!=0) | |
term2=(a*c)/term1;//second factor | |
else{ | |
term2=b; | |
coef1=1; | |
} | |
reduce(coef1,term1);//make sure it can't be simplified | |
reduce(coef2,term2); | |
displayOutput(); | |
return 0; | |
} | |
void reduce (int &num1, int &num2){//reduces a pair of numbers -removes common factors | |
for(int i=2;i<=min(abs(num1),abs(num2));i++){ | |
if((num1%i==0)&&(num2%i==0)) | |
{ | |
num1/=i; num2/=i; | |
} | |
} | |
} | |
int bestfactor(int n){//finds factors of n that add to b in equation- returns first one | |
for(int i=1;i<=abs(n);i++){ | |
if (n%i==0){ | |
int comp=n/i; | |
if (comp+i==b) | |
return i; | |
} | |
} | |
for(int i=-1;i>=-abs(n);i--){ | |
if (n%i==0){ | |
int comp=n/i; | |
if (comp+i==b) | |
return i; | |
} | |
} | |
return 0; | |
} | |
void setTextColor(int fg,int bg){//sets forground and background text colors | |
HANDLE hconsole= GetStdHandle(STD_OUTPUT_HANDLE); | |
SetConsoleTextAttribute(hconsole, fg|bg<<4); | |
} | |
void cycleColors(){//changes forground and background text colors | |
fgindex++; bgindex++; | |
setTextColor(fgindex,bgindex); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment