C Primer Plus(第六版)第8章 编程练习答案

题目字太多了略🤣有问题欢迎留言讨论~
这章重定向在mac os上我也没搞清楚怎么搞,mark一下。
IDE: Xcode


8-1

#include <stdio.h>

int main()
{
    int nchar = 0;
    char ch;
    while((ch = getchar()) != EOF)
        nchar++;
    printf("the character number of the file is %d.\n", nchar);//按ctrl+D,传输文件结尾信号(Xcode要连续按两次)
    return 0;
}

Output:
在这里插入图片描述
8-2

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

int main()
{
    char ch;
    int nchar = 0;
    bool clean_nchar = false;
    while((ch = getchar()) != EOF){
        if(ch == '\n'){
            printf("\\n");
            clean_nchar = true;
        }else if(ch == '\t'){
            printf("\\t");
            nchar++;
        }else{
            putchar(ch);
            printf("%d", (int)ch);
            nchar++;
        }
        if(nchar % 10 == 0)
            clean_nchar = true;

        if(clean_nchar == true){
            printf("\n");
            clean_nchar = false;
        }
    }
    return 0;
}

Output:
在这里插入图片描述
8-3

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

int main()
{
    int ch;
    int nlower = 0;
    int nupper = 0;
    printf("please enter a sentence.\n");
    while((ch = getchar()) != EOF){
        if(islower(ch))
            nlower++;
        else if(isupper(ch))
            nupper++;
    }
    printf("there are %d lower letters and %d upper letters.\n", nlower, nupper);
    return 0;
}

Output:
在这里插入图片描述
8-4

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

int main()
{
    int ch;
    int prech = 0;
    int nword = 0;
    int nletter = 0;
    printf("please enter a setence, then it will print out the number of words and average letters.\n");
    while((ch = getchar()) != EOF){
        if((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122))
            nletter++;
        else if((prech >= 65 && prech <= 90) || (prech >= 97 && prech <= 122)) //上一个是字母,下一个不是字母,就是一个单词
            nword++;
        prech = ch;
    }
    if((prech >= 65 && prech <= 90) || (prech >= 97 && prech <= 122))//如果句子最后结尾是字母,那word++
        nword++;
    printf("this sentence has %d words, which average letters is %d.\n", nword, nletter / nword);
    return 0;
}

Output:
在这里插入图片描述
8-5

#include <stdio.h>

int main()
{
    printf("Pick an integer from 1 to 100. I will try to guess it.\n");
    printf("respond with a 'y' if my guess is right, and with a 'b' if\n");
    printf("bigger than my guess, with a 's' if smaller than my geuss.\n");
    int guess = 50;
    int lower = 1;
    int upper = 100;
    int ch = 0;

    while(1){
        printf("is your number %d? or bigger? or smaller?\n", guess);
        ch = getchar();
        getchar(); //读取缓冲区enter键
        if (ch == 'y'){
            printf("I knew i could do it!\n");
            break;
        }else if(ch == 'b'){
            lower = guess;
            if(upper - lower == 1){  //当lower = 99,upper = 100,相加除以2一直都是99,所以100要单独处理
                guess = upper;
                continue;
            }
        }else if(ch == 's'){
            upper = guess;
        }else{
            printf("you enter the wrong respond.\n");
            continue;
        }
        guess = (lower + upper) / 2;
    }
    return 0;
}

Output:
在这里插入图片描述
8-6

#include <stdio.h>

int main()  //char get_first()
{
    int ch;
    ch = getchar();
    while(ch == ' ' || ch == '\n' || ch == '\t'){
        ch = getchar();
        continue;
    }
    putchar(ch);
    printf("\n");
    return 0; //return ch;
}

Output:
在这里插入图片描述
8-7

#include <stdio.h>
#define SALARY_1 8.75 //用define来表示各工资等级和税率
#define SALARY_2 9.33
#define SALARY_3 10.00
#define SALARY_4 11.20
#define TAX_RATE_300 0.15
#define TAX_RATE_NEXT_150 0.2
#define TAX_RATE_OTHERS 0.25
#define TAX_300 45
#define TAX_NEXT_150 30

int main()
{
    char wage_scale; //int wage_scale;
    int hours;
    float salary_hour, salary_week, tax;

    while(1){    //实现程序循环运行
        printf("******************************************************************\n");
        printf("Enter the number corresponding to the desired pay rate or actions:\n");
        printf("a) $%5.2f/hr                       b) $%5.2f/hr\n", SALARY_1, SALARY_2);
        printf("c) $%5.2f/hr                       d) $%5.2f/hr\n", SALARY_3, SALARY_4);
        printf("q) quit\n");
        printf("******************************************************************\n");

        wage_scale = getchar(); //scanf("%d", &wage_scale);
        getchar(); //读取缓冲区enter键

        switch (wage_scale){  //用switch实现salary等级的选择
            case 'a':
                salary_hour = SALARY_1;
                break;
            case 'b':
                salary_hour = SALARY_2;
                break;
            case 'c':
                salary_hour = SALARY_3;
                break;
            case 'd':
                salary_hour = SALARY_4;
                break;
            case 'q':
                goto EXIT;
            default:
                printf("ERROR: Please enter the right number.\n\n"); //输入除1~5之外的数字要重新运行程序重新输入
                continue;
        }

        printf("Please interput your work hours per week:");
        scanf("%d", &hours);
        getchar(); //读取缓冲区enter键

        if (hours > 40){ //计算每周薪资
            salary_week = (40 + (hours - 40) * 1.5) * salary_hour;
        }else {
            salary_week = hours * salary_hour;
        }

        if(salary_week <= 300){ //计算税收,(i)工资<=300 (ii) 300<工资<=450 (iii)工资>450
            tax = salary_week * TAX_RATE_300;
        }else if(salary_week > 300 && salary_week <= 450){
            tax = TAX_300 + (salary_week - 300) * TAX_RATE_NEXT_150;
        }else {
            tax = TAX_300 + TAX_NEXT_150 + (salary_week - 300 - 150) * TAX_RATE_OTHERS;
        }

        printf("salay = %.2f dollars\ntax = %.2f dollars\nnet income = %.2f dollars\n\n", salary_week, tax, salary_week - tax); //输出
    }

EXIT:
    printf("Done\n");

    return 0;
}

Output:
在这里插入图片描述
8-8

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

float get_number(char choice); //判断输入是否有效,输入必须为数字,若为除法,除数不能为0

int main()
{
    float first, second;
    char choice;
    bool exit = false;

    while(1){    //实现程序循环运行
        printf("Enter the operation of your choice:\n");
        printf("a. add             s. subtract\n");
        printf("m. multiply        d. divide\n");
        printf("q. quit\n");
        choice = getchar(); //scanf("%d", &wage_scale);
        getchar(); //读取缓冲区enter键

        if(choice == 'q')
            goto EXIT;

        printf("Enter first number:");
        first = get_number(choice);
        printf("Enter second number:");
        second = get_number(choice);

        switch (choice){
            case 'a':
                printf("%.1f + %.1f = %.1f\n", first, second, first + second); //保留一位小数
                break;
            case 's':
                printf("%.1f - %.1f = %.1f\n", first, second, first - second);
                break;
            case 'm':
                printf("%.1f x %.1f = %.1f\n", first, second, first * second);
                break;
            case 'd':
                printf("%.1f / %.1f = %.1f\n", first, second, first / second);
                break;
            default:
                printf("ERROR: Please enter the right choice.\n"); //输入除选项之外的字符要重新运行程序重新输入
                continue;
        }
    }
EXIT:
    printf("Bye.\n");
    return 0;
}

float get_number(char choice)
{
    float number;
    char ch;
    while(1){
        if(scanf("%f", &number)){
            getchar(); //读取缓冲区enter键
            if(choice == 'd' && number == 0){ //如果是除法,除数不能为0
                printf("Enter a number other than 0:");
                continue;
            }
            return number;
        }else{
            while(1){
                ch = getchar();
                if(ch == '\n')
                    break;
                putchar(ch);
            }
            printf(" is not an number.\n");
            printf("Please enter a number, such as 2.5, -1.78E8, or 3:");
        }
    }
}

Output:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值