/*
* 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; }