我的代码有什么问题? CS50 Pset2:可读性
What's wrong with my code? CS50 Pset2: Readability
1- 我不知道我的代码有什么问题,这是## 帮助## 可读性 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");
}
}
编译器准确地告诉您问题出在哪里 - 您没有在 grade
函数中声明 letters
、words
或 sentences
。
这些变量在其他函数中的声明对这些函数来说是 局部的 - 它们在 grade
函数中是不可见的。
不过,你有几个更大的问题。一方面,none 的函数正在从任何地方被 调用 - main
读取一个字符串,然后立即退出而不做任何其他事情。另一方面,grade
无法知道 letters
、words
或 sentences
.
中应包含哪些值
您可能想要做的是从 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
调用每个计数函数并将结果作为 arguments 传递给 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
结束。
如果不是,则在声明和定义中将函数的 return 类型从 float
更改为 void
:
void grade( string );
void grade( string s ) { ... } // definition
1- 我不知道我的代码有什么问题,这是## 帮助## 可读性 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");
}
}
编译器准确地告诉您问题出在哪里 - 您没有在 grade
函数中声明 letters
、words
或 sentences
。
这些变量在其他函数中的声明对这些函数来说是 局部的 - 它们在 grade
函数中是不可见的。
不过,你有几个更大的问题。一方面,none 的函数正在从任何地方被 调用 - main
读取一个字符串,然后立即退出而不做任何其他事情。另一方面,grade
无法知道 letters
、words
或 sentences
.
您可能想要做的是从 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
调用每个计数函数并将结果作为 arguments 传递给 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
结束。
如果不是,则在声明和定义中将函数的 return 类型从 float
更改为 void
:
void grade( string );
void grade( string s ) { ... } // definition