C Primer Plus 第六版第八章编程练习答案

部分习题代码丢失,需要请联系博主。
编译环境:Visual Studio 2017

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
void out_add(void);
void out_sub(void);
void out_mul(void);
void out_div(void);
int main(void)
//3
{
    int ch,i=0,I=0;//考虑到EOF的返回值
    while ((ch=getchar())!=EOF)
    {
        if (islower(ch))
            i++;
        if (isupper(ch))
            I++;
    }
    printf("i=%d\tI=%d", i, I);
    system("pause");
    return 0;

}
//4
{
    int ch;//考虑到EOF的返回值
    float i = 0, I = 0;
    while ((ch=getchar())!=EOF)
    {
    if (isalpha(ch))
    i++;
    if (isblank(ch))
    I++;
    }   
    printf("x=%f",i/(I+1));
    system("pause");
    return 0;
}
//5 二分法
{
    int i = 50, upper = 100, lower = 0;
    printf("50?\n");
    while (getchar()!= 'y')
    {
        printf("Tell me big or small:\n");
        while (getchar() != '\n')
            continue;
        if (getchar() == 's')
        {
            lower = i;
            i = (i+upper)/2;
            printf("%d?", i);
            while (getchar() != '\n')
                continue;
        }
        else
        {
            upper = i;
            i = (i + lower) / 2;
            printf("%d?",i);
            while (getchar() != '\n')
                continue;
        }

    }
    system("pause");
    return 0;
}
//此处遇到一个问题:
{
int i = 50, upper = 100, lower = 0;
printf("50?\n");
while (getchar() != 'y')
{
    printf("Tell me big or small:\n");
    while (getchar() != '\n')
        continue;
    if (getchar() == 'a')
    {
        i = 1;
        printf("%d?", i);
        while (getchar() != '\n')
            continue;
    }
    if (getchar() == 'b')
    {
        i = 2;
        printf("%d?", i);
        while (getchar() != '\n')
            continue;
    }
//6
{
    int i = 1;
    char ch;
    while(i)
        while ((ch = getchar()) != '\n' && (ch!= '\t') && (ch != ' '))
        {
            putchar(ch);
            i = 0;
            break;
        }

    system("pause");
    return 0;
}
//8
{
    char choice;
    printf("Enter the operation of your choice:\n");
    printf("a. add\ts. subtract\n");
    printf("m. multiply\td. divide\n");
    printf("q. quit\n");
    while ((choice=getchar())!='q')
    {
        switch (choice)
        {
        case 'a':out_add();
            break;
        case 's':out_sub();
            break;
        case 'm':out_mul();
            break;
        case 'd':out_div();
            break;
        default:
            break;
        }
        while ((getchar() != '\n'))
            continue;
        printf("Enter the operation of your choice:\n");
        printf("a. add\ts. subtract\n");
        printf("m. multiply\td. divide\n");
        printf("q. quit\n");
    }
    system("pause");
    return 0;
}

void out_add(void)
{
    float x, y, z;
    printf("Enter the first number:\n");
    if (scanf("%f", &x) != 1)
        printf("%s is not a number,try again!\n",x);
    printf("Enter the second number:\n");
    if (scanf("%f", &y) != 1)
        printf("%s is not a number,try again!\n", y);
    z = x + y;
    printf("%f+%f=%f\n", x, y, z);
}
void out_sub(void)
{
    float x, y, z;
    printf("Enter the first number:\n");
    if (scanf("%f", &x) != 1)
        printf("%s is not a number,try again!\n", x);
    printf("Enter the second number:\n");
    if (scanf("%f", &y) != 1)
        printf("%s is not a number,try again!\n", y);
    z = x - y;
    printf("%f-%f=%f\n", x, y, z);
}
void out_mul(void)
{
    float x, y, z;
    printf("Enter the first number:\n");
    if (scanf("%f", &x) != 1)
        printf("%s is not a number,try again!\n", x);
    printf("Enter the second number:\n");
    if (scanf("%f", &y) != 1)
        printf("%s is not a number,try again!\n", y);
    z = x * y;
    printf("%f*%f=%f\n", x, y, z);
}
void out_div(void)
{
    float x, y, z;
    printf("Enter the first number:\n");
    while (scanf("%f", &x) != 1)
        printf("%s is not a number,try again!\n", x);
    printf("Enter the second number:\n");
    while (scanf("%f", &y) != 1)
        printf("%s is not a number,try again!\n", y);
    while (y == 0)
    {
        printf("y cannot = 0,try again!\n");
        while (scanf("%f", &y) != 1)
            printf("%s is not a number,try again!\n", y);
    }
    z = x / y;
    printf("%f/%f=%f\n", x, y, z);
}

2015.6.1

猜你喜欢

转载自blog.csdn.net/qq_38967295/article/details/80532279