0

我正在尝试解析来自文件的输入以表示标准的一副纸牌(即 2C 代表两个俱乐部)。但是,我的解决方案没有按预期工作,并且声明所有输入都无效。我在我的代码中看不到任何逻辑错误,所以我想获得第二意见。代码如下:

/*
 * Determines if the input string is valid.
 * 
 * A string is considered valid if it begins with either a number (2-10) 
 * or a letter (J/j, Q/q, K/k) to deetermine rank, followed by a letter to
 * determine suit (C/c, D/d, H/h, S/s).
 */
bool inputValidator(string cardData)
{
    if (cardData.length() == 2) //Input string is two characters long
    {
        if (cardData[0] < '2' || cardData[0] > '9'
            || cardData[0] != 'J' || cardData[0] != 'j'
            || cardData[0] != 'Q' || cardData[0] != 'q'
            || cardData[0] != 'K' || cardData[0] != 'k'
            || cardData[0] != 'A' || cardData[0] != 'a')
        {
            cout << "Card with data " << cardData << " has an invalid rank." << endl;
            return false;
        }
        if (cardData[1] != 'C' || cardData[1] != 'c' //Parse suit
            || cardData[1] != 'D' || cardData[1] != 'd'
            || cardData[1] != 'H' || cardData[1] != 'h'
            || cardData[1] != 'S' || cardData[1] != 's')
        {
            cout << "Card with data " << cardData << " has an invalid suit." << endl;
            return false;
        }
        return true;
    }
    else if (cardData.length() == 3) //Input string is three characters long
        //This occurs only if the number is 10.
    {
        if (cardData[0] != '1' || cardData[1] != '0') //Parse rank
        {
            cout << "Card with data " << cardData << " has an invalid rank." << endl;
            return false;
        }
        if (cardData[2] != 'C' || cardData[2] != 'c' //Parse suit
            || cardData[2] != 'D' || cardData[2] != 'd'
            || cardData[2] != 'H' || cardData[2] != 'h'
            || cardData[2] != 'S' || cardData[2] != 's')
        {
            cout << "Card with data " << cardData << " has an invalid suit." << endl;
            return false;
        }
        return true;
    }
    return false;
}

如果有任何逻辑缺陷(或天生更好的方法),我会很感激被告知。谢谢。

4

4 回答 4

4

您正在编写这样的子句:

cardData[2] != 'D' || cardData[2] != 'd'

这始终是正确的,因为被测试的变量不能同时是两个值。您可能打算使用&&而不是||.

您当然可以简化逻辑,例如在比较之前将输入转换为小写或大写。

于 2013-02-01T22:28:43.527 回答
2

问题似乎在于您结合条件的方式。如果我正确理解您的期望,那么您想要的第一个条件是:

    if (!(cardData[0] > '2' && cardData[0] < '9')
        && cardData[0] != 'J' && cardData[0] != 'j'
        && cardData[0] != 'Q' && cardData[0] != 'q'
        && cardData[0] != 'K' && cardData[0] != 'k'
        && cardData[0] != 'A' && cardData[0] != 'a')

而你想要的第二个条件是这样的:

    if (cardData[1] != 'C' && cardData[1] != 'c' //Parse suit
        && cardData[1] != 'D' && cardData[1] != 'd'
        && cardData[1] != 'H' && cardData[1] != 'h'
        && cardData[1] != 'S' && cardData[1] != 's')
于 2013-02-01T22:33:16.543 回答
1

您可以稍微简化条件:并且必须更改您的条件以执行您想要的操作。

bool inputValidator(string cardData)
{
    if (cardData.length() == 2) //Input string is two characters long

   {
        if (!((cardData[0] >= '2' && cardData[0] <= '9')
            || (cardData[0]|32) == 'j'
            || (cardData[0]|32) == 'q'
            || (cardData[0]|32) == 'k'
            || (cardData[0]|32) == 'a'))
        {
           cout << "Card with data " << cardData << " has an invalid rank." << endl;
           return false;
         }

        if (!((cardData[1]|32) == 'c' //Parse suit
            || (cardData[1]|32) == 'd'
            || (cardData[1]|32) == 'h'
            || (cardData[1]|32) == 's'))
        {
            cout << "Card with data " << cardData << " has an invalid suit." << endl;
            return false;
        }
        return true;
    }
    else if (cardData.length() == 3) //Input string is three characters long
        //This occurs only if the number is 10.
    {
        if (!(cardData[0] == '1' || cardData[1] == '0')) //Parse rank
        {
            cout << "Card with data " << cardData << " has an invalid rank." << endl;
            return false;
        }
        if (!((cardData[2]|32) == 'C' //Parse suit
            || (cardData[2]|32)  == 'd'
            || (cardData[2]|32)  == 'h'
            || (cardData[2]|32)  == 's'))
        {
            cout << "Card with data " << cardData << " has an invalid suit." << endl;
            return false;
        }
        return true;
    }
    return false;
}

第二个/第三个字符的代码重复也应该重构。

于 2013-02-01T22:33:30.757 回答
1

您的逻辑表达式不正确,而且您正在复制代码,请尝试将它们简单地转换为函数。

bool inputValidator(string cardData)
{
    if (cardData.length() == 2 && IsValidCard(cardData[0])) //Input string is two characters long
    {
        return IsValidSuite(cardData[1]);
    }
    else if(cardData.length() == 3) 
    {
        if (isValidRank(cardData[0]))
        {
            return IsValidSuite(cardData[2]);
        }
    }
    return false;
}

bool isValidRank(char c)
{
    if (c =='0' || c=='1')[
    {
        return true;
    }
    return false;
}

bool IsValidCard(char c)
{
  if (c > '2' && c < '9')
  {
    return true;
  }

  switch(c)
  {
    case 'J':
    case 'j':
    case 'Q':
    case 'q':
    case 'K':
    case 'k':
    case 'A':
    case 'a':
      return true;
  }

  return false;
}    

bool IsValidSuite(char c)
{
  switch(c)
  {
    case 'C':
    case 'c':
    case 'D':
    case 'd':
    case 'H':
    case 'h':
    case 'S':
    case 's':
      return true;
  }
  return false;
}
于 2013-02-01T22:38:47.720 回答