C++ 程序中的级数求和

C++C++Beginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,我们将学习如何编写一个 C++ 程序来计算一系列数字的和。我们将提供两种方法来解决这个问题,并解释每种方法的代码。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL cpp(("`C++`")) -.-> cpp/BasicsGroup(["`Basics`"]) cpp(("`C++`")) -.-> cpp/ControlFlowGroup(["`Control Flow`"]) cpp(("`C++`")) -.-> cpp/IOandFileHandlingGroup(["`I/O and File Handling`"]) cpp(("`C++`")) -.-> cpp/StandardLibraryGroup(["`Standard Library`"]) cpp/BasicsGroup -.-> cpp/variables("`Variables`") cpp/BasicsGroup -.-> cpp/operators("`Operators`") cpp/ControlFlowGroup -.-> cpp/for_loop("`For Loop`") cpp/IOandFileHandlingGroup -.-> cpp/output("`Output`") cpp/IOandFileHandlingGroup -.-> cpp/user_input("`User Input`") cpp/StandardLibraryGroup -.-> cpp/math("`Math`") subgraph Lab Skills cpp/variables -.-> lab-96198{{"`C++ 程序中的级数求和`"}} cpp/operators -.-> lab-96198{{"`C++ 程序中的级数求和`"}} cpp/for_loop -.-> lab-96198{{"`C++ 程序中的级数求和`"}} cpp/output -.-> lab-96198{{"`C++ 程序中的级数求和`"}} cpp/user_input -.-> lab-96198{{"`C++ 程序中的级数求和`"}} cpp/math -.-> lab-96198{{"`C++ 程序中的级数求和`"}} end

包含库文件

我们将在 ~/project 目录下使用以下命令创建一个名为 main.cpp 的新文件:

touch ~/project/main.cpp

首先,我们需要在程序中包含必要的库文件以执行所需的操作。在本例中,我们需要 iostream 来处理输入输出操作,以及 cmath 来执行数学运算。

#include <iostream>
#include <cmath>

using namespace std;

定义 main() 函数

main 函数中,我们将要求用户输入 x 的值以及级数中的项数 n

int main() {
    int x, n;

    cout << "Enter the value of x: ";
    cin >> x;

    cout << "Enter the number of terms in the series, n: ";
    cin >> n;

    // code to calculate the sum of the series

    return 0;
}

计算级数的和(方法 1)

对于第一种方法,我们将使用 for 循环遍历级数中的给定项数。我们将 sum 初始化为 1(级数中的第一项),并在级数的每一项中将 x 与自身相乘,同时将该值加到 sum 中。

double sum = 1;
for (int i = 1; i < n; i++) {
    sum += pow(x, i);
}

在这里,我们使用 cmath 库中的 pow() 函数来计算 x 的幂次方值,然后将该值加到 sum 中。

计算级数的和(方法 2)

对于第二种方法,我们将使用 for 循环遍历级数中的给定项数。我们将 sum 初始化为 1(级数中的第一项),然后在级数的每一项中将 x 与自身相乘。我们将使用一个单独的变量 term 来计算级数中每一项的值。

double sum = 1;
double term = 1;
for (int i = 1; i < n; i++) {
    term *= x;
    sum += term;
}

在这里,我们使用 *= 运算符将 termx 相乘,并将结果赋值回 term。然后我们将 term 的值加到 sum 中。

输出结果

我们将使用以下代码输出级数和的最终结果。

cout << "The sum of the series is: " << fixed << sum << endl;

在这里,fixed 用于以十进制格式显示输出,而 endl 用于在输出后打印换行符。

本实验将在 ~/project/main.cpp 文件中完成,并可以在 Ubuntu 系统的终端中使用以下命令编译和运行:

g++ main.cpp -o main && ./main

完整代码

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    int x, n;

    cout << "Enter the value of x: ";
    cin >> x;

    cout << "Enter the number of terms in the series, n: ";
    cin >> n;

    // Method 1
    // double sum = 1;
    // for (int i = 1; i < n; i++) {
    //     sum += pow(x, i);
    // }

    // Method 2
    double sum = 1;
    double term = 1;
    for (int i = 1; i < n; i++) {
        term *= x;
        sum += term;
    }

    cout << "The sum of the series is: " << fixed << sum << endl;

    return 0;
}

总结

在本实验中,我们学习了如何编写一个 C++ 程序来计算一系列数字的和。我们还提供了两种方法来解决这个问题,并解释了每种方法的代码。用户可以根据需求选择任意一种方法来获得所需的结果。通过本实验,读者将学会如何在 C++ 编程中实现模式或和计算的代码。

您可能感兴趣的其他 C++ 教程