/*1. Nur hasanah Binti Sariddon				(B19EC0033)
  2. Mohd Anas Bin Adnan 					(B19EC0043)
  3. NG JING ER 							(A19EC0115)
  4. Nursyafiqah Binti Abdul Halim 			(A19EC3025)
  5. Roshandev Daniel a/l Sukhdev Singh 	(A19EC0156)
*/
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

double gradient(double aa, double bb, double c,double x)
{
	double gradient;
	gradient = ((aa)*pow(x,2))+(bb*x)+c;
	cout << endl<<"Gradient= " << gradient << endl;;
	return gradient;	//gradient
}

double tangent(double x,double y, double m)
{
	double t=y-(m*x);
	if(t<0)
		cout << "Equation of tangent: " << "y= " << m << "x - " << -t << endl;
	else
		cout << "Equation of tangent: " << "y= " << m << "x + " << t << endl;
	return t;
}

double stationary(double aa,double bb,double c,double d,double a, double b)
{
	double dis, ima, real, x1, x2, y1, y2;
	dis=pow(bb,2)-(4*aa*c);
	if(dis>0)
			{
				x1=(-bb+sqrt(dis))/(2*aa);
				x2=(-bb-sqrt(dis))/(2*aa);
				y1=(a*pow(x1,3))+(b*pow(x1,2))+(c*x1)+d;
				y2=(a*pow(x2,3))+(b*pow(x2,2))+(c*x2)+d; 
				cout << "Stationary Point: (" << x1 << ", " << y1 << "), " << "(" << x2 << ", " << y2 << ")" <<endl;
			}
			else if(dis<0)
			{
				real=-bb/(2*aa);
				ima=sqrt(-dis)/(2*aa);
				cout << "Roots are complex and different."  << endl;
		        cout << "x1 = " << real << "+" << ima << "i" << endl;
		        cout << "x2 = " << real << "-" << ima << "i" << endl;
			}
			else
			{
				cout << "Roots are real and same." << endl;
		        x1 = (-b + sqrt(dis)) / (2*a);
		        y1=((aa)*pow(x1,2))+(bb*x1)+c;
		        cout << "x1 = x2 =" << x1 << endl;
		        cout << "Stationary Point: (" << x1 << ", " << y1 << ") " <<endl;
			}
}

int main()
{
	double a,b,c,d,fx,x,y,r,m,cc,x1,x2,y1,y2,aa,bb,dis,ima,real;
	cout << "This is a program to calculate differentiation of the non-linear equations." << endl;
	cout << "f(x)= ax^3 + bx^2 + cx + d" << endl;
	do
	{
		cout << "You need to enter value a, b, c and d." << endl;
		cout << "a= ";
		cin >> a;
		cout << "b= ";
		cin >> b;
		cout << "c= ";
		cin >> c;
		cout << "d= ";
		cin >> d;
		if(a==0)
			cout << "Error! Value of 'a' cannot be 0." << endl <<endl;
	}while(a==0);
	cout << endl<<"You need to enter point P (x,y)." << endl;
	do
	{
		cout << "x= ";
		cin >> x;
		cout << "y= ";
		cin >> y;
		r=(a*pow(x,3))+(b*pow(x,2))+(c*x)+d;
		if(r!=y)
			cout << "ERROR!! The point that you enter must be on the curve. Please enter again.";
		else
		{	
			aa=a*3;
			bb=b*2;
			if(b<0 && c<0 && d<0)
			{
				cout << "\nf(x)= " << a << "x^3 - " << -b << "x^2 - " << -c << "x - " << -d <<"\nP(" << x << ", " << y << ")" << endl;
				cout << "\nf(x)'= " << aa << "x^2 - " << -bb << "x - " << -c;
			}
			else if(b<0 && c<0)
			{
				cout << "\nf(x)= " << a << "x^3 -" << -b << "x^2 - " << -c << "x + " << d <<"\nP(" << x << ", " << y << ")" << endl;
				cout << "\nf(x)'= " << aa << "x^2 - " << -bb << "x -" << -c;
			}
			else if(b<0 && d<0)
			{
				cout << "\nf(x)= " << a << "x^3 - " << -b << "x^2 + " << c << "x - " << -d <<"\nP(" << x << ", " << y << ")" << endl;
				cout << "\nf(x)'= " << aa << "x^2 - " << -bb << "x + " << c;
			}
			else if (c<0 && d<0)
			{
				cout << "\nf(x)= " << a << "x^3 + " << b << "x^2 - " << -c << "x - " << -d <<"\nP(" << x << ", " << y << ")" << endl;
				cout << "\nf(x)'= " << aa << "x^2 + " << bb << "x - " << -c;
			}
			else if(b<0)
			{
				cout << "\nf(x)= " << a << "x^3 - " << -b << "x^2 + " << c << "x + " << d <<"\nP(" << x << ", " << y << ")" << endl;
				cout << "\nf(x)'= " << aa << "x^2 - " << -bb << "x + " << c;
			}
			else if(c<0)
			{
				cout << "\nf(x)= " << a << "x^3 + " << b << "x^2 - " << -c << "x + " << d <<"\nP(" << x << ", " << y << ")" << endl;
				cout << "\nf(x)'= " << aa << "x^2 + " << bb << "x - " << -c;
			}
			else if(d<0)
			{
				cout << "\nf(x)= " << a << "x^3 + " << b << "x^2 + " << c << "x - " << -d <<"\nP(" << x << ", " << y << ")" << endl;
				cout << "\nf(x)'= " << aa << "x^2 + " << bb << "x + " << c;
			}
			else
			{
				cout << "\nf(x)= " << a << "x^3 + " << b << "x^2 + " << c << "x + " << d <<"\nP(" << x << ", " << y << ")" << endl;
				cout << "\nf(x)'= " << aa << "x^2 + " << bb << "x + " << c;
			}
			m=gradient(aa,bb,c,x);
			//y=mx+cc equation of tangent
			cc=tangent(x,y,m);
			//cout << "\nf(x)= " << a << "x^3 + " << b << "x^2 + " << c << "x + " << d <<"\nP(" << x << ", " << y << ")" << endl;
			//cout << "\nf(x)'= " << aa << "x^2 + " << bb << "x + " << c;
			stationary(aa,bb,c,d,a,b);
		}
		
	}while(r!=y);
	
	return 0;
}
