#include <stdio.h>
#define TRUE 1
#define FALSE 0
void limit(int stop)
{
int x;
for(x=0;x<=100;x=x+1)
{
printf("%d\t",x);
if(x==stop)
{
printf("You Won!\n");
return;
}
}
}
int verify(int check) /*this part*/
{
if(check<0 || check>100)
{
return FALSE;
}
return TRUE;
}
int main()
{
int x;
printf("Enter a stopping value(1-100): ");
scanf("%d",&x);
if(verify(x))/*this part too*/
{
limit(x);
}
else
{
printf("%d is out of range.\n",x);
}
return 0;
}
我想知道验证功能是如何工作的。不懂的部分评论一下。验证函数如何验证输入值是否在范围内? verify函数在超出范围时返回FALSE,即0,但它是如何使用的呢?
最佳答案
如果int
数字低于0
,假设-3
它超出范围,如果数字高于100
,假设 104
该函数将返回 false。
假设输入 104
不在指定范围内,verify
函数将获取该数字,检查该数字是否小于 0
code> 或高于 100
。在我们的例子中,数字高于 100
,验证函数将返回 FALSE
。
在 if(verify(x))
中,您将得到一个错误的 if 语句,因为验证函数返回了该语句。我们将直接进入 else
语句并执行其中的代码。
如果您选择的数字在范围内,例如 33
,验证函数将返回 TRUE
以及 main
中的 if 语句> 将为 TRUE
。
关于c - 请理解,我是新手。有人可以解释一下验证功能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43539513/