C Primer Plus(第6版)第八章答案

1.
#include <stdio.h>

int main (void)
{
    int ch;//字符
    int i = 0;//字符数

    while ((ch = getchar ()) != EOF)
        i++;

    printf ("字符数是:%d\n", i);

    return 0;
}


2.

#include <stdio.h>

int main (void)
{
    int ch;//字符
    int i;//ASCII值
    int j = 0;//一行的对数

    while ((ch = getchar()) != EOF)
    {
        printf ("%c: %d  ", ch, ch);
        j++;
        if (j % 10 == 0)
        {
            printf ("\n");
            j = 0;
        }
    }
}

3.

#include <stdio.h>

int main (void)
{
    char ch;
    int i, j;//大小写字母数量

    i = j = 0;
    while ((ch = getchar ()) != EOF)
    {
        if ((ch >= 'a') && (ch <= 'z'))
            j++;
        else if ((ch >= 'A') && (ch <= 'Z'))
            i++;
    }//while循环结束

    printf ("大写字母数量:%d\n"
            "小写字母数量:%d", i, j);
}


使用ctype.h头文件版
#include <stdio.h>
#include <ctype.h>

int main (void)
{
    char ch;
    int i = 0, j = 0;

    while ((ch = getchar ()) != EOF)
    {
        if (islower (ch))
            i++;
        if (isupper (ch))
            j++;
    }

    printf ("大写字母数量是:%d\n小写字母数量是:%d", j, i);

    return 0;
}

4.

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

int main (void)
{
    char ch;
    int i = 0, j = 1;

    while ((ch = getchar ()) != EOF)
    {
        if (ch == ' ')
            j++;
        else if (ispunct (ch))
            j++;
        else
            i++;
    }

    printf ("平均字母数:%f", (float)(i - 1) / j);

    return 0;
}

5.

#include <stdio.h>

int main (void)
{
    float num;
    float min = 0;
    float max = 100;
    char ch;

    printf ("Pick an integer from 0 to 100. I will try to guess "
            "it.\nRespond with a y if my guess is right.\n\n"
            "If the number I guess is lower, please input '<', and"
            "\nif it is upper, input '>'.\n");

    num = min;
    printf ("Is it %f?\n", num);
    while ((ch = getchar ()) != 'y')
    {
        if (ch == '<')
        {
            min = num;
            printf ("Is it %f?\n", num = (num + max) / 2);
        }
        else if (ch == '>')
        {
            max = num;
            printf ("Is it %f?\n", num = (num + min) / 2);
        }
    }

    printf ("The number is %f", num);

    return 0;
}

6.

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

char get_first (void);

int main (void)
{
    char ch;

    while ((ch = get_first ()) != EOF)
    {
        putchar (ch);
        putchar ('\n');
    }

    return 0;
}


char get_first (void)
{
    int ch_;

    while (isspace (ch_ = getchar ()));
    while (getchar () != '\n');
    return ch_;
}

7.

#include <stdio.h>
#define base 1000
#define lv1 300 * 0.15
#define lv2 150 * 0.2

int main (void)
{
    int time;
    char choice;
    int i;
    float wages;
    float money1, money2;

    for (i = 0; i < 65; i++)
        printf ("*");
    printf ("\nEnter 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");
    for (i = 0; i < 65; i++)
        printf ("*");
    printf ("\n");


    scanf ("%c", &choice);
    while (choice != 'q')
    {
        switch (choice)
        {
        case 'a':
            wages = 8.75;
            break;
        case 'b':
            wages = 9.33;
            break;
        case 'c':
            wages = 10.00;
            break;
        case 'd':
            wages = 11.20;
            break;
        }
        printf ("Enter your work time: ");
        scanf ("%d", &time);


        if (time <= 40)
            money1 = time * wages;
        else
            money1 = 40 * wages + (time - 40) * 1.5 * wages;


        if (money1 <= 300)
            money2 = money1 * (1 - 0.15);
        else if (money1 <= 450)
            money2 = money1 - lv1 - (money1 - 300) * 0.2;
        else
            money2 = money1 - lv1 - lv2 - (money1 - 450) * 0.25;
        printf ("%f\n%f\n%f\n", money1, money1 - money2, money2);


        printf ("Enter the number(q to quit): ");
        scanf ("%c", &choice);
    }

    return 0;
}

8.

#include <stdio.h>

float add (float a, float b);
float subtract (float a, float b);
float multiply (float a, float b);
float divide (float a, float b);
float get_float (void);

int main (void)
{
    float a, b;
    char ch;//选项

    printf ("Enter the option of your choice:\n"
            "a. add        s. subtract\n"
            "m. multiply   d. divide\n"
            "q. quit\n");
    while ((ch = getchar ()) != 'q')
    {
        printf ("Enter first number: ");
        a = get_float ();
        printf ("Enter second number: ");
        b = get_float ();

        switch (ch)
        {
        case 'a':
            printf ("%g", add (a, b));
            break;
        case 's':
            printf ("%g", subtract (a, b));
            break;
        case 'm':
            printf ("%g", multiply (a, b));
            break;
        case 'd':
            printf ("%g", divide (a, b));
            break;
        }//switch结束
        printf ("\nEnter the option of your choice: \n");
    }//while循环结束
    printf ("bye!");

    return 0;
}


float get_float (void)
{
    float num;
    char str[20];

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


float add (float a, float b)
{
    printf ("%g + %g = ", a, b);
    return a + b;
}


float subtract (float a, float b)
{
    printf ("%g - %g = ", a, b);
    return a - b;
}


float multiply (float a, float b)
{
    printf ("%g * %g = ", a, b);
    return a * b;
}


float divide (float a, float b)
{
    while (b == 0)
    {
        printf ("Enter a number other than 0:");
        scanf ("%g", &b);
        while (getchar () != '\n')
            continue;
    }
    printf ("%g / %g = ", a, b);
    return a / b;
}

猜你喜欢

转载自blog.csdn.net/sirius_black_tea/article/details/75646478