C Primer Plus 第6版 第八章 编程练习参考答案

编译环境VS Code+WSL GCC 源码请到文末下载 。 我给第一题写了Linux shell脚本,感兴趣的同学可以尝试修改并运行一下。

/*第1题*************************/
#include<stdio.h>
int main(void)
{
    int cnt = 0;
    while (getchar() != EOF)
    {
        cnt++;
    }
    printf("总共读取了%d个字符\n",cnt);
    return 0;
}
/*第2题*************************/
#include<stdio.h>
void format_print(int ch);
int main(void)
{
    int cnt = 0,ch = 0;
    while ((ch = getchar()) != EOF)
    {
        cnt++;
        format_print(ch);
        if(0 == cnt % 9)
            putchar(10);
    }
    printf("\n总共读取了%d个字符\n",cnt);
    return 0;
}
void format_print(int ch)
{
    if(ch < 33)
    {
        printf("^%c:%-5d",ch+32,ch);
    }
    else if(ch < 127)
    {
        printf("%c:%-5d",ch,ch);
    }
    else
    {
        printf("END:%-5d",ch);
    }
}
/*第3题*************************/
#include<stdio.h>
#include<ctype.h>
int main(void)
{
    int cnt_lower = 0,cnt_upper = 0,ch = 0;
    while ((ch = getchar()) != EOF)
    {
        
        if(islower(ch))
        {
            cnt_lower++;
        }
        else if (isupper(ch))
        {
            cnt_upper++;
        }
    }
    printf("总共读取了%d个大写字母,%d个小写字母\n",cnt_upper,cnt_lower);
    return 0;
}
/*第4题*************************/
#include<stdio.h>
#include<ctype.h>
#define YES -1
#define NO  0
int main(void)
{
    int cnt_words = 0,cnt_alpha = 0,start_cnt = NO,ch = 0;
    while ((ch = getchar()) != EOF)
    {
        
        if(isalpha(ch) && start_cnt == NO)
        {   /*如果没有开始计数并且字符是个字母,则开始计数*/
            start_cnt = YES;
        }
        else if (!isalpha(ch) && start_cnt == YES)
        {   /*如果已经有开始计数并且字符不是个字母,则停止计数,单词数量+1*/
            start_cnt = NO;
            cnt_words++;
        }
        else if (isalpha(ch) && start_cnt == YES)
        {   /*如果已经有开始计数并且字符是个字母,开始字母计数,字母数量+1*/
            cnt_alpha++;
        }
        
    }
    printf("总共读取了%d单词,%d字母,平均每个单词%g个字母\n",cnt_words,cnt_alpha,1.0 * cnt_alpha / cnt_words);
    return 0;
}
/*第5题*************************/
/* guess.c -- an inefficient and faulty number-guesser */
#include <stdio.h>
int main(void)
{
    int guess_max = 100,guess_min = 0,guess = 50;
    
    printf("想一个1~100的整数,我会尝试猜出它!\n");
    printf("如果我猜错了,请输入'n'\n如果我猜对了,请输入'y'\n");
    printf("呃...你想的是 %d?\n", guess);
    while (getchar() != 'y')      /* get response, compare to y */
    {
        while (getchar() != '\n')/*清空stdin缓存*/
            continue;
        printf("你想的数比%d大吗?(y\\n)\n", guess);
        if(getchar() == 'y')
        {
            guess_min = guess;
            guess = (guess_max + guess_min) / 2;
        }
        else
        {
            guess_max = guess;
            guess = (guess_max + guess_min) / 2;
        }
        while (getchar() != '\n')/*清空stdin缓存*/
            continue;
        printf("呃...你想的是 %d?\n", guess);
    }
        
    printf("我就知道我能猜中!\n");
    
    return 0;
}

/*第6题*************************/
/* menuette.c -- menu techniques */
#include <stdio.h>
#include <ctype.h>
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
int main(void)
{
    int choice;
    void count(void);
    
    while ( (choice = get_choice()) != 'q')
    {
        switch (choice)
        {
            case 'a' :  printf("Buy low, sell high.\n");
                break;
            case 'b' :  putchar('\a');  /* ANSI */
                break;
            case 'c' :  count();
                break;
            default  :  printf("Program error!\n");
                break;
        }
    }
    printf("Bye.\n");
    
    return 0;
}

void count(void)
{
    int n,i;
    
    printf("Count how far? Enter an integer:\n");
    n = get_int();
    for (i = 1; i <= n; i++)
        printf("%d\n", i);
    while ( getchar() != '\n')
        continue;
}

char get_choice(void)
{
    int ch;
    
    printf("Enter the letter of your choice:\n");
    printf("a. advice           b. bell\n");
    printf("c. count            q. quit\n");
    ch = get_first();
    while (  (ch < 'a' || ch > 'c') && ch != 'q')
    {
        printf("Please respond with a, b, c, or q.\n");
        ch = get_first();
    }
    
    return ch;
}

char get_first(void)
{
    int ch;
    while (isspace(ch = getchar()))
        continue;
    return ch;
}

int get_int(void)
{
    int input;
    char ch;
    
    while (scanf("%d", &input) != 1)
    {
        while ((ch = getchar()) != '\n')
            putchar(ch);  // dispose of bad input
        printf(" is not an integer.\nPlease enter an ");
        printf("integer value, such as 25, -178, or 3: ");
    }
    
    return input;
}

/*第7题*************************/
#include<stdio.h>
#include<ctype.h>
//#define BASE_TIME 10
#define OVER_TIME (BASE_TIME * 1.5)
#define BASE_TAX 0.15
#define ADD_TAX  0.2
#define OTHER_TAX 0.25
int main()
{       //总工作时间,总工资,净工资,总税收
    float work_times,all_salary,clear_salary,all_tax,BASE_TIME;
    int case_num;


    while (1)
    {
        printf("*************************************\n\n");
        printf("请输入数子对应的工资等级,输入q退出程序:\n");
        printf("1) $8.75/hr         2) $9.33/hr\n");
        printf("3) $10.0/hr         4) $11.2/hr\n");
        printf("q) exit\n");
        printf("*************************************\n");
        if(!isalnum(case_num = getchar()))
        {           //排除非数字和不在范围内
            printf("输入错误,请重新输入!\n");
            while (getchar() != '\n')
                continue;//清理缓存
            continue;//跳过下面的内容,重新开始循环
        }
        while (getchar() != '\n')
            continue;//清理缓存
        switch (case_num)
        {
        case '1':
            BASE_TIME =8.75;
            break;
        case '2':
            BASE_TIME =9.33;
            break;
        case '3':
            BASE_TIME =10;
            break;
        case '4':
            BASE_TIME =11.2;
            break;
        case 'q':
             printf("程序退出!\n");
            goto end;//跳出两重循环直接到程序结尾
        default:
            printf("输入错误,请重新输入!\n");
            break;
        }
    }
    printf("您一周工作了多少小时?\n");
    scanf("%f",&work_times);
    if(work_times > 40)
    {
        all_salary = (work_times * BASE_TIME) + (work_times - 40) * OVER_TIME;
    }
    else
    {
        all_salary = work_times * BASE_TIME;
    }

    if(all_salary <= 300)
    {
        all_tax = all_salary * BASE_TAX;
    }
    else if(all_salary <= 450 && all_salary >300)
    {
        all_tax = 300 * BASE_TAX + (all_salary - 300) * ADD_TAX;
    }
    else
    {
        all_tax = 300 * BASE_TAX + 150 * ADD_TAX + (all_salary - 450) * OTHER_TAX;
    }
    clear_salary =all_salary - all_tax;
    printf("总工作时间:%g\n总工资:%g\n净工资:%g\n总税收:%g\n",work_times,all_salary,clear_salary,all_tax);
    end:return 0;
}
/*第8题*************************/
#include<stdio.h>
#include<ctype.h>
void calculator(int i);
int main()
{
    int case_num;
    while (1)
    {
        printf("*************************************\n\n");
        printf("请选择计算符号,输入5退出程序:\n");
        printf("1) +         2) -\n");
        printf("3) *         4) /\n");
        printf("5) exit\n");
        printf("*************************************\n");
        if(scanf("%d",&case_num) != 1 && case_num < 5 && case_num > 0)
        {           //排除非数字和不在范围内
            printf("输入错误,请重新输入!\n");
            while (getchar() != '\n')
                continue;//清理缓存
            continue;//跳过下面的内容,重新开始循环
        }
        switch (case_num)
        {
        case 1:
            calculator(case_num);
            break;
        case 2:
            calculator(case_num);
            break;
        case 3:
            calculator(case_num);
            break;
        case 4:
            calculator(case_num);
            break;
        case 5:

        default:
            printf("程序退出!\n");
            return 0;
            break;
        }
    }
}
void calculator(int i)
{
    float l,r;
    while (1)
    {
        printf("请输入两个数,例如2.1 3:\n");
        if(scanf("%f %f",&l,&r) != 2)
        {           //排除非数字和不在范围内
            printf("输入错误,请重新输入!\n");
            while (getchar() != '\n')
                continue;//清理缓存
            continue;//跳过下面的内容,重新开始循环
        }
        break;
    }
    switch (i)
    {
    case 1:
        printf("%g + %g = %g\n",l,r,l+r);
        break;
    case 2:
        printf("%g - %g = %g\n",l,r,l-r);
        break;
    case 3:
        printf("%g * %g = %g\n",l,r,l*r);
        break;
    case 4:
        printf("%g / %g = %g\n",l,r,l/r);
        break;
    default:
        break;
    }
}

发表评论

您的电子邮箱地址不会被公开。