(求助) 单词数目统计程序,BOOL函数无法使用(已解决——)
#include<stdio.h>#include<stdbool.h>
bool alphabetic(const char c) /*判断是否是字母*/
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
return true;
else
return false;
}
void readline(char buffer[]) /*接收字符串*/
{
char character;
int i=1;
do
{
character=getchar();
buffer[i]=character;
}while(character!='\n');
buffer[i-1]='\0';
}
int countwords(const char string[]) /*判断单词个数*/
{
int i,wordcount=0;
int lookingforword=1;
bool alphabetic(const char c);
for(i=0;string[i]!='\0';++i)
if(alphabetic(string[i]))
{
if(lookingforword)
{
++wordcount;
lookingforword=0;
}
}
else
lookingforword=1;
return wordcount;
}
int main(void)
{
char text[81];
int totalwords=0;
int countwords(const char string[]);
void readline(char buffer[]);
int endoftext=0;
printf("type in your text.\n");
printf("when you are done ,press 'return'.\n\n");
while(!endoftext)
{
readline(text);
if(text[0]=='\0')
endoftext=1;
else
totalwords+=countwords(text);
}
printf("\nthere are %i words in the above text.\n",totalwords);
return 0;
}
这是一个对输入文本进行单词个数统计的程序,但是不知道问题出在哪里,改来改去始终无法正确统计单词个数,结果总是为0,麻烦大虾们看看。另外,我在使用bool变量作为字符是否为字母的判断标志时也总是出错,提示语法错误如下:
/* stdbool.h for GNU.*/
#ifndef __STDBOOL_H__
#define __STDBOOL_H__ 1
/* The type `bool' must promote to `int' or `unsigned int'. The constants
`true' and `false' must have the value 0 and 1 respectively.*/
typedef enum
{
false = 0,
true = 1
} bool;
/* The names `true' and `false' must also be made available as macros.*/
#define false false
#define true true
/* Signal that all the definitions are present.*/
#define __bool_true_false_are_defined 1
#endif /* stdbool.h */
提示如下错误:
--------------------配置: mingw2.95 - CUI Debug, 编译器类型: MinGW (Old)--------------------
检查文件依赖性...
正在编译 E:\Program Files\C-Free 4\example\totalword.cpp...
[Error] e:\PROGRA~1\C-FREE~1\mingw32\Include\G__~1\stdbool.h:9: parse error before `false'
[Error] e:\PROGRA~1\C-FREE~1\mingw32\Include\G__~1\stdbool.h:11: declaration does not declare anything
构建中止 totalword: 2 个错误, 0 个警告
……)
[[it] 本帖最后由 kscus 于 2008-10-15 12:47 编辑 [/it]]