[英]C++ isPrime function: How can I return a Boolean value from a function that takes an integer parameter?
I had to create a function that takes an integer parameter called number and the function would check to see if number is a prime number or not.我必须创建一个 function ,它接受一个名为 number 的 integer 参数,并且 function 将检查 number 是否为质数。 Everything works but the output to the user would be a either 0 or 1, which is false and true, respectively.
一切正常,但用户的 output 将是 0 或 1,分别为假和真。 But how can I change that to instead return "True" or "False" if the number is prime or not, respectively.
但是,如果数字是否为素数,我如何将其更改为分别返回“True”或“False”。
Here is my code:这是我的代码:
#include<iostream>
int isPrime(int);
int main()
{
int number = 3;
std::cout << isPrime(number);
return 0;
}
int isPrime(int number)
{
bool isPrime = true;
if(number < 0)
std::cout << "Number must be a positive integer.\n";
else if(number == 0 || number == 1)
isPrime = false;
else
{
for(int divisor = 2; divisor <= number / 2; divisor++)
{
if(number % divisor == 0)
{
isPrime = false;
break;
}
}
}
return isPrime;
}
First of all, your function should return a bool
instead of an int
:首先,您的 function 应该返回
bool
而不是int
:
bool isPrime(int number) ...
Next, you need to tell cout
that you want to print boolean values as true
or false
instead of 1
or 0
.接下来,您需要告诉
cout
您要将 boolean 值打印为true
或false
而不是1
或0
。 This can be done with std::boolalpha
:这可以通过
std::boolalpha
来完成:
std::cout << std::boolalpha << isPrime(number);
You have to #include <ios>
for that.为此,您必须
#include <ios>
。
To answer some questions from the comments:要回答评论中的一些问题:
What's the issue if the function is of type int and not bool?
如果 function 的类型是 int 而不是 bool,会出现什么问题?
The whole point of a type system is to express and restrict the behavior of a program.类型系统的全部意义在于表达和限制程序的行为。 If you call a function you better know what you pass into that function and what that function returns.
如果您调用 function,您最好知道传递给 function 的内容以及 function 返回的内容。 Types help you with that.
类型可以帮助你。
Take your isPrime
function as an example.以您的
isPrime
function 为例。 Let's say you didn't write that function but someone else did and you just use it as a library.假设您没有写 function 但其他人写了,您只是将它用作库。 You see the function signature
int isPrime(int);
您会看到 function 签名
int isPrime(int);
. . The function takes an
int
as an argument and you can probably guess what that argument has to be: the number that you want to check. function 将
int
作为参数,您可能会猜到该参数是什么:您要检查的数字。
Now look at the return type int
.现在看看返回类型
int
。 What does this mean?这是什么意思? The function name
isPrime
indicates that the function answers a simple yes/no question: is the given number a prime number? function 名称
isPrime
表示 function 回答了一个简单的是/否问题:给定的数字是质数吗? However, an int
can represent more than just yes
or no
.但是,
int
可以表示的不仅仅是yes
或no
。 For example, what does it mean if that function returns 2
or -1000
(remember: you didn't write this function and you cannot look at its implementation)?例如,如果 function 返回
2
或-1000
是什么意思(记住:你没有写这个 function 并且你看不到它的实现)? Which value represents yes
and which no
?哪个值代表
yes
,哪个no
? If you change the return type of that function to bool
, a type that only has 2 possible values: true
or false
, you immediately get what the return value means.如果您将该 function 的返回类型更改为
bool
,该类型只有 2 个可能值: true
或false
,您会立即了解返回值的含义。
do I print that out in the main function
我要在主 function 中打印出来吗
This is more of a design question and always depends on your very specific use case.这更像是一个设计问题,并且始终取决于您非常具体的用例。 Given only the code from the question: yes.
仅给出问题中的代码:是的。 You separated business logic, the part that does the prime check, from an IO / user interaction part.
您将业务逻辑(进行主要检查的部分)与 IO / 用户交互部分分开。 Generally, this makes code more readable and debuggable, two very important aspects of software development.
通常,这使代码更具可读性和可调试性,这是软件开发的两个非常重要的方面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.