include <iostream> include <cmath> 调用函数或主调函数 using name space stdi int maino cout < " Enter quadratic coefficients: I double a b, ci cin>> a>>b>>c 被调函数 if((a!=0)&&(b*b-4*a*c>0) 库函数 double radical= sqrt(b*b-4*a*c); double root =(-b radical 2*a); double root (-b radical) (2*a); cout < Roots: < root < ll<< root2 else cout < Does not have two real roots return 0 }2005428 北京邮电大学电信工程学院计算机技术中心
2005-4-28 北京邮电大学电信工程学院计算机技术中心 -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 stdi float CircleArea(float r / main(): manage circle computation int maino i cout < Enter radius: i float MyRadiusi cin > MyRadius i float Area CircleArea(My Radius)i cout < circle has area l << Area return o / CircleArea(): compute area of radius r circle float CircleArea(float r)( const float pi =3.1415 return Pi*x★; 2005-4-28 自定义函数信程学院计算机技术中心
2005-4-28 北京邮电大学电信工程学院计算机技术中心 -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); 2005-4-28 北京邮电大学电信工程学院计算机技术中心
2005-4-28 北京邮电大学电信工程学院计算机技术中心 -8- 2.数学库函数 C++语言提供的库函数中有一些是专门完成特定的数 学运算的,称为数学库函数。 实现常见的数学计算 例如: 求绝对值、平方根等。 调用数学函数: 函数名(参数1,…,参数n) 例如: cout<<sqrt(900.0);
2.数学库函数 ◆数学函数库中的多数函数都返回 double类型结果 ◆使用数学库函数,需要在程序中包含math.h头文 件,这个头文件在新的C++标准库中称为 cma th ◆函数参数可取常量、变量或表达式 例:如果C=13.0、d=3.0和f=4.0,则下列语句: cout<<sgrt(c+d*f) 计算并显示13.0+3.0*4.0=25.0的平方根,即5.0。 2005-4-28 北京邮电大学电信工程学院计算机技术中心
2005-4-28 北京邮电大学电信工程学院计算机技术中心 -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 函数定义