C Primer Plus Sixth Edition (Chinese Edition) Chapter 8 (Perfect Revised Edition) Programming Practice Answers

//The code written by this blogger is only for readers' reference;

//If you have any shortcomings, please raise them, and the blogger will do their best to modify;

//Attach the programming practice questions after class;

//If it is useful to you, please like or share it with others;



//8.11 - 1.c

#include <stdio.h>

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

    printf("Please enter some characters:\n");
    while ((ch = getchar()) != EOF)
    {
    
    
        ct++;
    }
    printf("Characters: %d.\n", ct);

    return 0;
}

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

//8.11 - 2.c

#include <stdio.h>

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

    printf("Please enter some characters:\n");
    while ((ch = getchar()) != EOF)
    {
    
    
        if (i++ == 10)
        {
    
    
            putchar('\n');
            i = 1;
        }
        if (ch >= 32) //空格的十进制ASCII码;
        {
    
    
            printf(" \'%c\' - %3d ", ch, ch);
        }
        else if (ch == '\n')
        {
    
    
            printf(" \\n - \\n\n ");
            i = 0;
        }
        else if (ch == '\t')
        {
    
    
            printf(" \\t - \\t ");
        }
        else
        {
    
    
            printf(" \'%c\' - ^%c ", ch, ch + 64);
        }
    }
    puts("Done.");

    return 0;
}

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

//8.11 - 3.c

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

int main(void)
{
    
    
    int ch;
    unsigned int uct = 0;
    unsigned int lct = 0;
    unsigned int oct = 0;

    printf("Please enter some characters:\n");
    while ((ch = getchar()) != EOF)
    {
    
    
        if (isupper(ch))
        {
    
    
            uct++;
        }
        else if (islower(ch))
        {
    
    
            lct++;
        }
        else
        {
    
    
            oct++;
        }
    }
    printf("Upper: %u.\n", uct);
    printf("Lower: %u.\n", lct);
    printf("Others: %u.\n", oct);

    return 0;
}

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

//8.11 - 4.c

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

int main(void)
{
    
    
    float count;
    bool inword = false;
    int ch, words, letter;
    words = letter = 0;

    printf("Please enter some words (EOF to quit):\n");
    while ((ch = getchar()) != EOF)
    {
    
    
        if (ispunct(ch))
        {
    
    
            continue;
        }
        if (isalpha(ch))
        {
    
    
            letter++;
        }
        if (!isspace(ch) && !inword)
        {
    
    
            inword = true;
            words++;
        }
        if (isspace(ch) && inword)
        {
    
    
            inword = false;
        }
    }
    count = (float)letter / words;
    printf("Total words: %d.\n", words);
    printf("Total letters: %d.\n", letter);
    printf("Average letters of words: %g.\n", count);

    return 0;
}

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

//8.11 - 5.c

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

int main(void)
{
    
    
    int high = 100;
    int low = 1;
    int guess = (high + low) / 2;
    int response;

    printf("Pick an integer from 1 to 100. I will try to guess ");
    printf("it.\nRespond with a y if my guess is right, with");
    printf("\na h if it is high, and with an l if it is low.\n");
    printf("Uh...is your number %d?\n", guess);
    while ((response = getchar()) != 'y')
    {
    
    
        if (response == '\n')
        {
    
    
            continue;
        }
        if (tolower(response) != 'h' && tolower(response) != 'l')
        {
    
    
            printf("I don't understand that response. Please enter h for\n");
            printf("high, l for low, or y for correct.\n");
            continue;
        }
        if (tolower(response) == 'h')
        {
    
    
            high = guess - 1;
        }
        else if (tolower(response) == 'l')
        {
    
    
            low = guess + 1;
        }
        guess = (high + low) / 2;
        printf("Well, then, is it %d?\n", guess);
    }
    printf("I knew I could do it!\n");

    return 0;
}

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

//8.11 - 6.c

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

int get_first(void);

int main(void)
{
    
    
    int ch;

    printf("Please enter some characters ('#' to quit):\n");
    while ((ch = get_first()) != STOP)
    {
    
    
        printf("Result: %c\n", ch);
        printf("You can enter again ('#' to quit):\n");
    }
    printf("Done.\n");

    return 0;
}

int get_first(void)
{
    
    
    int ch;

    do
    {
    
    
        ch = getchar();
    } while (isspace(ch));
    while (getchar() != '\n')
        continue;

    return ch;
}

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

//8.11 - 7.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()) != 'q')
    {
    
    
        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("%c", &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("a) $8.75/hr                         b) $9.33/hr\n");
    printf("c) $10.00/hr                        d) $11.20/hr\n");
    printf("q) Quit\n");
    printf("**********************************************************************\n");
    printf("Please you choose: ");
    ch = get_choice();

    while (ch != 'a' && ch != 'b' && ch != 'c' && ch != 'd' && ch != 'q')
    {
    
    
        printf("Please enter a, b, c, d or q: ");
        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;
}

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

//8.11 - 8.c

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

int get_first(void);
int get_choice(void);
float get_float(void);

int main(void)
{
    
    
    int ct;
    float i, j;

    while ((ct = get_choice()) != 'q')
    {
    
    
        printf("Enter first number: ");
        i = get_float();
        printf("Enter second number: ");
        j = get_float();
        switch (ct)
        {
    
    
        case 'a':
        {
    
    
            printf("%g + %g = %g\n", i, j, i + j);
            break;
        }
        case 's':
        {
    
    
            printf("%g - %g = %g\n", i, j, i - j);
            break;
        }
        case 'm':
        {
    
    
            printf("%g * %g = %g\n", i, j, i * j);
            break;
        }
        case 'd':
        {
    
    
            while (fabs(j) <= 1e-6)
            {
    
    
                //↑判断float型浮点数是否为0;
                printf("Enter a number other than 0: ");
                j = get_float();
            }
            printf("%g / %g = %g\n", i, j, i / j);
            break;
        }
        }
    }
    printf("Bye.\n");

    return 0;
}

int get_first(void)
{
    
    
    int ch;

    do
    {
    
    
        ch = tolower(getchar());
    } while (isspace(ch));
    while (getchar() != '\n')
        continue;

    return ch;
}

int get_choice(void)
{
    
    
    int ch;

    printf("Enter the operation of your choice:\n");
    printf("a. add           s. subtract\n");
    printf("m. multiply      d. divide\n");
    printf("q. quit\n");
    ch = get_first();

    while (ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q')
    {
    
    
        printf("Please enter with a,s,m,d or q :");
        ch = get_first();
    }
    return ch;
}

float get_float(void)
{
    
    
    int ch;
    float input;

    while (scanf("%f", &input) != 1)
    {
    
    
        while ((ch = getchar()) != '\n')
        {
    
    
            putchar(ch);
        }
        printf(" is not an number.\n");
        printf("Please enter a number such as 2.5, -1.78E8 or 3: ");
    }
    return input;
}

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

//----------------------------April 3, 2020-------------- -----------------

Guess you like

Origin blog.csdn.net/m0_46181359/article/details/105303220