////// file wordcount.C ////// #include #include "Cirbuf.h" // use circular buffer static int wcnt = 0; // global word count, internal linkage int readin(Cirbuf& b) // obtain input from stdin { char c; while ( ! b.is_full() ) // while circular buffer not full if ( cin.get(c) ) b.produce(c); // deposit into buffer else return(0); // input closed return(1); // buffer full } void word_count(Cirbuf& b) // count number of words { int c; static word = 0; // partial word indicator while ( ! b.is_empty() ) // while buffer not empty switch( c = b.consume() ) // remove one character from buffer { case ' ' : case '\t': case '\n': // word delimiters if ( word != 0 ) wcnt++; // word complete word = 0;// partial word indicator false break; default: word = 1;// partial word indicator true } } main() { Cirbuf bf(128); for (;;) if ( readin(bf) ) // input is producer word_count(bf); // word_count is consumer else { word_count(bf); break; } cout << "total " << wcnt << " words\n"; }