zoukankan      html  css  js  c++  java
  • 程序2:word count

    本程序改变自:http://blog.csdn.net/zhixi1050/article/details/72718638

    语言:C++

    编译环境:visual studio 2015

    运行环境:Win10

    做出修改的地方:在原码基础上修改了记录行数的功能,删去了不完整行数的记录,直接显示行数。

    修改后的代码如下:

    #include <stdio.h>
    //#include <stdlib.h>
    #include <ctype.h>//为isspace()提供原型
    #include <stdbool.h>
    #define STOP '|' //定义结束标志


    int main(void)
    {
        char c;
        char prev;//读取的前一个字符
        long n_chars = 0L;      //字符数
        int n_lines = 0;            //行数
        int n_words = 0;          //单词数
        bool inword = false;      //字符在单词中,inward等于ture

        printf("请输入字符( | 用于结束输入): ");
        prev = ' ';//识别完整的行

        while ((c = getchar()) != STOP)      //当读取的字符不为结束字符时
        {
            n_chars++      ;//统计字符数

            if (c == ' ')
                n_lines++;      //统计行

            if (!isspace(c) && !inword)
            {
                inword = true;      //开始一个新单词;
                n_words++;      //统计单词
            }
            if (isspace(c) && inword)
                inword = false;      //打到单词的结尾
            prev = c;
        }

        if (prev != ' ')
            n_lines ++;
        printf("字母数目=%ld,单词数=%d,行数=%d,", n_chars, n_words, n_lines);
        return 0;
    }

  • 相关阅读:
    Hibernate 3入門
    如何调整液晶显示器保护你的视力
    WEB前端开发经验总结实战篇
    【JS】引用类型之Array
    Oracle的DBA管理常用sql
    使用cos组建上传文件
    前台js将json转换成json对象的方法
    关于父页面访问iframe中元素和js的那点事
    数据库中IN和EXISTS的区别
    关于google浏览器有时莫名自动提交表单的问题
  • 原文地址:https://www.cnblogs.com/nictang/p/7561022.html
Copyright © 2011-2022 走看看