向主函数声明局部变量
Declaring local variable to the main function
所以我正在使用 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
?
不能在另一个函数中使用一个函数中定义的局部变量。如果您想保留 count
中计算的值,则必须以某种方式 return 它们。
由于您正在计算三个不同的值,但一个函数只能 return 一个值,我建议创建一个助手 struct
到 return:
struct result {
unsigned letters;
unsigned words;
unsigned sentences;
};
然后,在count
中你可以这样做:
struct result count(string text)
{
struct result res = {.letters = 0, .words = 0, .sentences = 0};
for(int i = 0, n = strlen(text); i < n; i++)
{
if ((text[i] >= 'A' && text[i] <= 'Z') || (text[i] >= 'a' && text[i] <= 'z')){
res.letters += 1;
}
else if (text[i] == ' '){
res.words += 1;
}
else if (text[i] == '!' || text[i] == '?' || text[i] == '.'){
res.sentences += 1;
}
}
printf("%i letters\n", res.letters);
printf("%i words\n", res.words);
printf("%i sentences\n", res.sentences);
return res;
}
最后在 main
:
int main(void)
{
string text = get_string("Text: ");
struct result res = count(text);
float index = 0.0588 * (100*((float)res.letters/(float)res.words)) - 0.296 * (100*((float)res.sentences/(float)res.words)) - 15.8;
grading(index);
}
或者,您可以只在 count
函数本身中计算结果:
float 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);
return 0.0588 * (100*((float)letter/(float)word)) - 0.296 * (100*((float)sentence/(float)word)) - 15.8;
}
然后在main
:
int main(void)
{
string text = get_string("Text: ");
float index = count(text);
grading(index);
}
所以我正在使用 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
?
不能在另一个函数中使用一个函数中定义的局部变量。如果您想保留 count
中计算的值,则必须以某种方式 return 它们。
由于您正在计算三个不同的值,但一个函数只能 return 一个值,我建议创建一个助手 struct
到 return:
struct result {
unsigned letters;
unsigned words;
unsigned sentences;
};
然后,在count
中你可以这样做:
struct result count(string text)
{
struct result res = {.letters = 0, .words = 0, .sentences = 0};
for(int i = 0, n = strlen(text); i < n; i++)
{
if ((text[i] >= 'A' && text[i] <= 'Z') || (text[i] >= 'a' && text[i] <= 'z')){
res.letters += 1;
}
else if (text[i] == ' '){
res.words += 1;
}
else if (text[i] == '!' || text[i] == '?' || text[i] == '.'){
res.sentences += 1;
}
}
printf("%i letters\n", res.letters);
printf("%i words\n", res.words);
printf("%i sentences\n", res.sentences);
return res;
}
最后在 main
:
int main(void)
{
string text = get_string("Text: ");
struct result res = count(text);
float index = 0.0588 * (100*((float)res.letters/(float)res.words)) - 0.296 * (100*((float)res.sentences/(float)res.words)) - 15.8;
grading(index);
}
或者,您可以只在 count
函数本身中计算结果:
float 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);
return 0.0588 * (100*((float)letter/(float)word)) - 0.296 * (100*((float)sentence/(float)word)) - 15.8;
}
然后在main
:
int main(void)
{
string text = get_string("Text: ");
float index = count(text);
grading(index);
}