C - The C Answer (2nd Edition) - Exercise 1-13

/* Write a program to print a histogram of the lengths of words in its input.
   It is easy to draw the histogram with the bars horizontal; a vertical
   orientation is more challenging. */

#include <stdio.h>

#define MAXHIST 15    /* max length of histogram */
#define MAXWORD 11    /* max length of a word */
#define IN      1     /* inside a word */
#define OUT     0     /* outside a word */

/* print horizontal histogram */
main()
{
	int c, i, nc, state;
	int len;          /* length of each bar */
	int maxvalue;     /* maximum value for wl[] */
	int ovflow;       /* number of overflow words */
	int wl[MAXWORD];  /* word length counters */

	state = OUT;
	nc = 0;           /* number of chars in a word */
	ovflow = 0;       /* number of words >= MAXWORD */

	for(i = 0; i < MAXWORD; ++i)
	{
		wl[i] = 0;
	}

	while((c = getchar()) != EOF)
	{
		if(c == ' ' || c == '\n' || c == '\t')
		{
			state = OUT;
			if(nc > 0)
			{
				if(nc < MAXWORD)
				{
					++wl[nc];
				}
				else
				{
					++ovflow;
				}
			}
			nc = 0;
		}
		else if(state == OUT)
		{
			state = IN;
			nc = 1;   /* beginning of a new word */
		}
		else
		{
			++nc;     /* inside a word */
		}
	}

	maxvalue = 0;
	for(i = 1; i < MAXWORD; ++i)
	{
		if(wl[i] > maxvalue)
		{
			maxvalue = wl[i];
		}
	}

	for(i = 1; i < MAXWORD; ++i)
	{
		printf("%5d - %5d : ", i, wl[i]);
		if(wl[i] > 0)
		{
			if((len = wl[i] * MAXHIST / maxvalue) <= 0)
			{
				len = 1;
			}
			else
			{
				len = 0;
			}
		}
		while(len > 0)
		{
			putchar('*');
			--len;
		}
		putchar('\n');
	}

	if(ovflow > 0)
	{
		printf("There are %d words >= %d\n", ovflow, MAXWORD);
	}
}
/* Write a program to print a histogram of the lengths of words in its input.
   It is easy to draw the histogram with the bars horizontal; a vertical
   orientation is more challenging. */

#include <stdio.h>

#define MAXHIST 15    /* max length of histogram */
#define MAXWORD 11    /* max length of a word */
#define IN      1     /* inside a word */
#define OUT     0     /* outside a word */

/* print vertical histogram */
main()
{
	int c, i, j, nc, state;
	int maxvalue;     /* maximum value for wl[] */
	int ovflow;       /* number of overflow words */
	int wl[MAXWORD];  /* word length counters */

	state = OUT;
	nc = 0;           /* number of chars in a word */
	ovflow = 0;       /* number of words >= MAXWORD */

	for(i = 0; i < MAXWORD; ++i)
	{
		wl[i] = 0;
	}

	while((c = getchar()) != EOF)
	{
		if(c == ' ' || c == '\n' || c == '\t')
		{
			state = OUT;
			if(nc > 0)
			{
				if(nc < MAXWORD)
				{
					++wl[nc];
				}
				else
				{
					++ovflow;
				}
			}
			nc = 0;
		}
		else if(state == OUT)
		{
			state = IN;
			nc = 1;   /* beginning of a new word */
		}
		else
		{
			++nc;     /* inside a word */
		}
	}

	maxvalue = 0;
	for(i = 1; i < MAXWORD; ++i)
	{
		if(wl[i] > maxvalue)
		{
			maxvalue = wl[i];
		}
	}

	for(i = MAXHIST; i > 0; --i)
	{
		for(j = 1; j < MAXWORD; ++j)
		{
			if(wl[j] * MAXHIST / maxvalue >= i)
			{
				printf(" * ");
			}
			else
			{
				printf("   ");
			}
		}
		putchar('\n');
	}
	for(i = 1; i < MAXWORD; ++i)
	{
		printf("%4d ", i);
	}
	putchar('\n');
	for(i = 1; i < MAXWORD; ++i)
	{
		printf("%4d ", wl[i]);
	}
	putchar('\n');

	if(ovflow > 0)
	{
		printf("There are %d words >= %d\n", ovflow, MAXWORD);
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-26 07:34:10

C - The C Answer (2nd Edition) - Exercise 1-13的相关文章

C - The C Answer (2nd Edition) - Exercise 1-18

/* Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines. */ #include <stdio.h> #define MAXLINE 1000 /* maximum input line size */ int getline(char line[], int maxline); int remove(char s[]);

C - The C Answer (2nd Edition) - Exercise 1-19

/* Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time. */ #include <stdio.h> #define MAXLINE 1000 /* maximum input line size */ int getline(char line[], int maxline);

C - The C Answer (2nd Edition) - Exercise 1-4

/* Write a program to print the corresponding Celsius to Fahrenheit table. */ #include <stdio.h> /* print Celsius-Fahrenheit table for celsius = 0, 20, ..., 300; floating-point version */ main() { float fahr, celsius; int lower, upper, step; lower =

C - The C Answer (2nd Edition) - Exercise 1-5

/* Modify the temperature conversion program to print the table in reverse order, that is, from 300 degrees to 0. */ #include <stdio.h> /* print Fahrenheit-Celsius table in reverse order */ main() { int fahr; for(fahr = 300; fahr >= 0; fahr -= 20

C - The C Answer (2nd Edition) - Exercise 1-7

/* Write a program to print the value of EOF. */ #include <stdio.h> main() { printf("EOF is %d\n", EOF); } /* Output: EOF is -1 */ 版权声明:本文为博主原创文章,未经博主允许不得转载.

C - The C Answer (2nd Edition) - Exercise 1-8

/* Write a program to count blanks, tabs, and newlines. */ #include <stdio.h> /* count blanks, tabs, and newlines */ main() { int c, nb, nt, nl; nb = 0; /* number of blanks */ nt = 0; /* number of tabs */ nl = 0; /* number of newlines */ while((c =

C - The C Answer (2nd Edition) - Exercise 1-9

/* Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank. */ #include <stdio.h> #define NONBLANK 'a' /* replace string of blanks with a single blank */ main() { int c, lastc; lastc = NONBLANK;

C - The C Answer (2nd Edition) - Exercise 1-10

/* Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b, and each backslash by \\. This makes tabs and backspaces visible in an unambiguous way. */ #include <stdio.h> #define NONBLANK 'a' /* replace tabs an

C - The C Answer (2nd Edition) - Exercise 1-1

/* Run the "hello, world" program on your system. Experiment with leaving out parts of the program to see what error messages you get. */ #include <stdio.h> main() { printf("hello world\n"); } 版权声明:本文为博主原创文章,未经博主允许不得转载.

C - The C Answer (2nd Edition) - Exercise 1-2

/* Experiment to find out what happens when printf's argument string contains \c, where c is some character not listed above. */ #include <stdio.h> main() { printf("hello world\y"); printf("hello world\7"); printf("hello wor