kscus 发布留言 2008-10-12 16:52
(求助) 单词数目统计程序,BOOL函数无法使用(已解决——)
#include
#include
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='\0';
}
int countwords(const char string[]) /*判断单词个数*/
{
int i,wordcount=0;
int lookingforword=1;
bool alphabetic(const char c);
for(i=0;string))
{
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 个警告
……)
[ 本帖最后由 kscus 于 2008-10-15 12:47 编辑 [/it]]Knocker 发布留言 2008-10-12 19:19
//#include
int i=1;
do
{
character=getchar();
buffer='\0';kscus 发布留言 2008-10-13 17:57
嗯……看懂LS的了,我犯了个低级错误,不过下面的BOOL函数还是没解决,而且出现的代码不是我自己写的,我想应该是BOOL库函数的源代码……Knocker 发布留言 2008-10-13 19:13
//#include
看不懂什么意思么?
bool变量C++的,C是没有bool变量。为了解决C代码能兼容C++的bool变量,C99在stdbool.h定义宏bool,true,false.
你代码include,但是却用了C++编译器编译(估计你用的文件后缀是.cpp),所以导致这个错误。
试试以下方法:
1。改文件后缀为.c并按C标准编译
2。注释掉//#include
未安装 MinGW ,以上言论纯属猜测,并未验证。kscus 发布留言 2008-10-13 19:35
哦,是这样啊,C-Free是C++的编译器吗?我还以为是C 的呢……
我的文件后缀确实是.cpp的!kscus 发布留言 2008-10-13 19:36
不过,我是《C语言编程》这本书上却看到过C语言中的BOOL函数的使用……sf469210604 发布留言 2008-10-13 19:50
麻烦LZ说下你看懂2楼说什么了,我怎么看都是一样的啊????????????sf469210604 发布留言 2008-10-13 20:59
呼唤强人来解答[em08] [em08]sf469210604 发布留言 2008-10-14 09:29
继续呼唤高人来解答[em03] [em03]kscus 发布留言 2008-10-14 12:16
我少写了个循环条件,下标的……sf469210604 发布留言 2008-10-14 20:05
你早已经修改了???
汗啊kscus 发布留言 2008-10-15 12:44
这是我编写的时候就留下的错误,不过现在弄好了,没有用BOOL函数……
页: [1]