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

这是一个关于C Primer Plus第六版第七章的编程练习解答,包括7-1到7-11共11道题目。每个题目都提供了运行结果,解答者使用Xcode作为集成开发环境。部分题目如7-11还强调了输入有效性检查的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目字太多了略🤣有问题欢迎留言讨论~
IDE: Xcode


7-1

#include <stdio.h>

int main()
{
    char ch;
    int n_word = 0;
    int n_line = 0;
    int n_space = 0;
    printf("enter text to be analyzed('#' to exit):\n");
    while((ch = getchar()) != '#'){
        if (ch == '\n')
            n_line++;
        else if(ch == ' ')
            n_space++;
        else
            n_word++;
    }
    printf("this text contain: lines = %d spaces = %d words = %d\n", n_line, n_space, n_word);
    return 0;
}

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

#include <stdio.h>

int main()
{
    char ch;
    int i = 0;
    printf("change word to ASCII('#' to exit):\n");
    while((ch = getchar()) != '#'){
        putchar(ch);
        printf(" = %d  ", (int)ch);
        i++;
        if((i % 8) == 0 && i != 0){
            printf("\n");
        }
    }
    printf("\nDone\n");
    return 0;
}

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

#include <stdio.h>

int main()
{
    int n;
    int n_even = 0, n_odd = 0;
    float sum_even = 0, sum_odd = 0;
    while(scanf("%d", &n)){
        if(n == 0){
            break;
        }else if(n % 2 == 0){
            n_even++;
            sum_even += n;
        }else {
            n_odd++;
            sum_odd += n;
        }
    }
    printf("n_even = %d avenue of even = %.2f\n", n_even, sum_even / n_even);
    printf("n_odd = %d avenue of odd = %.2f\n", n_odd, sum_odd / n_odd);
    return 0;
}

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

#include <stdio.h>

int main()
{
    char ch;
    printf("please enter text:\n");
    while ((ch = getchar()) != '#'){
        if(ch == '.'){
            putchar('!');
        }else if(ch == '!'){
            putchar('!');
            putchar('!');
        }else{
            putchar(ch);
        }
    }
    printf("\ndone.\n");
    return 0;
}

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

#include <stdio.h>

int main()
{
    char ch;
    printf("please enter text:\n");
    while ((ch = getchar()) != '#'){
        switch(ch){
            case '.':
                putchar('!');
                break;
            case '!':
                putchar('!');
                putchar('!');
                break;
            default:
                putchar(ch);
        }
    }
    printf("\ndone.\n");
    return 0;
}

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

#include <stdio.h>

int main()
{
    char ch;
    char prev = 0;
    int n_ei = 0;
    printf("calculate the number of 'ei' in text('#' to exit):\n");
    while((ch = getchar()) != '#'){
        if(ch == 'e')
            prev = ch; //当前字母是e,就把字母记录在prev
        else if(prev == 'e'){
            if(ch == 'i')
                n_ei++;
            prev = ' '; //上一个字母是e,且当前字母是i, n_ei++,把prev清掉。上一个字母是e,当前字母不是i,也把prev清掉。
        }
    }
    printf("the number of 'ei' in text is %d.\n", n_ei);
    return 0;
}

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

#include <stdio.h>
#define SALARY_HOUR 1000
#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()
{
    int hours;
    float salary, tax;
    printf("Please interput your work hours per week, and then output your salary/tax/net_income:");
    scanf("%d", &hours);
    if (hours > 40){
        salary = (40 + (hours - 40) * 1.5) * SALARY_HOUR;
    }else {
        salary = hours * SALARY_HOUR;
    }
    if(salary <= 300){
        tax = salary * TAX_RATE_300;
    }else if(salary > 300 && salary <= 450){
        tax = TAX_300 + (salary - 300) * TAX_RATE_NEXT_150;
    }else {
        tax = TAX_300 + TAX_NEXT_150 + (salary - 300 - 150) * TAX_RATE_OTHERS;
    }
    printf("salay = %.2f dollars\ntax = %.2f dollars\nnet income = %.2f dollars\n", salary, tax, salary - tax);
    return 0;
}

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

#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()
{
    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("1) $%5.2f/hr                       2) $%5.2f/hr\n", SALARY_1, SALARY_2);
        printf("3) $%5.2f/hr                       4) $%5.2f/hr\n", SALARY_3, SALARY_4);
        printf("5) quit\n");
        printf("******************************************************************\n");

        scanf("%d", &wage_scale);

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

        printf("Please interput your work hours per week:");
        scanf("%d", &hours);

        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:
在这里插入图片描述
7-9

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

int main()
{
    int n;
    bool flag; //素数是true,非素数是false
    printf("please enter a positive integer:");
    scanf("%d", &n);
    printf("prime number is:");
    for(int i = 1; i <= n; i++){
        flag = true;
        for(int j = 2; j < i; j++){ //把n去循环除以2~n-1之间的每一个整数,若可以整除,则不是素数,break退出循环,若均不能被整除,n就是一个素数。
            if(i % j == 0){
                flag = false;
                break;
            }
        }
        if(flag == true)
        printf("%d ", i);
    }
    printf("\nDone.\n");
    return 0;
}

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

#include <stdio.h>
#define TYPE_A 17850
#define TYPE_B 23900
#define TYPE_C 29750
#define TYPE_D 14875

int main()
{
    int salary, type;
    int tax_node;
    float tax;
    while(1){
        printf("Please enter your salary(enter 'q' for quit):");
        if(!scanf("%d", &salary))
            break;
        printf("Please enter your tax type:\n");
        printf("1.single 2.householder 3.married 4.divorce\n");
        scanf("%d", &type);
        if(type == 1)
            tax_node = TYPE_A;
        else if(type == 2)
            tax_node = TYPE_B;
        else if(type == 3)
            tax_node = TYPE_C;
        else if(type == 4)
            tax_node = TYPE_D;
        else{
            printf("wrong type.");
            continue;
        }
        if (salary > tax_node){
            tax = tax_node * 0.15 + (salary - tax_node) * 0.28;
        }else{
            tax = salary * 0.15;
        }
        printf("Your tax is %.2f.\n", tax);
    }
    printf("done.\n");
    return 0;
}

Output:
在这里插入图片描述
7-11 (修改:增加检查输入是否有效)

#include <stdio.h>
#include <stdbool.h>
#define A_PRICE 2.05  //洋蓟的单价
#define B_PRICE 1.15  //甜菜的单价
#define C_PRICE 1.09  //胡萝卜的单价
#define DISCOUNT 0.95  //超出100美元5%的折扣优惠
#define FREIGHT_LESS_5 6.5  //重量<=5镑运费6.5美元
#define FREIGHT_LESS_20 14   //5<重量<=20镑运费14美元
#define FREIGHT_MORE_20 0.5  //2重量>=20镑,超出20镑每磅0.5美元

int main()
{
    int weight_a = 0, weight_b = 0, weight_c = 0, weight_sum;
    float cost = 0, freight = 0, discount = 0, sum_cost;
    char type;
    int weight;
    char temp;
    bool is_weight;
    printf("Please choose the vegetable that you want to buy:\n");
    printf("(enter 'q' for closing the order and waiting the program to calculate the cost)\n");
    while(1){
        printf("a.artichoke    b.beet    c.carrot\n");
        scanf("%c", &type);
        getchar();//需要getchar将缓冲区中输入weight时的enter键接收,因为后面还有get char
        if (type == 'q'){
            printf("quit to order.\n");
            break;
        }else if(type != 'a' && type != 'b' && type != 'c'){ //检查type输出是否有效
            printf("Please enter right choice.\n");
            continue;
        }
        weight = 0; //每次输入weight之前要初始化
        is_weight = true;
        printf("Please enter the weight:");
        while((temp = getchar()) != '\n'){ //通常用getchar检查输入是否为有效数字, 例如可以排除负数,字符等
            if(temp < '0' || temp > '9'){
                is_weight = false;  //用一个bool来记录对weight的输入检查结果
                break;
            }
            weight = weight * 10 + (temp - 48);
        }
        if(!is_weight){
            printf("Weight must be positive integer.please enter again.\n"); //weight要是有效数字
            while(getchar() != '\n');//因为循环中有输入type是char,所以要清除缓冲区
            continue;
        }
        
        switch(type){
            case 'a':
                weight_a += weight;
                break;
            case 'b':
                weight_b += weight;
                break;
            case 'c':
                weight_c += weight;
                break;
            default:
                printf("Error.\n"); //前面已经检查过type,如果这里走进default,就是程序出了问题
                return 0;
        }
    }
    cost = weight_a * A_PRICE + weight_b * B_PRICE + weight_c * C_PRICE;
    if (cost >= 100)
        discount = cost * DISCOUNT;

    weight_sum = weight_a + weight_b + weight_c;
    if(weight_sum > 0 && weight_sum <= 5) //weight要大于0才能有运费(谢谢 id:棘空 的提醒啦)
        freight = FREIGHT_LESS_5;
    else if(weight_sum > 5 && weight_sum <= 20)
        freight = FREIGHT_LESS_20;
    else if(weight_sum > 20)
        freight = FREIGHT_LESS_20 + (weight_sum - 20) * FREIGHT_MORE_20;

    sum_cost = cost - discount + freight;

    printf("------------------------------------------\n");
    printf("a.artichoke's price:$%3.2f weight:%d\n", A_PRICE, weight_a);
    printf("b.  beef's    price:$%3.2f weight:%d\n", B_PRICE, weight_b);
    printf("c. carrot's   price:$%3.2f weight:%d\n", C_PRICE, weight_c);
    printf("vegetable cost:$%.2f", cost);
    if (discount > 0)
        printf("(cost >= $100)discount:$%.2f\n", discount);
    else
        printf("(cost < $100)no discount.\n");
    printf("freight:$%.2f\n", freight);
    printf("total cost:$%.2f\n", sum_cost);
    printf("done.\n");
    return 0;
}

Output:
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值