C prime plus第八章(字符控制)编程练习 --第五版

C prime plus第八章(字符控制)编程练习 –第五版

c语言编程练习都为本人学习时的编写代码记录或学习笔记,若有不当之处欢迎指正,感激不尽(其中题型当然可能有其他编程设计方案,比如利用自定义函数等,因为我在操练基础,所以就不涉及,仅作记录)。



1.设计一个程序,统计从输入到文件结尾为止的字符数。

#include<stdio.h>

int main(void)
{
    int i;
    char ch;
    for(i = 0; (ch = getchar()) != EOF; i++);
    printf("COUNT = %d \n", i);
    printf("%d\n", ch);
    return 0;
}


2.编写一个程序,把输入作为字符流读取,直到遇到 EOF。令该程序打印每个输入字符及其 ASCII编码的十进制值。注意在 ASCII 序列中空格字符前面的字符是非打印字符,要特殊处理这些字符。如果非打印字符是换行符或制表符,则分期打印\n 或\t。否则,使用控制字符符号。例如,ASCII 的 l 是 Ctrl+A,可以显示为 AA。注意 A ASCⅡ值是 Ctrl+A 的值加 64。对其他非打印字符也保持相似的关系。除去每次遇到一个换行符时就开始一个新行之外,每行打印 10 对值。

#include<stdio.h>

int main(void)
{
    char  ch;
    int i;

    for(i = 1; (ch = getchar()) != EOF; i++) {
        if(ch >= ' ' || ch == '\n' || ch == '\t')
            printf("%-5c", ch);
        else
            printf("%-4c", ch + 64);
        printf("%-5d", ch);
        if(i % 10 == 0)
            printf("\n");
    }
    return 0;
}


3.编写一个程序,把输入作为字符流读取,直至遇到 EOF。令其报告输入中的大写字母个数和小写字母个数。假设小写字母的数值是连续的,大写字母也是如此。或者你可以使用 ctypc,h 库中的合适的函数来区分大小写。

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

int main(void)
{
    char ch;
    int l = 0, u = 0;

    printf("Please input some characters:\n");
    while((ch = getchar()) != EOF) {
        if(islower(ch))
            l++;
        if(isupper(ch))
            u++;
    }
    printf("\nlower letter = %d,\nupper letter = %d.\n", l, u);

    return 0;
}


4.编写一个程序,把输入作为字符流读取,直至遇到 EOF。令其报告每个单词的平均字母数。 不要将空白字符记为单词中的字母。 实际上,标点符号也不应该计算,但现在不必考虑这一点 (如果您想做得好一些, 可以考虑使用 ctype.h系列中的 ispunct() 函数)。

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

int main(void)
{
    char ch;
    int word = 0, i = 0;
    bool inword = false;

    while((ch = getchar()) != EOF) {
        if(!ispunct(ch) && !isspace(ch))
            i++;
        if(isspace(ch) && !inword) {
            inword = true;
            word++;
        }
        if(!isspace(ch) && inword)
            inword = false;
    }
    printf("\ni: %d\nword: %d\naverage: i/word = %f\n", i, word, (float)i / (float)word);
    return 0;
}


5,修改程序清单 8,4 中的猜测程序,使其使用更智能的猜测策略。例如,程序最初猜 50,让其询问用户该猜测值是大、小还是正确。如果该猜测值小,则令下一次猜测值为 50 和 100 的中值,也就是75。如果 75 大,则下一次猜测值为 75 和 50 的中值,等等。使用这种二分搜索(binary search)策略,起码如果用户没有欺骗,该程序很快会获得正确答案。

扫描二维码关注公众号,回复: 52045 查看本文章
#include<stdio.h>

int main(void)
{
    int guess, max, min;
    min = 0, max = 100;
    char res; /* res-response */

    printf("Pick an integer from 1 to 100. I will try to guess ");
    printf("it. \nRespond with a b if my guess is big and with ");
    printf("\nan l if it is little.\n");
    printf("Also, Respond a y if it is right.\n");
    printf("Uh...is your number %d?\n", guess = (max + min)/2);

    while((res = getchar()) != 'y') /* 获取用户响应并和y比较 */ {
        if(res == 'b') {
            max = guess - 1;
            printf("Well, then, is it %d?\n", guess = (max + min)/2);
        }

        if(res == 'l') {
        min = guess + 1;
            printf("Well, then, is it %d?\n", guess = (max + min)/2);
        }

        else
            printf("Sorry, I understand only y or n.\n");

        while(getchar() != '\n');
    }

    printf("I knew I could do it!\n");

    return 0;
}


6.修改程序清单 8.8 中的 get_first()函数,使其返回所遇到的第一个非空白字符。在一个简单的程序中测试该h函数。

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

char get_first(void);

int main(void)
{
    char ch;

    while((ch = getchar()) != EOF) {
        putchar(ch);
    }

    return 0;
}

char get_first(void)
{
    int ch;

    while(isspace(ch = getchar()));

    while(getchar() != '\n');
    return ch;
}


7.修改第 7 章的练习 8,使菜单选项由字符代替数字进行标记。

#include<stdio.h>
#define TIME 40             //正常工作时间
#define ADD 1.5             //加班的折算时间倍数
#define L1 300              //工资额度1
#define RATE1 0.15          //L1的税率
#define L2 150              //工资额度2
#define RATE2 0.20          //L2的税率
#define RATE3 0.25          //余下工资额度的税率
int main(void)
{
    double h, g, t, BASIC;
    int d;
    char n;
    //h-hours,g-gross,t-tax,d-data

    for(d = 0; d < 69; d++)
        printf("*");
    printf("\n\n");

    printf("Enter the number corresponding to the desired pay rate or action:\n");
    printf("1) $8.75/hr\t\t\t2) $9.33/hr\n");
    printf("3) $10.00/hr\t\t\t4) $11.20/hr\n");
    printf("5) quit\n\n");

    for(d = 0; d < 69; d++)
        printf("*");
    printf("\n");
    n = getchar();
    while(getchar() != '\n' );
    while(1) {
        while( n < '1' || n > '5') {
            printf("error,it's wrong number,please input again!(1-5):\n");
            n = getchar();
        }
        switch(n) {
            case '1': BASIC = 8.75;
                break;
            case '2': BASIC = 9.33;
                break;
            case '3': BASIC = 10.00;
                break;
            case '4': BASIC = 11.20;
                break;
            case '5': printf("quit!\n");
                return 0;
        }

        printf("input the work hours of a week:\n");
        scanf("%lf", &h);
        if(h > 40)
            h = 40 + (h - 40) * ADD;
        g = h * BASIC;
        if(g <= L1)
            t = g * RATE1;
        else if(g <= L2)
            t = g * RATE1 + (g - L1) * RATE2;
        else
            t = g * RATE1 + (g - L1) * RATE2 + (g - L1 - L2) * RATE3;
        printf("gross income = %lf\n", g);
        printf("tax = %lf\n", t);
        printf("net income = %lf\n", g - t);
        printf("Please input your selection(1~4) again!(type 5 to quit).\n");
        while(getchar() != '\n');
        n = getchar();
    }
    return 0;
}


8.编写一个程序,显示一个菜单,为您提供加法、减法、乘法或除法的选项。获得您的选择后,该程序请求两个数,然后执行您选择的操作。该程序应该只接受它所提供的菜单选项。它应该使用 float类型的数,并且如果用户未能输入数字应允许其重新输入。在除法的情况中,如果用户输入 O 作为第二个数,该程序应该提示用户输入一个新的值。一个典型的程序运行应该如下所示:

8.8

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

float get_float(void);
char get_first(void);


int main(void)
{
    char se ;       /* select */
    float n1, n2;   /* number1, number2 */

    while(1) {
        printf("Enter the operation of your choice:\n");
        printf("a. add\t\t\ts. subtract\n");
        printf("m. multiply\t\td. divide\n");
        printf("q. quit\n");
        se = get_first();

        if(se != 'a' && se != 's' && se != 'm' && se != 'd') {
            printf("Bye!.\n");
            break;
        }

        printf("Enter first number:");
        n1 = get_float();
        printf("Enter second number:");
        n2 = get_float();

        while(se == 'd' && n2 == 0) {
            printf("Enter a number other than 0:");
            n2 = get_float();
        }
        switch(se) {
            case 'a': printf("%.2f + %.2f = %.2f\n", n1, n2, n1 + n2);
                    break;
            case 's': printf("%.2f - %.2f = %.2f\n", n1, n2, n1 - n2);
                    break;
            case 'm': printf("%.2f * %.2f = %.2f\n", n1, n2, n1 * n2);
                    break;
            case 'd': printf("%.2f / %.2f = %.2f\n", n1, n2, n1 / n2);
                    break;
            default:
                    break;
    }

    return 0;
}

float get_float(void)       /* 得到一个合适的浮点数 */
{
    float n;
    char str[40];
    while(scanf("%f", &n) != 1) {
        fgets(str, 40, stdin);
        printf("%s is not a number.\n", str);
        printf("Please enter a number, such as 3.2 -1.34E5, or 2:");
    }
    while(getchar() != '\n');

    return n;
}

char get_first(void)  /* 只获取字符串首字符 */
{
    int ch;

    while(isspace(ch = getchar()));
    while(getchar() != '\n');
    return ch;
}

猜你喜欢

转载自blog.csdn.net/sunshineddMMZ/article/details/80055650
今日推荐