⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vc0302.cpp

📁 VC面向对象的学习教程
💻 CPP
字号:
// Example 3.2:用牛顿迭代公式求平方根
#include <iostream.h>			//基本输入输出库
#include <math.h>				//数学运算库
#define EPS   1.0e-10
// 函数 newton_sqrt(): 用牛顿迭代法求平方根
double newton_sqrt(double x)
{
		double x0, x1;
	x1 = 1.0;
		if(x>0.0)
	{
			do
			{
				x0 = x1;
			x1 = (x0+x/x0)/2;
			}while(fabs((x0-x1)/x1)>=EPS);
			return x1;
	}
		else
			return x;
}
    // 用于计算平方根的主函数
void main()
{
		double x, y;
	cout << "Please input the value : ";
		cin  >> x;
	y = newton_sqrt(x);
		if(y<0)
			cout << "Negative Value have not square root !" << endl;
	else
			cout << "The square root of " << x << " is " << y << endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -