簡體   English   中英

似乎無法讓我的結構數組正確排序?

[英]Can't seem to get my array of structs to sort properly?

我有一個像這樣標題為字母的 letterData 結構數組:

struct letterData
{
    char letter = '0';
    double occurrencePercent = 0;
    int letterOccurrences = 0;
};

int main()
{
     letterData letters[26];
}

我正在嘗試通過這樣的occurrencePercent 成員對這個結構數組進行排序:

do
    {
        bool swapped = false;
        for (int count = 0; count < 25; count++)
        {
            if (letters[count].occurrencePercent < letters[count + 1].occurrencePercent)
            {
                swap(letters[count], letters[count + 1]);
                swapped = true;
            }
        }
    } while (swapped);

int width = 0;

for (int count = 0; count < 25; count++)
    {
        if (letters[count].occurrencePercent == 0)
            width = 36;
        else
            width = 30;
        cout << "\n" << letters[count].letter << ": " << letters[count].occurrencePercent << '%';
        cout << right << setw(width) << standLetters[count].letter << ": " << standLetters[count].occurrencePercent << '%';
    }

另外,這是我的交換功能:

void swap(struct letterData& a, struct letterData& b)
{
    struct letterData temp;
    temp = a;
    a = b;
    b = temp;
}

但是,數組沒有正確排序。 下面是調試器的樣子:

Given Letter Frequency                  Standard Letter Frequency


b: 8.05085%                             e: 12.02%
a: 1.69492%                             t: 9.1%
d: 2.54237%                             a: 8.12%
c: 0%                                   o: 7.68%
f: 2.54237%                             i: 7.31%
g: 8.47458%                             n: 6.95%
h: 11.0169%                             s: 6.28%
i: 1.69492%                             r: 6.02%
e: 0%                                   h: 5.92%
k: 4.23729%                             d: 4.32%
l: 2.54237%                             l: 3.98%
m: 2.54237%                             u: 2.88%
n: 1.27119%                             c: 2.71%
o: 5.50847%                             m: 2.61%
p: 9.32203%                             f: 2.3%
q: 8.47458%                             y: 2.11%
r: 5.50847%                             w: 2.09%
s: 1.69492%                             g: 2.03%
t: 6.77966%                             p: 1.82%
u: 1.27119%                             b: 1.49%
v: 1.27119%                             v: 1.11%
w: 8.05085%                             k: 0.69%
x: 5.50847%                             x: 0.17%
j: 0%                                   q: 0.11%
y: 0%                                   j: 0.1%

在左列中,最高百分比應位於頂部,反之亦然。 為什么它不起作用?

對於您的原始函數,我必須對其進行正確編譯和排序的唯一更改是將 swapped 的聲明移到 do...while 循環之外。

我懷疑您在代碼中的其他地方聲明了該變量,這避免了編譯錯誤,但是第二個聲明掩蓋了您的變量,並且 do...while 沒有正確檢測到交換已經發生並且只執行了一次傳遞在循環上。

bool swapped = false;
do
{
    swapped = false;
    for (int count = 0; count < 25; count++)
    {
        if (letters[count].occurrencePercent < letters[count + 1].occurrencePercent)
        {
            swap(letters[count], letters[count + 1]);
            swapped = true;
        }
    }
} while (swapped);

暫無
暫無

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

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