【问题标题】:What's wrong with my code? CS50 Pset2: Readability我的代码有什么问题? CS50 Pset2:可读性
【发布时间】:2020-08-06 09:31:28
【问题描述】:

1- 我不知道我的代码有什么问题,这是 ## help ## readability CS50 pset2。 2-问题似乎出在浮点等级(字符串等级)功能上。 3- 我是初学者,这是我第二次用 C 编写代码,所以如果我遗漏了一些重要的东西,请告诉我,或者如果您有任何提示可以使我的代码看起来更好,谢谢!

   //Libraries
    #include <cs50.h>
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    //Functions
    int count_letters(string letter);
    int count_words(string word);
    int count_sentences(string sentence);
    float grade(string grade);
    
    int main(void)
    {
        string par = get_string("text: ");
    }
    
    
    int count_letters(string letter)
    {
        int letters = 0;
        for (int i = 0, n = strlen(letter); i < n; i++)
            {
                 if (isalpha(letter[i]))
                      letters++;
            } 
        return letters;
    }
    
    
    int count_words(string word)
    {
        int words = 1;
        int spaces = 0;
        int nword;
        nword = strlen(word);
        for (int i = 0; i < nword; i++)
            {
                 if (isspace(word[i]))
                      spaces++;
                      words = spaces + 1;
            }
        return words;
    }
    
    
    int count_sentences(string sentence)
    {
        int sentences = 0;
        int nsentence;
        nsentence = strlen(sentence);
        for (int i = 0; i < nsentence; i++)
            {
                 if (sentence[i] == '.' || sentence[i] == '!' || sentence[i] == '?')
                      sentences++;
            }
        return sentences;
    }
    
    
    float grade(string grade)
    {
        //here comes my problem. variables like letters, words, and sentences are "undeclared identifier"
     float x = 0.0588 * (100 * letters / words) - 0.296 * (100 * sentences / words) - 15.8;
                 if (x < 16 && x >= 0)
                    {
                      printf("Grade %i\n", (int) round(x));
                    }
                 else if (x >= 16)
                    {
                      printf("Grade 16+\n");
                    }
                 else
                    {
                      printf("Before Grade 1");
                    }
    }               

【问题讨论】:

  • CS50 是一门 C 课程。 C 和 C++ 是非常不同的语言。如果您不询问 C++,请不要标记 C++。
  • 你需要在你最喜欢的书中阅读关于变量作用域的内容。
  • 谁应该调用所有这些函数?在main 中,你不要打电话给他们中的任何一个。

标签: c logic cs50 readability


【解决方案1】:

编译器会准确告诉您问题所在 - 您尚未在 grade 函数中声明 letterswordssentences

这些变量在其他函数中的声明对于这些函数是本地 - 它们在grade 函数中不可见。

不过,您还有几个更大的问题。一方面,您的任何函数都不会在任何地方被 调用 - main 读取一个字符串,然后立即退出而不做任何其他事情。另一方面,grade 无法知道 letterswordssentences 中应该包含哪些值。

可能想要做的是从grade 中调用您的计数函数,如下所示:

float grade( string s )
{
  int letters = count_letters( s );
  int words = count_words( s );
  int sentences = count_sentences( s );

  float x = ...;
  ...
}

然后从main调用grade作为

int main( void )
{
  string par = get_string( "Text: " );
  grade( par );
}

或者,您可以从main 调用每个计数函数并将结果作为参数 传递给grade

int main( void )
{
  string par = get_string( "Text: " );

  int letters = count_letters( par );
  int words = count_words( par );
  int sentences = count_sentences( par );

  grade( letters, words, sentences );
}

在这种情况下,grade 的声明和定义都应该是

float grade( int, int, int ); // declaration

float grade( int letters, int words, int sentences ) // definition
{                                                    // since they are declared as arguments,
  float x = ...;                                     // there's no need to declare them in the body of the function
}

现在,有一个问题 - 您是否打算让任何人在 grade 退出后使用 x 的值?如果是这样,那么您需要添加一个

return x;

grade结尾。

如果不是,则在声明和定义中将函数的返回类型从 float 更改为 void

void grade( string ); 

void grade( string s ) { ... } // definition

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多