统计各个数字、空白符及其他字符出现的次数及1.13,1.14

统计各个数字、空白符及其他字符出现的次数

1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     int i, j, c, nc, nl;
 6     int nw[10];
 7     nc = nl = 0;
 8     for(i = 0; i < 10; i++)
 9         nw[i] = 0;
10     while((c = getchar()) != EOF){
11         if(c >= '0' && c <= '9')
12             nw[c - '0']++;
13         else if(c == ' ' || c == '\n' || c == '\t')
14             nc++;
15         else
16             nl++;
17     }
18     for(j = 0; j <10; j++)
19         printf("%d\t", j);
20     printf("\n");
21     for(i = 0; i < 10; i++)
22         printf("%d\t", nw[i]);
23     printf("\n%d, %d", nc, nl);
24     return 0;
25 }

1.13 编写一个程序,打印输入中单词长度的直方图

水平直方图

(1)书中答案

1 #include<stdio.h>
 2 
 3 #define MAXHIST 15
 4 #define MAXWORD 11
 5 #define IN 1
 6 #define OUT 0
 7 
 8 int main()
 9 {
10     int c, i, nc, state;
11     int len;
12     int maxvalue;
13 
14 
15     int ovflow;
16     int wl[MAXWORD];
17 
18     state = OUT;
19     nc = 0;
20     ovflow = 0;
21     for(i = 0; i < MAXWORD; i++)
22         wl[i] = 0;
23     while((c = getchar()) != EOF){
24         if(c == ' ' || c == '\n' || c == '\t'){
25             state = OUT;
26             if(nc > 0)
27                 if(nc < MAXWORD)
28                     wl[nc]++;
29                 else
30                     ovflow++;
31             nc = 0;
32         }
33         else if(state == OUT){
34             state = IN;
35             nc = 1;
36         }
37         else
38             nc++;
39     }
40     maxvalue = 0;
41     for(i = 1; i < MAXWORD; i++)
42         if(wl[i] > maxvalue)
43             maxvalue = wl[i];
44     for(i = 1; i < MAXWORD; i++){
45         printf("%5d - %5d : ", i, wl[i]);
46         if(wl[i] > 0){
47             if((len = wl[i] * MAXHIST / maxvalue) <= 0)
48                 len = 1;
49         }
50         else
51             len = 0 ;
52         while(len > 0){
53             putchar('*');
54             --len;
55         }
56         putchar('\n');
57     }
58     if(ovflow > 0)
59         printf("There are %d words >= %d\n", ovflow, MAXWORD);
60     return 0;
61 }

(2)稍作改变。书中水平直方图是按照比例来打印,以下程序是将该单词长度的所有个数打印出来。


1 #include<stdio.h>
 2 
 3 #define MAXHIST 15
 4 #define MAXWORD 11
 5 #define IN 1
 6 #define OUT 0
 7 
 8 int main()
 9 {
10     int c, i, nc, state;
11     int ovflow;
12     int wl[MAXWORD];
13 
14     state = OUT;
15     nc = 0;
16     ovflow = 0;
17     for(i = 0; i < MAXWORD; i++)
18         wl[i] = 0;
19     while((c = getchar()) != EOF){
20         if(c == ' ' || c == '\n' || c == '\t'){
21             state = OUT;
22             if(nc < MAXWORD && nc > 0)
23                 wl[nc]++;
24             else
25                 ovflow++;
26             nc = 0;
27         }
28         else if(state == OUT){
29             state = IN;
30             nc = 1;
31         }
32         else
33             nc++;
34     }
35     for(i = 1; i < MAXWORD; i++){
36         printf("%5d - %5d : ", i, wl[i]);
37         while(wl[i] > 0){
38                 putchar('*');
39                 wl[i]--;
40         }
41         putchar('\n');
42     }
43     if(ovflow > 0)
44         printf("There are %d words >= %d\n", ovflow, MAXWORD);
45     return 0;
46 }

以上两组代码都将标点符号当做了单词的一部分,比如输入:(qaz),将按照单词长度为5的单词输出。

垂直直方图

#include<stdio.h>

#define MAXHIST 15
#define MAXWORD 11
#define IN 1
#define OUT 0

int main()
{
    int c, i, j, nc, state;
    int maxvalue;
    int ovflow;
    int wl[MAXWORD];

    state = OUT;
    nc = 0;
    ovflow = 0;
    for(i = 0; i < MAXWORD; i++)
        wl[i] = 0;
    while((c = getchar()) != EOF){
        if(c == ' ' || c == '\n' || c == '\t'){
            state = OUT;
            if(nc < MAXWORD && nc > 0)
                wl[nc]++;
            else
                ovflow++;
            nc = 0;
        }
        else if(state == OUT){
            state = IN;
            nc = 1;
        }
        else
            nc++;
    }
    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("*\t");
            else
                printf(" \t");
        putchar('\n');
    }
    for(i = 1; i < MAXWORD; i++)
        printf("%d\t", i);
    putchar('\n');
    for(i = 1; i < MAXWORD; i++)
        printf("%d\t", wl[i]);
    putchar('\n');
    if(ovflow > 0)
        printf("There are %d words >= %d\n", ovflow, MAXWORD);
    return 0;
}

1.14 编写一个程序,打印输入中各个字符出现频度的直方图

#include<stdio.h>
#include<ctype.h>
#define MAXHIST 15
#define MAXCHAR 128

int main()
{
    int c, i;
    int maxvalue;
    int len;
    int cc[MAXCHAR];
    for(i = 0; i < MAXCHAR; i++)
        cc[i] = 0;
    while((c = getchar()) != EOF){
            if(c < MAXCHAR)
                cc[c]++;
    }
    maxvalue = 0;
    for(i = 1; i < MAXCHAR; i++)
        if(cc[i] > maxvalue)
            maxvalue = cc[i];
    for(i = 1; i < MAXCHAR; i++){
        if(isprint(i))
            printf("\t%d - %c - %d: ", i, i, cc[i]);
        else
            printf("\t%d -  - %d: ", i, cc[i]);
        if(cc[i] >0){
            if((len = cc[i] * MAXHIST / maxvalue) <= 0)
                len = 1;
        }
        else
            len = 0;
        while(len > 0){
                putchar('*');
                len--;
        }
        putchar('\n');
    }
    return 0;
}

原文链接: https://www.cnblogs.com/wwtbdbc/p/7747380.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/262036

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 下午2:55
下一篇 2023年2月14日 下午2:56

相关推荐