#include <stdio.h>
#include <math.h>

int main(void)
{
	float x0, x1, x2, y0, y1, y2;

	do
	{
		printf("Please input x1, x2: ");
		scanf("%f,%f", &x1, &x2);
		y1 = x1 * x1 * x1 - 3 * x1 * x1 + x1 + 1 = 0;
		y2 = x2 * x2 * x2 - 3 * x2 * x2 + x2 + 1 = 0;
	} while (y1*y2 > 0);			//输入x1, x2, 直到f(x1)与f(x2)异号
	
	printf("The root in [%f, %f] is:", x1, x2);

	do
	{
		x0 = (x1 + x2) / 2;			//二分
		y0 = x0 * x0 * x0 - 3 * x0 * x0 + x0 + 1;
		if (y0*y1 < 0)				//将求解空间缩小为[x1, x0]
		{
			x2 = x0;
			y2 = y0;
		}
		else				//将求解空间缩小为[x0, x2]
		{
			x1 = x0;
			y1 = y0;
		}
	} while (fabs(y0) >= 1.0e-6);

	printf("%f\n", x0);

	system("pause");
	return 0;
}