这个是不使用模块化的程序
#include
#include
#include
int main(void)
{
int ch, word, alpha;
double result;
alpha = word = 0;
bool inword = false;
while ((ch = getchar()) != EOF)
{
if (isalpha(ch))
alpha++;
if (isalpha(ch) && !inword)
{
inword = true;
word++;
}
if (!isalpha(ch) && inword)
inword = false;
}
result = alpha / word;
printf("%d word and %d alpha,per %.2lf alpha in a word.", word, alpha, result);
getchar();
getchar();
return 0;
}
调试结果正常
然后我使用模块化
#include
#include
#include
int get_word(char ch);
int get_alpha(char ch);
int main(void)
{
int ch,word,alpha;
double result;
while ((ch = getchar()) != '#')
{
word = get_word(ch);
alpha = get_alpha(ch);
}
result = (float)alpha / (float)word;
printf("%d word and %d alpha,per %.2lf alpha in a word.", word, alpha,result );
getchar();
getchar();
return 0;
}
int get_word(char ch)
{
int word1=0;
bool inword = false;
if (isalpha(ch) && !inword)
{
inword = true;
word1++;
}
if (!isalpha(ch)&&inword)
inword = false;
return word1;
}
int get_alpha(char ch)
{
int alpha1=0;
if (isalpha(ch))
alpha1++;
return alpha1;
}
调试结果全是1
请问这是怎么回事?