C Programming Language 1-13

/*
 * temp.c
 *打印输入中单词长度的直方图 													 *  Created on: 2012-7-18
 *      Author: Administrator
 */
//横着的
#include<stdio.h>
#include<stdlib.h>

#define  IN 1
#define OUT 0
#define MAX 10
void pic();
int main()
{
	pic();
	return 0;
}

void pic()
{
	char c;
	int cnum = 0, clen[MAX] = {0};
	int stat = OUT;
	int i, j;
	int done = 0;
	while(done == 0)
	{
		c = getchar();
		if(c != ' ' && c != '\t' && c != '\n' && c != EOF)
		{
		
				clen[cnum]++;
				stat = IN;
		}
		else if(stat == IN)
		{
			if(cnum < MAX - 1 && cnum >= 0)
			{
				cnum ++;
				stat = OUT;
			}
			else 
			{
				printf("length is out, please input again!\n");
				continue;
			}
		}
		if(c == EOF)
			done = 1;
	}

	for(i = 0; i < cnum; i++)
	{
		for(j = 0; j < clen[i]; j++)
			printf("*");
		printf("\n");
	}
}
 
//竖着的
/*
 * temp.c
 *
 *  Created on: 2012-7-20
 *      Author: Administrator
 */
#include<stdio.h>
#include<stdlib.h>

#define  FALSE 1
#define   TRUE 0
#define  MAX_LENGTH 20
int main()
{
	char c;
	int offset = 0;
	int iclen[MAX_LENGTH] = {0};
	int i, j;

	int cstat = FALSE;
	int roop_stat = TRUE;
	int max_value = 0;

	while(roop_stat == TRUE)
	{
		c = getchar();
		if(c != ' ' && c != '\t' && c != '\n' && c != EOF)
		{
			iclen[offset]++;
			cstat = TRUE;
		}else if(cstat == TRUE)//这个符号是一个单词的结尾吗
		{
			cstat = FALSE;

			if(iclen[offset] > max_value)
				max_value = iclen[offset];//算出最长的单词的字符数

			if(offset >= 0 && offset < MAX_LENGTH - 1)
				offset ++;
			else
				offset = MAX_LENGTH - 1;
		}

		if(c == EOF)
			roop_stat = FALSE;
	}

	for(i = max_value; i > 0; i--)
	{
		printf("%4d | ",i);
		for(j = 0; j < MAX_LENGTH; j++)
		{
			if(iclen[j] >= i)//此处i代表当前比较的值,只要单词长度不小于这个数,则就需要输出*
			{
				printf("*  ");
			}
			else
			{
				printf("   ");
			}
		}
		printf("\n");
	}

	printf("    +");
	for(j = 0; j < MAX_LENGTH; j++)
	{
		printf("---");
	}

	printf("\n      ");
	for(j = 0; j < MAX_LENGTH - 1; j++)
	{
		printf("%2d ",j + 1);
	}
	printf(">%d\n", MAX_LENGTH - 1);



	return 0;
}



 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值