Created
October 26, 2017 18:26
-
-
Save nsfyn55/4615d6a0cf8c3c59a959ac4d523a4dac to your computer and use it in GitHub Desktop.
Kernighan and Richie's Vertical Word Histogram Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define IN 1 /* we're inside a word */ | |
#define OUT 0 /* we're outside a word */ | |
#define SPACE 8 /* we're outside a word */ | |
#define PSPREV 0 | |
#define PSNULL 1 | |
int main(){ | |
int i, c, w, f, pstate, wsize, state, maxdepth; | |
int sizes[10]; | |
pstate = PSNULL; | |
state = OUT; | |
wsize=1; | |
maxdepth=0; | |
/* loop over array and initialize to zero */ | |
for (i = 0; i < 10; ++i) | |
sizes[i] = 0; | |
while ((c = getchar()) != EOF) { | |
/* if ws, nl, or tab STAY OUT! */ | |
if(state == IN && (c == ' ' || c == '\n' || c == '\t')){ | |
state = OUT; | |
++sizes[wsize]; | |
if (sizes[wsize] > maxdepth){ | |
maxdepth = sizes[wsize]; | |
} | |
wsize=1; | |
}else if (state == IN){ | |
/* if we're in keep counting the letters */ | |
++wsize; | |
}else if (state == OUT) | |
/* if none of those things but we're OUT | |
* Move to in and start counting letters | |
*/ | |
state = IN; | |
} | |
/* Print Results */ | |
/* Print bars from top down */ | |
for(i=maxdepth; i > 0; i--){ | |
putchar('\n'); | |
for(w=0; w < 10; w++){ | |
if(w>0) | |
if(pstate == PSNULL) | |
for(f=0; f < SPACE; f++) | |
putchar(' '); | |
if(pstate == PSPREV) | |
for(f=0; f < SPACE - 1; f++) | |
putchar(' '); | |
if (sizes[w] == i){ | |
putchar('_'); | |
pstate = PSPREV; | |
--sizes[w]; | |
}else | |
pstate = PSNULL; | |
} | |
} | |
/* Print bottom axis */ | |
putchar('\n'); | |
for(i=0; i < 10; i++){ | |
printf("--------"); | |
} | |
putchar('\n'); | |
for(i=0; i < 10; i++){ | |
printf("%d\t", i); | |
} | |
putchar('\n'); | |
for(i=0; i < 10; i++){ | |
printf("--------"); | |
} | |
putchar('\n'); | |
} |
Author
nsfyn55
commented
Oct 26, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment