【发布时间】:2023-03-24 11:36:01
【问题描述】:
所以我正在使用 C 进行 CS50 Readability problem 设置,我对如何在 main 函数中使用其他函数的局部变量有点困惑。
这是我的代码:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
int count(string text);
int grading(float index);
int main(void)
{
string text = get_string("Text: ");
count(text);
float index = 0.0588 * (100*((float)letter/(float)word)) - 0.296 * (100*((float)sentence/(float)word)) - 15.8;
grading(index);
}
int count(string text)
{
int letter = 0;
int word = 1;
int sentence = 0;
for(int i = 0, n = strlen(text); i < n; i++)
{
if(text[i] >= 'A' && text[i] <= 'z'){
letter += 1;
}
else if(text[i] == ' '){
word += 1;
}
else if(text[i] == '!' || text[i] == '?' || text[i] == '.'){
sentence += 1;
}
}
printf("%i letters\n", letter);
printf("%i words\n", word);
printf("%i sentences\n", sentence);
}
int grading(float index)
{
if (index >= 16) {
printf("Grade 16+\n");
}
else if (index < 1) {
printf("Before Grade 1\n");
}
else {
printf("Grade %f\n", round(index));
}
return 0;
}
在调用count 后,如何在main 中使用letter、word 和sentence?
【问题讨论】:
-
Anyone can help me?帮助具体是什么? link -
FWIW,
if (text[i] >= 'A' && text[i] <= 'z')错了,Z之后和a之前有一堆字符不是字母。 -
float index = 0.0588 * (100*((flo....所有这些变量只存在于count中,所以也许该计算应该去那里。 -
@JohnnyMopp 哦,谢谢,是的,我对编程有点陌生,它真的很有帮助!已经解决了:D