码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-07-25 13:52:49      阅读:128      评论:0      收藏:0      [点我收藏+]

标签: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);
	}
}

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

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

标签:exercise 1-13

原文地址:http://blog.csdn.net/troubleshooter/article/details/47055221

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!