#include<stdio.h>
#include<math.h>
#include<conio.h>
#include <iostream.h>

float f(float x)
{
	return x*x*x + 3*x - 2;
}

float fdx(float x)
{
	return 3*x*x +3;
}

float fd2x(float x)
{
	return 6*x;
}
float absolute(float x)
{
	return x < 0 ? -x : x;
}

void chordes(float a, float b, float eps)
{

	if(f(a)*f(b) < 0)
	{
		cout << "Chordes: a i b nepravilnye";
		return;
	}
	float x0 = a;
	float xf = b;

	if(f(a)*fd2x(a) > 0)
	{
		x0 = b;
		xf = a;
	}

	int step = 1;

	float x1 = x0 - f(x0)*(xf-x0)/(f(xf)-f(x0));


	while(absolute(x1-x0) > eps)
	{
		x0 = x1;
		x1 = x0 - f(x0)*(xf-x0)/(f(xf)-f(x0));
		step = step + 1;
	}

	cout << "Chordes: x: " << x1 << "\nf(x): " << f(x1) << "\nsteps:" << step;
}

void sek(float a, float b, float eps)
{

	if(f(a)*f(b) < 0)
	{
		cout << "Sekush: a i b nepravilnye";
		return;
	}
	float x0 = a;
	float x1 = a + eps;

	if(f(a)*fd2x(a) < 0)
	{
		x0 = b;
		x1 = b - eps;
	}

	int step = 1;

	float x = x1 - f(x1)*(x1 - x0)/(f(x1) - f(x0));

	x0 = x1;
	x1 = x;


	while(absolute(x1-x0) > eps)
	{
		x = x1 - f(x1)*(x1 - x0)/(f(x1) - f(x0));
		x0 = x1;
		x1 = x;
		step = step + 1;
	}

	cout << "Sek: x: " << x1 << "\nf(x): " << f(x1) << "\nsteps:" << step;
}

void kasat(float a, float b, float eps)
{

	if(f(a)*f(b) < 0)
	{
		cout << "Sekush: a i b nepravilnye";
		return;
	}
	float x0 = a;

	if(f(a)*fd2x(a) < 0)
	{
		x0 = b;
	}

	float x1 = x0 - f(x0)/fdx(x0);

	while(absolute(x1-x0) > eps)
	{
		x0 = x1;
		x1 = x0 - f(x0)/fdx(x0);
	}

	cout << "Kasat: x: " << x1 << "\nf(x): " << f(x1) << "\nsteps:" << step;
}

void main()
{
	float a, b, eps;
	cout << "Vvedite a, b, epsilon: ";
	cin >> a >> b >> eps;

	chordes(a, b, eps);
	sek(a, b, eps);
	//kasat(a, b, eps);

}// end main()
