进行CS50实验室时的分割故障(核心倾倒)错误

发布于 2025-01-19 19:14:22 字数 1284 浏览 2 评论 0原文

当我输入 ./scrabble 并输入玩家 1 和 2 的单词时,结果是分段错误(核心已转储)。我查看了其他类似的解决方案,他们没有错误,那么我哪里出错了。

 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
        if ( score1 > score2 )
        {
            printf("Player 1 Wins\n");
        }
        else if (score1 < score2)
        {
            printf("player 2 Wins\n");
        }
        else if (score1 == score2)
        {
            printf("Tie\n");
        }
    }
    
    int compute_score(string word)
    {
        // TODO: Compute and return score for string
        int score= 0;
        for (int i = 0; i < strlen(word); i++)
        {
            if islower(word)
            {
                score = POINTS[(word[i])-65];
            }
            if isupper(word)
            {
                score = POINTS[(word[i])-97];
            }
        }
        return score;
    }

when I type ./scrabble and enter Player 1 and 2's words, the result is segmentation fault(core dumped). I looked at other similar solutions and they had no errors, so where did i make the mistake.

 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
        if ( score1 > score2 )
        {
            printf("Player 1 Wins\n");
        }
        else if (score1 < score2)
        {
            printf("player 2 Wins\n");
        }
        else if (score1 == score2)
        {
            printf("Tie\n");
        }
    }
    
    int compute_score(string word)
    {
        // TODO: Compute and return score for string
        int score= 0;
        for (int i = 0; i < strlen(word); i++)
        {
            if islower(word)
            {
                score = POINTS[(word[i])-65];
            }
            if isupper(word)
            {
                score = POINTS[(word[i])-97];
            }
        }
        return score;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云胡 2025-01-26 19:14:22

您正在从字符代码中减去错误的值,因为65a的ASCII代码,而不是a。通过使用角色文字而不是ASCII代码来避免这样的问题。

islower()isuper()的参数必须是单词word [i]的当前字符,而不是整个字符串Word

您也不会增加分数,而是在循环中覆盖它。因此,得分将只是单词中的最后一个字母。使用+=添加到变量而不是=进行分配。

    int compute_score(string word)
    {
        int score= 0;
        for (int i = 0; i < strlen(word); i++)
        {
            if islower(word[i])
            {
                score += POINTS[(word[i])-'a'];
            }
            else if isupper(word[i])
            {
                score += POINTS[(word[i])-'A'];
            }
        }
        return score;
    }

You're subtracting the wrong values from the character codes, because 65 is the ASCII code for A, not a. Avoid problems like this by using character literals rather than ASCII codes.

The argument to islower() and isupper() must be the current character in the word word[i], not the whole string word.

You're also not adding to the score, you're overwriting it in the loop. So the score will just be for the last letter in the word. Use += to add to the variable instead of = to assign it.

    int compute_score(string word)
    {
        int score= 0;
        for (int i = 0; i < strlen(word); i++)
        {
            if islower(word[i])
            {
                score += POINTS[(word[i])-'a'];
            }
            else if isupper(word[i])
            {
                score += POINTS[(word[i])-'A'];
            }
        }
        return score;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文