神秘博客

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

1

/*
编写一个程序,创建一个包含26个元素的数组,并在其中储存26个小写字母。
然后打印数组的所有内容
*/
#include <stdio.h>
#define SIZE 26

int main()
{
  char ch[26];
  int index;

  for (index = 0; index < SIZE; index++)
  {
    ch[index] = 'a' + index;
    printf("%c ", ch[index]);
  }
  printf("\n");

  system("pause");
  return 0;
  
}

2

#include <stdio.h>
#define SIZE 5

int main()
{
  int x, y;

  for (x = 1; x <= SIZE; x++)
  {
    for (y = 1; y <= x; y++)
      printf("$");
    printf("\n");
  }

  system("pause");
  return 0;
}

3

#include <stdio.h>
#define SIZE 6

int main()
{
  int x, y;
  char ch[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  int index;

  for (x = 1; x <= SIZE; x++)
  {
    for (y = 1, index = 6; y <= x; y++)
      printf("%c", ch[index - y]);
    printf("\n");
  }

  system("pause");
  return 0;
}

4

#include <stdio.h>
#define SIZE 6

int main()
{
  int x, y, index = 0;
  char ch[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

  for (x = 0; x < SIZE; x++)
  {
    for (y = 0; y <= x; y++)
      printf("%c", ch[index++]);
    printf("\n");
  }

  system("pause");
  return 0;
}

5

这个题目还真拦住我了,循环太多搞懵了,钻研了好久,终于搞懂了,后面还写了一个输入大写字母以倒金字塔格式打印字母

/*
编写一个程序,提示用户输入大写字母。
使用嵌套循环以下面金字塔型的格式打印字母。
*/
#include <stdio.h>

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

  printf("请输入一个大写字母: ");
  scanf_s("%c", &ch);

  for (x = 0; x <= (ch - 'A'); x++)
  {
    char temp = 'A' - 1;
    for (y = 0; y < ((ch-'A') - x); y++)
    {
      printf(" ");
    }
    for (y = 0; y <= x; y++)
    {
      printf("%c", ++temp);
    }
    for (y = 0; y < x; y++)
    {
      printf("%c", --temp);
    }

    printf("\n");
  }

  system("pause");
  return 0;
}
/*
编写一个程序,提示用户输入大写字母。
使用嵌套循环以倒金字塔型的格式打印字母。
*/
#include <stdio.h>

int main()
{
  int x, y;
  char ch;
  char index;
  printf("请输入一个大写字母: ");
  scanf_s("%c", &ch);

  for (x = 0; x <= (ch - 'A'); x++)
  {
    index = 'A'-1;
    for (y = x; y > 0; y--)
    {
      printf(" ");
    }
    for (y = ch - 'A'; y >= x; y--)
    {
      printf("%c", ++index);
    }
    
    for (y = ch - 'A'; y > x; y--)
    {
      printf("%c", --index);
    }
    
    printf("\n");
  }

  system("pause");
  return 0;
}

6

/*
编写一个程序打印一个表格,每一行打印一个整数,该数的平方,立方。
要求用户输入表格的上下限。使用一个for循环。
*/
#include <stdio.h>

int main()
{
  int x, y, z;
  int a, b;

  printf("请输入上限: ");
  scanf_s("%d", &a);
  printf("请输入下限: ");
  scanf_s("%d", &b);

  printf("整数   平方   立方\n");
  for (x = a; x <= b; x++)
  {
    y = x * x;
    z = x * x * x;
    printf("%d %7d %7d\n", x, y, z);
  }
  


  system("pause");
  return 0;
}

7

/*
编写一个程序把一个单词读入一个字符数组中,然后倒序打印这个单词。
*/
#include <stdio.h>
#include <string.h>

int main()
{
  char ch[20];
  int i, j, size;

  printf("请输入一个单词: ");
  scanf("%s", ch);
  i = strlen(ch);
  for (size = 0; size < i; size++)
  {
    j = i - size - 1;
    printf("%c", ch[j]);
    
  }
  
  
  printf("\n");

  system("pause");
  return 0;
}

8

/*
编写一个程序,要求用户输入两个浮点数,并打印两数之差除以两数乘积的结果。
在用户输入非数字之前,程序应该循环处理用户输入的每对值。
*/
#include <stdio.h>

int main()
{
  double x, y, a, b;

  printf("请输入两组浮点数: ");
  while (scanf_s("%lf %lf", &x,&y) == 2)
  {
    a = x - y;
    b = x * y;
    printf("(%g - %g) / (%g * %g) = %g\n", x, y, x, y, a / b);
    printf("请输入两组浮点数: ");
  }

  system("pause");
  return 0;
}

9

/*
编写一个程序,要求用户输入两个浮点数,并打印两数之差除以两数乘积的结果。
在用户输入非数字之前,程序应该循环处理用户输入的每对值。
*/
#include <stdio.h>

double index(double a,double b);

int main()
{
  double x, y, f;

  printf("请输入两组浮点数: ");
  while (scanf_s("%lf %lf", &x, &y) == 2)
  {
    f = index(x, y);
    printf("(%g - %g) / (%g * %g) = %g\n", x, y, x, y, f);
    printf("请输入两组浮点数: ");
  }

  system("pause");
  return 0;
}

double index(double a, double b)
{
  double i, j, z;
  i = a - b;
  j = a * b;
  z = i / j;

  return z;
}

10

#include <stdio.h>

int main()
{
  int x, y, i, j;

  printf("请输入下限整数和上限整数: ");
  scanf_s("%d%d", &x, &y);
  while (y - x > 0)
  {
    for (i = x, j = 0; i <= y; i++)
      j += i * i;
    printf("%d to %d is %d\n", x*x, y*y, j);
    printf("请输入上限整数和下限整数: ");
    scanf_s("%d%d", &x, &y);
  }
  system("pause");
  return 0;
}

11

#include <stdio.h>

int main()
{
  int index[8];
  int i, j;

  printf("请输入8组整数: ");
  
  for (j = 0; j < 8; j++)
  {
    scanf_s("%d", &index[j]);
  }
  for (i = 7; i >= 0; i--)
  {
    printf("%d ", index[i]);
  }
  printf("\n");

  system("pause");
  return 0;
}

12

#include <stdio.h>

int main()
{
  double j, z;
  int x, i;

  printf("请输入运行多少项: ");
  scanf_s("%d", &x);
  while (x > 0)
  {
    j = 0.0;
    z = 0.0;
    for ( i = 1; i <= x; i++)
    {
      j += 1.0 / i;
      if (i % 2 == 1)
      {
        z += 1.0 / i;
      }
      else
      {
        z -= 1.0 / i;
      }
    }
    printf("1.0 + 1.0/2.0 + 1.0/3.0 + 1.0/4.0 + ~ 1.0/%d.0 = %g\n", x, j);
    printf("1.0 - 1.0/2.0 + 1.0/3.0 - 1.0/4.0 + ~ 1.0/%d.0 = %g\n", x, z);
    printf("请输入运行多少项: ");
    scanf_s("%d", &x);
    getchar();
  }
  
  system("pause");
  return 0;
}

13

#include <stdio.h>
#define SIZE 8

int main()
{
  int i[SIZE];
  int x;
  int	y = 1;

  
  for (x = 0; x < SIZE; x++)
  {
    i[x] = y;
    y *= 2;
  }
  
  x = 0;
  do
  {
    printf("%d ", i[x]);
    x++;
  } while (x < SIZE);
  printf("\n");

  system("pause");
  return 0;


}

 

14

#include <stdio.h>
#define SIZE 8

int main()
{
  double i[SIZE], j[SIZE];
  int x, y;

  printf("请输入8组浮点数: ");
  for (x = 0; x < SIZE; x++)
    scanf_s("%lf", &i[x]);
  for (y = 1, j[0] = i[0]; y < SIZE; y++)	
    j[y] = j[y - 1] + i[y];
  
  for (x = 0; x < SIZE; x++)
    printf("%10g ", i[x]);
  printf("\n");
  for (x = 0; x < SIZE; x++)
    printf("%10g ", j[x]);
  printf("\n");
  
  system("pause");
  return 0;
}

15

#include <stdio.h>
#define SIZE 255

int main()
{
  char ch[SIZE];
  int i, j;
  i = -1;
  printf("请输入内容: \n");
  do
  {
    i++;
    scanf_s("%c", &ch[i]);
  } while (ch[i] != '\n');
  
  printf("倒序打印内容: \n");
  for (j = i - 1; j >= 0; j--)
  {
    printf("%c", ch[j]);
  }
  printf("\n");

  system("pause");
  return 0;
}

16

#include <stdio.h>
#define M 100
#define I 0.1
#define J 0.05

int main()
{
  int years;
  double a, b;
  a = M;
  b = M;

  for (years = 0; a >= b; years++)
  {
    a += M * I;
    b += b * J;
  }

  printf("%d 年之后 Deirdre 的投资额会超过 Daphne.\n", years);
  printf("Daphne: %g     Deirdre: %g\n", a, b);

  system("pause");
  return 0;
}

17

#include <stdio.h>
#define M 0.08
#define Q 1.0E+6

int main()
{
  int years;
  double i;
  i = Q;

  for (years = 0; i >= 0; years++)
  {
    i += i * M - 1.0E+5;
  }
  printf("%d 年后 Chuckie 会取完账户的钱. \n", years);

  system("pause");
  return 0;
}

18

#include <stdio.h>
#define F 5
int main()
{
  int i, j;

  j = F;
  for (i = 1; j <= 150; i++)
  {
    j = (j - i) * 2;
    printf("Rabnud 博士第 %d 周朋友有 %d 人.\n", i, j);
  }

  system("pause");
  return 0;
}

 

版权说明:
点赞

发表评论

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

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

微信扫一扫打赏