Selected solutions to exercise of "The C Programming Language" 2e (Part 2)

Exercise 1-13

Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.

Horizontal version:

#include <stdio.h>

 

#define min(a, b) ((a)<(b)?(a):(b))

#define N 11

 

int isspace(int ch) {

    if (ch == ' ' || ch == '/t' || ch == '/n') {

        return 1;

    }

    else

        return 0;       

}

 

int main(void)

{

    int i, j, c;

    int length = 0;

    int inwordflag = 0;

   

    int counter[N] = {0};

   

    while((c = getchar()) != EOF) {

        if (!isspace(c)) {

            inwordflag = 1;

            length++;

        }

        if (isspace(c) && inwordflag) {

            inwordflag = 0;

            length = min(length, N);

            counter[length - 1]++;

            length = 0;

        }

    }

 

    /* if there is no space after the end of the last word */

    if (inwordflag && c == EOF) {

        length = min(length, N);

        counter[length - 1]++;

    }

    /* print the result */

    for (i = 1; i <= N; i++) {

        printf("%s%-2d ", (i<=10?"  ":">="), i);

        j = counter[i-1];

        while (j-- > 0) {

            printf("*");

        }

        printf("%d", counter[i-1]);

        printf("/n");

    }

   

    return 0;

}

 

Vertical version:

#include <stdio.h>

 

#define min(a, b) ((a)<(b)?(a):(b))

#define N 11

 

int isspace(int ch) {

    if (ch == ' ' || ch == '/t' || ch == '/n') {

        return 1;

    }

    else

        return 0;       

}

/* find the max value from a int array */

int findmax(int data[], int len)

{

    int i, maxvalue = data[0];

    for (i = 1; i < len; i++) {

        if (data[i] > maxvalue) {

            maxvalue = data[i];

        }

    }

    return maxvalue;

}

 

int main(void)

{

    int i, j, c;

    int length = 0;

    int inwordflag = 0;

   

    int counter[N] = {0};

    int maxvalue = 0;

   

    while((c = getchar()) != EOF) {

        if (!isspace(c)) {

            inwordflag = 1;

            length++;

        }

        if (isspace(c) && inwordflag) {

            inwordflag = 0;

            length = min(length, N);

            counter[length - 1]++;

            length = 0;

        }

    }

 

    /* if there is no space after the end of the last word */

    if (inwordflag && c == EOF) {

        length = min(length, N);

        counter[length - 1]++;

    }

   

    /* print the result */

    maxvalue = findmax(counter, N) + 1;

    printf("%5s^/n", " ");

 

    for (i = maxvalue; i >= 1; i--) {

        printf("%4d | ", i);

        for (j = 0; j < N; j++) {

            if (counter[j] >= maxvalue) {

                printf("%-3s", "*");

            }

            else

                printf("%-3s", " ");

        }

        printf("/n");

        maxvalue--;

    }

    printf("%5s%s"," ","+ ");

    for (i = 1; i <= N; i++) {

        printf("%s", "---");

    }     

    printf(">/n%7s"," ");

 

    for (i = 1; i < N; i++) {

        printf("%-3d", i);

    }

    printf(">%-d", i - 1);

    printf("/n");

   

    return 0;

}

 

Exercise 1-14

Write a program to print a histogram of the frequencies of different characters in its input.

/* print a histogram of the frequencies of different characters

* in the input */

#include <stdio.h>

 

#define min(a, b) ((a)<(b)?(a):(b))

#define N 27

 

int isletter(int ch) {

    if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {

        return 1;

    }

    else

        return 0;

}

 

/* change a upper case char to lower case */

int tolower(int ch) {

    if (ch >= 'A' && ch <= 'Z') {

        return ch + 32;

    }

    else return ch;

}

 

/* find the max value from a int array */

int findmax(int data[], int len)

{

    int i, maxvalue = data[0];

    for (i = 1; i < len; i++) {

        if (data[i] > maxvalue) {

            maxvalue = data[i];

        }

    }

    return maxvalue;

}

 

int main(void)

{

    int i, j, c;

   

    int counter[N] = {0};

    int maxvalue = 0;

   

    while((c = getchar()) != EOF) {

        if (isletter(c)) {           

            counter[tolower(c) - 'a']++;

        } else

            counter[N - 1]++;

    }

  

   

    /* print the result */

    maxvalue = findmax(counter, N) + 1;

    printf("%5s^/n", " ");

 

    for (i = maxvalue; i >= 1; i--) {

        printf("%4d | ", i);

        for (j = 0; j < N; j++) {

            if (counter[j] >= maxvalue) {

                printf("%-3s", "*");

            }

            else

                printf("%-3s", " ");

        }

        printf("/n");

        maxvalue--;

    }

    printf("%5s%s"," ","+ ");

    for (i = 1; i <= N; i++) {

        printf("%s", "---");

    }     

    printf(">/n%7s"," ");

 

    for (i = 0; i < N - 1; i++) {

        printf("%-3c", i + 'a');

    }

    printf("other");   

    printf("/n");

   

    return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值