#if 1 #include #include coroutine Fmt { char ch; // communication variables int g, b; // needed in destructor }; void main( Fmt & fmt ) with( fmt ) { for () { // for as many characters for ( g = 0; g < 5; g += 1 ) { // groups of 5 blocks for ( b = 0; b < 4; b += 1 ) { // blocks of 4 characters do { suspend(); } while ( ch == '\n' || ch == '\t' ); sout | ch; // print character } sout | " "; // block separator } sout | nl; // group separator } } void ?{}( Fmt & fmt ) { resume( fmt ); } // prime (start) coroutine void ^?{}( Fmt & fmt ) with( fmt ) { // destructor if ( g != 0 || b != 0 ) // special case sout | nl; } void send( Fmt & fmt, char c ) { fmt.ch = c; resume( fmt ); } int main() { Fmt fmt; sout | nlOff; // turn off auto newline for ( 41 ) send( fmt, 'a' ); // sout | nlOff; // turn off auto newline // eof: for () { // read until end of file // sin | fmt.ch; // read one character // if ( eof( sin ) ) break eof; // eof ? // format( fmt ); // push character for formatting // } } #else #include struct Format { char ch; // used for communication int g, b; // global because used in destructor }; void format( struct Format * fmt ) { if ( fmt->ch != -1 ) { // not EOF ? // if ( fmt->ch == '\n' || fmt->ch == '\t' ) return; // printf( "%c %d %d", fmt->ch, fmt->g, fmt->b ); // character printf( "%c", fmt->ch ); // character fmt->b += 1; if ( fmt->b == 4 ) { // block ? printf( " " ); // separator fmt->b = 0; fmt->g += 1; } if ( fmt->g == 5 ) { // group ? printf( "\n" ); // separator fmt->g = 0; } } else { if ( fmt->g != 0 || fmt->b != 0 ) printf( "\n" ); } } int main() { struct Format fmt = { 0, 0, 0 }; for ( ;; ) { scanf( "%c", &fmt.ch ); if ( feof( stdin ) ) break; format( &fmt ); } fmt.ch = -1; format( &fmt ); } #endif // Local Variables: // // tab-width: 4 // // fill-column: 120 // // compile-command: "cfa Format.cfa" // // End: //