This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center。
9年前关闭。
这是我的讲师提供的驱动程序代码,并不意味着我可以对其进行编辑。
PlayingCardTest.cpp :
样式:分别命令“公共(public)”,“ protected ”,“私有(private)”。私有(private)部分不应公开。这在技术上不是必需的,而是相当标准的做法。 样式nit:使用单独的语句声明每个变量,每个语句单独一行。使用逗号是遇到麻烦的好方法(例如,在声明指针类型时)并且样式很差。 在构造函数中使用初始化列表,而不要使用赋值运算符。 您应该在 header 中包含“”以使用std::string。
第二轮评论:
您正在怪异地初始化数组;您应该使用{}作为方括号。 您无需在初始化中指定数组的大小。 风格:请勿在代码中使用魔术常量,例如“12”。而是将它们分配给诸如value_length或value_count的变量,并使用命名的变量。 您的意思是在if语句中进行均等比较(“==”)或赋值(“=”))吗?如果您打算进行分配,则应将其移至if之外。
第三轮评论:
您可以在非默认构造函数和setCard函数之间不必要地复制代码。您应该能够在这两个函数之间共享代码。由于setCard不是虚函数,因此您应该能够从构造函数中简单地调用它。 您的setCard逻辑似乎相当复杂。大多数“设置”功能远不那么简单。您应该考虑添加说明其所要执行的逻辑的文档。 应将“getValue()”,“getCardCode()”,“getSuit()”和“isValid()”函数声明为“const”。
第四轮评论:
由于您的教授执行“PlayingCard卡= makeValidCard(....)”,因此很明显他希望您的卡类支持作业。由于您的“setCard()”函数和非默认构造函数除了简单地设置属性外,还需要执行其他操作,因此提供“PlayingCard&operator =(const PlayingCard&);”是有意义的。赋值运算符以及“PlayingCard::PlayingCard(const PlayingCard&)”副本构造函数。如果您不提供这些信息,则最好添加注释,以表明已故意允许使用默认分配/副本进行复制。
9年前关闭。
这是我的讲师提供的驱动程序代码,并不意味着我可以对其进行编辑。
PlayingCardTest.cpp :
#include <iostream>
#include "PlayingCard.h"
PlayingCard makeValidCard(int value, int suit);
int main()
{
// Create a playing card
PlayingCard card1;
// Test the default constructor and GetCardCode
std::cout << "Testing default constructor. Expect card code to be 00\n card code is :";
std::cout << card1.getCardCode() << std::endl << std::endl;
// Test the setter and getter
std::cout << "Seting card to 'AH' using SetValue and SetSuit" << std::endl;
card1.setCard('A', 'H');
std::cout << "GetValue returns :" << card1.getValue() << std::endl;
std::cout << "GetSuit returns :" << card1.getSuit() << std::endl << std::endl;
// Test overloaded constructor
PlayingCard tenOfSpades('T', 'S');
std::cout << "Testing overloaded constructor. Expect card code to be TS\n card code is :";
std::cout << tenOfSpades.getCardCode() << std::endl << std::endl;
// Test IsValid with valid cards
std::cout << "Testing valid card codes.\n"
<< "Expect isValid to return true for all (except perhaps Jokers.)"
<< std::endl;
// Create and test valid cards
int validCards = 0; // cards that return true for IsValid
int invalidCards = 0; // cards that return false for IsValid
// Create and test four suits plus the jokers
for(int suit = 1; suit <= 5; suit++)
{
// Create and test ace, 2 - 9, Jack, Queen, and King
for(int value = 1; value <= 13; value++)
{
PlayingCard aCard = makeValidCard(value, suit);
std::cout << "Card Code: " << aCard.getCardCode() << " IsValid :";
if (aCard.isValid())
{
validCards++;
std::cout << "true" << std::endl;
}
else
{
invalidCards++;
std::cout << "false" << std::endl;
}
// suit 5 is just for creating the two Jokers
if (suit == 5 && value >= 2)
break;
}
}
std::cout << "IsValid returned false for " << invalidCards << " card codes" << std::endl;
std::cout << "IsValid returned true for " << validCards << " card codes" << std::endl;
std::cout << std::endl;
// Test IsValid with invalid cards
// Create and test invalid cards
std::cout << "Testing invalid card codes; isValid should return false for all." << std::endl;
validCards = 0;
invalidCards = 0;
// Loop through all possible ASCII character codes for card codes
for(int suit = 0; suit <= 255; suit++)
for(int value = 0; value <= 255; value++)
{
// Only check card codes that are not valid
PlayingCard aCard = makeValidCard(value, suit);
if (aCard.getCardCode() == "00")
{
if (aCard.isValid())
{
std::cout << "value :" << value << " suit :" <<suit << " IsValid :";
std::cout << "true" << std::endl;
validCards++;
}
else
{
invalidCards++;
}
}
}
std::cout << "IsValid returned false for " << invalidCards << " card codes" << std::endl;
std::cout << "IsValid returned true for " << validCards << " card codes" << std::endl;
return 0;
}
/******************************************************/
/* Test Functions */
/******************************************************/
PlayingCard makeValidCard(int iValue, int iSuit)
{
char value = '0';
char suit = '0';
switch (iValue)
{
case 1:
value = 'A';
break;
case 10:
value = 'T';
break;
case 11:
value = 'J';
break;
case 12:
value = 'Q';
break;
case 13:
value = 'K';
break;
default:
if ((iValue >= 2) && (iValue <= 9))
value = '0' + iValue;
break;
}
switch (iSuit)
{
case 1:
suit = 'D';
break;
case 2:
suit = 'S';
break;
case 3:
suit = 'C';
break;
case 4:
suit = 'H';
break;
// Special case for the Joker
case 5:
if(iValue == 1)
{
value = 'Z';
suit = 'B';
}
else if(iValue == 2)
{
value = 'Z';
suit = 'R';
}
else
{
value = '0';
suit = '0';
}
break;
}
PlayingCard testCard(value, suit);
return testCard;
}
这是我的头文件 PlayingCard.h :#ifndef PLAYINGCARD_H_INCLUDED
#define PLAYINGCARD_H_INCLUDED
class PlayingCard
{
private:
char suit, value;
public:
PlayingCard(){suit = '0'; value = '0';}
PlayingCard(char myValue, char mySuit);
char getValue() {return value;}
char getSuit() {return suit;}
std::string getCardCode();
bool setCard(char myValue, char mySuit);
bool isValid();
#endif // PLAYINGCARD_H_INCLUDED
这是我的类实现文件 PlayingCard.cpp :#include "PlayingCard.h"
PlayingCard::PlayingCard (char myValue, char mySuit)
{
char aValue[13] ('2','3','4','5','6','7','8','9','T','J','Q','K','A'))
char aSuit[4] {'D','H','C','S']
for(count = 0; count <= 12; count++)
{
if (myValue = aValue[count])
{
for (count2 = 0; count2 <= 3; count2++)
{
if (mySuit = aSuit[count2++])
{
suit = mySuit;
value = myValue;
}
}
}
}
}
bool PlayingCard::setCard(char myValue, char mySuit)
{
char aValue[13] ('2','3','4','5','6','7','8','9','T','J','Q','K','A'))
char aSuit[4] {'D','H','C','S']
for(count = 0; count <= 12; count++)
{
if (myValue = aValue[count])
{
for (count2 = 0; count2 <= 3; count2++)
{
if (mySuit = aSuit[count2++])
{
suit = mySuit;
value = myValue;
}
else
{
return false;
}
}
}
}
}
string PlayingCard::getCardCode()
{
return suit + value;
}
bool PlayingCard::isValid()
{
char aValue[13] ('2','3','4','5','6','7','8','9','T','J','Q','K','A'))
char aSuit[4] {'D','H','C','S']
for(count = 0; count <= 12; count++)
{
if (myValue = aValue[count])
{
for (count2 = 0; count2 <= 3; count2++)
{
if (mySuit = aSuit[count2++])
{
return true;
}
else
{
return false;
}
}
}
}
}
这就是我得到的编译器错误。我不确定该怎么办,好像它们在我不应该编辑的文件中。我非常感谢您能提供的帮助。最佳答案
第一轮评论:
第二轮评论:
第三轮评论:
第四轮评论:
10-06 00:59