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

1

#include <stdio.h>
#include <limits.h>
#include <string.h>
#define SLEN 33
//输入
char *s_gets(char *st, int n);
//二进制转换为int
void convert(char *st, int n, int *m);


int main()
{

  char ch[SLEN];
  int i;
  int total;

  puts("请输入二进制: ");
 bk:while (s_gets(ch, SLEN) != NULL && ch[0] != '\0')
  {
    total = 0;
    for (i = 0; ch[i] != '\0'; i++)
    {
      if (ch[i] != '0'&&ch[i] != '1')
      {
        fputs("错误: 二进制只能为 0 or 1, 请重新输入\n", stderr);
        goto bk;
      }
    }
    convert(ch, i-1, &total);

    printf("二进制: %s\n", ch);
    printf("10进制整数为: %d\n", total);
    puts("请输入二进制[空行退出]: ");
  }
  puts("完成");

  system("pause");
  return 0;
}

void convert(char *st, int n, int *m)
{
  int i;
  int j = 1;

  for (i = n; i >= 0; i--)
  {
    if (i < n)
      j *= 2;
    if (st[i] == '0')
      *m += 0 * j;
    else
      *m += 1 * j;
  }


}

char *s_gets(char *st, int n)
{
  char *ret_val, *find;

  ret_val = fgets(st, n, stdin);

  if (ret_val)
  {
    find = strchr(st, '\n');
    if (find)
      *find = '\0';
    else
      while (getchar() != '\n')
        continue;
  }

  return ret_val;
}

2

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>

//二进制转换为整数
int convert(char *st, int n);
//整数转换为二进制字符串
char *itobs(int n, char * ps, int m);

int main(int argc, char *argv[])
{
  char ch[CHAR_BIT * sizeof(int) + 1];
  int a, b;
  int i, j;

  if (argc != 3)
  {
    fprintf(stderr, "用法: %s 二进制字符串 二进制字符串\n", argv[0]);
    exit(EXIT_FAILURE);

  }
  for (i = 0; argv[1][i] != '\0'; i++)
  {
    if (argv[1][i] != '0'&&argv[1][i] != '1')
    {
      fputs("错误: 二进制只能为 0 or 1, 请重新输入\n", stderr);
      exit(EXIT_FAILURE);
    }
  }
  for (j = 0; argv[2][j] != '\0'; j++)
  {
    if (argv[2][j] != '0'&&argv[2][j] != '1')
    {
      fputs("错误: 二进制只能为 0 or 1, 请重新输入\n", stderr);
      exit(EXIT_FAILURE);
    }
  }
  a = convert(argv[1], i - 1);
  b = convert(argv[2], j - 1);
  printf("~%-32s = %s\n", argv[1], itobs(~a, ch, i));
  printf("~%-32s = %s\n", argv[2], itobs(~b, ch, j));
  printf("%s & %s = %s\n", argv[1], argv[2], itobs(a&b, ch, a > b ? a : b));
  printf("%s | %s = %s\n", argv[1], argv[2], itobs(a | b, ch, a > b ? a : b));
  printf("%s ^ %s = %s\n", argv[1], argv[2], itobs(a^b, ch, a > b ? a : b));

  puts("完成");

  return 0;
}
char *itobs(int n, char * ps, int m)
{
  int i, j = 0;
  static int size = CHAR_BIT * sizeof(int);

  for (i = size - 1; i >= 0; i--, n >>= 1)
  {
    if (j < m)
      ps[i] = (1 & n) + '0';
    else
      ps[i] = '0';
    j++;
  }
  ps[size] = '\0';

  return ps;
}

int convert(char *st, int n)
{
  int i;
  int j = 1;
  int tot = 0;

  for (i = n; i >= 0; i--)
  {
    if (i < n)
      j *= 2;
    if (st[i] == '0')
      tot += 0 * j;
    else
      tot += 1 * j;
  }

  return tot;
}

3

#include <stdio.h>
#include <limits.h>

int count(int n);

int main()
{
  int num;

  puts("请输入一个整数:");
  while (scanf_s("%d", &num) == 1)
  {
    printf("%d 打开位的数量 %d\n", num, count(num));
    puts("请输入下一个整数[q退出]:");
  }
  puts("退出");

  system("pause");
  return 0;
}
int count(int n)
{
  int i;
  int tot = 0;
  static int size = CHAR_BIT * sizeof(int);

  for (i = size; i > 0; i--, n >>= 1)
  {
    if ((1 & n) == 1)
      tot++;
  }

  return tot;
}

4

#include <stdio.h>
#include <limits.h>

//整数转二进制获取指定位的开关
int number(int n, int m);

int main()
{
  int n;
  int m;

  puts("请输入一个整数:");
  while (scanf_s("%d", &n) == 1)
  {
    puts("请输入需要查看的位:");
    scanf_s("%d", &m);
    printf("整数 %d 的 %d 位的值是 %d\n", n, m, number(n, m));
    puts("请输入一个整数[q退出]:");
  }

  puts("完成");

  system("pause");
  return 0;
}

int number(int n, int m)
{
  int i, j;
  int num;
  static int size = CHAR_BIT * sizeof(int);

  for (i = size, j = 0; i > 0; j++, i--, n >>= 1)
  {
    if (j == m)
    {
      num = 1 & n;
      break;
    }
      
  }

  return num;
}

5

#include <stdio.h>
#include <limits.h>

void str(char *st, unsigned int n, unsigned int m);

int main()
{
  char ch[CHAR_BIT * sizeof(int) + 1];
  unsigned int x, y;

  puts("请输入一个无符号整数:");
  while (scanf_s("%ud", &x) == 1)
  {
    puts("请输入需要移动的位:");
    scanf_s("%ud", &y);
    str(ch, x, y);
    printf("%s\n", ch);
    puts("请输入一个无符号整数[q退出]:");
  }
  puts("完成");

  system("pause");
  return 0;
}

void str(char *st, unsigned int n, unsigned int m)
{
  int i;
  unsigned int tot;
  static int size = CHAR_BIT * sizeof(int);
  
  m %= size;
  tot = (n >> (size - m)) | (n << m);
  

  for (i = size - 1; i >= 0; i--, tot >>= 1)
  {
    st[i] = (1 & tot) + '0';
      
  }
  st[size] = '\0';

}

6

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

//菜单
void menu(struct font *pst);
//丢失多余字符
void d_stdin();
//功能
int function(struct font *pst, char ch);

struct font
{
  unsigned int id : 8;
  unsigned int size : 7;
  unsigned int : 1;
  unsigned int aline : 2;
  bool bold : 1;
  bool italic : 1;
  bool underline : 1;
  unsigned int : 3;
};

char *option1[] = { "off","on" };
char *option2[] = { "left","center","right" };

int main()
{
  char ch;
  struct font list = { 1,12,0,false,false,false };
  
  menu(&list);
  while (scanf_s("%c", &ch, 1) == 1 && ch != 'q')
  {
    if (ch < 'a' || ch > 'z')
    {
      d_stdin();
      printf("错误: 请输入菜单正确的选项\n");
      continue;
    }
    if (function(&list, ch) == 0)
    {
      d_stdin();
      printf("错误: 请输入菜单正确的选项\n");
      continue;
    }
    putchar('\n');
    menu(&list);
    d_stdin();
  }


  system("pause");
  return 0;
}
int function(struct font *pst, char ch)
{
  int n = 0;
  int size;
  char a;

  switch (ch)
  {
  case 'f':
    puts("请输入字体的大小[0-255]:");
    scanf_s("%d", &size);
    pst->id = size & 255;
     n = 1;
     break;
  case 's':
    puts("请输入字体的大小[0-127]:");
    scanf_s("%d", &size);
    pst->size = size & 127;
    n = 1;
    break;
  case 'a':
    d_stdin();
    puts("请选择对齐方式:");
    puts("l. left \tc. center \tr. right");
    scanf_s("%c", &a, 1);
    if (a == 'l')
      pst->aline = 0;
    else if (a == 'c')
      pst->aline = 1;
    else if (a == 'r')
      pst->aline = 2;
    else
      puts("错误: 输入错误不做任何修改");
    n = 1;
    break;
  case 'b':
    pst->bold = true;
    n = 1;
    break;
  case 'i':
    pst->italic = true;
    n = 1;
    break;
  case 'u':
    pst->underline = true;
    n = 1;
    break;
  default:
    break;
  }

  return n;
}

void menu(struct font *pst)
{

  printf("ID  SIZE  ALIGNMENT   B   I   U\n");
  printf("%-3d %3d %8s     %-3s %-3s %-3s\n", pst->id, pst->size, option2[pst->aline], option1[pst->bold], option1[pst->italic], option1[pst->underline]);

  puts("f. 更改字体 \ts. 更改大小 \ta. 更改对齐");
  puts("b. 切换粗体 \ti. 切换斜体 \tu. 切换下划线");
  puts("q. 退出");

}

void d_stdin()
{
  while (getchar() != '\n')
    continue;
}

7

#include <stdio.h>
#define ID 0x1
#define SIZE 0xC00
#define SIZE_MAX 0x7f
#define ALINE_L 0x00000
#define ALINE_C 0x10000
#define ALINE_R 0x20000
#define ALINE_MAX 0x3
#define BOLD 0x40000
#define BOLD_MAX 0x1
#define ITALIC 0x80000
#define ITALIC_MAX 0x1
#define UNDERLINE 0x100000
#define UNDERLINE_MAX 0x1
typedef unsigned long font;
//菜单
void menu(font *pst);
//清空输入行
void d_stdin();
//功能
int function(font *pst, char ch);

char *option1[] = { "off","on" };
char *option2[] = { "left","center","right" };

int main()
{
  font list;
  list = ID | SIZE ;
  char ch;

  menu(&list);
  while (scanf_s("%c", &ch, 1) == 1 && ch != 'q')
  {
    if (ch < 'a' || ch > 'z')
    {
      d_stdin();
      printf("错误: 请输入菜单正确的选项\n");
      continue;
    }
    if (function(&list, ch) == 0)
    {
      d_stdin();
      printf("错误: 请输入菜单正确的选项\n");
      continue;
    }
    putchar('\n');
    menu(&list);
    d_stdin();
  }


  system("pause");
  return 0;
}

int function(font *pst, char ch)
{
  font temp1, temp2, temp3;
  int n = 0;
  int size;
  char a;
  switch (ch)
  {
  case 'f':
    puts("请输入字体的大小[0-255]:");
    scanf_s("%d", &size);
    temp1 = *pst >> 8;
    *pst &= temp1 << 8;
    *pst |= size & 255;
    n = 1;
    break;
  case 's':
    puts("请输入字体的大小[0-127]:");
    scanf_s("%d", &size);
    temp1 = *pst >> 16;
    temp2 = *pst << 24;
    temp3 = (temp1 << 16) | (temp2 >> 24);
    *pst &= temp3;
    *pst |= (size & 127) << 8;
    n = 1;
    break;
  case 'a':
    d_stdin();
    puts("请选择对齐方式:");
    puts("l. left \tc. center \tr. right");
    scanf_s("%c", &a, 1);
    temp1 = *pst >> 18;
    temp2 = *pst << 16;
    temp3 = (temp1 << 18) | (temp2 >> 16);
    *pst &= temp3;
    if (a == 'l')
      *pst |= ALINE_L;
    else if (a == 'c')
      *pst |= ALINE_C;
    else if (a == 'r')
      *pst |= ALINE_R;
    else
      puts("错误: 输入错误不做任何修改");
    n = 1;
    break;
  case 'b':
    temp1 = *pst >> 19;
    temp2 = *pst << 14;
    temp3 = (temp1 << 19) | (temp2 >> 14);
    *pst &= temp3;
    *pst |= BOLD;
    n = 1;
    break;
  case 'i':
    temp1 = *pst >> 20;
    temp2 = *pst << 13;
    temp3 = (temp1 << 20) | (temp2 >> 13);
    *pst &= temp3;
    *pst |= ITALIC;
    n = 1;
    break;
  case 'u':
    temp1 = *pst >> 21;
    temp2 = *pst << 12;
    temp3 = (temp1 << 21) | (temp2 >> 12);
    *pst &= temp3;
    *pst |= UNDERLINE;
    n = 1;
    break;
  default:
    break;
  }
  return n;
}

void menu(font *pst)
{
  printf("ID  SIZE  ALIGNMENT   B   I   U\n");
  printf("%-3d %3d %8s     %-3s %-3s %-3s\n", *pst & 255, *pst >> 8 & SIZE_MAX, option2[*pst >> 16 & ALINE_MAX], option1[*pst >> 18 & BOLD_MAX], option1[*pst >> 19 & ITALIC_MAX], option1[*pst >> 20 & UNDERLINE_MAX]);
  puts("f. 更改字体 \ts. 更改大小 \ta. 更改对齐");
  puts("b. 切换粗体 \ti. 切换斜体 \tu. 切换下划线");
  puts("q. 退出");
}
void d_stdin()
{
  while (getchar() != '\n')
    continue;
}

 

 

 

版权说明:
点赞

发表评论

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

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

微信扫一扫打赏