include <iostream> include <cmath> 调用函数或主调函数 using namespace std; int maino t cout < Enter Quadratic coefficients: I double a, b, ci cin >>b>>c 被调函数 i((a1=0)&6(b*一4*a*。>0) 库函数 double radical sqrt(b*b- 4*a*c) double roo七1 C-b radical) (2*a); double root2 =(-b- radical)/(2*a) cout<< Roots: < root < n < root2 else cout < Does not have two real roots return 0: 2021/2/24
2021/2/24 -6- #include <iostream> #include <cmath> using namespace std; int main() { cout << "Enter Quadratic coefficients: "; double a, b, c; cin >> a >> b >> c; if ( (a != 0) && (b*b - 4*a*c > 0) ) { double radical = sqrt(b*b - 4*a*c); double root1 = (-b + radical) / (2*a); double root2 = (-b - radical) / (2*a); cout << "Roots: " << root1 << " " << root2; } else { cout << "Does not have two real roots"; } return 0; } 调用函数 或主调函数 被调函数 库函数
include <iostream> using namespace std; float CircleArea(float r) main(: manage circle computation int maino i cout < Enter radius float My Radius i cin > MyRadius float Area CircleArea(MyRadius)i cout < circle has area < Areai return 0 CircleArea(): compute area of radius r circle float CircleArea(float r)( const float Pi =3.1415: return pi★x★x; 2021/2/24 自定义函数
2021/2/24 -7- #include <iostream> using namespace std; float CircleArea(float r); // main(): manage circle computation int main() { cout << "Enter radius: "; float MyRadius; cin >> MyRadius; float Area = CircleArea(MyRadius); cout << "Circle has area " << Area; return 0; } // CircleArea(): compute area of radius r circle float CircleArea(float r) { const float Pi = 3.1415; return Pi * r * r; } 自定义函数
2.数学库函数 ◆C++语言提供的库函数中有一些是专门完成特定的 数学运算的,称为数学库函数 ◆实现常见的数学计算 例如:求绝对值、平方根等。 ◆调用数学函数:函数名(参数1,…参数n) 例如:cout<<sqrt(900.0) 2021/2/24
2021/2/24 -8- 2.数学库函数 C++语言提供的库函数中有一些是专门完成特定的 数学运算的,称为数学库函数。 实现常见的数学计算 例如: 求绝对值、平方根等。 调用数学函数: 函数名(参数1,…,参数n) 例如: cout<<sqrt(900.0);
2.数学库函数 ◆数学函数库中的多数函数都返回 double类型结果 ◆使用数学库函数,需要在程序中包含math.h头文件, 这个头文件在新的C++标准库中称为 cmath。 ◆函数参数可取常量、变量或表达式 例:如果C=13.0、d=3.0和f=4.0,则下列语句 cout<<sgrt(c+d*f) 计算并显示13.0+3.0*4.0=25.0的平方根,即5.0。 2021/2/24
2021/2/24 -9- 2.数学库函数 数学函数库中的多数函数都返回double类型结果。 使用数学库函数,需要在程序中包含math.h头文件, 这个头文件在新的C++标准库中称为cmath。 函数参数可取常量、变量或表达式。 例: 如果c=13.0、d=3.0和f=4.0,则下列语句: cout<<sqrt(c+d*f); 计算并显示13.0+3.0*4.0=25.0的平方根,即5.0
4.2函数定义
4.2 函数定义