C 中函数的问题(可读性)
Problem with functions in C (readability)
我正在做一个 cs50 pset2 可读性练习,我正在尝试计算文本中有多少个句子。到目前为止我的代码是这样的:
#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>
int isdelim(char c, int sentences)
int main (void)
{
string text = get_string("Text: ");
int sentences = 0;
for(int i = 0; i < strlen(text);i++)
{
sentences = isdelim(text[i], sentences);
}
printf("Sentences: %i\n", sentences);
}
int isdelim(char c, int sentences)
{
return c == '.' || c == '!' || c == '?';
sentences++;
}
但是当我执行 ./readability
并键入文本:Hello, world! Beautiful day!
时,它给出的输出为:Sentences:0
.
我是 C 的新手,所以我不能真正理解所有这些函数。我尝试搜索网络并使用其他外部资源,但没有任何意义。如果你告诉我答案,请务必告诉我它为什么有效,以便我学习。我将来可能需要这些知识。非常感谢。
有几个问题。
- 当你执行
return
语句时,函数结束。之后什么都不会执行。所以 sentences++
永远不会被执行。
- 在C语言中,函数参数是按值传递的。因此在
isdelim()
函数中递增 sentences
不会影响 main()
. 中的变量
int delim(char c, int sentences);
是函数声明,不是函数调用。函数调用不包括类型声明。
您可以通过使用检查条件的 if
语句来解决第一个问题,而不是立即 returning 条件的结果。
您可以通过将指针传递给变量并取消引用它来解决第二个问题,或者让函数 return 成为 sentences
的新值。
#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>
int isdelim(char c, int sentences);
int main (void)
{
string text = get_string("Text: ");
int sentences = 0;
for(int i = 0; i < strlen(text);i++)
{
sentences = isdelim(text[i], sentences);
}
printf("Sentences: %i\n", sentences);
}
int isdelim(char c, int sentences)
{
if (c == '.' || c == '!' || c == '?') {
sentences++;
}
return sentences;
}
@Baramar 提出了一些很好的观点,但我认为代码可以使用重构。 isdelim
函数令人困惑。它被命名为一个布尔函数,所以它应该 return a bool
。而且该函数不应采用 sentences
参数;相反,让调用者处理递增。而且我已经冒昧地在循环之前调用 strlen
所以它不会在每次迭代时重新计算。所以:
#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>
bool isdelim(char c);
int main (void)
{
string text = get_string("Text: ");
int sentences = 0;
int length = strlen(text);
for (int i=0; i<length; i++)
{
if (isdelim(text[i]))
{
sentences++;
}
}
printf("Sentences: %i\n", sentences);
}
bool isdelim(char c)
{
return c == '.' || c == '!' || c == '?';
}
我正在做一个 cs50 pset2 可读性练习,我正在尝试计算文本中有多少个句子。到目前为止我的代码是这样的:
#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>
int isdelim(char c, int sentences)
int main (void)
{
string text = get_string("Text: ");
int sentences = 0;
for(int i = 0; i < strlen(text);i++)
{
sentences = isdelim(text[i], sentences);
}
printf("Sentences: %i\n", sentences);
}
int isdelim(char c, int sentences)
{
return c == '.' || c == '!' || c == '?';
sentences++;
}
但是当我执行 ./readability
并键入文本:Hello, world! Beautiful day!
时,它给出的输出为:Sentences:0
.
我是 C 的新手,所以我不能真正理解所有这些函数。我尝试搜索网络并使用其他外部资源,但没有任何意义。如果你告诉我答案,请务必告诉我它为什么有效,以便我学习。我将来可能需要这些知识。非常感谢。
有几个问题。
- 当你执行
return
语句时,函数结束。之后什么都不会执行。所以sentences++
永远不会被执行。 - 在C语言中,函数参数是按值传递的。因此在
isdelim()
函数中递增sentences
不会影响main()
. 中的变量
int delim(char c, int sentences);
是函数声明,不是函数调用。函数调用不包括类型声明。
您可以通过使用检查条件的 if
语句来解决第一个问题,而不是立即 returning 条件的结果。
您可以通过将指针传递给变量并取消引用它来解决第二个问题,或者让函数 return 成为 sentences
的新值。
#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>
int isdelim(char c, int sentences);
int main (void)
{
string text = get_string("Text: ");
int sentences = 0;
for(int i = 0; i < strlen(text);i++)
{
sentences = isdelim(text[i], sentences);
}
printf("Sentences: %i\n", sentences);
}
int isdelim(char c, int sentences)
{
if (c == '.' || c == '!' || c == '?') {
sentences++;
}
return sentences;
}
@Baramar 提出了一些很好的观点,但我认为代码可以使用重构。 isdelim
函数令人困惑。它被命名为一个布尔函数,所以它应该 return a bool
。而且该函数不应采用 sentences
参数;相反,让调用者处理递增。而且我已经冒昧地在循环之前调用 strlen
所以它不会在每次迭代时重新计算。所以:
#include<stdio.h>
#include<cs50.h>
#include<string.h>
#include<stdbool.h>
#include<ctype.h>
bool isdelim(char c);
int main (void)
{
string text = get_string("Text: ");
int sentences = 0;
int length = strlen(text);
for (int i=0; i<length; i++)
{
if (isdelim(text[i]))
{
sentences++;
}
}
printf("Sentences: %i\n", sentences);
}
bool isdelim(char c)
{
return c == '.' || c == '!' || c == '?';
}