C Primer Plus第八章课后答案

其他章节答案

/*Project 1*/
#include<stdio.h>
int main(void)
{
    int ch;
    int n = 0;
    while ((ch = getchar()) != EOF)
        n++;
    printf("%d",n);
    return 0;
 } 
/*Project 2*/
#include<stdio.h>
int main(void)
{
    char ch;
    int n = 0;
    while ((ch = getchar()) != EOF)
    {
        switch(ch)
        {
            case '\n':
                printf("\\n");
                printf(" %d,",ch);
                break;
            case '\t':
                printf("\\t");
                printf(" %d,",ch);
                break;
            default:
                if (ch < ' ')
                {
                    putchar('^');
                    putchar(ch + 64);
                    printf(" %d,",ch);
                }
                else
                {
                    putchar(ch);
                    printf(" %d,",ch);
                }
                break;
        }
        n++;
        if(n == 10)
        {
            putchar('\n');
            n = 0;
        }
    }
    return 0;
 } 
或者
#include<stdio.h>
int main(void)
{
    char ch;
    int n = 0;
    while ((ch = getchar()) != EOF)
    {
        if(ch < 32)
        {
            if(ch == '\n')
                printf("\\n %d,",ch);
            else if(ch == '\t')
                printf("\\t %d,",ch);
            else
                printf("'^'%c %d,",ch + 64,ch);
        }
        else
            printf("%c %d,",ch,ch);
        n++;
        if(n == 10)
        {
            printf("\n");
            n = 0;
        }
    }
    return 0;
 } 
/*Project 3*/
#include<stdio.h>
int main(void)
{
    int n = 0;
    int m = 0;
    char ch;
    while((ch = getchar()) != EOF)
    {
        if (ch >= 'A' && ch <= 'Z')
        {
            n++;
        }
        else if (ch >= 'a' && ch <= 'z')
        {
            m++;
        }
    }
    printf("大写字母个数:%d\n小写字母个数:%d",n,m);
    return 0;
 } 
或者
#include<stdio.h>
#include<ctype.h>
int main(void)
{
    int n = 0;
    int m = 0;
    char ch;
    while((ch = getchar()) != EOF)
    {
        if (isupper(ch))
        {
            n++;
        }
        else if (islower(ch))
        {
            m++;
        }
    }
    printf("大写字母个数:%d\n小写字母个数:%d",n,m);
    return 0;
 } 
/*Project 4*/
/*假设字母间均用空格隔开,标点符号后若有单词则也加空格*/
#include<stdio.h>
#include<ctype.h>
int main(void)
{
    int n = 0;  /*字母数*/ 
    int m = 0;  /*单词数*/ 
    char ch;
    while((ch = getchar()) != EOF)
    {
        if (isalpha(ch))
        {
            n++;
        }
        else if (ch == ' ')
        {
            m++;
        }
    }
    printf("平均每个字母的单词数:%d\n",n/m);
    return 0;
 } 
/*Project 5*/
#include<stdio.h>
int main(void)
{
    int guess = 50;
    int min = 1;
    int max = 100;
    char 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 and with");
    printf("\nan u if it is upper and with an l if it is lower.\n");
    printf("Un...is your number %d?\n", guess);
    while((response = getchar()) != 'y')
    {
        if (response == 'u')
        {
            max = guess;
            guess = (min + max) / 2;
            printf("Well,then, is it %d?\n",guess);
        }
        else if (response == 'l')
        {
            min = guess;
            guess = (min + max) / 2;
            printf("Well,then, is it %d?\n",guess);
        }
        else
            printf("Sorry,I can't understand only y or u or l.\n");
        while(getchar() != '\n')
            continue;           
    }
    printf("I knew I could do it!\n");
    return 0;
}
/*Project 6*/
#include<stdio.h>
char get_first(void);
int main(void)
{
    int ch;
    printf("请输入一个字符:"); 
    ch = get_first();
    printf("你输入的是:%c",ch);
    return 0; 
}
char get_first(void)
{
    int ch;
    while((ch = getchar()) == ' ')
        ;
    return ch;
}
/*Project 7*/
#include<stdio.h>
#define OVERTIME 1.5
#define TAX11 300
#define TAX12 0.15
#define TAX21 150
#define TAX22 0.2
#define TAX3 0.25
#define TIME 40
int main(void)
{
    float time,pay,tax,RATE;
    char num;
    printf("********************************************************************\n");
    printf("Enter the number corresponding to the desired pay rate or action:\n");
    printf("a) $8.75/hr\tb) $9.33/hr\n");
    printf("c) $10.00/hr\td) $11.20/hr\n");
    printf("q) quit\n");
    printf("********************************************************************\n");
    scanf("%c",&num);
    while((num < 'a' || num > 'd') && num != 'q')
    {
        printf("请输入正确的选项!\n");
        scanf("%c",&num);
    }
    if(num != 'q')
    {
        printf("请输入一周工作小时数:");
        scanf("%f",&time);
        while(time < 0 || time > 168)
        {
            printf("请输入正确的时间!\n");
            scanf("%f",&time);
        }
        switch(num)
        {
            case 'a': 
                RATE = 8.75;
                break;
            case 'b':
                RATE = 9.33;
                break;
            case 'c':
                RATE = 10.00;
                break;
            case 'd':
                RATE = 11.20;
                break;
        }
        if(time <= TIME)
            {
                pay = time * RATE;
                if(pay <= TAX11)
                    tax = pay * TAX12;
                else if(pay <= (TAX11 + TAX21) && pay > TAX11)
                    tax = TAX11 * TAX12 + (pay - TAX11) * TAX22;
                else if(pay > (TAX11 + TAX21))
                    tax = TAX11 * TAX12 + TAX21 * TAX22 + (pay - TAX11 - TAX21) * TAX3;
            } 
            else if(time > TIME)
            {
                pay = RATE * OVERTIME * time;
                tax = TAX11 * TAX12 + TAX21 * TAX22 + (RATE * TIME - TAX11 - TAX21) * TAX3;
            }
            printf("工资总额:%.2f$.\n税金:%.2f$.\n净收入:%.2f$.\n",pay,tax,pay - tax); 
    }
    else
        printf("Done!");
    return 0;
}
/*Project 8*/
#include<stdio.h>
int main(void)
{
    float first,second;
    char choice;
    char ch[40];
    printf("Enter the operation of you choice:\n");
    printf("a) add     \tb) subtract\n");
    printf("c) multiply\td) divide\n");
    printf("q) quit\n");
    while(scanf("%c",&choice))
    {
        while(choice != 'a' && choice != 'b' && choice != 'c' && choice != 'd' && choice != 'q')
        {
            printf("请输入正确的选项!\n");
            scanf("%c",&choice);
        }
        switch(choice)
        {
            case 'a':
                printf("Enter first number: ");
                while(scanf("%f",&first) != 1)
                {
                    scanf("%s",&ch);
                    printf("%s is not an number.\n",ch);
                    printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
                }
                printf("Enter second number: ");
                while(scanf("%f",&second) != 1)
                {
                    scanf("%s",&ch);
                    printf("%s is not an number.\n",ch);
                    printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
                }
                printf("%g + %g = %g\n",first,second,first+second);     /*%g能自动选择合适的格式输出*/
                break;
            case 's':
                printf("Enter first number: ");
                while(scanf("%f",&first) != 1)
                {
                    scanf("%s",&ch);
                    printf("%s is not an number.\n",ch);
                    printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
                }
                printf("Enter second number: ");
                while(scanf("%f",&second) != 1)
                {
                    scanf("%s",&ch);
                    printf("%s is not an number.\n",ch);
                    printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
                }
                printf("%g - %g = %g\n",first,second,first-second); 
                break;
            case 'm':
                printf("Enter first number: ");
                while(scanf("%f",&first) != 1)
                {
                    scanf("%s",&ch);
                    printf("%s is not an number.\n",ch);
                    printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
                }
                printf("Enter second number: ");
                while(scanf("%f",&second) != 1)
                {
                    scanf("%s",&ch);
                    printf("%s is not an number.\n",ch);
                    printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
                }
                printf("%g * %g = %g\n",first,second,first*second); 
                break;
            case 'd':
                printf("Enter first number: ");
                while(scanf("%f",&first) != 1)
                {
                    scanf("%s",&ch);
                    printf("%s is not an number.\n",ch);
                    printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
                }
                printf("Enter second number: ");
                while((scanf("%f",&second) != 1))
                {
                        scanf("%s",&ch);
                        printf("%s is not an number.\n",ch);
                        printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
                }
                const float EPSINON = 0.000001; 
                /*由于浮点数的表示是不精确的,所以不能直接比较两个数是否完全相等。
                一般都是在允许的某个范围内认为某个个浮点数相等
                最好设置个变量0.000001再比较,直接比较容易出错*/
                while(( second >= -EPSINON ) && ( second <= EPSINON ))  
                {
                    printf("Enter a number other than 0: ");
                    scanf("%f",&second);
                }
                printf("%g / %g = %g\n",first,second,first/second);
                break;
            default:
                break;
        }
        while(getchar() == EOF)
            break;
        if (choice == 'q')
        {
            printf("Bye!");
            break;
        }
        printf("Enter the operation of you choice:\n");
        printf("a) add     \tb) subtract\n");
        printf("c) multiply\td) divide\n");
        printf("q) quit\n");
    }
    return 0;   
}
 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

撼沧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值