拼字游戏点计数器c ++

时间:2014-11-05 20:50:06

标签: c++ counter point

#include <iostream>
#include <string>

using namespace std;
int score(string s);
char scrabbleLetters[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int scrabblePoints[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};

int main()
{
    string sWord;
    cout << "Enter the scrabble word you'd like to score.";
    cin >> sWord;
    cout << "You scored " << score(sWord)<< " points for that word!";

}

int score(string s)
{   int points = 0;
    for (int i = 0; i < s.length(); i++)
    {
        for (int j = 0; j < scrabbleLetters.length(); j++)
        {
            if (s[i] == scrabbleLetters[j])
                points += scrabblePoints[j];
        }
    }
    return points;
}

我似乎无法弄清楚为什么我的代码没有编译。该程序应该询问用户一个单词,然后根据每个字母的点数对该单词进行评分。

我收到的当前错误是:“错误:在'scrabbleLetters'中请求成员'长度',这是非类型'char [26]'|”

3 个答案:

答案 0 :(得分:2)

C ++内置数组没有length()成员函数。找到大小的一种方法是使用

for (int i = 0; i < std::distance(std::begin(s), std::end(s)); ++i) {
    ...
}

鉴于上述方法有点难看,可以将其打包成一个函数,例如:

template <typename T, std::size_t Size>
constexpr std::size_t length(T const (&)[Size]) {
    return Size;
}
...
for (std::size_t i(0); i != length(s); ++i) {
    ...
}

具体来说,对于char(或者通常适用于T的任何类型sizeof(T) == 1)的数组,使用sizeof(s)。但请注意,适用于sizeof(T) != 1的类型。您可能最好使用内置数组,而是使用std::vector<char>

std::vector<int> s{'a', 'b' /*...*/ };
for (std::size_t i(0); i != s.size(); ++i) {
    ...
}

答案 1 :(得分:0)

您可以取消搜索并使用直接访问权限。

  1. 将字符串转换为全部小写
  2. 从字母中减去'a'以获得相对偏移。
  3. 使用相对偏移量作为点数组的索引
  4. 以下是一些代码段示例:

    const unsigned int scrabblePoints[] =
    {1, 3, 3,  2, 1, 4, 2, 4, 1, 8, 5, 1,  3,
     1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
    
    int main()
    {
        string sWord;
        cout << "Enter the scrabble word you'd like to score.";
        cin >> sWord;
    
        // Transform the word into all lowercase.
        std::transform(sWord.begin(), sWord.end(), sWord.begin, std::tolower);
    
        unsigned int points = 0;
        for (unsigned int i = 0; i < sWord.length(); ++i)
        {
           const char c = sWord[i];
    
           // Check if the character is a letter,
           //    it could be something like '?'.
           if (isalpha(c))
           {
             // Since the point's array starts with the letter 'a',
             // the index can be calculated by subtracting 'a' from
             // the character.
             unsigned int index = c - 'a';
             points += scrabblePoints[index];
           }
        }
        cout << "You scored "
             << points
             << " points for that word!"
             << "\n";
      return 0; // Since main() returns a value.    
    }
    

答案 2 :(得分:-1)

解决问题的方法还有很多(除了DietmarKühl的答案)

  1. 在循环开始之前计算持有拼字游戏字母的数组的长度。

    int score(string s)
    {
       int points = 0;
       int len = sizeof(scrabbleLetters);
       for (int i = 0; i < s.length(); i++)
       {
          for (int j = 0; j < len; j++)
          {
             if (s[i] == scrabbleLetters[j])
                points += scrabblePoints[j];
          }
       }
       return points;
    }
    

    提醒:这种做法很脆弱。 scrabbleLetters的定义必须对函数可见才能实现。否则,sizeof(scrabbleLetters)将最终成为sizeof(char*),这将无效。

  2. 更好的方法 - 完全避免内循环。

    int score(string s)
    {
       int points = 0;
       for (int i = 0; i < s.length(); i++)
       {
          char ch = s[i];
          points += scrabblePoints[ch-'a'];
       }
       return points;
    }