简体   繁体   English

请说明此功能

[英]Please explain this function

The code below is used to sort the int array in ascending or descending order 下面的代码用于按升序或降序对int数组进行排序

//codes borrowed from learncpp.com
#include<iostream>
#include<algorithm>
using namespace std;
void SelectionSort(int *anArray, int nSize, bool(*pComparison)(int, int)) {
    for(int iii=0; iii<nSize; iii++) {
        int nCurrent = iii;
        for(int jjj=iii+1; jjj<nSize; jjj++) {
            if(pComparison(anArray[nCurrent], anArray[jjj]))
                nCurrent = jjj;
            }
        swap(anArray[nCurrent], anArray[iii]);
    }
}
bool Ascending(int nX, int nY) {
    return nY>nX;
}
bool Descending(int nX, int nY) {
    return nY<nX;
}
bool EvensFirst(int nX, int nY) {
    if((nX%2)&&!(nY%2))
        return false;
    if(!(nX%2)&&(nY%2))
        return true;
    return Ascending(nX, nY);
}
void PrintArray(int *pArray, int nSize) {
    for(int kkk=0; kkk<nSize; kkk++)
        cout << pArray[kkk] << " ";
    cout << endl;
}
int main() {
    int anArray[9] = {3, 5, 1, 8, 9, 4, 6, 2, 7};
    SelectionSort(anArray, 9, EvensFirst);
    PrintArray(anArray, 9);
    return 0;
}

Printing Result is 9 7 5 3 1 8 6 4 2 instead of 2 4 6 8 1 3 5 7 9 打印结果是9 7 5 3 1 8 6 4 2而不是2 4 6 8 1 3 5 7 9

Can anyone here please explain how does the bool function EvensFirst work? 这里有人可以解释bool函数EvensFirst如何工作吗?

The main thing in this program is the use of funtion pointer. 该程序的主要内容是功能指针的使用。 bool(*pComparison)(int, int) is a pointer to a function which returns bool type values and takes as parameter 2 int values. bool(*pComparison)(int, int)是指向函数的指针,该函数返回bool类型值并将参数2 int值作为参数。 You can check the different outputs SelectionSort(anArray, 9, Ascending); 您可以检查不同的输出SelectionSort(anArray, 9, Ascending); or SelectionSort(anArray, 9, Descending); SelectionSort(anArray, 9, Descending); (if you have coded the selection sort function correctly). (如果您已正确编码选择排序功能)。

Note: Based on different function pointer this general sorting routine will give different outputs. 注意:基于不同的函数指针,此常规排序例程将提供不同的输出。 And the rest of the routine is basic sorting routine swapping the values of min or max to current element. 例程的其余部分是基本的排序例程,将minmax的值交换到当前元素。

bool EvensFirst(int nX, int nY) {
    if((nX%2)&&!(nY%2)) //if nX is odd and nY is even the 
//nY should be in first position. So to change the order return false
        return false;
    if(!(nX%2)&&(nY%2))//if nX is even and nY is odd the 
//nX should be in first position. So to retain the order return true
        return true;
    return Ascending(nX, nY);// both even or both odd return the ascending function's output.
}

If nx is even the if we divide it by 2 it will give remainder of 0. For example, 12 12%2=0 如果nx是偶数,如果将其除以2,它将得到0的余数。例如12 12%2 = 0

34%2=0 34%2 = 0

9%2=1 ----> that's why this is odd. 9%2 = 1 ---->这就是为什么这很奇怪。

Every odd number can be written in the form 2m+1 Now if we divide this number by 2 we will get remainder of 1 as 1st part is giving remainder 0. 每个奇数都可以2m+1的形式写。现在,如果将这个数字除以2,我们将得到1的余数,因为第一部分给出的余数为0。

Even number the explanation is same even number is represented by 2m .So when divided by 2 it will give remainder 0. 偶数的解释是相同的,偶数用2m表示。因此,如果将其除以2,将得到余数0。

if((nX%2)&&!(nY%2))  
       return false;
if nX is even it nX%2=0 and if nY is odd then ny%2=1
So expressin becomes
if(0 && 1)-->which evaluates to false and go to next condition. 

Few wxamples to clarify your idea-- 很少有人可以澄清您的想法-

Check if x is even 检查x是否为偶数

if(x%2==0) 
  //x is even.

Also can be written as
if(!x%2)
  //x is even.

Check if x is odd 检查x是否为奇数

 if(x%2==1)
      // x is odd
    Also can be written as

    if(x%2)
      // x is odd.

Check if x and y are both even 检查x和y是否均为偶数

   if(!x%2 && !y%2)
    //both even

check if either of one is even 检查是否有一个是偶数

 if(!x%2 || !y%2)
    //either of them is even

The operator modulo % returns the remainder of the division of its operands. 运算符以%返回其操作数除法的余数。 It can be used to check if a number is even or odd, because if the remainder of x/2 is 0 it means that x is even. 它可以用来检查数字是偶数还是奇数,因为如果x/2的余数为0则意味着x是偶数。 That means that if x%2=0 than x is even, else it is odd. 这意味着如果x%2=0大于x,则为奇数,否则为奇数。


Here's how EvensFirst works : 这是EvensFirst的工作方式

bool EvensFirst(int nX, int nY) {
    if((nX%2)&&!(nY%2)) //if nX is even and nY is odd
        return false;   //returns false
    if(!(nX%2)&&(nY%2)) //if nX is odd and nY is even
        return true;    //returns true
    //if they are both odd or even returns the result of ascending
    return Ascending(nX, nY); //true if nY > nX 
}

Here's how you use EvensFirst : 这是使用EvensFirst的方法

void SelectionSort(int *anArray, int nSize, bool(*pComparison)(int, int) {
    for(int iii=0; iii<nSize; iii++) {   //for each number in the array
        int nCurrent = iii;
        for(int jjj=iii+1; jjj<nSize; jjj++) {  //for each following elemnt
            if(pComparison(anArray[nCurrent], anArray[jjj])) //compare the two elements
                nCurrent = jjj; 
            }
        swap(anArray[nCurrent], anArray[jjj]); //and switch their positions if they are in the wrong order
    }
}

Quote from The Standard: 引用标准:

Tthe binary % operator yields the remainder from the division of the first expression by the second. 二进制%运算符从第一个表达式除以第二个表达式得出余数。 If the second operand of / or % is zero the behavior is undefined; 如果/或%的第二个操作数为零,则行为不确定。 otherwise (a/b)*b + a%b is equal to a. 否则(a / b)* b + a%b等于a。 If both operands are nonnegative then the remainder is nonnegative; 如果两个操作数均为非负数,则其余为非负数; if not, the sign of the remainder is implementation-defined (74) 如果不是,则其余符号由实现定义(74)

(74) According to work underway toward the revision of ISO C, the preferred algorithm for integer division follows the rules defined in the ISO Fortran standard, ISO/IEC 1539:1991, in which the quotient is always rounded toward zero. (74)根据为进行ISO C修订而进行的工作,整数除法的首选算法遵循ISO Fortran标准ISO / IEC 1539:1991中定义的规则,其中商总是四舍五入。

Example: 例:

11 % 3 = 2 , because 11 = 3*3 + 2 11 % 3 = 2 ,因为11 = 3*3 + 2

Now, operation modulo-2 can give us only two possible results: 0 or 1. Also: 现在,模2运算只能给我们两个可能的结果:0或1。

1) For any given even number N , N % 2 will always give 0. That's because even numbers are by definition multiplications of 2. 1)对于任何给定的偶数NN % 2始终为0。这是因为偶数按定义是2的乘积。

2) For any given odd number M , M % 2 will always give 1. That's because any even number can be defined as 2K + 1 , where K is an N0 (natural number or zero). 2)对于任何给定的奇数MM % 2始终为1。这是因为可以将任何偶数定义为2K + 1 ,其中KN0 (自然数或零)。

Because of the above, we can use x % 2 expression as logical condition, because in C/C++ "0 is false, everything other is true". 由于上述原因,我们可以将x % 2表达式用作逻辑条件,因为在C / C ++中,“ 0为false,其他所有为true”。

So: 所以:

if(x%2)
{
  //x%2 != 0, which means x%2 = 1, which means x is an odd number
}
else
{
  //x%2 == 0, which means x is an even number
}

Now, let's go back to your EvensFirst function: 现在,让我们回到您的EvensFirst函数:

bool EvensFirst(int nX, int nY)
{
    if((nX%2)&&!(nY%2))
        return false;
    if(!(nX%2)&&(nY%2))
        return true;
    return Ascending(nX, nY);
}

This function accepts two arguments: nX and nY and it returns true if nX should be placed before nY (false otherwise). 该函数接受两个参数: nXnY ,如果将nX放在nY之前,则返回true(否则返回false)。 First, it checks the condition (nX%2)&&!(nY%2) . 首先,它检查条件(nX%2)&&!(nY%2) This condition essentially means: 此条件实质上意味着:

"Check, if nX is an odd number and nY is an even number." “检查,如果nX是一个奇数,而nY是一个偶数。”

If it evaluates to true, function returns false - evens are taken first, so nY should be placed before nX ). 如果计算结果为true,则函数返回false-首先执行nY ,因此应将nY放在nX之前。

Then, it checks for the second condition: !(nX%2)&&(nY%2) , which means: 然后,它检查第二个条件: !(nX%2)&&(nY%2) ,这意味着:

"Check, if nX is an even number and nY is an odd number." “检查, nX是偶数, nY是奇数。”

If it evaluates to true, function returns true - evens are taken first, so nX should be placed before nY . 如果计算结果为true,则函数返回true-首先执行nX ,因此nX应该放在nY之前。

If both these conditions evaluated to false, it means that either both of them are odd or both are evens. 如果这两个条件都被评估为假,则意味着它们两个都是奇数或偶数。 In this case, functions compares them using "Ascending" comparison - lower number will be taken first. 在这种情况下,函数使用“升序”比较对它们进行比较-首先将采用较小的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM