source:
doc/papers/concurrency/examples/Format1.c
@
e67a82d
Last change on this file since e67a82d was a573c22, checked in by , 5 years ago | |
---|---|
|
|
File size: 1.0 KB |
Rev | Line | |
---|---|---|
[466fa01] | 1 | #include <stdio.h> |
2 | ||
3 | typedef struct { | |
[a573c22] | 4 | int restart, g, b; |
[466fa01] | 5 | char ch; |
6 | } Fmt; | |
7 | ||
8 | void format( Fmt * f ) { | |
[a573c22] | 9 | static void * states[] = {&&s0, &&s1}; |
10 | goto *states[f->restart]; | |
11 | s0: f->restart = 1; | |
[466fa01] | 12 | for ( ;; ) { |
13 | for ( f->g = 0; f->g < 5; f->g += 1 ) { // groups | |
14 | for ( f->b = 0; f->b < 4; f->b += 1 ) { // blocks | |
15 | return; | |
[a573c22] | 16 | s1: if ( f->ch == '\0' ) goto fini; // EOF ? |
[466fa01] | 17 | while ( f->ch == '\n' ) return; // ignore |
[a573c22] | 18 | // printf( "%c", f->ch ); // print character |
[466fa01] | 19 | } |
[a573c22] | 20 | // printf( " " ); // block separator |
[466fa01] | 21 | } |
[a573c22] | 22 | // printf( "\n" ); // group separator |
[466fa01] | 23 | } |
[a573c22] | 24 | fini:; |
25 | // if ( f->g != 0 || f->b != 0 ) printf( "\n" ); | |
[466fa01] | 26 | } |
27 | ||
28 | int main() { | |
[a573c22] | 29 | Fmt fmt = { 0 }; |
[466fa01] | 30 | format( &fmt ); // prime |
[a573c22] | 31 | fmt.ch = 'a'; |
32 | for ( long int i = 0; i < 1000000000; i += 1 ) { | |
33 | // scanf( "%c", &fmt.ch ); // direct read into communication variable | |
34 | // if ( feof( stdin ) ) break; | |
[466fa01] | 35 | format( &fmt ); |
36 | } | |
[a573c22] | 37 | fmt.ch = '\0'; // sentential (EOF) |
[466fa01] | 38 | format( &fmt ); |
39 | } | |
40 | ||
41 | // Local Variables: // | |
42 | // tab-width: 4 // | |
43 | // compile-command: "gcc-8 Format1.c" // | |
44 | // End: // |
Note: See TracBrowser
for help on using the repository browser.