gpt4 book ai didi

c++ - 选择排序方法似乎什么都不做

转载 作者:太空宇宙 更新时间:2023-11-04 13:45:59 25 4
gpt4 key购买 nike

我正在研究一个牌组类,它由一系列卡片对象组成。我必须为这个牌组类编写的方法之一是排序方法,它应该按数字对纸牌进行排序,以顺序排列的纸牌数组结束:每套花色 1 件,每件花色 2 件,每件花色 3 件,等等。有了这个卡类,

class card {
public:
int number;
string suit;
card() {number = 1;suit = "blank";}
card(int n, string s) {number = n; suit = s;}

void printCard() {
cout << number << " of " << suit << "s";
}
};

我正在尝试为 deck 类实现一个方法,该方法使用选择排序对数组进行排序。

这是我的方法 sort() 的代码,在 deck 类中声明和定义:

void sort() {
int min, j;
for(int i=0;i<numCards-1;i++) {
min = i;
for(j=i+1;j<numCards;j++) {
if(cardArray[i].number < cardArray[min].number)
min = j;
}
if(min != i)
swapCards(cardArray[i], cardArray[min]);
}
}

void swapCards(card &a, card &b) {
card temp = a;
a = b;
b = temp;
}

但是,当我在甲板对象 d 上调用它时,似乎什么也没有发生。我已经编写了一些其他方法来洗牌或切牌并交换每一半,这两种方法都有效,但是当我调用 sort() 时,没有任何反应。我一直在通过 printCards() 方法检查数组,当我调用 shuffle() 或 cut() 时它会发生变化,但在我调用 sort() 后它与之前的打印输出保持相同。

在 main() 中:

deck d;

d.printDeck();
cout << endl;
d.shuffle();
d.printDeck();
cout << endl;
d.sort();
d.printDeck();

调试后的打印输出:

1 of hearts
2 of hearts
3 of hearts
4 of hearts
5 of hearts
6 of hearts
7 of hearts
8 of hearts
9 of hearts
10 of hearts
11 of hearts
12 of hearts
13 of hearts
1 of clubs
2 of clubs
3 of clubs
4 of clubs
5 of clubs
6 of clubs
7 of clubs
8 of clubs
9 of clubs
10 of clubs
11 of clubs
12 of clubs
13 of clubs
1 of spades
2 of spades
3 of spades
4 of spades
5 of spades
6 of spades
7 of spades
8 of spades
9 of spades
10 of spades
11 of spades
12 of spades
13 of spades
1 of diamonds
2 of diamonds
3 of diamonds
4 of diamonds
5 of diamonds
6 of diamonds
7 of diamonds
8 of diamonds
9 of diamonds
10 of diamonds
11 of diamonds
12 of diamonds
13 of diamonds

7 of diamonds
6 of spades
5 of diamonds
12 of clubs
2 of diamonds
5 of spades
8 of diamonds
4 of clubs
1 of hearts
8 of clubs
10 of hearts
13 of diamonds
11 of diamonds
12 of spades
2 of hearts
3 of spades
12 of diamonds
7 of spades
11 of hearts
5 of clubs
5 of hearts
1 of spades
10 of diamonds
7 of clubs
4 of diamonds
3 of diamonds
13 of spades
13 of hearts
12 of hearts
6 of clubs
1 of diamonds
11 of spades
9 of hearts
3 of hearts
9 of diamonds
9 of clubs
10 of clubs
10 of spades
9 of spades
2 of spades
6 of hearts
8 of spades
4 of hearts
1 of clubs
11 of clubs
4 of spades
3 of clubs
2 of clubs
6 of diamonds
8 of hearts
13 of clubs
7 of hearts

7 of diamonds
6 of spades
5 of diamonds
12 of clubs
2 of diamonds
5 of spades
8 of diamonds
4 of clubs
1 of hearts
8 of clubs
10 of hearts
13 of diamonds
11 of diamonds
12 of spades
2 of hearts
3 of spades
12 of diamonds
7 of spades
11 of hearts
5 of clubs
5 of hearts
1 of spades
10 of diamonds
7 of clubs
4 of diamonds
3 of diamonds
13 of spades
13 of hearts
12 of hearts
6 of clubs
1 of diamonds
11 of spades
9 of hearts
3 of hearts
9 of diamonds
9 of clubs
10 of clubs
10 of spades
9 of spades
2 of spades
6 of hearts
8 of spades
4 of hearts
1 of clubs
11 of clubs
4 of spades
3 of clubs
2 of clubs
6 of diamonds
8 of hearts
13 of clubs
7 of hearts
Press any key to continue . . .

考虑到我已经成功地为其他涉及数组和类的项目实现了选择排序,我不太清楚为什么数组似乎没有任何反应。我在这里做的唯一新奇的事情是对具有多个变量的对象数组进行排序,但我很确定我正在比较 sort() 中每张卡片之间的正确变量。

如果有人看到明显的错误或知道可能导致此问题的原因,请告诉我。这对帮助我解决这个问题大有帮助。

编辑:为 deck 类和调试输出中使用的其他方法添加了代码。

最佳答案

您的问题出在 min 的选择上:

for(j=i+1;j<numCards;j++) {
if(cardArray[i].number < cardArray[min].number)
min = j;
}

在整个循环中,min 没有改变,所以 if 语句没有意义。您可能想根据当前最小值检查 cardArray[j]:

for(j=i+1;j<numCards;j++) {
if(cardArray[j].number < cardArray[min].number)
min = j;
}

关于c++ - 选择排序方法似乎什么都不做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25956077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com