拼字游戏点计数器 c++
Scrabble point counter c++
#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]'|">
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
数组(或者,通常对于任何带有sizeof(T) == 1
的T
类型(是使用sizeof(s)
。但是请注意,这不适用于sizeof(T) != 1
的类型。您可能最好不要使用内置数组,而是使用std::vector<char>
:
std::vector<int> s{'a', 'b' /*...*/ };
for (std::size_t i(0); i != s.size(); ++i) {
...
}
您可以消除搜索并使用直接访问。
- 将字符串转换为全部小写
- 从字母中减去"a"以获得相对偏移量。
- 使用相对偏移量作为点数组的索引
下面是一些代码片段示例:
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.
}
还有几种解决问题的方法(除了Dietmar Kühl的答案(
-
在循环开始之前计算保存拼字游戏字母的数组的长度。
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*)
,这是行不通的。 -
一个更好的方法 - 完全避免内部循环。
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; }
相关文章:
- 循环在计数器中不起作用
- python集合的C++等价物是什么.计数器
- Python 集合.计数器,如何避免重复查找
- 请解释字谜的代码,我看不懂计数器数组,每个值已经是0
- std::shared_ptr 自定义参考计数器
- 英特尔 TBB 程序不会终止,可能会误用参考计数器
- 不正确的比较和交换计数器输出用于快速排序功能
- OpenSSL API,使用GCM(伽罗瓦计数器模式)进行AES加密
- C++ 在循环中添加计数器变量并再次初始化其值
- 需要帮助才能在C++中创建类计数器
- 漂亮的计数器习语的错误或格式错误的静态订单惨败?
- 几乎总是自动和带计数器的循环
- 线程使用计数器C++
- 为什么使用 2 个嵌套循环 O(n^2) 复杂度来解决二和问题,当只改变循环计数器逻辑时运行得更快?
- 对于循环计数器不递增
- C++输出的就地计数器
- 在两个值之间嵌入C计数器
- 如何在C++中创建 2d 数组,其中一部分包含循环计数器,另一部分包含数字列表?
- 当输出达到某个值时,如何在 c++ 中中断计数器循环
- OpenCV CV 查找单应断言错误计数器 = > 4