程序2:word count
本程序改變自:http://blog.csdn.net/zhixi1050/article/details/72718638
語(yǔ)言:C++
編譯環(huán)境:visual studio 2015
運(yùn)行環(huán)境:Win10
做出修改的地方:在原碼基礎(chǔ)上修改了記錄行數(shù)的功能,刪去了不完整行數(shù)的記錄,直接顯示行數(shù)。
修改后的代碼如下:
#include <stdio.h>
//#include <stdlib.h>
#include <ctype.h>//為isspace()提供原型
#include <stdbool.h>
#define STOP '|' //定義結(jié)束標(biāo)志
int main(void)
{
?? ?char c;
?? ?char prev;//讀取的前一個(gè)字符
?? ?long n_chars = 0L;??????//字符數(shù)
?? ?int n_lines = 0;????? ? ? ? //行數(shù)
?? ?int n_words = 0;???? ? ?? //單詞數(shù)
?? ?bool inword = false;??????//字符在單詞中,inward等于ture
?? ?printf("請(qǐng)輸入字符( | 用于結(jié)束輸入):\n");
?? ?prev = '\n';//識(shí)別完整的行
?? ?while ((c = getchar()) != STOP)??????//當(dāng)讀取的字符不為結(jié)束字符時(shí)
?? ?{
?? ??? ?n_chars++??????;//統(tǒng)計(jì)字符數(shù)
?? ??? ?if (c == '\n')
?? ??? ??? ?n_lines++;??????//統(tǒng)計(jì)行
?? ??? ?if (!isspace(c) && !inword)
?? ??? ?{
?? ??? ??? ?inword = true;??????//開(kāi)始一個(gè)新單詞;
?? ??? ??? ?n_words++;??????//統(tǒng)計(jì)單詞
?? ??? ?}
?? ??? ?if (isspace(c) && inword)
?? ??? ??? ?inword = false;??????//打到單詞的結(jié)尾
?? ??? ?prev = c;
?? ?}
?? ?if (prev != '\n')
?? ??? ?n_lines ++;
?? ?printf("字母數(shù)目=%ld,單詞數(shù)=%d,行數(shù)=%d,", n_chars, n_words, n_lines);
?? ?return 0;
}
轉(zhuǎn)載于:https://www.cnblogs.com/nictang/p/7561022.html
總結(jié)
以上是生活随笔為你收集整理的程序2:word count的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [测试题]打地鼠
- 下一篇: 利用ssh反向代理以及autossh实现