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

编译环境VS Code+WSL GCC 源码请到文末下载 。 注意:本章部分题目有些超纲(回用到函数调用),不理解的同学可以先去看一下后面的知识点和注释。

/*第1题*************************/
#include<stdio.h>
#include<ctype.h>
int main()
{
    char ch;
    int space,new,others;
    space = new = others = 0;
    while ((ch = getchar()) != '#')
    {
        if(ch == ' ')
            space++;
        else if(ch == '\n')
            new++;
        else
            others++;
    }
    printf("空格有:%d个\n换行有:%d个\n其他有:%d个\n",space,new,others);
    return 0;
}
/*第2题*************************/
#include<stdio.h>
#include<ctype.h>
int main()
{
    char arry[999]={0};
    char ch;
    int cnt = 0,i = 0;
    while ((ch = getchar()) != '#'){
        arry[cnt++] = ch;
    }
    for(i=1;i<cnt+1;i++){
        if(isalnum(arry[i-1])||ispunct(arry[i-1]))
            printf("\'%c\'-%-3d ",arry[i-1],arry[i-1]);
        else
            printf("\'?\'-%-3d ",arry[i-1]);
        if(i != 0 && i%8 == 0)
        putchar('\n');
    }
    putchar('\n');
    return 0;
}
/*第3题*************************/
#include<stdio.h>
int main()
{
    int s_num_sum,d_num_sum,s_num_cnt,d_num_cnt,input_num;
    double s_num_avg,d_num_avg;
    s_num_sum = d_num_sum = s_num_cnt = d_num_cnt = input_num = 0;
    printf("请输入整数,输入0退出:");
    do{
        if(scanf("%d",&input_num) == 1 && input_num != 0){
            //如果输入的是一个不为0的整数,将继续下面的操作(排除其他情况)
            if(input_num%2 == 0){       //如果输入是双数
                d_num_sum += input_num; //双数总和累加
                d_num_cnt++;            //双数个数++
            }
            else{
                s_num_sum += input_num;
                s_num_cnt++;
            }
        }
    } while (input_num != 0);
    s_num_avg = 1.0 * s_num_sum / s_num_cnt;
    d_num_avg = 1.0 * d_num_sum / d_num_cnt;
    printf("输入的偶数个数:%d\n输入的偶数平均值:%g\n输入的奇数个数:%d\n输入的奇数平均值:%g\n",
    d_num_cnt,d_num_avg,s_num_cnt,s_num_avg);
    return 0;
}
/*第4题*************************/
#include<stdio.h>
int main()
{
    char arry[999]={0};
    char ch;
    int i=0,cnt=0;
    printf("请输入字符,直到#停止:");
    while ((ch = getchar()) != '#')
    {
        if(ch == '.'){
            arry[i++]='!';
            cnt++;
        }
        else if(ch == '!'){
            arry[i++]='!';
            arry[i++]='!';
            cnt += 2;
        }
        else{
            arry[i++]=ch;
        }
    }
    arry[i]='\0';
    printf("替换后为:\n%s\n",arry);
    printf("进行了%d次替换\n",cnt);
    return 0;
}
/*第5题*************************/
#include<stdio.h>
int main()
{
    char arry[999]={0};
    char ch;
    int i=0,cnt=0;
    printf("请输入字符,直到#停止:");
    while ((ch = getchar()) != '#')
    {
        switch (ch)
        {
        case '.':
            arry[i++]='!';
            cnt++;
            break;
            
        case '!':
            arry[i++]='!';
            arry[i++]='!';
            cnt += 2;
            break;
        
        default:
            arry[i++]=ch;
            break;
        }
    }
    arry[i]='\0';
    printf("替换后为:\n%s\n",arry);
    printf("进行了%d次替换\n",cnt);
    return 0;
}
/*第6题*************************/
#include<stdio.h>
int main()
{
    char arry[999]={0};
    char ch;
    int i=0,cnt=0;
    printf("请输入字符,直到#停止:");
    while ((ch = getchar()) != '#')
    {
        if(ch == '.'){
            arry[i++]='!';
            cnt++;
        }
        else if(ch == '!'){
            arry[i++]='!';
            arry[i++]='!';
            cnt += 2;
        }
        else{
            arry[i++]=ch;
        }
    }
    arry[i]='\0';
    printf("替换后为:\n%s\n",arry);
    printf("进行了%d次替换\n",cnt);
    return 0;
}
/*第7题*************************/
#include<stdio.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;
    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);
    return 0;
}
/*第8题*************************/
#include<stdio.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("请输入数子对应的工资等级,输入5退出程序:\n");
        printf("1) $8.75/hr         2) $9.33/hr\n");
        printf("3) $10.0/hr         4) $11.2/hr\n");
        printf("5) exit\n");
        printf("*************************************\n");

        if((scanf("%d",&case_num) != 1) || !(case_num <= 5 && case_num >= 0))
        //排除非数字和不在范围内
        {
            printf("输入错误,请重新输入!\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 5:
        default:
            printf("程序退出!\n");
            goto end;//跳出两重循环直接到程序结尾
            break;
        }
        break;//结束while循环
    }
    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;
}
/*第9题*************************/
#include<stdio.h>
#include<math.h>
int main()
{
    int ipt,i,j;
    printf("请输入一个大于0的数:");
    while ((scanf("%d",&ipt)!=1) && (ipt <= 0))
    {
         printf("输入错误!\n");
         printf("请输入一个大于0的数:");
    }
    if(ipt<4)
    {
        for(i=1;i<=ipt;i++)
            printf("%-4d",i);
    }
    else
    {
        for(i=1;i<4;i++)
            printf("%-4d",i);

        for(i=4;i<ipt+1;i++){
            for(j=2;j<i;j++){
                if(i%j == 0){
                    break;
                }
            }
            if(j == i){
                printf("%-4d",i);
            }
        }
    }
    putchar(10);
    return 0;
}
/*第10题*************************/
#include<stdio.h>
//#define class 10
#define CLASS_1 17850
#define CLASS_2 23900
#define CLASS_3 29750
#define CLASS_4 14875
#define BASE_TAX 0.15
#define OVER_TAX  0.28
int main()
{          //类别,总工资,总税收
    float base,all_salary,all_tax;
    int case_num;


    while (1)
    {
        printf("------------------------------------------------\n");
        printf("请输入数子对应的类别,输入5退出程序:\n");
        printf("------------------------------------------------\n");
        printf("1) 单身         17850$按15%%计,超出部分按28%%计算\n");
        printf("2) 户主         23900$按15%%计,超出部分按28%%计算\n");
        printf("3) 已婚,共有    29750$按15%%计,超出部分按28%%计算\n");
        printf("4) 单身         14875$按15%%计,超出部分按28%%计算\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;//跳过下面的内容,重新开始循环
        }
        switch (case_num)
        {
        case 1:
            base = CLASS_1;
            break;
        case 2:
            base = CLASS_2;
            break;
        case 3:
            base = CLASS_3;
            break;
        case 4:
            base = CLASS_4;
            break;
        case 5:
        default:
            printf("程序退出!\n");
            goto end;//跳出两重循环直接到程序结尾
            break;
        }
        break;//结束while循环
    }
    while (1)
    {
        printf("您的收入是多少?输入Q退出:\n");
        while(scanf("%f",&all_salary) != 1){
            printf("程序退出!\n");
            goto end;//跳出两重循环直接到程序结尾
        }
        if(all_salary <= base)
        {
            all_tax = all_salary * BASE_TAX;
        }
        else
        {
            all_tax = base * BASE_TAX +(all_salary - base) * OVER_TAX;
        }
        printf("您的总税收为:%g$\n",all_tax);
    }
    end:return 0;
}
/*第11题*************************/
#include<stdio.h>
//#define class 10
#define YANG_LI 2.05
#define TIAN_CAI 1.05
#define HUOLUO_BO 1.19

double yl_weight = 0,tc_weight = 0,hlb_weight = 0;
double yl_prices = 0,tc_prices = 0,hlb_prices = 0,all_prices = 0;

double get_weight(int case_class);
void show_info();
int user_ui();

int main()
{
    user_ui();
    
    return 0;
}

double get_weight(int case_class)//传入需要购买的品种
{
    double weight=0;
    printf("您要购买多少");
    switch (case_class)
    {
    case 1:
       printf("洋蓟");
        break;
    case 2:
        printf("甜菜");
        break;
    case 3:
        printf("胡萝卜");
        break;
    default:
        printf("???");
        break;
    }
    printf("(单位/磅)?输入0退回:\n");

    while(scanf("%lf",&weight) != 1||weight < 0){//排除非数字和不在范围内
        while (getchar() != '\n');//清空输入缓存
        printf("输入错误,请重新输入!\n");
    }
    return weight;
}

void show_info()
{
    double all_weight,weight_prices,free_prices = 0;
    yl_prices = yl_weight * YANG_LI;
    tc_prices = tc_weight * TIAN_CAI;
    hlb_prices = hlb_weight * HUOLUO_BO;
    all_prices = yl_prices + tc_prices + hlb_prices;
    all_weight = yl_weight + tc_weight + hlb_weight;
    if(yl_weight > 0){printf("您已购买洋蓟:%g 磅,%g 美元\n",yl_weight,yl_prices);}
    if(tc_prices > 0){printf("您已购买甜菜:%g 磅,%g 美元\n",tc_weight,tc_prices);}
    if(hlb_prices > 0){printf("您已购买胡萝卜:%g 磅,%g 美元\n",hlb_weight,hlb_prices);}
    if(all_prices > 0){printf("您已购买%g 美元\n",all_prices);}

    if(all_weight <= 5){
        weight_prices = 6.5;
    }
    else if(all_weight > 5 && all_weight <= 20){
        weight_prices = 14;
    }
    else
    {
        weight_prices = 14 + (all_weight-20)*0.5;
    }

    if(all_prices >= 100){
        free_prices = all_prices * 0.05;
        printf("已为您优惠%g 美元\n",free_prices);
    }
    printf("合计重量:%g 磅,共需运费:%g 美元,合计支付价格:%g 美元\n",
    all_weight,weight_prices,all_prices - free_prices + weight_prices);
}

int user_ui()
{
    int case_num;
    while (1)
    {
        printf("------------------------------------------------\n");
        printf("欢迎光临ABC邮购杂货店!\n");
        printf("------------------------------------------------\n");
        printf("1) 洋蓟         2.05美元/磅\n");
        printf("2) 甜菜         1.15美元/磅\n");
        printf("3) 胡萝卜       1.09美元/磅\n");
        printf("4) exit\n");
        printf("------------------------------------------------\n");
        printf("您要购买什么?输入4退出程序:\n");        
        printf("------------------------------------------------\n");
        if((scanf("%d",&case_num) != 1) || !(case_num <= 4 && case_num >= 0))
        //排除非数字和不在范围内
        {
            printf("输入错误,请重新输入!\n");
            while (getchar() != '\n');//清空输入缓存
            continue;//跳过下面的内容,重新开始循环
        }
        switch (case_num)
        {
        case 1:
            yl_weight += get_weight(case_num);
            //debug printf("TEST:%g\n",yl_weight);
            show_info();
            break;
        case 2:
            tc_weight += get_weight(case_num);
            show_info();
            break;
        case 3:
            hlb_weight += get_weight(case_num);
            show_info();
            break;
        case 4:
        default:
            printf("程序退出!\n");
            goto end;//跳出两重循环直接到程序结尾
            break;
        }
    }
    end:return 0;
}

还有什么问题的可以点击标题进入文章给我留言!
我的博客太冷清了QAQ