📜  C / C++中的islessgreater()

📅  最后修改于: 2021-05-30 18:11:36             🧑  作者: Mango

在C++中,islessgreater()是用于数学计算的预定义函数。 math.h是各种数学功能所需的头文件。

islessgreater()函数用于检查赋予函数的第一个参数是否小于或大于赋予函数的第二个参数。表示如果a是第一个参数, b是第二个参数,那么它将检查a> b ||是否a 与否。

句法:

bool islessgreater(a, b)
// CPP code to illustrate
// the exception of function
#include 
using namespace std;
  
int main()
{
    // Take any values
    int a = 5;
    double f1 = nan("2.0");
    bool result;
  
    // Since f1 value is NaN so
    // with any value of a, the function
    // always return false(0)
    result = islessgreater(f1, a);
    cout << f1 << " islessgreater than " << a
         << ": " << result;
  
    return 0;
}

输出:

nan islessgreater than 5: 0

例子:

  • 程序:
    // CPP code to illustrate
    // the use of islessgreater function
    #include 
    using namespace std;
      
    int main()
    {
        // Take two any values
        float a, b;
        bool result;
        a = 10.2;
        b = 8.5;
      
        // Since 'a' is not less than 'b'
        // but greater than 'b' so answer
        // is true(1)
        result = islessgreater(a, b);
        cout << a << " islessgreater than " << b
             << ": " << result << endl;
      
        int x = 2;
      
        // Since 'x' is not greater than 'b'
        // but less than 'a' vaiable so answer
        // is true(1)
        result = islessgreater(x, a);
        cout << x << " islessgreater than " << a
             << ": " << result << endl;
      
        a = 8.5;
      
        // Since value of 'a' and 'b' are
        // equal so answer is false(0)
        result = islessgreater(a, b);
        cout << a << " islessgreater than " << b
             << ": " << result;
      
        return 0;
    }
    

    输出:

    10.2 islessgreater than 8.5: 1
    2 islessgreater than 10.2: 1
    8.5 islessgreater than 8.5: 0
    

应用:
此函数可以在各种地方使用,其中之一可以在线性搜索中使用。

// CPP code to illustrate the
// use of islessgreater function
#include 
using namespace std;
  
int main()
{
    // taking inputs
    int arr[] = { 5, 2, 8, 3, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Lets we want to search for 1 in
    // the arr array
    int a = 1;
    int flag = 0;
      
    for (int i = 0; i < n; i++) 
    {
        if (islessgreater(arr[i], a)) 
        {
            flag = 1;
        }
    }
  
    if (flag == 0) 
    {
        cout << a << " is present in array";
    }
      
    else 
    {
        cout << a << " is not present in array";
    }
    return 0;
}

输出:

1 is not present in array
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”