开天辟地的卷毛 2022-01-06 19:54 采纳率: 85.4%
浏览 29
已结题

c primer plus中关于bool类型的使用

#include<stdio.h>
#include<stdbool.h>
#include<ctype.h>

int main()
{
    bool inword = false;
    float count;
    int word, letter;
    char ch;
    word = letter = 0;
    printf("Please enter the some words( EOF to quit):\n");
    while ((ch = getchar()) != EOF)
    {
        if (ispunct(ch))
            continue;
        if (isalpha(ch))
            letter++;
        if (!isspace(ch) && !inword)
        {
            inword = true;
            word++;
        }
        if (isspace(ch) && inword)
            inword = false;
    }
    count = (float)letter / word;
    printf("Total words:%d\n", word);
    printf("Total letter:%d\n", letter);
    printf("Average letter of the word:%g\n", count);
    return 0;
}

想问一下,定义bool inword为false,但是当getchar()读取到到第一个字母的时候,经过第三个if的时候,为什么判断为真

  • 写回答

2条回答 默认 最新

  • 书山客 2022-01-06 20:06
    关注

    !isspace(ch) && !inword
    读到的是字母,那isspace(ch)判断是否为空格结果一定返回false,!isspace(ch)就是true
    inword是false, !inword就是true
    两个true执行&&运算,结果就是true

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月15日
  • 已采纳回答 1月7日
  • 创建了问题 1月6日

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本