C++系统函数

一、系统函数的概念

C++ 不仅允许用户根据需要自定义函数,其系统库中还提供了几百个可供程序员直接使用的函数。例如求平方根的 sqrt、求绝对值的 abs、求三角函数值的 sin、cos、tan 等。

系统函数的原型声明以及函数体全部由系统提供,分类保存在不同的头文件中。程序员需要做的就是用 #include 指令嵌入相应的头文件,然后就可以直接调用系统函数。

二、头文件与系统函数的关系

不同的系统函数分布在不同的头文件中,常用的头文件包括:

头文件包含的主要函数
<cmath>数学函数:sqrt、pow、sin、cos、tan、abs、fabs、log、exp 等
<cstdlib>通用工具:rand、srand、atoi、exit、system 等
<cstring>字符串处理:strlen、strcpy、strcat、strcmp 等
<ctime>时间日期:time、clock 等
<iostream>输入输出:cin、cout、getline 等
<iomanip>格式化输出:setw、setprecision 等
<cctype>字符判断:isdigit、isalpha、isupper、toupper 等
<algorithm>算法函数:sort、find、reverse 等

注意:从标准 C 继承过来的头文件在 C++ 中命名为 c+原头文件名(去掉 .h),如 <cmath> 对应 C 语言的 <math.h>。

三、示例:求角度的正弦、余弦和正切值

需求:从键盘输入一个角度值(以度为单位),求出该角度的正弦值、余弦值和正切值。

思路:C++ 的三角函数 sin、cos、tan 接受的参数是弧度值,因此需要将角度转换为弧度。转换公式:弧度 = 角度 × π / 180。

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double angle;
    const double PI = 3.14159265358979323846;

    cout << "请输入角度值:";
    cin >> angle;

    // 角度转换为弧度
    double radian = angle * PI / 180.0;

    cout << angle << " 度的正弦值 sin = " << sin(radian) << endl;
    cout << angle << " 度的余弦值 cos = " << cos(radian) << endl;
    cout << angle << " 度的正切值 tan = " << tan(radian) << endl;

    return 0;
}Code language: PHP (php)

四、代码示例

以下代码演示了常用系统函数的多种应用场景:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cctype>
using namespace std;

int main()
{
    // ===== 示例1:数学函数 =====
    double x = 16.0;
    cout << "sqrt(" << x << ") = " << sqrt(x) << endl;
    cout << "pow(2, 10) = " << pow(2, 10) << endl;
    cout << "abs(-100) = " << abs(-100) << endl;
    cout << "fabs(-3.14) = " << fabs(-3.14) << endl;
    cout << "log(10) = " << log(10) << endl;
    cout << "exp(2) = " << exp(2) << endl;
    cout << "ceil(3.2) = " << ceil(3.2) << endl;
    cout << "floor(3.8) = " << floor(3.8) << endl;

    // ===== 示例2:随机数 =====
    srand((unsigned int)time(NULL));   // 设置随机种子
    cout << "\n随机数: " << rand() % 100 << endl;

    // ===== 示例3:字符串函数 =====
    char str1[] = "Hello";
    char str2[] = "World";
    cout << "\nstr1 长度: " << strlen(str1) << endl;
    cout << "比较 str1 和 str2: " << strcmp(str1, str2) << endl;

    // ===== 示例4:字符判断 =====
    char ch = 'A';
    cout << "\n字符 '" << ch << "':" << endl;
    cout << "isalpha? " << (isalpha(ch) ? "是" : "否") << endl;
    cout << "isdigit? " << (isdigit(ch) ? "是" : "否") << endl;
    cout << "isupper? " << (isupper(ch) ? "是" : "否") << endl;
    cout << "toupper('a') = " << (char)toupper('a') << endl;

    // ===== 示例5:时间函数 =====
    time_t now = time(NULL);
    cout << "\n当前时间戳: " << now << endl;

    // ===== 示例6:综合应用 =====
    double radius;
    cout << "\n请输入圆的半径:";
    cin >> radius;
    cout << "圆面积 = " << (acos(-1.0) * pow(radius, 2)) << endl;
    cout << "圆周长 = " << (2 * acos(-1.0) * radius) << endl;

    return 0;
}Code language: PHP (php)

五、注意事项

  • 使用系统函数前,必须包含对应的头文件。例如使用数学函数要写 #include <cmath>。
  • 数学函数 sin、cos、tan、asin、acos、atan 的参数和返回值均以弧度为单位,不是角度。
  • abs 函数在 <cstdlib> 中针对整数,<cmath> 中的 fabs 针对浮点数。C++11 起 <cmath> 也提供了 std::abs 对浮点数的重载。
  • 随机数函数 rand() 返回 0 到 RAND_MAX 之间的整数,使用前建议用 srand(time(NULL)) 设置种子以获得不同序列。
  • 系统函数分为标准 C++ 函数库(从标准 C 继承并扩充)和一些非标准函数库(通常与操作系统相关)。标准函数库可跨平台使用。
  • 查阅系统函数的原型、参数说明和返回值,可以参考官方文档网站:https://zh.cppreference.com/w/

六、常见错误

1. 忘记包含头文件
int main()
{
    double x = sqrt(16.0);   // 错误:未包含 <cmath>
    return 0;
}Code language: JavaScript (javascript)

解决:添加 #include <cmath>

2. 混淆角度和弧度
double angle = 90.0;
double s = sin(angle);   // 错误:sin 期望弧度,90 弧度不是 90 度Code language: JavaScript (javascript)

解决:转换为弧度 sin(angle * 3.14159 / 180.0)

3. 对浮点数使用错误的绝对值函数
float f = -3.14f;
int result = abs(f);     // 警告:f 被截断为整数Code language: JavaScript (javascript)

解决:使用 fabs(f) 或 std::abs(f)(C++11)

4. 随机数每次运行结果相同
int main()
{
    cout << rand() % 100 << endl;   // 每次运行输出相同
    return 0;
}Code language: JavaScript (javascript)

解决:添加 srand((unsigned int)time(NULL));

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注