神秘博客

C Primer Plus [第六版]第8章编程练习题答案

1

#include <stdio.h>

int main()
{
  int n;
  char ch;
  n = 0;

  while ((ch = getchar()) != EOF)
  {
    n++;
  }
  printf("字符数: %d\n", n);

  system("pause");
  return 0;
}

2

#include <stdio.h>

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

  
  while ((ch = getchar()) != EOF)
  {
    if (ch < ' ')
    {
      
      if (ch == '\t')
      {
        i++;
        printf("\\t:%4d  ", ch + 64);
      }
        
      else if (ch == '\n')
      {
        i++;
        printf("\\n:%4d  ", ch + 64);
      }
        
    }
    
    else 
    {
      
      if (i % 10 == 0 && i != 0)
        printf("\n");
      i++;
      printf("%2c:%4d  ", ch, ch);
    }
    
  }
  
  system("pause");
  return 0;
}

3

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

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

  while ((ch = getchar()) != EOF)
  {
    if (islower(ch))
      i++;
    else if (isupper(ch))
      j++;
  }
  printf("小写字母: %d个 \t大写字母: %d个\n", i, j);

  system("pause");
  return 0;
}

4

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

int main()
{
  char ch;
  int i, j;
  bool word;
  word = false;
  i = 0;
  j = 0;

  while ((ch = getchar()) != EOF)
  {
    
    if (isalpha(ch))
    {
      word = true;
      i++;
    }
    else if (isspace(ch))
      word = false;
    
    if (!word)
      j++;
  }
  printf("单词个数: %d, 平均每个单词的字母数: %d\n", j, i / j);

  system("pause");
  return 0;
}

5

a 原版

//一个拖沓且错误的猜数字程序
#include <stdio.h>


int main()
{
  int guess = 50;
  int min = 0;
  int max = 101;
  char ch;
  char responst;

  printf("选择一个从1到100的整数。我会尝试猜测它。\n");
  printf("如果我的猜测是正确的,那么回应一个 y,如果它是错误的,则回答 n。\n");
  printf("呃...你的号码是 %d?\n", guess);

  while ((responst = getchar()) != 'y') //获取响应,与Y做对比
  {
    while (getchar() != '\n')
      continue;
    if (responst == 'n')
    {
      bk:printf("我猜大了还是猜小了: \n");
      printf("a.大了 \tb.小了\n");
      scanf_s("%c", &ch);
      while (getchar() != '\n')
        continue;
      if (ch == 'a')
      {
        max = guess;
        guess -= (max - min) / 2;
        printf("恩,然后,它是 %d?\n", guess);
      }

      else if (ch == 'b')
      {
        min = guess;
        guess += (max - min) / 2;
        printf("恩,然后,它是 %d?\n", guess);
      }
      else
      {
        printf("对不起,我只懂a或b。\n");
        goto bk;
      }
        
    }
    else
      printf("对不起,我只懂y或n。\n");

  }


  printf("我知道我可以做到!\n");

  system("pause");
  return 0;
}

b 使用指针版本

//一个拖沓且错误的猜数字程序
#include <stdio.h>

void guess_a(int *guess, int *min, int *max);

int main()
{
  int guess = 50;
  char responst;
  int min = 0, max = 101;

  printf("选择一个从1到100的整数。我会尝试猜测它。\n");
  printf("如果我的猜测是正确的,那么回应一个 y,如果它是错误的,则回答 n。\n");
  printf("呃...你的号码是 %d?\n", guess);
  while ((responst = getchar()) != 'y') //获取响应,与Y做对比
  {
    
    while (getchar() != '\n')
      continue;
    if (responst == 'n')
    {
      guess_a(&guess, &min, &max);
      printf("恩,然后,它是 %d?\n", guess);
    }
    else
      printf("对不起,我只懂y或n。\n");
  }
  printf("我知道我可以做到!\n");
  system("pause");
  return 0;
}

void guess_a(int *guess, int *min, int *max)
{
  char ch;


  bk:printf("我猜大了还是猜小了: \n");
  printf("a.大了 \tb.小了\n");
  scanf_s("%c", &ch);
  
  while (getchar() != '\n')
    continue;
  if (ch == 'a')
  {
    *max = *guess;
    *guess -= (*max - *min) / 2;
  }
  else if (ch == 'b')
  {
    *min = *guess;
    *guess += (*max - *min) / 2;

  }
  else
  {
    printf("对不起,我只懂a或b。\n");
    goto bk;

  }
    
  

  
    
  
  
  

}

 

 

 

6

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

char get_first(void);

int main()
{
  char ch;
  ch = get_first();

  putchar(ch);
  putchar('\n');
  system("pause");
  return 0;
}


char get_first(void)
{
  int ch;

  while (!isalpha(ch = getchar()))
    continue;
    


  return ch;
}

7

#include <stdio.h>  //标准输入输出函数库文件
#define MONEY1 8.75    //基本工资8.75美元/小时
#define MONEY2 9.33    //基本工资9.33美元/小时
#define MONEY3 10.00    //基本工资10.00美元/小时
#define MONEY4 11.20    //基本工资11.20美元/小时
#define HOUR 1.5    //超过40小时1.5倍
#define TAX1 300*0.15   //税率前300美元15%
#define TAX2 150*0.2    //续150美元为20%
#define TAX3 0.25   //余下的为25%

char ch_a(void);

int main()
{
  double hour, money, money_1, tax;
  int i;


b:printf("输入对应基本工资的编号:\n");
  printf("a) $8.75/hr  \tb)$9.33/hr\n");
  printf("c) $10.00/hr \td)$11.20/hr\n");
  printf("q) 退出\n");
  i = ch_a();
  
  switch (i)
  {
  case 'a':
    money_1 = MONEY1;
    break;
  case 'b':
    money_1 = MONEY2;
    break;
  case 'c':
    money_1 = MONEY3;
    break;
  case 'd':
    money_1 = MONEY4;
    break;
  case 'q':
    goto quit;
  default:
    printf("输入错误请重新输入: \n");
    goto b;

  }
      

  
  printf("请输入你的工作时间[小时]: ");
  scanf_s("%lf", &hour);
  if (hour <= 40)
    money = hour * money_1;
  else
    money = (money_1 * 40) + (hour - 40)*HOUR*money_1;

  if (money <= 300)
    tax = money * 0.15;
  else if (money <= 450)
    tax = TAX1 + (money - 300)*0.2;
  else
    tax = TAX1 + TAX2 + (money - 450)*TAX3;

  printf("你工作时间 %.2lf 小时, 工资总额: $%.2lf, 税金: $%.2lf, 净收入: $%.2lf.\n", hour, money, tax, money - tax);

quit:printf("完成再见!\n");

  system("pause");
  return 0;

}

char ch_a(void)
{
  char ch, ch_a;

  while ((ch = getchar()) != '\n')
  {
    if ((ch >= 'a'&&ch <= 'd') || ch == 'q')
      ch_a = ch;
    else
    {
      while (getchar() != '\n')
        continue;
      printf("输入有误, 请重新输入, 只能输入a, b, c, d或者q: \n");
      continue;
    }
    
  }

  return ch_a;
}

8

#include <stdio.h>
#include <ctype.h>
#define WIDTH 40

char ch_a(void);
float num(void);
void starbar(void);

int main()
{
  float x, y;
  char ch;

bk:starbar();
  printf("输入您选择的操作: \n");
  printf("a.加法 \t\ts.减法\nm.乘法 \t\td.除法\nq.退出\n");
  starbar();
  ch = ch_a();
  if (ch == 'q')
    goto done;
  printf("请输入第一个数值: ");
  x = num();
  printf("请输入第二个数值: ");
  y = num();
  while (ch == 'd'&&y == 0)
  {
    printf("输入除0以外的数字: ");
    y = num();
  }
  
  switch (ch)
  {
  case 'a':
    printf("%g + %g = %g\n", x, y, x + y);
    goto bk;
  case 's':
    printf("%g - %g = %g\n", x, y, x - y);
    goto bk;
  case 'm':
    printf("%g x %g = %g\n", x, y, x * y);
    goto bk;
  case 'd':
    printf("%g / %g = %g\n", x, y, x / y);
    goto bk;
  }
  done:printf("\n再见\n");

  system("pause");
  return 0;
}

char ch_a(void)
{
  char ch, ch_b;
  ch_b = 0;

  while ((ch = getchar()) != '\n')
  {
    if (ch == 'a' || ch == 's' || ch == 'm' || ch == 'd' || ch == 'q')
      ch_b = ch;
    else
    {
      while (getchar() != '\n')
        continue;
      printf("输入有误, 请重新输入, 只能输入a, s, m, d或者q: \n");
      continue;
    }

  }

  return ch_b;
}

float num(void)
{
  float x;
  char ch;

  while (scanf_s("%f",&x) != 1)
  {
  
    
    while ((ch = getchar()) != '\n')
      putchar(ch);
    printf(" 不是一个数字.\n请重新输入, 例如2.5, -1.78E8或3: ");
    continue;
    
  }
  while (getchar() != '\n')
    continue;

  return x;
}

void starbar(void)
{
  int count;

  for (count = 1; count <= WIDTH; count++)
    putchar('*');
  putchar('\n');
}

 

 

 

版权说明:
点赞

发表评论

电子邮件地址不会被公开。 必填项已用*标注

觉得文章有用就请我吃包辣条吧

微信扫一扫打赏