15题给个C++的程序,谢谢
1个回答
展开全部
代码如下:
#include<iostream>
#include<cmath>
using namespace std;
int main() {
double dbLeftX = -10.0; double dbRightX = 10.0;
double dbResultY = 0.0;
double dbTmp = 0.0;
double dbMidleX =0.0;
while ( abs(dbRightX-dbLeftX) > 1e-7 )
{
dbMidleX = ( dbLeftX + dbRightX) / 2.0;
dbResultY = 2 * pow(dbMidleX,3) - 4 * pow(dbMidleX,2) + 3 * dbMidleX - 6;
if ( abs(dbResultY) < 0.1e-6 )
{
printf("Finde a root in [-10,10] , value = [%lf].\n",dbMidleX);
}
if ( dbResultY < 0.0 )
{
dbTmp = 2 * pow(dbLeftX,3) - 4 * pow(dbLeftX,2) + 3 * dbLeftX - 6;
if ( dbTmp < 0.0 ) { dbLeftX = dbMidleX; } else { dbRightX = dbMidleX; }
}
else { dbTmp = 2 * pow(dbLeftX,3) - 4 * pow(dbLeftX,2) + 3 * dbLeftX - 6; if ( dbTmp > 0.0 )
{
dbLeftX = dbMidleX; }
else { dbRightX = dbMidleX; }
}
}
if( 2 * pow(dbLeftX,3) - 4 * pow(dbLeftX,2) + 3 * dbLeftX - 6 < 0 && 2 * pow(dbRightX,3) - 4 * pow(dbRightX,2) + 3 * dbRightX - 6 >0 )
{
cout<<"Finde a root in [-10,10] ,value = "<<dbMidleX<<endl;
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询