#include <iostream>
#include <cmath> // 引入数学库
// 定义一个计算平方的函数
double square(double num) {
return num * num; // 返回num的平方
}
int main() {
double number = 5.0;
std::cout << "The square of " << number << " is " << square(number) << std::endl;
// 使用cmath库中的pow函数计算平方
double squaredUsingPow = pow(number, 2);
std::cout << "The square of " << number << " using pow is " << squaredUsingPow << std::endl;
return 0;
}
#include <iostream>
用于输入输出操作,#include <cmath>
用于使用数学函数(如 pow
)。square
函数接受一个 double
类型的参数并返回其平方值。main
函数中,定义了一个变量 number
并调用 square
函数计算其平方。此外,还展示了如何使用 cmath
库中的 pow
函数来计算平方。std::cout
输出计算结果。如果你需要更详细的解释或其他功能,请告诉我!
上一篇:c++ 菜鸟
下一篇:c++ filesystem
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站