2014-11-05 74 views
-1
#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; 
} 

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

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

+0

堆栈段并不意味着对C++,仅供参考。 – 2014-11-05 20:51:06

+1

这个用例真的需要'std :: map '而不是保留两个平行的数组。 – CoryKramer 2014-11-05 20:51:43

+1

您似乎将Java与C++混为一谈。重新阅读你的教科书介绍数组的章节。它应该包含演示如何在循环中使用它们的代码。 – 2014-11-05 20:52:31

回答

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(或者通常对于任何类型的Tsizeof(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) { 
    ... 
} 
+0

你的答案比我要放大声好很多。当我的upvote充电时,为你+1。 – 2014-11-05 20:55:35

+0

如果数组被衰减,这个答案会受到影响,即将它作为一个参数传递给一个函数 – LoPiTaL 2014-11-05 21:15:40

+0

@LoPiTaL:您的意思是,前两个代码示例将会 - 正确 - 在这种情况下不会编译,提醒用户注意问题? ...激励进一步改变使用'std :: vector '? – 2014-11-05 21:19:06

-1

的更多的方式来解决这个问题(除了迪特马尔库尔答案)

    夫妇
  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; 
    } 
    
+0

如果数组衰减,即将它作为参数传递给一个函数,则第一种解决方案将受到影响。这个解决方案比Dietman Kuhl的解决方案更糟糕,因为他会抛出一个编译时错误,而你的默认编译sizeof(scrabbleLetters)的尺寸是sizeof(char *) – LoPiTaL 2014-11-05 21:18:26

+0

@LoPiTaL,我意识到这一点。它正在被使用的地方,变量的完整声明是可见的。因此'sizeof'将计算为数组中元素的个数,因为它的元素是'char'类型。 – 2014-11-05 21:22:35

0

您可以消除搜索和使用直接访问。

  1. 将字符串到所有小写
  2. 减“一”从信中来获得相对偏移。
  3. 使用相对作为索引偏移量点阵列

这里的一些代码段的例子:

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.  
}