C/C中的ismorer()++

在C++中,ISGORDER()是用于数学计算的预定义函数。 数学H 是各种数学函数所需的头文件。 isgreater()函数用于检查给定给函数的第一个参数是否大于给定给函数的第二个参数。意味着如果 A. 第一个论点是 B 第二个参数是否检查 a> b 或者不是。 语法:

null
bool isgreater(a, b)Parameters:a, b => These two are the parameters whose value we want to compare.Result:The function will return the true if a>b else it returns false.Error: No error occurs with this function.Exception: If a or b or both is NaN,then the function raised an exception and return false(0).

CPP

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


输出:

nan isgreater than 5: 0

  • 节目:

CPP

// CPP code to illustrate
// the use of isgreater function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Take two any values
int a, b;
bool result;
a = 5;
b = 8;
// Since 'a' is not greater
// than 'b' so answer
// is false(0)
result = isgreater(a, b);
cout << a << " isgreater than " << b
<< ": " << result << endl;
char x = 'd' ;
// Since 'd' ascii value is greater
// than b variable so answer
// is true(1)
result = isgreater(x, b);
cout << x << " isgreater than " << b
<< ": " << result;
return 0;
}


  • 输出:
5 isgreater than 8: 0d isgreater than 8: 1
  • 此函数可用于任何基于比较的排序算法。让我们把它用在 气泡排序 :

CPP

// CPP code to illustrate the
// use of isgreaterequal function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// taking inputs
int arr[] = { 5, 2, 8, 3, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
for ( int i = 0; i < n - 1; i++)
{
for ( int j = 0; j < n - i - 1; j++)
{
if (isgreater(arr[j], arr[j + 1]))
{
int k = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = k;
}
}
}
cout << "Sorted array: " ;
for ( int i = 0; i < n; i++) {
cout << arr[i] << ", " ;
}
return 0;
}


  • 输出:
Sorted array: 2, 3, 4, 5, 8, 

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享