如何引用点数组,对要求用户输入的每个字符进行评分,C

How to refer back to an array of points, to score each character that user has been asked to input, C

提问人:Nermin 提问时间:9/30/2020 最后编辑:Nermin 更新时间:11/4/2023 访问量:396

问:

所以,我有一个数组,看起来像这样:

// Points assigned to each letter of the alphabet

 int POINTS[] = {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};

按顺序,数组中的每个整数都对应于字母表中的一个字母。A 或 a 值 1 分,B 或 B 值 3 分。

我已经提示用户输入,我将如何评估用户输入并将其与数组内整数的位置进行比较?

我知道我必须创建一个函数,并在该函数内部使用某种循环,但我不确定循环内的检查条件应该是什么。 我还猜我应该将输入的字符串转换为整数,希望能取回 ASCII 数字。

// Points assigned to each letter of the alphabet
int POINTS[] = {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 compute_score(string word);

int main(void)
{
    // Get input words from both players
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    // Score both words
    int score1 = compute_score(word1);
    int score2 = compute_score(word2);

    // TODO: Print the winner
}

int compute_score(string word)
{
    // TODO: Compute and return score for string
}

我不太确定如何以及在我的compute_score函数中准确计算什么。

数组 C 函数 for 循环

评论

0赞 Sourav Ghosh 9/30/2020
太宽泛了...你能说得更具体一点吗?
0赞 Nermin 9/30/2020
我已经用我的代码更新了我的帖子,现在可能更有意义。

答:

0赞 Sourav Ghosh 9/30/2020 #1

我会尽力帮助你解决逻辑问题(基于我对问题的理解)。

假设您使用的是具有 ASCII 字符编码的平台(该编码严格增加字母的序列值):

  • 使用 读取用户输入。fgets()
  • 从输入中剥离换行符后,测量输入的长度。
  • 循环遍历输入中的每个元素,然后
    • 如果输入的值介于 和 之间,则从该值中减去,并将其用作索引值以从数组中查找权重。'A''Z''A'POINTS
    • 否则,如果输入的值介于 和 之间,则从该值中减去,并将其用作索引值以从数组中查找权重。'a''z''a'POINTS
    • 否则,请跳到下一个值。
  • 继续添加值,直到用户输入中的最后一个元素,并根据最终值决定结果。
0赞 Halfa 10/1/2020 #2

我认为这可以工作:

int get_letter_nb(char c)
    {
        if (c > 64 && c < 91)
            return (c - 64);
        if (c > 96 && c < 123)
            return (c - 96);
        return (-1)      // error : it's not a letter
    }

int compute_score(string word)
{
    int count = 0;
    int temp = 0;

    for (int i = 0; word[i], i++) {
        if (temp = get_letter_nb(word[i]) == -1)
            return (-1);          // error : handling
        count += temp;
    }
    return POINTS[count];
}

问任何问题,我取了字母的 ASCII 代码,我减去 have A 或 A == 1get_letter_nb

例如 'a' == 97 in ascii,所以我返回 97 - 97,然后 'a' 等于 0,在你的数组中它是 POINTS[0] for a

请随时询问^^

0赞 IsaacHarrison 11/4/2023 #3

您可以通过创建另一个实际字母数组 LETTERS 来实现此结果。由于 POINTS 数组中的数字值按与其字母顺序相对应的正确顺序排列,这意味着实际字母数组中 'a' 的索引 LETTERS 将对应于其在 POINTS 数组中的值。

为了在 compute_score() 中完成程序的实现,我们可以遍历 LETTERS 数组和作为参数给出的 'word' 字符串,并检查 word[index] 中的字符是否等于 LETTERS[index] 中的字符,那么因为 LETTERS 数组中的字符的索引对应于它们在 POINTS 数组中同一索引处的分数值, 我们可以在 LETTERS 中获取字符的索引并在 POINTS[letter_index_in_LETTERS] 处获取其值,然后将该值从 POINTS 添加到计数器 score 中。

#include <ctype.h>
#include <stdio.h>
#include <string.h>

// Points assigned to each letter of the alphabet
int POINTS[] = {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};
char LETTERS[] = {'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 compute_score(string word);

int main(void)
{
    // Get input words from both players
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    // Score both words
    int score1 = compute_score(word1);
    int score2 = compute_score(word2);

    // TODO: Print the winner
    if (score1 > score2)
    {
        printf("player 1 wins! \n");
    }
    else if (score1 < score2)
    {
        printf("player 2 wins! \n");
    }
    else
    {
        printf("tie! \n");
    }
}

int compute_score(string word)
{
    // TODO: Compute and return score for string
    int stringLength = strlen(word);

    // keep track of the score
    int score = 0;

    // looping through the word so we can have individual letters compared with their position in Letters array
    for (int i = 0; i < stringLength; i++)
    {

        // convert all upperCase letters to lowerCase letters equivalent to the ones in LETTERS
        char letterInWord = tolower(word[i]);

        
        for (int j = 0, arraySize = sizeof(LETTERS) / sizeof(LETTERS[1]); j < arraySize; j++)
        {
            // if the character at index i is not a letter then we have to add 0 to score
            if (isalpha(word[i]) == 0)
            {
                score += 0;
            }
            else if (LETTERS[j] == letterInWord)
            {
                // if the characters match then we get the index of the character in LETTERS to find out its relative score
                // This is possible because we have created our LETTERS array in the same order we have been told the POINTS are
                // made
                printf("currently adding %i to score because of %c", POINTS[j], word[i]);
                score += POINTS[j];
            }
        }
    }

    return score;
}

但是,请谨慎提示用户输入 word1 和 word2。之所以如此,是因为 get_string() 方法不是 C 语言中的内置函数,而是由哈佛大学在 cs50 课程中使用的 <cs50.h> 标头标签提供的,因此如果代码中未使用此标头,则提供的实际代码可能不起作用。我只在我的解决方案中使用了它,因为问题的作者将其包含在问题的代码片段中。 请注意这一点!