神秘博客

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

1

#include <stdio.h>

int main()
{
  char ch;
  int a, b, c;
  a = 0;
  b = 0;
  c = 0;

  printf("请输入内容[#退出]: ");
  while ((ch = getchar()) != '#')
  {
    if (ch == ' ')
      a++;
    else if (ch == '\n')
      b++;
    else
      c++;
  }
  printf("空格 = %d, 换行 = %d, 其他字符 = %d.\n", a, b, c);

  system("pause");
  return 0;
}

2

#include <stdio.h>

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

  printf("该程序用于打印出对应字符的 ASCII 码\n\n");
  printf("请输入字符[#退出]: ");

  while ((ch = getchar()) != '#')
  {
    if (ch != '\n')
    {
      
      if ((i % 8 == 0) && (i != 0))
      {
        printf("\n");
      }
      i++;
      printf("%c:%d ", ch, ch);
    }
    
  }

  system("pause");
  return 0;
}

3

#include <stdio.h>

int main()
{
  int n, i, j;
  int x, y;
  i = 0;
  j = 0;
  x = 0;
  y = 0;


  printf("请输入整数: ");

  while (scanf_s("%d", &n) == 1)
  {
    if (n == 0)
      break;
    if (n % 2 == 0)
    {
      i++;
      x += n;
    }
    else
    {
      j++;
      y += n;
    }
      
  }
  if (i > 0 && j > 0)
  {
    printf("偶数: %d   平均值: %d\n", i, x / i);
    printf("奇数: %d   平均值: %d\n", j, y / j);
  }	
  else if (i > 0)
    printf("偶数: %d   平均值: %d\n", i, x / i);
  else if (j > 0)
    printf("奇数: %d   平均值: %d\n", j, y / j);
  else
    printf("错误: 你没有输入任何整数.\n");
       

  system("pause");
  return 0;
}

4

#include <stdio.h>

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

  printf("请输入内容: ");
  
  while ((ch = getchar()) != '#')
  {
    if (ch == '.')
    {
      i++;
      putchar('!');
    }
    else if (ch == '!')
    {
      j++;
      printf("!!");
    }
    else
        putchar(ch);
  }
  printf("\n句号替换: %d\n", i);
  printf("感叹号替换: %d\n", j);

  system("pause");
  return 0;

}

5

#include <stdio.h>

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

  printf("请输入内容: ");

  while ((ch = getchar()) != '#')
  {
    switch (ch)
    {
    case '.':
      i++;
      printf("!");
      break;
    case '!':
      j++;
      printf("!!");
      break;
    default:
      putchar(ch);
      break;
    }
    
  }
  printf("\n句号替换次数: %d\n", i);
  printf("感叹号替换次数: %d\n", j);

  system("pause");
  return 0;
}

6

#include <stdio.h>

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

  printf("请输入内容: ");
  while ((ch = getchar()) != '#')
  {
    i++;
    if (ch == 'e')
      x = i;
    if (ch == 'i')
      {
        y = i;
        if ((y - x) == 1)
          j++;
      }

  }
  printf("字符数: %d, 'ei'出现次数: %d\n", i, j);

  system("pause");
  return 0;
}

7

#include <stdio.h>  //标准输入输出函数库文件
#define MONEY 1000    //基本工资1000美元/小时
#define MONEY1 1000*40 
#define TAX1 300*0.15   //税率前300美元15%
#define TAX2 150*0.2    //续150美元为20%
#define TAX3 0.25   //余下的为25%

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

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

  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("你工作时间 %.2f 小时, 工资总额: $%.2f, 税金: $%.2f, 净收入: $%.2f.\n", hour, money, tax, money - tax);
  
  system("pause");
  return 0;

}

 

8

#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%

int main()
{
  double hour, money, money_1, tax;
  int i;
  
  
b:printf("输入对应基本工资的编号:\n");
  printf("1) $8.75/hr  \t2)$9.33/hr\n");
  printf("3) $10.00/hr \t4)$11.20/hr\n");
  printf("5) 退出\n");
  while ((i=getchar()) != 0)
  {
    if (i == '\n')
      continue;
    else
    {
      switch (i)
      {
      case '1':
        money_1 = MONEY1;
        break;
      case '2':
        money_1 = MONEY2;
        break;
      case '3':
        money_1 = MONEY3;
        break;
      case '4':
        money_1 = MONEY4;
        break;
      case '5':
        goto quit;
      default:
        printf("输入错误请重新输入: \n");
        goto b;

      }
      break;
    }
    break;
  }
  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;

}

9

#include <stdio.h>

int main()
{
  unsigned int i, j, x, y, z, n;

  printf("请输入一个整数: ");
  scanf_s("%u", &i);
  printf("小于 %u 的质数: \n", i);
  for (j = 2, n = 0; j <= i; j++)
  {
    for (x = 2, y = 0; x < j; x++)
    {
      if (j%x == 0)
        y++;
      
        

    }
    if (n % 10 == 0 && n != 0)
      putchar('\n');

    if (y == 0)
    {
      n++;
      z = j;
      printf("%5u ", z);
    }
    
  }
  printf("\n");

  system("pause");
  return 0;
}

10

#include <stdio.h>
#define MONEY1 17850  //单身税金界限
#define MONEY2 23900  //户主税金界限
#define MONEY3 29750  //已婚,共有界限
#define MONEY4 14875  //已婚,离异界限
#define TAX1 0.15 
#define TAX2 0.28

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

bk:printf("请选择您缴纳税金的种类: \n");
  printf("1) 单身 \t2) 户主\n");
  printf("3) 已婚共有 \t4) 已婚离异\n");
  printf("5) 退出\n");
  while ((i = getchar()) != 0)
  {
    if (i == '\n')
      continue;
    switch (i)
    {
    case '1':
      money_1 = MONEY1;
      break;
    case '2':
      money_1 = MONEY2;
      break;
    case '3':
      money_1 = MONEY3;
      break;
    case '4':
      money_1 = MONEY4;
      break;
    case '5':
      goto done;
    default:
      printf("输入有误, 请重新输入\n");
      goto bk;
    }
    break;
  }

  printf("请输入你的工资收入: ");
  while (scanf_s("%lf", &money) == 1)
  {
    if (money <= money_1)
      tax = money * TAX1;
    else
      tax = money_1 * TAX1 + (money - money_1)*TAX2;

    printf("您的工资收入为 $%.2f 应缴纳税金为 $%.2f\n", money, tax);

    printf("请继续输入你的工资收入[q退出]: ");
  }
  
  
  

done:printf("完成\n");

  system("pause");
  return 0;
}

11

#include <stdio.h>
#define GOODS1 2.05  //洋蓟 2.05美元/磅
#define GOODS2 1.15  //甜菜 1.15美元/磅
#define GOODS3 1.09  //胡萝卜 1.09美元/磅
#define DIS 0.05     //100美元订单优惠5%
#define MONEY1 6.5   //小于或等于5磅的订单收6.5美元运费和包装费
#define MONEY2 14    //5~20磅的订单收取14美元运费和包装费
#define MONEY3 0.5   //超过20磅续重每磅加0.5美元
//验证重量输入是否正确函数
double a_pound(void);

char a_ch(void);

int main()
{
  char ch;
  double money, money_1, money_2, money_3;
  double pound_1, pound_2, pound_3, pound_11, pound_22, pound_33, dis;
  double pfc;

  money_1 = 0;
  money_2 = 0;
  money_3 = 0;
  pound_1 = 0;
  pound_2 = 0;
  pound_3 = 0;

bk:printf("请选择需要购买的商品\n");
  printf("a) 洋蓟[$%.2f] \t\tb) 甜菜[$%.2f]\nc) 胡萝卜[$%.2f] \tq) 退出订购\n",GOODS1,GOODS2,GOODS3);
  
  while ((ch = a_ch()) != 'q')
  {
    
    switch (ch)
    {
    case 'a':
      printf("请输入你要购买洋蓟的重量[磅]: ");
      pound_11 = a_pound();
      pound_1 += pound_11;
      money_1 = GOODS1 * pound_1;
      goto bk;

    case 'b':
      printf("请输入你要购买甜菜的重量[磅]: ");
      pound_22 = a_pound();
      pound_2 += pound_22;
      money_2 = GOODS2 * pound_2;
      goto bk;
    case 'c':
      printf("请输入你要购买胡萝卜的重量[磅]: ");
      pound_33 = a_pound();
      pound_3 += pound_33;
      money_3 = GOODS3 * pound_3;
      goto bk;
    
    default:
      
      printf("输入有误请重新输入\n");
      goto bk;
    }
  }
  


    printf("您的购买信息为: \n");
  if ((pound_1 + pound_2 + pound_3) > 0)
  {
    if ((pound_1 + pound_2 + pound_3) <= 5)
      pfc = MONEY1;
    else if ((pound_1 + pound_2 + pound_3) <= 20)
      pfc = MONEY2;
    else
      pfc = MONEY2 + ((pound_1 + pound_2 + pound_3) - 20)*MONEY3;
  }
  else
    pfc = 0;

  if ((money_1 + money_2 + money_3) >= 100)
  {
    dis = (money_1 + money_2 + money_3 + pfc)*DIS;
    money = (money_1 + money_2 + money_3 + pfc) - dis;
  }
  else
  {
    dis = 0;
    money = money_1 + money_2 + money_3 + pfc;
  }
    
  if (pound_1 > 0)
    printf("洋蓟售价: $%.2f \t订购的重量: %.2f[磅] \t订购的费用: $%.2f\n", GOODS1, pound_1, money_1);
  if (pound_2 > 0)
    printf("甜菜售价: $%.2f \t订购的重量: %.2f[磅] \t订购的费用: $%.2f\n", GOODS2, pound_2, money_2);
  if (pound_3 > 0)
    printf("胡萝卜售价: $%.2f \t订购的重量: %.2f[磅] \t订购的费用: $%.2f\n", GOODS3, pound_3, money_3);
  printf("订单总费用: $%.2f \t运费和包装费: $%.2f \t折扣: -$%.2f\n应付费用总额: $%.2f\n",
    money_1 + money_2 + money_3, pfc, dis, money);

    printf("完成退出\n");

  system("pause");
  return 0;
}

char a_ch(void)
{
  int ch;
  ch = getchar();
  while (getchar() != '\n')
    continue;

  return ch;
}

double a_pound(void)
{
  double found_a, found_b;

  input:if (scanf_s("%lf", &found_a) == 1)
  {
    while (getchar() != '\n')
      continue;
    found_b = found_a;
  }
  else
  {
    while (getchar() != '\n')
      continue;
    printf("输入有误, 请重新输入: ");
    goto input;

  }

      return found_b;
}

 

 

 

 

版权说明:
点赞

发表评论

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

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

微信扫一扫打赏