[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Re: need some help with this histogram of words program........

io_x

2/12/2011 8:16:00 AM


"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4d4fa842$0$1339$4fafbaef@reader2.news.tin.it...
>
> "Ceriousmall" <divadsmall@gmail.com> ha scritto nel messaggio
> news:f9200200-273b-403d-88cf-2832fb6c6de1@j11g2000yqh.googlegroups.com...
>>I think this is better tho...................
>>
>> /* copyright 2011 Ceriousmall. . . .
>>
>> Program prints a histogram of the length of words in its input */
>
> What is one "histogram"?
> if it is
> 1:--------
> 2:----
> 3:-----

but this count number as words too

#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 wordlength[LIMIT];

for(i=0; i<LIMIT; ++i) wordlength[i]=0;
nchar=0; in_word=0; nwords=0; y=0;

while(1)
{if((c=getchar())==EOF||
!(isalpha(c)||isdigit(c)||c=='_')
) {if(in_word==1)
{in_word=0; ++nwords;
if(nwords==0xFFFFFFFF) goto err;
if(nchar>=LIMIT)
{printf("Errore parola troppo lunga\n");
return 0;}
++wordlength[nchar];
if(wordlength[nchar]==0xFFFFFFFF)
{err:
printf("Errore troppe parole\n");
return 0;}
nchar=0;
}
if(c==EOF) goto end;
}
else {in_word=1;
++nchar;
}
}
end:
for(i=0, x=0; i<LIMIT; ++i)
if(x<wordlength[i]) x=wordlength[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)
wordlength[i]=(long)(wordlength[i] *d);
}
printf(
"Numero di parole=%u, Unita\': \'-\' %.2f\n",
nwords, (1.0/d));
for(j=0; j<=76; ++j)
{if(j<3||(j+2)%5!=0) printf(" ");
else {c=printf("%u", (u32)((j-3)*(1.0/d)));
if(c<=0) {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=wordlength[i];
if(x!=0) {printf("|%2i:", i);
for(j=0; j< x; ++j) printf("-");
for( ; j<74; ++j) printf(" ");
printf("|\n");
}
}
printf(" ");
for(j=0; j<=76; ++j) printf("-");
printf("\n");

return 0;
}



6 Answers

io_x

2/12/2011 8:34:00 AM

0


"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4d56419e$0$1356$4fafbaef@reader1.news.tin.it...
>
> "io_x" <a@b.c.invalid> ha scritto nel messaggio
> news:4d4fa842$0$1339$4fafbaef@reader2.news.tin.it...
>>
>> "Ceriousmall" <> ha scritto nel messaggio
>> news:f9200200-273b-403d-88cf-2832fb6c6de1@j11g2000yqh.googlegroups.com...
>>>I think this is better tho...................
>>>
>>> /* copyright 2011 Ceriousmall. . . .
>>>
>>> Program prints a histogram of the length of words in its input */
>>
>> What is one "histogram"?
>> if it is
>> 1:--------
>> 2:----
>> 3:-----
>
> but this count number as words too

this not
#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 wordlength[LIMIT];

for(i=0; i<LIMIT; ++i) wordlength[i]=0;
nchar=0; in_word=0; nwords=0; y=0;

while(1)
{if((c=getchar())==EOF||
!(isalpha(c)||isdigit(c)||c=='_')
) {if(in_word==1 && y==1)
{in_word=0; ++nwords;
if(nwords==0xFFFFFFFF) goto err;
if(nchar>=LIMIT)
{printf("Errore parola troppo lunga\n");
return 0;}
++wordlength[nchar];
if(wordlength[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:
for(i=0, x=0; i<LIMIT; ++i)
if(x<wordlength[i]) x=wordlength[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)
wordlength[i]=(long)(wordlength[i] *d);
}
printf(
"Numero di parole=%u, Unita\': \'-\' %.2f\n",
nwords, (1.0/d));
for(j=0; j<=76; ++j)
{if(j<3||(j+2)%5!=0) printf(" ");
else {c=printf("%u", (u32)((j-3)*(1.0/d)));
if(c<=0) {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=wordlength[i];
if(x!=0) {printf("|%2i:", i);
for(j=0; j< x; ++j) printf("-");
for( ; j<74; ++j) printf(" ");
printf("|\n");
}
}
printf(" ");
for(j=0; j<=76; ++j) printf("-");
printf("\n");

return 0;
}

this is one source code file: what in language is it? :)

Numero di parole=1392, Unita': '-' 6.99
0 34 69 104 139 174 209 244 279 314 349 384 419 454 489
-----------------------------------------------------------------------------
| 1:------------------------- |
| 2:----------- |
| 3:--------------------------------------------------------------------------|
| 4:--------------------- |
| 5:----------------------------- |
| 6:--------------- |
| 7:----- |
| 8:------- |
| 9:--- |
|11:- |
|12:- |
|15:- |
-----------------------------------------------------------------------------



io_x

2/12/2011 8:51:00 AM

0


"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4d5645f2$0$1358$4fafbaef@reader2.news.tin.it...
>>> "Ceriousmall" <> ha scritto nel messaggio
>>> news:f9200200-273b-403d-88cf-2832fb6c6de1@j11g2000yqh.googlegroups.com...
>>>>I think this is better tho...................
>>>>
>>>> /* copyright 2011 Ceriousmall. . . .
>>>>
>>>> Program prints a histogram of the length of words in its input */
>>>
>>> What is one "histogram"?
>>> if it is
>>> 1:--------
>>> 2:----
>>> 3:-----
>>
>> but this count number as words too

i found one error

#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 wordlength[LIMIT];

for(i=0; i<LIMIT; ++i) wordlength[i]=0;
nchar=0; in_word=0; nwords=0; y=0;

/* word is alphanumeric with at last one letter or '_' */
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;}
++wordlength[nchar];
if(wordlength[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:
for(i=0, x=0; i<LIMIT; ++i)
if(x<wordlength[i]) x=wordlength[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)
wordlength[i]=(long)(wordlength[i] *d);
}
printf(
"Numero di parole=%u, Unita\': \'-\' %.2f\n",
nwords, (1.0/d));
for(j=0; j<=76; ++j)
{if(j<3||(j+2)%5!=0) printf(" ");
else {c=printf("%u", (u32)((j-3)*(1.0/d)));
if(c<=0) {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=wordlength[i];
if(x!=0) {printf("|%2i:", i);
for(j=0; j< x; ++j) printf("-");
for( ; j<74; ++j) printf(" ");
printf("|\n");
}
}
printf(" ");
for(j=0; j<=76; ++j) printf("-");
printf("\n");

return 0;
}
Numero di parole=1392, Unita': '-' 8.74
0 43 87 131 174 218 262 306 349 393 437 480 524 568 612
-----------------------------------------------------------------------------
| 1:------------------------ |
| 2:----------- |
| 3:--------------------------------------------------------------------------|
| 4:------------- |
| 5:---------------------- |
| 6:------ |
| 7:--- |
| 8:- |
| 9:- |
-----------------------------------------------------------------------------



io_x

2/12/2011 9:07:00 AM

0


"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4d5649d4$0$1371$4fafbaef@reader1.news.tin.it...

#define u32 unsigned
....
> {/* 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)
> wordlength[i]=(long)(wordlength[i] *d);
this line above should be

wordlength[i]=(u32)(wordlength[i] *d);

> }



io_x

2/12/2011 10:37:00 AM

0


"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 |
+-----------------------------------------------------------------------------+




io_x

2/12/2011 11:15:00 AM

0


"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4d5662cf$0$1357$4fafbaef@reader1.news.tin.it...
>
> "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

i add log10() for know the len of one number

#include <stdio.h>
#include <ctype.h>
#include <math.h>

/* from copyright 2011 Ceriousmall. . . .

Program prints a histogram of the length of words in its input */

#define LIMIT 35
#define u32 unsigned

/* one word is alhpanumeric + _ not only numeric */

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\': \'-\' %.2lf\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("-");
y=(u32)(log10(NofWords[i])+1.0); /* N cifre */
if(y+j<=74)
{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;
}



io_x

2/12/2011 2:08:00 PM

0

"io_x" <a@b.c.invalid> ha scritto nel messaggio
news:4d566b98$0$1366$4fafbaef@reader1.news.tin.it...
> if(x==0) printf("No WORDS\n");
> else for(i=1; i<LIMIT; ++i)
> {x=NofWords1[i];
> if(x!=0) {printf("|%2i:", i);
^^^^^
this above should be "printf("|%2u:", i);"
but there is someone read this?