注册 登录
编程论坛 C语言论坛

初学者遇到难题了,求教

gerald1236 发布于 2019-10-11 16:50, 2730 次点击
为什么bool inword = false;赋值了,但是下面的!inword还是等于false,不应该时!inword=true吗?初学者,求各位大大指教!
程序代码:
#include <stdio.h>
#include <ctype.h>    //为isspac()函数提供原型
#include <stdbool.h>    //为bool、true、false提供定义
#define STOP '|'
int main(void)
{
    char c;        //读入字符
    char prev;        //读入的前一个字符
    long n_chars = 0L;        //字符数
    long n_lines = 0;        //行数
    long n_words = 0;        //单词数
    long p_lines = 0;        //不完整的行数
    bool inword = false;    //如果c在单词中,inword等于true

    printf("Enter text to be analyzed (| to terminate): \n");
    prev = '\n';        //用于识别完整的行
    while ((c = getchar()) != STOP)
    {
        n_chars++;            //统计字符
        if (c == '\n')
            n_lines++;        //统计行
        if (!isspace(c) && !inword)
        {
            inword = true;        //开始一个新的单词
            n_words++;        //统计单词
        }
        if (isspace(c) && inword)
            inword = false;        //打到单词的末尾
        prev = c;        //保存字符的值
    }
    if (prev != '\n')
        p_lines = 1;
    printf("characters = %ld,words = %d,lines = %d,",
        n_chars, n_words, n_lines);
    printf("partial lines = %d\n", p_lines);

    return 0;
}
4 回复
#2
gerald12362019-10-11 16:51
看得确实有点晕。。。已经把自己绕进去了,蒙圈了,求大大指教,谢谢!
#3
xhxh2019-10-12 20:44

     if (isspace(c) && inword)
            inword = false;        //打到单词的末尾


你不是重新赋值为false了吗
#4
jack_shi2019-10-20 11:58
打印了一下inword 确实为true。并不是false。

[此贴子已经被作者于2019-10-20 12:04编辑过]

#5
zbjzbj2019-10-20 13:31
没看懂楼主在说什么
1