c primer plus(第六版) 第八章答案(vscode编译运行通过)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
1.

#include <stdio.h>

int main(void)
{
    int count = 0;

    while(getchar() != EOF)
    {
        count++;    
    }
    printf("%d",count);
    return 0;
}
#include <stdio.h>

int main(void)
{
    int ch;
    int count = 0;
    puts("Please enter characters:");
    while((ch = getchar()) != EOF)
    {
        if(count >= 10)
        {
            putchar('\n');
            count = 0;
        }
        if(ch >= ' ')
        {
           
            printf(" \'%c\' - %-3d",ch,ch);
        }
        else if(ch == '\n')
        {
            printf(" \\n - \\n");
            count = 0;
        }
        else if(ch == '\t')
        {
            printf(" \\t - \\t");

        }
        else
        {
            printf(" \'%c\' - ^%c",ch,ch+64);
        }
        count++;
    }
    return 0;
}
#include <stdio.h>
#include <ctype.h>

int main(void)
{
    int num_capital = 0;
    int num_lowercase = 0;
    int ch;

    puts("Please enter some letters:");
    while((ch = getchar()) != EOF)
    {
        if(islower(ch))
            num_lowercase++;
        if(isupper(ch))
            num_capital++;
    }
    printf("The capital's number are %d\n",num_capital);
    printf("The lowercase's number are %d\n",num_lowercase);
    return 0;
}
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
bool flag = false;

int main(void)
{
    int ch;
    int alphabet_character = 0;
    int num_alphabet = 0;
    while((ch = getchar()) != EOF)
    {
        if(isalpha(ch))
        {
            if(flag == false)
            {
                num_alphabet++;
                flag = true;
            }
            alphabet_character++;
        }
        if(isspace(ch) && flag)
        {
            flag = false;
        }
    }
    printf("The alphabet_character's number are %d\n", alphabet_character);
    printf("The alphabet's number are %d\n",num_alphabet);
    printf("Average the number of letters per word are %g\n",(double)alphabet_character/num_alphabet);
    return 0;
}
#include <stdio.h>

int main(void)
{
    int up = 100,down = 0,guess = 50;
    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 n if it is wrong.\n");
    printf("Uh...is your number %d?",guess);
    while((response = getchar()) != 'y')
    {
        while(getchar() != '\n')
            continue;
        if(response == 'n')
        {
            printf("Uh...The number is getting bigger or smaller?\n");
            printf("Please enter b for a larger number\n");
            printf("Please enter s for a smaller number:\n");
            while (((response = getchar()) != 'b') && (response != 's'))
            {
                printf("Please input \'b\' or \'s'\':\n");
                while(getchar() != '\n')
                    continue;
            }
            while(getchar() != '\n')
                continue;
            if(response == 'b')
            {
                down = guess + 1;
                guess = (down + up) / 2;
            }
            else
            {
                up = guess - 1;
                guess = (down + up) / 2;
            }
             printf("Well, then, is it %d?\n",guess);
        }
        else
        {
            printf("Sorry, I understand only y or n.\n");
        }
        
    }
        
    printf("I knew I could do it!\n");

    return  0;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);

int main(void)
{
  int choice;
  while((choice = get_choice()) != 'q')
  {
      switch(choice)
      {
          case 'a':printf("Buy low, sell high.");break;
          case 'b':putchar('\a');break;
          case 'c':count();break;
          default:printf("Program error!\n");break;
      }
  }
  printf("Bye.\n");
  printf("end");
  printf("\n");
  system("pause");
  
    return 0;
}

void count(void)
{
    int n,i;

    printf("Count how far? Enter an integer:\n");
    n = get_int();
    for(i = 1;i <= n;i++)
        printf("%d\n",i);
    while(getchar() != '\n')
        continue;
}

char get_choice(void)
{
    int ch;

    printf("Enter the letter of your choice\n");
    printf("a.advice                   b.bell\n");
    printf("c.count                    q.quit\n");
    ch = get_first();
    while((ch < 'a' || ch > 'c') && ch != 'q')
    {
        printf("Please respond with a,b,c,or,q.\n");
        ch = get_first();
    } 

    return ch;
}

char get_first(void) 
{
    int ch;

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

int get_int(void)
{
    int inupt;
    char ch;

    while(scanf("%d",&inupt) != 1)
    {
        while((ch = getchar()) != '\n')
            putchar(ch);
        printf(" is not an integer.\nPlease enter an ");
        printf("integer value,such as 25,-178,or 3: ");
    }

    return inupt;
}
#include <stdio.h>
#include <ctype.h>

#define OVERTIME 40
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
#define THE_TOTAL_SALARY1 300
#define THE_TOTAL_SALARY2 150

void show_menu(void)
{
     for(int i = 0; i < 100; i++)
        printf("*");
    printf("\n");
    printf("Enter the number corresponding to the desired pay rate or action\n");
    printf("a) $8.7h/hr                                       b) $9.33/hr\n");
    printf("c) $10.00h/hr                                     d) $11.20/hr\n");
    printf("q) quit\n");
    for(int i = 0; i < 100; i++)
        printf("*");
    printf("\n");
}

int main(void)
{
    int hour_per_week;
    float total_wage_bil;
    float taxes;
    float net_inclome;
    int choice;
    float salary;

    show_menu();
    while(((choice = getchar()) != EOF) && choice != 'q')
    {
        while(getchar() != '\n')
            continue;
        if(choice != 'a' &&  choice != 'b'  &&  choice != 'c'  &&  choice != 'd')
        {
            show_menu();
            continue;
        }
        switch(choice)
        {
            case 'a':salary = 8.75;break;
            case 'b':salary = 9.33;break;
            case 'c':salary = 10.00;break;
            case 'd':salary = 11.20;break;
            default:break;
        }
        printf("Please enter the number of hours you work in a week: ");
        scanf("%d",&hour_per_week);
        while(getchar() != '\n')
            continue;
        if(hour_per_week > OVERTIME)
        {
            total_wage_bil = hour_per_week * salary * 1.5;
        }
        else
        {
            total_wage_bil = hour_per_week * salary;
        }
        if(total_wage_bil < THE_TOTAL_SALARY1)
        {
            taxes = total_wage_bil * RATE1;
        }
        else if(total_wage_bil < THE_TOTAL_SALARY1 + THE_TOTAL_SALARY2)
        {
            taxes = THE_TOTAL_SALARY1 * RATE1 + (total_wage_bil - THE_TOTAL_SALARY1) * RATE2;
        }
        else
        {
            taxes =  THE_TOTAL_SALARY1 * RATE1 \
                + THE_TOTAL_SALARY2 * RATE2 \
                + (total_wage_bil- THE_TOTAL_SALARY1-THE_TOTAL_SALARY2)*RATE3;
        }
        net_inclome = total_wage_bil - taxes;
        printf("The total wage bil is %.2f\n", total_wage_bil);
        printf("The taxes is %.2f\n",taxes);
        printf("The net inclome is %.2f\n",net_inclome);
        show_menu();
    }
    return 0;
}
#include <stdio.h>
#include <ctype.h>

void show_menu(void)
{
    printf("Enter the operation of your choice:\n");
    printf("a. add            s. subtract\n");
    printf("m. multiply       d. divide\n");
    printf("q. quit\n");
}

void eat_line(void)
{
    while(getchar() != '\n')
        continue;
}
     
int get_choice(void)
{
    int ch;
    ch = getchar();
    while(ch != 'a' && ch != 's' && ch != 'm' && ch != 'd' && ch != 'q')
    {
        printf("Please input right option\n");
        show_menu();
        ch = getchar();
    }
    return ch;
}

double get_double(void)
{
    double num;
    int ch;

    while(scanf("%lf",&num) != 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: ");
    }
    while(getchar() != '\n')
        continue;
    return num;
}


int main(void)
{
    int ch;
    double num1,num2;

    show_menu();
    while((ch = get_choice()) !=  'q')
    {
        switch(ch)
        {
            case 'a':
                    printf("Enter first number: ");
                    num1 = get_double();
                    printf("Enter second number: ");
                    num2 = get_double();
                    printf("%g + %g = %g\n",num1,num2,num1+num2);
                    break;
            case 's':
                    printf("Enter first number: ");
                    num1 = get_double();
                    printf("Enter second number: ");
                    num2 = get_double();
                    printf("%g - %g = %g\n",num1,num2,num1-num2);
                    break;
            case 'm':
                    printf("Enter first number: ");
                    num1 = get_double();
                    printf("Enter second number: ");
                    num2 = get_double();
                    printf("%g * %g = %g\n",num1,num2,num1*num2);
                    break;
            case 'd':
                    printf("Enter first number: ");
                    num1 = get_double();
                    printf("Enter second number: ");
                    num2 = get_double();
                    while(num2 == 0)
                    {
                        printf("Enter a number other than 0: ");
                        num2 = get_double();
                    }
                    printf("%g / %g = %g\n",num1,num2,num1/num2);
                    break;
        }
        printf("Enter the operation of your choice:\n");
    }
    printf("Bye.");  
   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值