io_x
2/12/2011 10:37:00 AM
"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4d564da7$0$1369$4fafbaef@reader1.news.tin.it...
Buon Giorno,
this is my final version; Do someone like it?
some of you find some error?
thank you
#include <stdio.h>
#include <ctype.h>
/* from copyright 2011 Ceriousmall. . . .
Program prints a histogram of the length of words in its input */
#define LIMIT 35
#define u32 unsigned
int main(void)
{double d;
int c;
u32 y,i, j, in_word;
u32 nwords, nchar, x;
u32 NofWords[LIMIT]; /* NofWords[3] is the number
of word of 3 chars */
u32 NofWords1[LIMIT]; /* for their printing */
for(i=0; i<LIMIT; ++i) NofWords[i]=0;
nchar=0; in_word=0; nwords=0; y=0;
while(1)
{if((c=getchar())==EOF||
!(isalpha(c)||isdigit(c)||c=='_')
) {if(y==0){in_word=0; nchar=0; }
else if(in_word==1)
{in_word=0; ++nwords;
if(nwords==0xFFFFFFFF) goto err;
if(nchar>=LIMIT)
{printf("Errore parola troppo lunga\n");
return 0;}
++NofWords[nchar];
if(NofWords[nchar]==0xFFFFFFFF)
{err:
printf("Errore troppe parole\n");
return 0;}
nchar=0; y=0;
}
if(c==EOF) goto end;
}
else {if(isalpha(c)||c=='_') y=1;
in_word=1;
++nchar;
}
}
end:
/* calcola il massimo per trovare
il fattore di riduzione*/
for(i=0, x=0; i<LIMIT; ++i)
if(x<NofWords[i]) x=NofWords[i];
nchar=74; // 74+2+1+1+1
d=1.0;
if(x>nchar)
{/* cerco di mantenere la linea < 80 chars con un
coefficiente se necessario */
/* x': x'=nchar x'=x*y => nchar=x*y => y=nchar/x */
d=x;
d=(double) nchar/d;
}
for(i=0; i<LIMIT; ++i)
NofWords1[i]=(u32)(NofWords[i] *d);
printf(
"Numero di parole=%u, Unita\': \'-\' %.2f\n",
nwords, (1.0/d));
for(j=0; j<=76; ++j)
{if(j<3||(j-3)%5!=0) printf(" ");
else {c=printf("%u", (u32)((j-3)*(1.0/d)));
if(c<=0) {err1: printf("Errore di stampa\n");
return 0;}
j=j+c-1;}
}
printf("\n+");
for(j=0; j<=76; ++j) printf("-");
printf("+\n");
if(x==0) printf("No WORDS\n");
else for(i=1; i<LIMIT; ++i)
{x=NofWords1[i];
if(x!=0) {printf("|%2i:", i);
for(j=0; j< x; ++j) printf("-");
if(j<70)
{c=printf("%u", NofWords[i]);
if(c<=0) goto err1;
j+=c;
}
for( ; j<74; ++j) printf(" ");
printf("|\n");
}
}
printf("+");
for(j=0; j<=76; ++j) printf("-");
printf("+\n");
return 0;
}
Numero di parole=302, Unita': '-' 1.43
0 7 14 21 28 35 42 50 57 64 71 78 85 93 100
+-----------------------------------------------------------------------------+
| 1:------------------------------------------------------------------------- |
| 2:-----------------------34 |
| 3:--------------------30 |
| 4:---------13 |
| 5:------------------------35 |
| 6:------------------------------44 |
| 7:-----------17 |
| 8:-------11 |
| 9:----7 |
|10:--3 |
+-----------------------------------------------------------------------------+