C Primer Plus第六版第七章编程题目与参考答案⭐

1.编写一个程序读取输入,读到#字符停止,然后报告读取的空格数、换行符数和所有其他字符的数量。

#include <stdio.h>
#define STOP '#'
#define SPACE ' ' 

int main(void)
{
    int ch, space, enter, others;
    space = enter = others = 0;

    printf("请输入信息(输入#结束):\n");
    while ((ch = getchar()) != STOP)    //字符输入的方式,读到'#'停止
    {
        if (ch == SPACE)
        {
            space++;
        }
        else if (ch == '\n')
        {
            enter++;
        }
        else
        {
            others++;
        }
    }
    printf("打印结果是:\n");
    printf("空格:%10d个\n", space);
    printf("换行符:%8d个\n", enter);
    printf("其它字符:%6d个\n", others);
    return 0;
}

2.编写一个程序读取输入,读到#字符停止。程序要打印每个输入的字符以及对应的ASCII码(十进制)。一行打印8个字符。建议:使用字符计数和求模运算符(%)在每8个循环周期时打印一个换行符。3.编写一个程序,读取整数直到用户输入0。输入结束后,程序应报告用户输入的偶数(不包括0)个数、这些偶数的平均值、输入的奇数个数及其奇数的平均值。

#include <stdio.h>
#define STOP '#'

int main(void)
{
    int ch;
    int i = 0;
    printf("请输入信息(输入#结束):\n");
    while ((ch = getchar()) != STOP)
    {
        if (i++ % 8 == 0)
        {
            printf("\n");
        }
        if (ch == '\n')
        {
            printf("'\\n'-%3d ", ch);
        }
        else if (ch == '\t')
        {
            printf("'\\t'-%3d ", ch);
        }
        else
        {
            printf("'%c'-%3d ", ch, ch);
        }
    }
    printf("\nDone!\n");
    return 0;
}

3.编写一个程序,读取整数直到用户输入0。输入结束后,程序应报告用户输入的偶数(不包括0)个数、这些偶数的平均值、输入的奇数个数及其奇数的平均值。

#include <stdio.h>

int main(void)
{
    int n, odd, even;
    int e_sum, o_sum;
    odd = even = 0;
    e_sum = o_sum = 0;
    printf("请输入一个整数(0退出程序):");
    while ((scanf("%d", &n) == 1) && (n != 0))
    {
        if (n % 2 == 0)
        {
            even++;
            e_sum += n;
        }
        else
        {
            odd++;
            o_sum += n;
        }
        printf("您可以再次输入(0则退出):");
    }
    printf("偶数:%d个\n", even);
    if (even > 0)
    {
        printf("偶数的平均值是%g\n", (float)e_sum / even);
    }
    printf("奇数:%d个\n", odd);
    if (odd > 0)
    {
        printf("奇数的平均值是%g\n", (float)o_sum / odd);
    }
    printf("本程序完成!\n");
    return 0;
}

4.使用if else 语句编写一个程序读取输入,读到#停止。用感叹号替换句号,用两个感叹号替换原来的感叹号,最后报告进行了多少次替换。

#include <stdio.h>
#define STOP '#'

int main(void)
{
    int ch;
    int n = 0;

    printf("请输入信息(输入#结束):\n");
    while ((ch = getchar()) != STOP)
    {
        if (ch == '.')
        {
            putchar('!');
            n++;
        }
        else if (ch == '!')
        {
            printf("!!");
            n++;
        }
        else
        {
            putchar(ch);
        }
    }
    printf("\n共替代了%d次.\n", n);
    printf("('.'->'!') or ('!'->'!!').\n");

    return 0;
}

5.使用switch重写练习4。

#include <stdio.h>
#define STOP '#'

int main(void)
{
    int ch;
    int n = 0;

    printf("请输入信息(输入#结束):\n");
    while ((ch = getchar()) != STOP)
    {
        switch (ch)
        {
        case '.':
        {
            putchar('!');
            n++;
            break;
        }
        case '!':
        {
            printf("!!");
            n++;
            break;
        }
        default:
        {
            putchar(ch);
        }
        }
    }
    printf("\n共出现了%d次.\n", n);
    printf("('.'->'!') or ('!'->'!!').\n");
    return 0;
}

6.编写程序读取输入,读到#停止,报告ei出现的次数。
(注意:该程序要记录前一个字符和当前字符。用“Receive your eieio award”这样的输入来测试.)

#include <stdio.h>
#define STOP '#'

int main(void)
{
    int ch;
    int n = 0;
    int flag = 0;

    printf("请输入一个整数(0退出程序):\n");
    while ((ch = getchar()) != STOP)
    {
        switch (ch)
        {
        case 'e':
        {
            flag = 1; 
            break;
        }
        case 'i':
        {
            if (1 == flag)
            {
                n++;
            }
            flag = 0;
            break;
        }
        default:
        {
            flag = 0;
        }
        }
    }
    printf("\n共出现了%d次 \'ei\'.\n", n);
    return 0;
}

7.编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额、税金和净收入。做如下假设:
a.基本工资= 1000美元/小时
b. 加班(超过40小时)= 1.5倍的时间
c. 税率:前300美元为15%
 续150美元为20%
 余下的为25%
用#define定义符号常量。不用在意是否符合当前的税法。

#include <stdio.h>
#define BASE_SALARY 10.0f
#define EXTRA_HOUR 1.5f
#define BASE_TAX 0.15f
#define EXTRA_TAX 0.2f
#define EXCEED_TAX 0.25f

int main(void)
{
    float hours = 0.0f;
    float salary, tax, taxed_salary;

    printf("Enter the working hours a week:");
    while ((!scanf("%f", &hours)) || (hours < 0))
    {
        while (getchar() != '\n')
            continue;
        printf("Please enter a positive number:");
    }
    if (hours <= 30)
    {
        salary = hours * BASE_SALARY;
        tax = salary * BASE_TAX;
        taxed_salary = salary - tax;
    }
    else if (hours <= 40)
    {
        salary = hours * BASE_SALARY;
        tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX;
        taxed_salary = salary - tax;
    }
    else
    {
        salary = (40 + (hours - 40) * EXTRA_HOUR) * BASE_SALARY;
        if (salary <= 450)
        {
            tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX;
        }
        else
        {
            tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX + (salary - 450) * EXCEED_TAX;
        }
        taxed_salary = salary - tax;
    }
    printf("salary(before tax):$%g\n", salary);
    printf("tax:$%g\n", tax);
    printf("salary(after tax):$%g\n", taxed_salary);

    return 0;
}

8.修改练习7的假设a,让程序可以给出一个供选择的工资等级菜单。使用switch完成工资等级选择。
运行程序后,显示的菜单应该类似这样:

Enter the number corresponding to the desired pay rate or action:1) $8.75/hr
2)$9.33/hr
3)$10.00 /hr
4)$11.20 /hr
5) quit
如果选择1-4其中的一个数字,程序应该询问用户工作的小时数。程序要通过循环运行,除非用户输入5。如果输入1~5以外的数字,程序应提醒用户输入正确的选项,然后再重复显示菜单提示用户输入。使用#define创建符号常量表示各工资等级和税率。

#include <stdio.h>
#include <ctype.h>
#define EXTRA_HOUR 1.5f
#define BASE_TAX 0.15f
#define EXTRA_TAX 0.2f
#define EXCEED_TAX 0.25f

int show_menu(void);
void show_salary(float base_salary, float hours);
int get_choice(void);
void eatline(void);

int main(void)
{
    int ch;
    float n;

    while ((ch = show_menu()) != 5)
    {
        printf("Enter the working hours a week:");
        while ((!scanf("%f", &n)) || (n < 0))
        {
            eatline();
            printf("Enter a positive number:");
        }
        eatline();
        switch (ch)
        {
        case 1:
        {
            show_salary(8.75f, n);
            break;
        }
        case 2:
        {
            show_salary(9.33f, n);
            break;
        }
        case 3:
        {
            show_salary(10.00f, n);
            break;
        }
        case 4:
        {
            show_salary(11.20f, n);
            break;
        }
        }
        putchar('\n');
    }
    printf("Done!\n");

    return 0;
}

int get_choice(void)
{
    int ch = 0;

    scanf("%d", &ch);
    eatline();

    return ch;
}

void eatline(void)
{
    while (getchar() != '\n')
        continue;
    return;
}

int show_menu(void)
{
    int ch;

    printf("**********************************************************************\n");
    printf("Enter the number corresponding to the desired pay rate or action:\n");
    printf("1) $8.75/hr                  2) $9.33/hr\n");
    printf("3) $10.00/hr                 4) $11.20/hr\n");
    printf("5) Quit\n");
    printf("**********************************************************************\n");
    printf("Please you choose:");
    ch = get_choice();

    while (ch != 1 && ch != 2 && ch != 3 && ch != 4 && ch != 5)
    {
        printf("Please enter 1,2,3,4 or 5:");
        ch = get_choice();
    }
    return ch;
}

void show_salary(float base_salary, float hours)
{
    float salary, tax, taxed_salary;

    if (hours <= 30)
    {
        salary = hours * base_salary;
        tax = salary * BASE_TAX;
        taxed_salary = salary - tax;
    }
    else if (hours <= 40)
    {
        salary = hours * base_salary;
        tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX;
        taxed_salary = salary - tax;
    }
    else
    {
        salary = (40 + (hours - 40) * EXTRA_HOUR) * base_salary;
        if (salary <= 450)
        {
            tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX;
        }
        else
        {
            tax = 300 * BASE_TAX + (salary - 300) * EXTRA_TAX + (salary - 450) * EXCEED_TAX;
        }
        taxed_salary = salary - tax;
    }
    printf("salary(before tax):$%g\n", salary);
    printf("tax:$%g\n", tax);
    printf("salary(after tax):$%g\n", taxed_salary);
    return;
}

9.编写一个程序,只接受正整数输入,然后显示所有小于或等于该数的素数。

#include <stdio.h>
#include <math.h>

int main(void)
{
    int i, n, number, prime;

    printf("请您输入一个正整数(<=0退出):");
    while ((scanf("%d", &number) == 1) && (number > 0))
    {
        if (number == 1)
        {
            printf("1不是素数!\n");
            printf("您可以再次输入一个正整数(<=0退出):");
            continue;
        }
        printf("小于或等于%d的素数有:\n", number);
        for (i = 2; i <= number; i++)
        {
            prime = 1;
            for (n = 2; n <= sqrt(i); n++)
            {
                if (i % n == 0)
                {
                    prime = 0;
                    break;
                }
            }
            if (prime)
            {
                printf("%-3d", i);
            }
        }
        printf("\n您可以再次输入一个正整数(<=0退出):");
    }
    printf("本程序完成!\n");

    return 0;
}

10.1988年的美国联邦税收计划是近代最简单的税收方案。它分为4个类别,每个类别有两个等级。
下面是该税收计划的摘要(美元数为应征税的收入):

类别            税金
单身      17850美元按15%计,超出部分按28%计
户主      23900美元按15%计,超出部分按28%计
已婚,共有   29750美元按15%计,超出部分按28%计
已婚,离异   14875美元按15%计,超出部分按28%计

例如,一位工资为20000美元的单身纳税人,应缴纳税费0.15×17850+0.28×(20000-17850)美元。编写一个程序,让用户指定缴纳税金的种类和应纳税收入,然后计算税金。程序应通过循环让用户可以多次输入。

#include <stdio.h>
#define PLAN1 17850
#define PLAN2 23900
#define PLAN3 29750
#define PLAN4 14875
#define RATE1 0.15
#define RATE2 0.28

int main(void)
{
    int n;
    double wage, tax;

    while (1)
    {
        printf("********************************\n");
        printf("1)单身              2)户主\n");
        printf("3)已婚,共有        4)已婚,离异\n");
        printf("5)退出本程序\n");
        printf("********************************\n");
        printf("请您选择您的身份(输入5退出本程序):");
        while ((!scanf("%d", &n)) || (n > 5 || n < 1))
        {
            while (getchar() != '\n')
                continue;
            printf("请重新输入1,2,3,4或5:");
        }
        if (1 == n)
        {
            printf("请输入您的工资:");
            scanf("%lf", &wage);
            if (wage <= PLAN1)
            {
                tax = wage * RATE1;
                printf("您需要交$%g税金\n\n", tax);
            }
            else
            {
                tax = PLAN1 * RATE1 + (wage - PLAN1) * RATE2;
                printf("您需要交$%g税金\n\n", tax);
            }
        }
        else if (2 == n)
        {
            printf("请输入您的工资:");
            scanf("%lf", &wage);
            if (wage <= PLAN2)
            {
                tax = wage * RATE1;
                printf("您需要交$%g税金\n\n", tax);
            }
            else
            {
                tax = PLAN2 * RATE1 + (wage - PLAN2) * RATE2;
                printf("您需要交$%g税金\n\n", tax);
            }
        }
        else if (3 == n)
        {
            printf("请输入您的工资:");
            scanf("%lf", &wage);
            if (wage <= PLAN3)
            {
                tax = wage * RATE1;
                printf("您需要交$%g税金\n\n", tax);
            }
            else
            {
                tax = PLAN3 * RATE1 + (wage - PLAN3) * RATE2;
                printf("您需要交$%g税金\n\n", tax);
            }
        }
        else if (4 == n)
        {
            printf("请输入您的工资:");
            scanf("%lf", &wage);
            if (wage <= PLAN4)
            {
                tax = wage * RATE1;
                printf("您需要交$%g税金\n\n", tax);
            }
            else
            {
                tax = PLAN4 * RATE1 + (wage - PLAN4) * RATE2;
                printf("您需要交$%g税金\n\n", tax);
            }
        }
        else if (5 == n)
        {
            break;
        }
    }
    printf("本程序完成!\n");

    return 0;
}

11.ABC邮购杂货店出售的洋蓟售价为2.05美元/磅,甜菜售价为1.15美元/磅,胡萝卜售价为1.09美元/磅。在添加运费之前,100美元的订单有5%的打折优惠。少于或等于5磅的订单收取6.5美元的运费和包装费,5磅~20磅的订单收取14美元的运费和包装费,超过20磅的订单在14美元的基础上每续重Ⅰ磅增加0.5美元。编写一个程序,在循环中用switch语句实现用户输入不同的字母时有不同的响应,即输入a的响应是让用户输入洋蓟的磅数,b是甜菜的磅数,c是胡萝卜的磅数,q是退出订购。程序要记录累计的重量。即,如果用户输入4磅的甜菜,然后输入5磅的甜菜,程序应报告9磅的甜菜。然后,该程序要计算货物总价、折扣(如果有的话)、运费和包装费。随后,程序应显示所有的购买信息:物品售价、订购的重量(单位:磅)、订购的蔬菜费用、订单的总费用、折扣(如果有的话)、运费和包装费,以及所有的费用总额。

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

int main(void)
{
    const double price_artichokes = 2.05;
    const double price_beets = 1.15;
    const double price_carrots = 1.09;
    const double DISCOUNT_RATE = 0.05;
    const double under5 = 6.50;
    const double under20 = 14.00;
    const double base20 = 14.00;
    const double extralb = 0.50;

    int ch;
    double lb_artichokes = 0;
    double lb_beets = 0;
    double lb_carrots = 0;
    double lb_temp;
    double lb_total;

    double cost_artichokes;
    double cost_beets;
    double cost_carrots;
    double cost_total;
    double final_total;
    double discount;
    double shipping;

    printf("Enter a to buy artichokes, b for beets, ");
    printf("c for carrots, q to quit: ");
    while ((ch = tolower(getchar())) != 'q')
    {
        if (isspace(ch))
        {
            continue;
        }
        while (getchar() != '\n')
            continue;
        switch (ch)
        {
        case 'a':
        {
            printf("Enter pounds of artichokes: ");
            scanf("%lf", &lb_temp);
            lb_artichokes += lb_temp;
            break;
        }
        case 'b':
        {
            printf("Enter pounds of beets: ");
            scanf("%lf", &lb_temp);
            lb_beets += lb_temp;
            break;
        }
        case 'c':
        {
            printf("Enter pounds of carrots: ");
            scanf("%lf", &lb_temp);
            lb_carrots += lb_temp;
            break;
        }
        default:
        {
            printf("%c is not a valid choice.\n", ch);
        }
        }
        printf("Enter a to buy artichokes, b for beets, ");
        printf("c for carrots, q to quit: ");
    }

    cost_artichokes = price_artichokes * lb_artichokes;
    cost_beets = price_beets * lb_beets;
    cost_carrots = price_carrots * lb_carrots;
    cost_total = cost_artichokes + cost_beets + cost_carrots;
    lb_total = lb_artichokes + lb_beets + lb_carrots;

    if (lb_total <= 0)
    {
        shipping = 0.0;
    }
    else if (lb_total < 5.0)
    {
        shipping = under5;
    }
    else if (lb_total < 20.0)
    {
        shipping = under20;
    }
    else
    {
        shipping = base20 + extralb * lb_total;
    }
    if (cost_total > 100.0)
    {
        discount = DISCOUNT_RATE * cost_total;
    }
    else
    {
        discount = 0.0;
    }
    final_total = cost_total + shipping - discount;

    printf("Your order:\n");
    printf("%.2f lbs of artichokes at $%.2f per pound:$ %.2f\n",
           lb_artichokes, price_artichokes, cost_artichokes);
    printf("%.2f lbs of beets at $%.2f per pound: $%.2f\n",
           lb_beets, price_beets, cost_beets);
    printf("%.2f lbs of carrots at $%.2f per pound: $%.2f\n",
           lb_carrots, price_carrots, cost_carrots);
    printf("Total cost of vegetables: $%.2f\n", cost_total);
    if (cost_total > 100)
    {
        printf("Volume discount: $%.2f\n", discount);
    }
    printf("Shipping: $%.2f\n", shipping);
    printf("Total charges: $%.2f\n", final_total);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值