29_函数的嵌套调用

来源:互联网 发布:ubuntu装锐捷修改文件 编辑:程序博客网 时间:2024/05/18 18:55
//_29_函数的嵌套调用//_29_main.cpp//使用弦截法求解方程的根#include <stdio.h>#include <stdlib.h>#include <math.h>//定义函数f,从而实现x^3-8*x^2+12*x-30=0float f(float);//定义函数xpoint,求出弦与x轴的焦点横坐标float xpoint(float,float);//定义函数root,求解区间(x1,x2)的实根float root(float,float);int main(){float x1,x2,f1,f2,x;do{printf("Please input x1,x2:\n");scanf("%f,%f",&x1,&x2);f1 = f(x1);f2 = f(x2);}while(f1*f2 > 0);x = root(x1,x2);printf("A root of equation is %9.6f\n",x);system("pause");return 0;}float f(float x){float y;y = (float)(((x-8.0)*x+12.0)*x-30.0);return y;}float xpoint(float x1,float x2){float x;x = (x1*f(x2) - x2*f(x1))/(f(x2)-f(x1));return x;}float root(float x1,float x2){float x,y;float y1 = f(x1);do{x = xpoint(x1,x2);y = f(x);if(y*y1 > 0){y1 = y;x1 = x;}else{x2 = x;}}while(fabs(y)>=0.0001);return x;}

0 0
原创粉丝点击