我需要帮助检查我的代码。任何评论都非常感谢

I'm in need of help checking my code. Any reviewing is greatly apreciated

本文关键字:评论 任何 非常 感谢 代码 帮助 检查 我的      更新时间:2023-10-16

这是我的老师提供的驱动程序代码,我不打算编辑。

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 00n 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 TSn 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;
                }
            }
        }
    }
}

这是我得到的编译器错误。我不知道该怎么办,看起来它们在我不应该编辑的文件中。我很感激你能给予的帮助。

PlayingCardTest.cpp|103|错误:'PlayingCard PlayingCard::makeValidCard(int, int)'无法重载|

PlayingCardTest.cpp|5|error: with 'PlayingCard PlayingCard::makeValidCard(int, int)'|

PlayingCardTest.cpp|169|错误:期望'}'在输入结束|

PlayingCardTest.cpp|169|error: expected unqualified-id at end of input|

||===构建完成:4个错误,0个警告===|

您的头文件末尾缺少};

第一轮评论:

  1. Style unit:顺序section为"public","protected",然后是"private"。私人部门不应该先于公共部门。这在技术上不是必需的,但却是相当标准的做法。
  2. Style nit:使用单独的语句声明每个变量,每个变量在自己的行上。使用逗号是陷入麻烦的好方法(例如在声明指针类型时),并且是糟糕的风格。
  3. 在构造函数中使用初始化列表,而不是使用赋值操作符。
  4. 你应该在你的头文件中包含""来使用std::string。

第二轮评论:

  1. 你在奇怪地初始化数组;你应该使用{}作为括号。
  2. 初始化时不需要指定数组的大小。
  3. Style nit:不要在你的代码中使用像"12"这样的神奇常数。相反,将它们赋值给一个变量,如value_length或value_count,并使用已命名的变量。
  4. 你的意思是做一个相等比较("==")或赋值("=")在你的if语句?如果你想做一个赋值,你应该把它移出If。

第三轮评论:

    在非默认构造函数和setCard函数之间不必要地重复代码。您应该能够在这两个函数之间共享代码。因为setCard不是一个虚函数,所以你应该能够简单地从构造函数中调用它。你的setCard逻辑似乎相当复杂。大多数"集合"函数都比这简单得多。您应该考虑添加文档来解释它试图做的事情的逻辑。
  1. "getValue()"、"getCardCode()"、"getSuit()"answers"isValid()"函数应声明为"const"。

第四轮评语:

  1. 既然你的教授做"PlayingCard card = makeValidCard(....)",很明显,他希望你的卡类支持分配。因为你的"setCard()"函数和你的非默认构造函数做一些事情,而不是简单地设置属性,这将是有意义的提供一个"PlayingCard&operator=(const PlayingCard&);"赋值操作符以及"PlayingCard::PlayingCard(const PlayingCard&)"复制构造函数。如果您没有提供这些,最好添加注释,说明使用默认赋值/copy进行复制是有意允许的。