#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<iomanip>

using namespace std;

void createQuadEqn(double*,double*,double*);
void createCubicEqn(double*,double*,double*,double*);
void fixedPointQuad(double,double,double);
void fixedPointCube(double,double,double,double);

int main(){

int i;
char equationType;
double a,b,c,d;
double coefficient_a,coefficient_b,coefficient_c,coefficient_d;

cout << "Press ANY NUMBER to START.......Press 0 to STOP : ";
cin >> i;

while(i!=0){
   cout << "\n\nPlease enter which type of Equation you want to solve" << endl;
   cout << "Press q for Quadratic equation" << endl;
   cout << "Press c for cubic equation " << endl;
   cin >> equationType; 
   
   switch(equationType){
   
   case  'q':
   createQuadEqn(&coefficient_a,&coefficient_b,&coefficient_c);
  
   a=coefficient_a;
   b=coefficient_b;
   c=coefficient_c;
   
   fixedPointQuad(a,b,c);
   
   break;
   
   case  'c':
   createCubicEqn(&coefficient_a,&coefficient_b,&coefficient_c,&coefficient_d);	
   
   a=coefficient_a;
   b=coefficient_b;
   c=coefficient_c;
   d=coefficient_d;
   
   fixedPointCube(a,b,c,d);
   
   break;
   
   default :	
   cout << "Wrong character was entered" << endl;
   cout << "Please reenter again" << endl;
   break;
   }
   
cout << "\nPress ANY NUMBER to CONTINUE.......Press ANY NUMBER to STOP : ";
cin >> i;
}
cout << "\n END!!!";

return 0;
}


void createQuadEqn(double *coefficient_a,double *coefficient_b,double *coefficient_c){
	int createEqn;
	double a,b,c,discriminant;
	
	cout << "\nCreate a quadratic equation" << endl;
	cout << "Press 0 for random creating" << endl;
	cout << "Press ANY NUMBER for self creating" << endl;
	cin >> createEqn;
	
	if(createEqn==0){
		srand(time(NULL));
		
		a = rand() % 5 + 1;
        b = -1*(rand() % 50 + 1);
        c = rand() % 5 + 1;
	    
	    cout << "\nThis is a program to create a simple quadratic equation"<< endl;
	    cout << "The coefficients a, b and c are created ramdomly"<< endl;
	    cout << "\t aX^2 + bX + C = 0" << endl;
	    cout << "The coefficient a : " << a << endl;
	    cout << "The coefficient b : " << b << endl;
	    cout << "The coefficient c : " << c << endl;
	    cout << "Equation - " << a << "X^2 + " << b << "X + " << c << " =0"<<endl;
	    }
	
	else{
		cout << "\nThis is a program to create a simple quadratic equation"<<endl; 
		cout << "The coefficients a, b and c are entered by user"<< endl;
		cout << "\t aX^2 + bX + C = 0" << endl;
        cout << "Please enter the coefficient of a, b and c of equation accordingly" << endl;	
	    cin  >> a >> b >> c; 
	    
	    cout << "The coefficient a : " << a << endl;
	    cout << "The coefficient b : " << b << endl;
	    cout << "The coefficient c : " << c << endl;
	    cout << "Equation - " << a << "X^2 + " << b << "X + " << c << " =0"<<endl;
	}    
      
	  discriminant = pow(b,2) - 4*a*c;
      
	  if(discriminant == 0)
        cout << "\nb^2 - 4*a*c = 0\nIt has one real root" << endl;
      
	  else if(discriminant > 0 )
        cout << "\nb^2 - 4*a*c > 0\nIt has two real roots" << endl;
      
	  else if(discriminant < 0)
        cout << "\nb^2 - 4*a*c < 0\nIt has no root"<< endl;
	  
	  *coefficient_a=a;
      *coefficient_b=b;
      *coefficient_c=c;
}



void createCubicEqn(double* coefficient_a,double* coefficient_b,double* coefficient_c,double* coefficient_d){
	int createEqn;
	double a,b,c,d,discreminant;
	
	cout << "\nCreate a Cubic equation" << endl;
	cout << "Press 0 for random creating" << endl;
	cout << "Press ANY NUMBER for self creating" << endl;
	cin >> createEqn;
	
	if(createEqn==0){
		srand(time(NULL));
		
		a = rand() % 5 + 1;
		b = -1*(rand() % 50 + 1);
        c = -1*(rand() % 50 + 1);
        d = rand() % 5 + 1;
	    
	    cout << "\nThis is a program to create a simple cubic equation"<< endl;
	    cout << "The coefficients a, b, c and d are created ramdomly"<< endl;
	    cout << "\t aX^3 + bX^2 + cX + d = 0" << endl;
	    cout << "The coefficient a : " << a << endl;
	    cout << "The coefficient b : " << b << endl;
	    cout << "The coefficient c : " << c << endl;
	    cout << "The coefficient d : " << d << endl;
		
		cout << "Equation - " << a << "X^3 + "<< b << "X^2 + " << c << "X + " << d << " =0"<<endl;
	    }
	
	else{
		cout << "\nThis is a program to create a simple cubic equation"<<endl;
		cout << "The coefficients a, b, c and d are entered by user"<< endl;
		cout << "\t aX^3 + bX^2 + cX + d = 0" << endl;
        cout << "Please enter the coefficient of a, b, c and d of equation accordingly" << endl;	
	    cin  >> a >> b >> c >> d; 
	    
	    cout << "The coefficient a : " << a << endl;
	    cout << "The coefficient b : " << b << endl;
	    cout << "The coefficient c : " << c << endl;
	    cout << "The coefficient d : " << d << endl;
	    cout << "Equation - " << a << "X^3 + "<< b << "X^2 + " << c << "X + " << d << " =0"<<endl;
	}    
      
	  discreminant= ((a*a)*(b*b)) + 18*a*b*c - 4*pow(b,3) - 4*pow(a,3)*c - 27*pow(c,2);
	  
	  if(discreminant==0)
	    cout << "\na^2*b^2 + 18*a*b*c - 4b^3 - 4a^3*c - 27*c^2 = 0\nIt has a multiple root" << endl;
	  
	  else if(discreminant > 0)
	    cout << "\na^2*b^2 + 18*a*b*c - 4b^3 - 4a^3*c - 27*c^2 > 0\nIt has three distinct real roots" << endl;
	  
	  else if(discreminant < 0)
	    cout << "\na^2*b^2 + 18*a*b*c - 4b^3 - 4a^3*c - 27*c^2 < 0\nIt has one real root and two distinct complex conjugate roots" << endl;
	  
	  *coefficient_a=a;
      *coefficient_b=b;
      *coefficient_c=c;
      *coefficient_d=d;
}

void fixedPointQuad(double a,double b,double c){
	int Nit = 15;
    int counter = 0;
    
    double root1,root2;
    
    cout << "\nEnter the initial value : ";
    cin >> root1;
    
    root2=root1;
    
    cout << "The initial value is : " << root1;
	cout << endl;
    
    cout << "Root finding by using fixed point iteration....."<< endl<< endl;
    
    cout << "Iteration    counter                root1          root2" << endl;
    cout << "----------------------------------------------------------"<<endl;
    
    for(counter=0;counter<=Nit;counter++){
    	root1 = -b - (c/(a*root1));
    	
    	root2= pow(-((b/a)*root2)-(c/a),0.5);
	
	    cout <<setw(5)<<"Iteration "<<setw(5)<<"("<<setw(2)<< counter <<setw(2)<<")" <<setw(13) << "x value: "<<setw(10)<< root1 <<setw(15)<< root2 << endl;
	}
}
    
void fixedPointCube(double a,double b,double c,double d){
	int Nit = 15;
    int counter = 0;
    
    double root1,root2,root3;
    
    cout << "\nEnter the initial value : ";
    cin >> root1;
    
    root1=root2=root3;
    
    cout << "The initial value is : " << root1;
	cout << endl;
    
    cout << "Root finding by using fixed point iteration....."<< endl<< endl;

    cout << "Iteration    counter                root1          root2        root3" << endl;
    cout << "------------------------------------------------------------------------"<<endl;
    
}
    	
    




