簡體   English   中英

請說明此功能

[英]Please explain this function

下面的代碼用於按升序或降序對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;
}

打印結果是9 7 5 3 1 8 6 4 2而不是2 4 6 8 1 3 5 7 9

這里有人可以解釋bool函數EvensFirst如何工作嗎?

該程序的主要內容是功能指針的使用。 bool(*pComparison)(int, int)是指向函數的指針,該函數返回bool類型值並將參數2 int值作為參數。 您可以檢查不同的輸出SelectionSort(anArray, 9, Ascending); SelectionSort(anArray, 9, Descending); (如果您已正確編碼選擇排序功能)。

注意:基於不同的函數指針,此常規排序例程將提供不同的輸出。 例程的其余部分是基本的排序例程,將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.
}

如果nx是偶數,如果將其除以2,它將得到0的余數。例如12 12%2 = 0

34%2 = 0

9%2 = 1 ---->這就是為什么這很奇怪。

每個奇數都可以2m+1的形式寫。現在,如果將這個數字除以2,我們將得到1的余數,因為第一部分給出的余數為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. 

很少有人可以澄清您的想法-

檢查x是否為偶數

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

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

檢查x是否為奇數

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

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

檢查x和y是否均為偶數

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

檢查是否有一個是偶數

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

運算符以%返回其操作數除法的余數。 它可以用來檢查數字是偶數還是奇數,因為如果x/2的余數為0則意味着x是偶數。 這意味着如果x%2=0大於x,則為奇數,否則為奇數。


這是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 
}

這是使用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
    }
}

引用標准:

二進制%運算符從第一個表達式除以第二個表達式得出余數。 如果/或%的第二個操作數為零,則行為不確定。 否則(a / b)* b + a%b等於a。 如果兩個操作數均為非負數,則其余為非負數; 如果不是,則其余符號由實現定義(74)

(74)根據為進行ISO C修訂而進行的工作,整數除法的首選算法遵循ISO Fortran標准ISO / IEC 1539:1991中定義的規則,其中商總是四舍五入。

例:

11 % 3 = 2 ,因為11 = 3*3 + 2

現在,模2運算只能給我們兩個可能的結果:0或1。

1)對於任何給定的偶數NN % 2始終為0。這是因為偶數按定義是2的乘積。

2)對於任何給定的奇數MM % 2始終為1。這是因為可以將任何偶數定義為2K + 1 ,其中KN0 (自然數或零)。

由於上述原因,我們可以將x % 2表達式用作邏輯條件,因為在C / C ++中,“ 0為false,其他所有為true”。

所以:

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
}

現在,讓我們回到您的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);
}

該函數接受兩個參數: nXnY ,如果將nX放在nY之前,則返回true(否則返回false)。 首先,它檢查條件(nX%2)&&!(nY%2) 此條件實質上意味着:

“檢查,如果nX是一個奇數,而nY是一個偶數。”

如果計算結果為true,則函數返回false-首先執行nY ,因此應將nY放在nX之前。

然后,它檢查第二個條件: !(nX%2)&&(nY%2) ,這意味着:

“檢查, nX是偶數, nY是奇數。”

如果計算結果為true,則函數返回true-首先執行nX ,因此nX應該放在nY之前。

如果這兩個條件都被評估為假,則意味着它們兩個都是奇數或偶數。 在這種情況下,函數使用“升序”比較對它們進行比較-首先將采用較小的數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM