用C/C++实现,通过,二分法求解表达式的值:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 int ZP_MAX = 1000; 5 string str; 6 7 double calc(double x) 8 { 9 return (5*x+6)*9-(2*x+3); 10 } 11 12 int main() 13 { 14 15 // 二分法, x1 = 0, x2 = 20 16 double x1 = -30.0, x2 = 30.0; 17 18 while(1) 19 { 20 double temp = (x1 + x2) / 2.0; 21 double ans = calc(temp) - 0; 22 23 if (fabs(ans) < 1e-5) 24 { 25 printf("%.2lf ", temp); 26 break; 27 } 28 if (ans > 0) 29 x2 = temp; 30 if (ans < 0) 31 x1 = temp; 32 } 33 return 0; 34 }