我正在开发一个由一系列卡片对象组成的甲板类。我必须为这个甲板类写的方法之一是一种排序方法,它应该按照数字对卡片进行排序,按顺序排列成一组卡片:每件西装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类和调试输出中使用的其他方法添加了代码。
答案 0 :(得分:0)
您的问题在于选择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;
}