简介
在这个实验中,你将学习如何在 C++ 中判断一个给定的数字是否为完全平方数。完全平方数是指可以表示为一个整数与其自身相乘的结果的数。例如,1、4、9、16 和 25 都是完全平方数,因为它们分别可以表示为 1×1、2×2、3×3、4×4 和 5×5。
我们将创建一个 C++ 程序,使用标准库中的 sqrt()
函数来计算一个数的平方根,并判断它是否为完全平方数。这个实验将向你介绍基本的 C++ 编程概念,包括函数、条件语句和数学运算。
在这个实验中,你将学习如何在 C++ 中判断一个给定的数字是否为完全平方数。完全平方数是指可以表示为一个整数与其自身相乘的结果的数。例如,1、4、9、16 和 25 都是完全平方数,因为它们分别可以表示为 1×1、2×2、3×3、4×4 和 5×5。
我们将创建一个 C++ 程序,使用标准库中的 sqrt()
函数来计算一个数的平方根,并判断它是否为完全平方数。这个实验将向你介绍基本的 C++ 编程概念,包括函数、条件语句和数学运算。
在这一步中,我们将创建一个新的 C++ 文件,并为我们的程序包含必要的库。
首先,让我们在项目目录中创建一个名为 main.cpp
的新文件:
project
文件夹,选择“新建文件”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()
函数,用于显示欢迎信息通过按 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);
}
让我们来理解一下这个函数的工作原理:
number
,并返回一个布尔值(true 或 false)cmath
库中的 sqrt()
函数来计算输入数字的平方根static_cast<int>()
将平方根转换为整数,这样会去掉任何小数部分true
;否则返回 false
例如:
到目前为止,你的完整代码应该如下所示:
#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()
函数,使其能够:
isPerfectSquare()
函数来检查该数字是否为完全平方数用以下代码更新你的 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;
}
让我们来理解这段代码的功能:
userNumber
来存储用户的输入cout
提示用户输入一个正整数cin
读取用户的输入,并将其存储在 userNumber
中userNumber
作为参数调用我们的 isPerfectSquare()
函数squareRoot
中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
程序将启动并提示你输入一个正整数。
让我们使用不同的输入来测试我们的程序,以验证它是否能正常工作:
当提示输入时,输入 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!
再次运行程序,当提示输入时,输入 10
。你应该会看到类似以下的输出:
Please enter a positive integer: 10
The number 10 is not a perfect square.
Thank you for using the Perfect Square Checker Program!
再次运行程序,当提示输入时,输入 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++ 程序。让我们回顾一下你所完成的内容:
你学习了如何在 C++ 程序中包含必要的库:
iostream
用于输入和输出操作cmath
用于像 sqrt()
这样的数学函数你实现了 isPerfectSquare()
函数,该函数使用数学方法来判断一个数是否为完全平方数:
你在 main()
函数中创建了一个用户友好的界面,该界面:
isPerfectSquare()
函数检查该数字是否为完全平方数你使用不同的输入对程序进行了编译和测试,以验证其功能。
通过这个实验,你获得了几个重要的 C++ 编程概念的实践经验:
你可以通过添加以下功能来进一步增强这个程序:
通过持续练习并巩固这些基本概念,你将提升自己的 C++ 编程技能,从而能够应对更复杂的问题。