用于检查两个int值是否在彼此的给定范围内的函数

时间:2021-09-13 18:04:09

For example, take int x = 30; and int y = 27;. My program would call the function bool CheckWithinRange(int x, int y, int range); as CheckWithinRange(x, y, 3) or CheckWithinRange(y, x, 3) and it would return true.

例如,取int x = 30;并且int y = 27;。我的程序会调用函数bool CheckWithinRange(int x,int y,int range);作为CheckWithinRange(x,y,3)或CheckWithinRange(y,x,3),它将返回true。

EDIT Sorry, my bad, my question is simply: How can such a function be written?

编辑对不起,我的不好,我的问题很简单:如何编写这样的函数?

2 个解决方案

#1


1  

#include <stdio.h>
#include <stdlib.h>

bool CheckWithinRange(int x, int y, int range)
{
    if (abs(x-y) <= range)
    {
        return true;
    }

    return false;
}

int main()
{
    int x = 30;
    int y = 27;
    int range = 5;

    if (CheckWithinRange(x,y,range))
    {
        printf("X and Y are within 5 digits");
    }
    else
    {
        printf("X and Y are not within 5 digits.");
    }

    return 0;
}

The abs() function from stdlib can be used to find the absolute difference between 2 numbers in C. Enjoy!

来自stdlib的abs()函数可用于查找C中2个数字之间的绝对差异。享受!

If you are worried about arithmetic overflow then @chqrlie's answer is what you are looking for.

如果你担心算术溢出,那么@ chqrlie的答案正是你要找的。

#2


2  

This question is actually a little tricky to answer in the general case. If x or y can be very large or very far apart, x-y may overflow. Here is a solution that solves the question for all int values of x, y and range:

在一般情况下,回答这个问题实际上有点棘手。如果x或y可能非常大或相隔很远,则x-y可能溢出。这是一个解决x,y和范围的所有int值的问题的解决方案:

#include <limits.h>
#include <stdbool.h>

bool CheckWithinRange(int x, int y, int range) {
    if (range < 0)
        return false;
    if (x <= y)
        return x >= INT_MAX - range || x + range >= y;
    else
        return y >= INT_MAX - range || y + range >= x;
}

#1


1  

#include <stdio.h>
#include <stdlib.h>

bool CheckWithinRange(int x, int y, int range)
{
    if (abs(x-y) <= range)
    {
        return true;
    }

    return false;
}

int main()
{
    int x = 30;
    int y = 27;
    int range = 5;

    if (CheckWithinRange(x,y,range))
    {
        printf("X and Y are within 5 digits");
    }
    else
    {
        printf("X and Y are not within 5 digits.");
    }

    return 0;
}

The abs() function from stdlib can be used to find the absolute difference between 2 numbers in C. Enjoy!

来自stdlib的abs()函数可用于查找C中2个数字之间的绝对差异。享受!

If you are worried about arithmetic overflow then @chqrlie's answer is what you are looking for.

如果你担心算术溢出,那么@ chqrlie的答案正是你要找的。

#2


2  

This question is actually a little tricky to answer in the general case. If x or y can be very large or very far apart, x-y may overflow. Here is a solution that solves the question for all int values of x, y and range:

在一般情况下,回答这个问题实际上有点棘手。如果x或y可能非常大或相隔很远,则x-y可能溢出。这是一个解决x,y和范围的所有int值的问题的解决方案:

#include <limits.h>
#include <stdbool.h>

bool CheckWithinRange(int x, int y, int range) {
    if (range < 0)
        return false;
    if (x <= y)
        return x >= INT_MAX - range || x + range >= y;
    else
        return y >= INT_MAX - range || y + range >= x;
}