判断完全平方数

C++C++Beginner
立即练习

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

简介

在这个实验中,你将学习如何在 C++ 中判断一个给定的数字是否为完全平方数。完全平方数是指可以表示为一个整数与其自身相乘的结果的数。例如,1、4、9、16 和 25 都是完全平方数,因为它们分别可以表示为 1×1、2×2、3×3、4×4 和 5×5。

我们将创建一个 C++ 程序,使用标准库中的 sqrt() 函数来计算一个数的平方根,并判断它是否为完全平方数。这个实验将向你介绍基本的 C++ 编程概念,包括函数、条件语句和数学运算。

创建新的 C++ 文件并包含必要的库

在这一步中,我们将创建一个新的 C++ 文件,并为我们的程序包含必要的库。

首先,让我们在项目目录中创建一个名为 main.cpp 的新文件:

  1. 在 WebIDE 中,点击左侧侧边栏的资源管理器图标
  2. 右键点击 project 文件夹,选择“新建文件”
  3. 将文件命名为 main.cpp,然后按回车键

现在,让我们为程序添加必要的库。我们需要两个主要的库:

  • iostream:这个库提供了输入和输出操作的功能
  • cmath:这个库包含了数学函数,包括我们将使用的 sqrt() 函数

将以下代码添加到你的 main.cpp 文件中:

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    cout << "\nWelcome to the Perfect Square Checker Program\n\n";

    // We will add more code here in the next steps

    return 0;
}

这段代码:

  • 包含了所需的库
  • 使用 namespace std 避免在标准库函数前写 std::
  • 创建了一个基本的 main() 函数,用于显示欢迎信息
  • 返回 0 表示程序执行成功

通过按 Ctrl+S 或从菜单中选择“文件 > 保存”来保存你的文件。

实现完全平方数检查函数

现在,我们将创建一个函数来判断一个数是否为完全平方数。完全平方数的平方根是一个整数。例如,16 是一个完全平方数,因为它的平方根恰好是 4。

在你的 main.cpp 文件的 main() 函数上方添加以下函数:

bool isPerfectSquare(int number) {
    // Calculate the square root of the number
    double squareRoot = sqrt(number);

    // Convert the square root to an integer
    int intSquareRoot = static_cast<int>(squareRoot);

    // A number is a perfect square if squaring its integer square root gives the original number
    return (intSquareRoot * intSquareRoot == number);
}

让我们来理解一下这个函数的工作原理:

  1. 该函数接受一个整数参数 number,并返回一个布尔值(true 或 false)
  2. 我们使用 cmath 库中的 sqrt() 函数来计算输入数字的平方根
  3. 我们使用 static_cast<int>() 将平方根转换为整数,这样会去掉任何小数部分
  4. 如果一个数是完全平方数,那么将其整数平方根自乘将得到原数
  5. 如果该数是完全平方数,函数返回 true;否则返回 false

例如:

  • 对于数字 16:
    • sqrt(16) = 4.0
    • 将 4.0 转换为整数得到 4
    • 4 × 4 = 16,等于原数,所以 16 是完全平方数
  • 对于数字 10:
    • sqrt(10) ≈ 3.16227766
    • 将 3.16227766 转换为整数得到 3
    • 3 × 3 = 9,不等于原数,所以 10 不是完全平方数

到目前为止,你的完整代码应该如下所示:

#include <iostream>
#include <cmath>

using namespace std;

bool isPerfectSquare(int number) {
    // Calculate the square root of the number
    double squareRoot = sqrt(number);

    // Convert the square root to an integer
    int intSquareRoot = static_cast<int>(squareRoot);

    // A number is a perfect square if squaring its integer square root gives the original number
    return (intSquareRoot * intSquareRoot == number);
}

int main() {
    cout << "\nWelcome to the Perfect Square Checker Program\n\n";

    // We will add more code here in the next step

    return 0;
}

在进行下一步之前,请保存你的文件。

完善主函数以实现用户输入和输出

现在,让我们增强 main() 函数,使其能够:

  1. 提示用户输入一个数字
  2. 获取用户的输入
  3. 使用我们的 isPerfectSquare() 函数来检查该数字是否为完全平方数
  4. 根据检查结果显示相应的消息

用以下代码更新你的 main() 函数:

int main() {
    cout << "\nWelcome to the Perfect Square Checker Program\n\n";

    int userNumber;

    // Prompt the user to enter a number
    cout << "Please enter a positive integer: ";
    cin >> userNumber;

    // Check if the entered number is a perfect square
    if (isPerfectSquare(userNumber)) {
        int squareRoot = static_cast<int>(sqrt(userNumber));
        cout << "\nThe number " << userNumber << " is a perfect square!" << endl;
        cout << "It is equal to " << squareRoot << " × " << squareRoot << endl;
    } else {
        cout << "\nThe number " << userNumber << " is not a perfect square." << endl;
    }

    cout << "\nThank you for using the Perfect Square Checker Program!\n" << endl;

    return 0;
}

让我们来理解这段代码的功能:

  1. 我们声明一个整数变量 userNumber 来存储用户的输入
  2. 使用 cout 提示用户输入一个正整数
  3. 使用 cin 读取用户的输入,并将其存储在 userNumber
  4. userNumber 作为参数调用我们的 isPerfectSquare() 函数
  5. 如果该数字是完全平方数:
    • 我们计算其平方根并将其存储在 squareRoot
    • 显示一条消息,表明该数字是完全平方数
    • 展示哪两个相同的整数相乘得到原数
  6. 如果该数字不是完全平方数:
    • 显示一条消息,表明该数字不是完全平方数
  7. 最后,显示一条感谢消息并从 main() 函数返回

现在,你的完整程序应该如下所示:

#include <iostream>
#include <cmath>

using namespace std;

bool isPerfectSquare(int number) {
    // Calculate the square root of the number
    double squareRoot = sqrt(number);

    // Convert the square root to an integer
    int intSquareRoot = static_cast<int>(squareRoot);

    // A number is a perfect square if squaring its integer square root gives the original number
    return (intSquareRoot * intSquareRoot == number);
}

int main() {
    cout << "\nWelcome to the Perfect Square Checker Program\n\n";

    int userNumber;

    // Prompt the user to enter a number
    cout << "Please enter a positive integer: ";
    cin >> userNumber;

    // Check if the entered number is a perfect square
    if (isPerfectSquare(userNumber)) {
        int squareRoot = static_cast<int>(sqrt(userNumber));
        cout << "\nThe number " << userNumber << " is a perfect square!" << endl;
        cout << "It is equal to " << squareRoot << " × " << squareRoot << endl;
    } else {
        cout << "\nThe number " << userNumber << " is not a perfect square." << endl;
    }

    cout << "\nThank you for using the Perfect Square Checker Program!\n" << endl;

    return 0;
}

在进行下一步之前,请保存你的文件。

编译并测试程序

现在我们已经完成了 C++ 程序,是时候对其进行编译和运行了。编译的过程是将我们人类可读的代码转换为机器可执行的程序。

编译程序

在 WebIDE 中,点击菜单中的“Terminal”并选择“New Terminal”来打开一个终端。

在终端中,导航到项目目录:

cd ~/project

使用 g++ 编译器编译程序:

g++ main.cpp -o perfect_square_checker

这个命令告诉编译器:

  • 读取我们的源文件 main.cpp
  • 将其编译成一个名为 perfect_square_checker 的可执行文件

如果编译成功,你将不会看到任何输出。如果有错误,请阅读错误信息,修复代码中的问题,然后再次尝试编译。

运行程序

成功编译程序后,使用以下命令运行它:

./perfect_square_checker

程序将启动并提示你输入一个正整数。

使用不同的输入进行测试

让我们使用不同的输入来测试我们的程序,以验证它是否能正常工作:

测试用例 1:一个完全平方数

当提示输入时,输入 16。你应该会看到类似以下的输出:

Please enter a positive integer: 16

The number 16 is a perfect square!
It is equal to 4 × 4

Thank you for using the Perfect Square Checker Program!

测试用例 2:非完全平方数

再次运行程序,当提示输入时,输入 10。你应该会看到类似以下的输出:

Please enter a positive integer: 10

The number 10 is not a perfect square.

Thank you for using the Perfect Square Checker Program!

测试用例 3:另一个完全平方数

再次运行程序,当提示输入时,输入 25。你应该会看到类似以下的输出:

Please enter a positive integer: 25

The number 25 is a perfect square!
It is equal to 5 × 5

Thank you for using the Perfect Square Checker Program!

通过使用不同的输入进行测试,你可以验证你的程序是否能正确识别完全平方数和非完全平方数。

恭喜你!你已经成功创建了一个能判断一个数是否为完全平方数的 C++ 程序。

总结

在这个实验中,你成功创建了一个用于判断一个数是否为完全平方数的 C++ 程序。让我们回顾一下你所完成的内容:

  1. 你学习了如何在 C++ 程序中包含必要的库:

    • iostream 用于输入和输出操作
    • cmath 用于像 sqrt() 这样的数学函数
  2. 你实现了 isPerfectSquare() 函数,该函数使用数学方法来判断一个数是否为完全平方数:

    • 计算该数的平方根
    • 检查平方根的整数部分的平方是否等于原数
  3. 你在 main() 函数中创建了一个用户友好的界面,该界面:

    • 提示用户输入一个数字
    • 使用 isPerfectSquare() 函数检查该数字是否为完全平方数
    • 根据检查结果显示相应的消息
  4. 你使用不同的输入对程序进行了编译和测试,以验证其功能。

通过这个实验,你获得了几个重要的 C++ 编程概念的实践经验:

  • 创建和调用函数
  • 使用条件语句(if-else)
  • 处理用户输入和输出
  • 进行类型转换
  • 使用标准库中的数学函数
  • 编译和运行 C++ 程序

你可以通过添加以下功能来进一步增强这个程序:

  • 输入验证,以确保用户输入的是正整数
  • 无需重启程序即可检查多个数字的功能
  • 找出与非完全平方数最接近的完全平方数

通过持续练习并巩固这些基本概念,你将提升自己的 C++ 编程技能,从而能够应对更复杂的问题。