CS50实验室2给我找麻烦

jv4diomz  于 2023-04-29  发布在  其他
关注(0)|答案(2)|浏览(113)

我最近一直在做很多CS50项目,当我进入实验2时,我遇到了一个障碍。这个实验对我来说很难,但我还是成功了。..有点。当我运行它时,它会显示两个提示,但无论我输入什么,它总是一个平局。

#include <ctype.h>
#include <cs50.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};

int compute_score(string word1, string word2);

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

    // Convert to uppercase
    int toupper(int word1);
    int toupper(int word2);

    // Score both words
    int score = compute_score(word1, word2);
    // TODO: Print the winner
    if (score == 1) {
        printf("Player 1 Wins!");
    }
    else if (score == 2) {
        printf("Player 2 Wins!");
    }
    else {
        printf("Tie!");
    }
    printf("\n");
}

int compute_score(string word1, string word2)
{
    int i, j, k, n1=strlen(word1), n2=strlen(word2), p1=0, p2=0;
    const int alphabetSize = 26;
    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'};
    for (i=0; i<n1 + n2; i++) {
        for (j=0; j<alphabetSize; j++) {
            if (letters[j] == word1[i]) {
                p1 += POINTS[j];
            }
        }
        for (k=0; k<alphabetSize; k++) {
            if (letters[k] == word2[i]) {
                p2 += POINTS[k];
            }
        }

    }
    if (p1 > p2) {
        return 1;
    }
    else if (p1 < p2) {
        return 2;
    }
    else {
        return 3;
    }
}
7nbnzgx9

7nbnzgx91#

这两条线

// Convert to uppercase
int toupper(int word1);
int toupper(int word2);

只不过是同一个函数toupper的声明。
如果你想把字符串转换成大写,你可以自己编写一个单独的函数,例如

string string_toupper( string word )
{
    for ( string s = word; *s != '\0'; ++s )
    {
        *s = toupper( ( unsigned char ) *s );
    }

    return word;
}

main中,而不是上面所示的两行

// Convert to uppercase
string_toupper( word1 );
string_toupper( word2 );

函数compute_score中的for循环

for (i=0; i<n1 + n2; i++) {

由于访问索引在[0, n1 + n2)范围内的字符串之外的内存,导致未定义的行为
相反,编写一个单独的函数来计算字符串的得分,如

int compute_score( string word )
{
    int score = 0;

    string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    // 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};

    for ( ; *word != '\0'; ++word ) 
    {
        string p = strchr( letters, *word );
        if ( p != NULL ) score += POINTS[p - letters];
    }

    return score;
}

在main中你可以写

int score1 = compute_score( word1 );
int score2 = compute_score( word2 );

// TODO: Print the winner
if ( score2 < score1 ) {
    puts("Player 1 Wins!");
}
else if ( score1 < score2 ) {
    puts("Player 2 Wins!");
}
else {
    puts("Tie!");
}

请注意,使用char *const char *来代替typedef名称string会更好。

dddzy1tm

dddzy1tm2#

下面的解决方案是使用Ada编程语言完成的。这是为了表明,选择解决问题的语言可以影响解决方案的表达方式。

-- Ada version of CS50 Lab 2

with Ada.Text_IO;             use Ada.Text_IO;
with Ada.Characters.Handling; use Ada.Characters.Handling;

procedure Main is
   subtype Upper_Case is Character range 'A' .. 'Z';
   Letters : constant array (Upper_Case) of Positive :=
     ('A' => 1, 'B' => 3, 'C' => 3, 'D' => 2, 'E' => 1, 'F' => 4, 'G' => 2,
      'H' => 4, 'I' => 1, 'J' => 8, 'K' => 5, 'L' => 1, 'M' => 3, 'N' => 1,
      'O' => 1, 'P' => 3, 'Q' => 10, 'R' => 1, 'S' => 1, 'T' => 1, 'U' => 1,
      'V' => 4, 'W' => 4, 'X' => 8, 'Y' => 4, 'Z' => 10);
   type Winner is (Player1, Player2, tie);

   function compute_score (left : String; right : String) return Winner is
      p1   : Natural := 0;
      p2   : Natural := 0;
      Temp : Character;
   begin
      for value of left loop
         Temp := To_Upper (value);
         if Temp in Upper_Case then
            p1 := p1 + Letters (Temp);
         end if;
      end loop;
      for value of right loop
         Temp := To_Upper (value);
         if Temp in Upper_Case then
            p2 := p2 + Letters (Temp);
         end if;
      end loop;
      if p1 > p2 then
         return Player1;
      elsif p2 > p1 then
         return Player2;
      else
         return tie;
      end if;
   end compute_score;

begin
   Put ("Enter two lines of text:");
   declare
      Line1  : String := Get_Line;
      Line2  : String := Get_Line;
      Result : Winner;
   begin
      Result := compute_score (Line1, Line2);
      case Result is
         when Player1 =>
            Put_Line ("Player 1 wins");
         when Player2 =>
            Put_Line ("Player 2 wins");
         when tie =>
            Put_Line ("Tie");
      end case;
   end;
end Main;

分数仅基于字母,而不是输入字符串中的任何其他字符。您必须确保您没有试图累积白色、数字或标点符号的分数。

相关问题