C Primer Plus 第六版(中文版)第七章(完美修订版)编程练习答案

//本博主所写的代码仅为阅读者提供参考;

//若有不足之处请提出,博主会尽其所能修改;

//附上课后编程练习题目;

//若是对您有用的话请点赞或分享提供给它人;




//7.12 - 1.c

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

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

    printf("Please enter some characters ('#' to quit):\n");
    while ((ch = getchar()) != STOP)
    {
    
    
        if (ch == ' ')
        {
    
    
            space++;
        }
        else if (ch == '\n')
        {
    
    
            enter++;
        }
        else
        {
    
    
            others++;
        }
    }
    printf("Here are the messages\n");
    printf("Space: %d\n", space);
    printf("Linefeed: %d\n", enter);
    printf("Others: %d\n", others);

    return 0;
}

//-------------

//7.12 - 2.c

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

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

    printf("Enter some characters('#' to quit):\n");
    while ((ch = getchar()) != STOP)
    {
    
    
        if (i++ % 8 == 0)
        {
    
    
            putchar('\n');
        }
        if (ch == '\n')
        {
    
    
            printf("'\\n'-%3d ", ch);
        }
        else if (ch == '\t')
        {
    
    
            printf("'\\t'-%3d ", ch);
        }
        else
        {
    
    
            printf("'%c'-%3d ", ch, ch);
        }
    }
    printf("Done.\n");

    return 0;
}

//-------------

//7.12 - 3.c

#include <stdio.h>

int main(void)
{
    
    
    int n, odd, even;
    int e_sum, o_sum;
    odd = even = 0;
    e_sum = o_sum = 0;

    printf("Please enter a integer (0 to quit): ");
    while (scanf("%d", &n) == 1 && (n != 0))
    {
    
    
        if (n % 2 == 0)
        {
    
    
            even++;
            e_sum += n;
        }
        else
        {
    
    
            odd++;
            o_sum += n;
        }
        printf("You can enter again (0 to quit): ");
    }
    printf("Even number: %d\n", even);
    if (even > 0)
    {
    
    
        printf("The average of even: %g\n", (float)e_sum / even);
    }
    printf("Odd number: %d\n", odd);
    if (odd > 0)
    {
    
    
        printf("The average of odd: %g\n", (float)o_sum / odd);
    }
    printf("Done.\n");

    return 0;
}

//-------------

//7.12 - 4.c

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

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

    printf("Enter some characters('#' to quit):\n");
    while ((ch = getchar()) != STOP)
    {
    
    
        if (ch == '.')
        {
    
    
            putchar('!');
            n++;
        }
        else if (ch == '!')
        {
    
    
            printf("!!");
            n++;
        }
        else
        {
    
    
            putchar(ch);
        }
    }
    printf("\nTotal replace %d times.\n", n);
    printf("('.'->'!') or ('!'->'!!').\n");

    return 0;
}

//-------------

//7.12 - 5.c

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

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

    printf("Enter some characters('#' to quit):\n");
    while ((ch = getchar()) != STOP)
    {
    
    
        switch (ch)
        {
    
    
        case '.':
        {
    
    
            putchar('!');
            n++;
            break;
        }
        case '!':
        {
    
    
            printf("!!");
            n++;
            break;
        }
        default:
        {
    
    
            putchar(ch);
        }
        }
    }
    printf("\nTotal replace %d times.\n", n);
    printf("('.'->'!') or ('!'->'!!').\n");

    return 0;
}

//-------------

//7.12 - 6.c

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

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

    printf("Enter some characters('#' to quit):\n");
    while ((ch = getchar()) != STOP)
    {
    
    
        switch (ch)
        {
    
    
        case 'e':
        {
    
    
            flag = 1;
            //↑保存一个出现e的标记;
            break;
        }
        case 'i':
        {
    
    
            if (1 == flag)
            {
    
    
                n++;
            }
            //↑统计ei出现的次数;
            flag = 0;
            break;
        }
        default:
        {
    
    
            flag = 0;
            //↑如果e的下一个字符不是i则清除标记;
        }
        }
    }
    printf("\nTotally exist %d times \'ei\'.\n", n);

    return 0;
}

//-------------

//7.12 - 7.c

#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) != 1 || (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;
}

//-------------

//7.12 - 8.c

#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) != 1 || (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;

    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;
}

//-------------

//7.12 - 9.c

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

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

    printf("Please enter a number (<= 0 to quit): ");
    while (scanf("%d", &number) == 1 && (number > 0))
    {
    
    
        if (number == 1)
        {
    
    
            printf("1 isn't a prime!\n");
            printf("You can enter again (<= 0 to quit): ");
            continue;
        }
        printf("Here are the prime (<= %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("\nYou can enter again (<= 0 to quit): ");
    }
    printf("Done.\n");

    return 0;
}

//-------------

//7.12 - 10.c

#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) single\n");
        printf("2) householder\n");
        printf("3) married\n");
        printf("4) married but divorced\n");
        printf("5) quit\n");
        printf("********************************\n");
        printf("Please you choose: ");
        while (scanf("%d", &n) != 1 || (n > 5 || n < 1))
        {
    
    
            while (getchar() != '\n')
                continue;
            printf("Please enter 1, 2, 3, 4 or 5: ");
        }
        if (n != 5)
        {
    
    
            printf("Please enter your wage: ");
            scanf("%lf", &wage);
        }
        if (1 == n)
        {
    
    
            if (wage <= PLAN1)
            {
    
    
                tax = wage * RATE1;
            }
            else
            {
    
    
                tax = PLAN1 * RATE1 + (wage - PLAN1) * RATE2;
            }
            printf("Your tax: %g\n\n", tax);
        }
        else if (2 == n)
        {
    
    
            if (wage <= PLAN2)
            {
    
    
                tax = wage * RATE1;
            }
            else
            {
    
    
                tax = PLAN2 * RATE1 + (wage - PLAN2) * RATE2;
            }
            printf("Your tax: %g\n\n", tax);
        }
        else if (3 == n)
        {
    
    
            if (wage <= PLAN3)
            {
    
    
                tax = wage * RATE1;
            }
            else
            {
    
    
                tax = PLAN3 * RATE1 + (wage - PLAN3) * RATE2;
            }
            printf("Your tax: %g\n\n", tax);
        }
        else if (4 == n)
        {
    
    
            if (wage <= PLAN4)
            {
    
    
                tax = wage * RATE1;
            }
            else
            {
    
    
                tax = PLAN4 * RATE1 + (wage - PLAN4) * RATE2;
            }
            printf("Your tax: %g\n\n", tax);
        }
        else if (5 == n)
        {
    
    
            break;
        }
    }
    printf("Done.\n");

    return 0;
}

//-------------

//7.12 - 11.c

#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;
}

//-------------

//-------------------------------------------2020年4月3日 ------------------------------------------

猜你喜欢

转载自blog.csdn.net/m0_46181359/article/details/105302817
今日推荐