神秘博客

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

1

//使用标准 I/O
#include <stdio.h>
#include <stdlib.h>  //提供 exit() 原型

int main()
{
  int ch;   //读取文件时, 储存每个字符的地方
  FILE *fp;   //文件指针
  errno_t err;
  char name[81];
  unsigned long count = 0;

  puts("请输入文件名: ");
  scanf_s("%s", name, 81);
  
  if ((err = fopen_s(&fp, name, "r")) != 0)
  {
    printf("无法打开 %s\n", name);
    exit(EXIT_FAILURE);
  }
  while ((ch = getc(fp)) != EOF)
  {
    putc(ch, stdout);  //与 putchar(ch); 相同
    if (ch != '\n')
      count++;
  }
  if (fclose(fp) != 0)
    fputs("关闭文件失败.\n", stderr);
  putchar('\n');
  printf("文件 %s 有 %u 个字符\n", name, count);

  system("pause");
  return 0;
}

2

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

int main(int argc, char *argv[])
{
  errno_t err;   // 用于fopen_s函数返回值,检测打开文件是否正确,如果打开成功返回0
  FILE *fp1, *fp2;  //文件指针
  char ch;
  long count, last;

  if (argc != 3)
  {
    fprintf(stderr, "用法: %s 原始文件名 拷贝文件名\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  else
  {
    if (strcmp(argv[1], argv[2]) == 0)
    {
      fputs("错误: 原始文件名与拷贝文件名相同\n", stderr);
      exit(EXIT_FAILURE);
    }

    if ((err = fopen_s(&fp1, argv[1], "rb")) != 0)
    {
      fprintf(stderr, "无法打开原始文件: %s\n", argv[1]);
      exit(EXIT_FAILURE);
    }
    if ((err = fopen_s(&fp2, argv[2], "wb")) != 0)
    {
      fprintf(stderr, "无法创建拷贝文件: %s\n", argv[2]);
      exit(EXIT_FAILURE);
    }

    fseek(fp1, 0L, SEEK_END);  //设置原始文件结尾处
    last = ftell(fp1);   //计算文件总字节数
    fseek(fp1, 0L, SEEK_SET);//回到文件开始处
    for (count = 0L; count < last; count++)
    {
      
      ch = getc(fp1);
      putc(ch, fp2);
    }

  }

  if (fclose(fp1) != 0)
    fputs("关闭原始文件失败.\n", stderr);
  if (fclose(fp2) != 0)
    fputs("关闭拷贝文件失败.\n", stderr);


  puts("拷贝完成");

  return 0;
}

3

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

int main()
{
  errno_t err;
  FILE *fp1, *fp2;
  char temp[] = "temp";
  char name[81];
  char ch;
  int num;


  puts("此程序用于把原来文本转换成大写");
  puts("请选择模式:");
  puts("1. 覆盖转换 \t2. 在末尾增加");
  while (scanf_s("%d", &num) != 1 || (num != 1 && num != 2))
  {
    fputs("输入错误, 请输入1或者2: ", stderr);
    while (getchar() != '\n')
      continue;
  }
    
  puts("请输入文件名: ");
  while (fscanf_s(stdin, "%80s", name, 81) != 1)
    fputs("输入错误, 请重新输入.\n", stderr);

  if (num == 1)
  {
    if ((err = fopen_s(&fp1, name, "r+")) != 0)
    {
      fprintf(stderr, "打开 %s 文件失败.\n", name);
      exit(EXIT_FAILURE);
    }
  }
  else if (num == 2)
  {
    if ((err = fopen_s(&fp1, name, "a+")) != 0)
    {
      fprintf(stderr, "打开 %s 文件失败.\n", name);
      exit(EXIT_FAILURE);
    }
    fseek(fp1, -1L, SEEK_END);
    ch = getc(fp1);
    if (ch != '\n')
    {
      ch = '\n';
      fseek(fp1, 0L, SEEK_END);
      putc(ch, fp1);
    }
    fseek(fp1, 0L, SEEK_SET);
  }
  


  if ((err = fopen_s(&fp2, temp, "w+")) != 0)
  {
    fprintf(stderr, "打开 %s 文件失败.\n", name);
    exit(EXIT_FAILURE);
  }
  while ((ch = getc(fp1)) != EOF)
  {
    ch = toupper(ch);
    putc(ch, fp2);
  }

  fseek(fp1, 0L, SEEK_SET);
  fseek(fp2, 0L, SEEK_SET);
  
  while ((ch = getc(fp2)) != EOF)
  {
    putc(ch, fp1);
  }

  if (fclose(fp1) != 0)
    fputs("关闭文件失败.\n", stderr);
  if (fclose(fp2) != 0)
    fputs("关闭缓存文件失败.\n", stderr);

  puts("完成");


  system("pause");
  return 0;
}

 

4

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

int main(int argc, char *argv[])
{
  errno_t err;
  FILE *fp;
  int i;
  //long last, j;
  char ch;

  if (argc < 2)
  {
    fprintf(stderr, "用法: %s 文件名\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  else
  {
    for (i = 1; i < argc; i++)
    {
      if ((err = fopen_s(&fp, argv[i], "r")) != 0)
      {
        fprintf(stderr, "无法打开 %s 文件.", argv[i]);
        exit(EXIT_FAILURE);
      }
      //方法1:
      fprintf(stdout, "文件 %s 内容: \n", argv[i]);
      while ((ch = getc(fp)) != EOF)
      {
        putc(ch, stdout);
      }
      //方法2:
      /*
      fseek(fp, 0L, SEEK_END);
      last = ftell(fp);
      fseek(fp, 0L, SEEK_SET);
      fprintf(stdout, "文件 %s 内容: \n", argv[i]);
      for (j = 0; j < last; j++)
      {
        ch = getc(fp);
        putc(ch, stdout);
      }
      */
      putchar('\n');
    }
    
  }

  puts("完成");

  return 0;
}

5

//倒序显示文件内容
#include <stdio.h>
#include <stdlib.h>
#define CNTL_Z '\032'  // DOS 文本文件中的结尾标记
#define SLEN 81

int main(int argc, char *argv[])
{
  char ch;
  FILE *fp;
  errno_t err;
  long count, last;

  if (argc != 2)
  {
    fprintf(stderr, "用法: %s 文件名\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  else
  {
    if ((err = fopen_s(&fp, argv[1], "rb")) != 0)  // rb 只读模式
    {
      printf("无法打开 %s\n", argv[1]);
      system("pause");
      exit(EXIT_FAILURE);
    }
    fseek(fp, 0L, SEEK_END);    //定位到文件末尾
    last = ftell(fp);
    for (count = 1L; count <= last; count++)
    {
      fseek(fp, -count, SEEK_END);  //回退
      ch = getc(fp);
      if (ch != CNTL_Z && ch != '\r')   //MS-DOS 文件
        putchar(ch);
    }
    putchar('\n');
    if (fclose(fp) != 0)
      fprintf(stderr, "关闭文件 %s 失败.", argv[1]);


  }





  return 0;
}

6

//把文件压缩成原来的 1/3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 40

int main()
{
  errno_t err;
  FILE *in, *out;  //声明2个指向FILE的指针
  char ch;
  char name[LEN];   //储存输出文件名
  int count = 0;

  puts("请输入要打开的文件名");
  fscanf_s(stdin, "%s", name, LEN);
  if ((err = fopen_s(&in, name, "rb")) != 0)
  {
    fprintf(stderr, "打开文件 %s 失败\n", name);
    exit(EXIT_FAILURE);
  }
  
  strcat_s(name, LEN, ".red");
  if ((err = fopen_s(&out, name, "wb")) != 0)
  {
    fprintf(stderr, "创建文件 %s 失败\n", name);
    exit(EXIT_FAILURE);
  }
  while ((ch = getc(in)) != EOF)
  {
    if (count % 3 == 0)
      putc(ch, out);
    count++;
  }

  if (fclose(in) != 0 || fclose(out) != 0)
    fputs("关闭文件失败\n", stderr);

  puts("完成");

  system("pause");
  return 0;
}

 

7

a.

#include <stdio.h>
#include <stdlib.h>
#define SLEN 81
#define SIZE 256

int main(int argc, char *argv[])
{
  errno_t err;
  FILE *fp1, *fp2;
  char name[SLEN];
  char line1[SIZE];
  char line2[SIZE];
  long last1, last2;
  char ch;

  if (argc == 3)
  {
    if ((err = fopen_s(&fp1, argv[1], "a+")) != 0)
    {
      fprintf(stderr, "打开文件 %s 失败.\n", argv[1]);
      exit(EXIT_FAILURE);
    }
    if ((err = fopen_s(&fp2, argv[2], "a+")) != 0)
    {
      fprintf(stderr, "打开文件 %s 失败.\n", argv[2]);
      exit(EXIT_FAILURE);
    }
  }
  else
  {
    puts("请输入第一个文件名:");
    fscanf_s(stdin, "%s", name, SLEN);
    if ((err = fopen_s(&fp1, name, "a+")) != 0)
    {
      fprintf(stderr, "打开文件 %s 失败.\n", name);
      exit(EXIT_FAILURE);
    }
    puts("请输入第二个文件名:");
    fscanf_s(stdin, "%s", name, SLEN);
    if ((err = fopen_s(&fp2, name, "a+")) != 0)
    {
      fprintf(stderr, "打开文件 %s 失败.\n", name);
      exit(EXIT_FAILURE);
    }
  }
  //检测文件末尾有没有换行符,如果没有则添加换行符
  fseek(fp1, -1L, SEEK_END);
  if ((ch = getc(fp1)) != '\n')
  {
    fseek(fp1, 0L, SEEK_END);
    ch = '\n';
    putc(ch, fp1);

  }
  fseek(fp2, -1L, SEEK_END);
  if ((ch = getc(fp2)) != '\n')
  {
    fseek(fp2, 0L, SEEK_END);
    ch = '\n';
    putc(ch, fp2);

  }
  //检测结束
  //检测文件总字节大小
  fseek(fp1, 0L, SEEK_END);
  last1 = ftell(fp1);
  fseek(fp2, 0L, SEEK_END);
  last2 = ftell(fp2);
  //检测结束

  //把文件恢复到开头
  fseek(fp1, 0L, SEEK_SET);
  fseek(fp2, 0L, SEEK_SET);

  if (last1 >= last2)
  {
    while (fgets(line1, SIZE, fp1) != NULL)
    {
      printf("文件1: ");
      fputs(line1, stdout);
      if (fgets(line2, SIZE, fp2) != NULL)
        {
          printf("文件2: ");
          fputs(line2, stdout);
        }			
    }
  }
  else
  {
    while (fgets(line2, SIZE, fp2) != NULL)
    {
      if (fgets(line1, SIZE, fp1) != NULL)
        {
          printf("文件1: ");
          fputs(line1, stdout);
        }
      printf("文件2: ");
      fputs(line2, stdout);
    }
  }



  if (fclose(fp1) != 0 || fclose(fp2) != 0)
    fputs("关闭文件失败.\n", stderr);

  puts("打印完成.");

  system("pause");
  return 0;
}

b

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SLEN 81
#define SIZE 256

int main(int argc, char *argv[])
{
  errno_t err;
  FILE *fp1, *fp2;
  char name[SLEN];
  char line1[SIZE];
  char line2[SIZE];
  long last1, last2;
  char ch;
  char *find;
  char *judge;

  if (argc == 3)
  {
    if ((err = fopen_s(&fp1, argv[1], "a+")) != 0)
    {
      fprintf(stderr, "打开文件 %s 失败.\n", argv[1]);
      exit(EXIT_FAILURE);
    }
    if ((err = fopen_s(&fp2, argv[2], "a+")) != 0)
    {
      fprintf(stderr, "打开文件 %s 失败.\n", argv[2]);
      exit(EXIT_FAILURE);
    }
  }
  else
  {
    puts("请输入第一个文件名:");
    fscanf_s(stdin, "%s", name, SLEN);
    if ((err = fopen_s(&fp1, name, "a+")) != 0)
    {
      fprintf(stderr, "打开文件 %s 失败.\n", name);
      exit(EXIT_FAILURE);
    }
    puts("请输入第二个文件名:");
    fscanf_s(stdin, "%s", name, SLEN);
    if ((err = fopen_s(&fp2, name, "a+")) != 0)
    {
      fprintf(stderr, "打开文件 %s 失败.\n", name);
      exit(EXIT_FAILURE);
    }
  }
  //检测文件末尾有没有换行符,如果没有则添加换行符
  fseek(fp1, -1L, SEEK_END);
  if ((ch = getc(fp1)) != '\n')
  {
    fseek(fp1, 0L, SEEK_END);
    ch = '\n';
    putc(ch, fp1);

  }
  fseek(fp2, -1L, SEEK_END);
  if ((ch = getc(fp2)) != '\n')
  {
    fseek(fp2, 0L, SEEK_END);
    ch = '\n';
    putc(ch, fp2);

  }
  //检测结束
  //检测文件总字节大小
  fseek(fp1, 0L, SEEK_END);
  last1 = ftell(fp1);
  fseek(fp2, 0L, SEEK_END);
  last2 = ftell(fp2);
  //检测结束

  //把文件恢复到开头
  fseek(fp1, 0L, SEEK_SET);
  fseek(fp2, 0L, SEEK_SET);

  if (last1 >= last2)
  {
    while (fgets(line1, SIZE, fp1) != NULL)
    {
      if ((judge = fgets(line2, SIZE, fp2)) != NULL)
      {
        find = strchr(line1, '\n');
        if (find)
          *find = '\0';
        printf("文件1: ");
        fputs(line1, stdout);
        printf("文件2: ");
        fputs(line2, stdout);

      }
      if (judge == NULL)
      {
        printf("文件1: ");
        fputs(line1, stdout);
      }
    }
  }
  else
  {
    while (fgets(line2, SIZE, fp2) != NULL)
    {
      if (fgets(line1, SIZE, fp1) != NULL)
        {
        find = strchr(line1, '\n');
        if (find)
          *find = '\0';
          printf("文件1: ");
          fputs(line1, stdout);
        }
      printf("文件2: ");
      fputs(line2, stdout);
    }
  }



  if (fclose(fp1) != 0 || fclose(fp2) != 0)
    fputs("关闭文件失败.\n", stderr);

  puts("打印完成.");

  system("pause");
  return 0;
}

8

#include <stdio.h>
#include <stdlib.h>
#define SLEN 81



int main(int argc, char *argv[])
{
  errno_t err;
  FILE *fp;
  char name[SLEN];
  int i = 2;
  int count;
  char ch;

  if (argc == 2)
  {
    if (argv[1][1] != '\0')
    {
      fprintf(stderr, "用法1: %s 字符\n", argv[0]);
      fprintf(stderr, "用法2: %s 字符 文件名\n", argv[0]);
      exit(EXIT_FAILURE);
    }
    count = 0;
    puts("请输入文件名: ");
    scanf_s("%s", name, SLEN);
    if ((err = fopen_s(&fp, name, "r")) != 0)
    {
      fprintf(stderr, "打开文件 %s 失败\n", name);
      exit(EXIT_FAILURE);
    }
    while ((ch = getc(fp)) != EOF)
    {
      if (ch == argv[1][0])
        count++;
    }
    fprintf(stdout, "文件 %s: %c %d\n", name, argv[1][0], count);
  }
  else if (argc > 2)
  {
    if (argv[1][1] != '\0')
    {
      fprintf(stderr, "用法1: %s 字符\n", argv[0]);
      fprintf(stderr, "用法2: %s 字符 文件名\n", argv[0]);
      exit(EXIT_FAILURE);
    }
    while (i < argc)
    {
      count = 0;
      if ((err = fopen_s(&fp, argv[i], "r")) != 0)
      {
        fprintf(stderr, "打开文件 %s 失败\n", argv[i]);
        i++;
        continue;
      }
      while ((ch = getc(fp)) != EOF)
      {
        if (ch == argv[1][0])
          count++;
      }
      fprintf(stdout, "文件 %s: %c %d\n", argv[i], argv[1][0], count);
      i++;
    }
  }
  else
  {
    fprintf(stderr, "用法1: %s 字符\n", argv[0]);
    fprintf(stderr, "用法2: %s 字符 文件名\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if (fclose(fp) != 0)
    fputs("错误: 关闭文件失败\n", stderr);
  puts("完成");

  return 0;
}

 

9

//使用 fprintf() fscanf() 和 rewind()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 41

int main()
{
  FILE *fp;
  errno_t err;
  char words[MAX];
  char num[MAX];
  char ch;
  int count;
  int i;

  if ((err = fopen_s(&fp, "words", "a+")) != 0)
  {
    fprintf(stdout, "无法打开 \"words\" 文件.\n");
    exit(EXIT_FAILURE);
  }
  if (fgets(words, MAX, fp) == NULL)
  {
    count = 1;
  }
  else
  {
    rewind(fp);
    while (fgets(words, MAX, fp) != NULL)
    {
      for (i = 0; words[i] != ':'; i++)
      {
        num[i] = words[i];
      }
      num[i] = '\0';
    }
    sscanf_s(num, "%d", &count);
    count++;
  }
  puts("输入文字添加到文件; 在一行开头按 # 键结束.");
  rewind(fp);
  while ((fscanf_s(stdin, "%40s", words, 41) == 1) && (words[0] != '#'))
  {
    fprintf(fp, "%d: %s\n", count, words);
    count++;
  }
    




  puts("文件内容:");
  rewind(fp);   //返回到文件开始处
  while ((ch = getc(fp)) != EOF)
    putc(ch, stdout);
    
  
  if (fclose(fp) != 0)
    fprintf(stderr, "关闭文件时出错\n");

  puts("完成");


  system("pause");
  return 0;
}

10

#include <stdio.h>
#include <stdlib.h>
#define SLEN 81
#define SIZE 256

int main()
{
  errno_t err;
  FILE *fp;
  char name[SLEN];
  int num;
  char words[SIZE];

  puts("请输入文件名:");
  scanf_s("%s", name, SLEN);
  if ((err = fopen_s(&fp, name, "r")) != 0)
  {
    fprintf(stderr, "打开文件 %s 失败\n", name);
    exit(EXIT_FAILURE);
  }



  puts("请输入你要查看文件位置[负数退出]: ");
  while (scanf_s("%d", &num) == 1 && num >= 0)
  {
    fseek(fp, num, SEEK_SET);
    if (fgets(words, SIZE, fp) != NULL)
    {
      if (words[0] == '\n' || words[0] == '\0')
        fputs("错误: 没有内容\n", stderr);
      else
        fputs(words, stdout);
    }
    else
      fputs("错误: 超过文本内容\n", stderr);
  }
  if (fclose(fp)!=0)
    fputs("错误: 关闭文件失败\n", stderr);
  puts("完成");

  system("pause");
  return 0;


}

11

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 256

int main(int argc, char *argv[])
{
  errno_t err;
  FILE *fp;
  char words[SIZE];

  if (argc != 3)
  {
    fprintf(stderr, "用法: %s 字符串 文件名\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  else
  {
    if ((err = fopen_s(&fp, argv[2], "r")) != 0)
    {
      fprintf(stderr, "错误: 打开文件 %s 失败\n", argv[2]);
      exit(EXIT_FAILURE);
    }
    while (fgets(words, SIZE, fp) != NULL)
    {
      if (strstr(words, argv[1]) != NULL)
        fputs(words, stdout);
    }
    if (fclose(fp) != 0)
      fputs("错误: 关闭文件失败\n", stderr);
  }

  puts("完成");

  return 0;
}

12

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SLEN 81
#define SIZE1 20
#define SIZE2 31


int main()
{
  errno_t err;
  FILE *fp;
  char name[SLEN];
  char num;
  char string[SIZE1][SIZE2];
  int i, j;
  i = 0;
  j = 0;

  puts("请输入文件名:");
  scanf_s("%s", name, SLEN);
  if ((err = fopen_s(&fp, name, "r+")) != 0)
  {
    fprintf(stderr, "错误: 无法打开 %s\n", name);
    exit(EXIT_FAILURE);
  }
  

  while ((num = getc(fp)) != EOF)
  {
    if (num == '\n' || num == '\0')
      continue;
    switch (num)
    {
    case '0':
      num = ' ';
      break;
    case '1':
      num = '.';
      break;
    case '2':
      num = '\'';
      break;
    case '3':
      num = ':';
      break;
    case '4':
      num = '~';
      break;
    case '5':
      num = '*';
      break;
    case '6':
      num = '=';
      break;
    case '8':
      num = '%';
      break;
    case '9':
      num = '#';
      break;
    default:
      break;
    }
    string[i][j] = num;
    j++;
    if (j == 30)
    {
      string[i][j] = '\0';
      i++;
      j = 0;
      if (i == SIZE1)
        break;
    }
    
  }
  rewind(fp);
  puts("转换输出:");
  for (i = 0; i < SIZE1; i++)
  {
    fputs(string[i], stdout);
    putchar('\n');
    fputs(string[i], fp);
    fputs("\n", fp);
  }

  if (fclose(fp) != 0)
    fputs("错误: 关闭文件失败\n", stderr);
  puts("完成");
  
  system("pause");
  return 0;
}

13

a.由于用的VS2017不支持VLA变长数组,用malloc()动态内存代替变长数组

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SLEN 81
#define LEN 256


int main()
{
  errno_t err;
  FILE *fp;
  char name[SLEN];
  char ch[LEN];
  char words[LEN];
  char num;
  char **string;
  int size1 = 0, size2 = 0;
  int i, j, n;
  i = 0;
  j = 0;

  puts("请输入文件名:");
  scanf_s("%s", name, SLEN);
  if ((err = fopen_s(&fp, name, "r+")) != 0)
  {
    fprintf(stderr, "错误: 无法打开 %s\n", name);
    exit(EXIT_FAILURE);
  }

  while ((num = getc(fp)) != '\n')
    size2++;
  size2++;
  rewind(fp);
  while (fgets(ch, LEN, fp) != NULL)
    size1++;
  rewind(fp);

  string = (char **)malloc(size1 * sizeof(char *));
  for (n = 0; n < size1; n++)
    string[n] = (char *)malloc(size2 + 1);
  
  while ((num = getc(fp)) != EOF)
  {
    if (num == '\n' || num == '\0')
      continue;
    switch (num)
    {
    case '0':
      num = ' ';
      break;
    case '1':
      num = '.';
      break;
    case '2':
      num = '\'';
      break;
    case '3':
      num = ':';
      break;
    case '4':
      num = '~';
      break;
    case '5':
      num = '*';
      break;
    case '6':
      num = '=';
      break;
    case '8':
      num = '%';
      break;
    case '9':
      num = '#';
      break;
    default:
      break;
    }
    words[j] = num;
    j++;
    if (j == size2-1)
    {
      words[j] = '\n';
      j++;
      words[j] = '\0';
      strcpy_s(string[i], strlen(words) + 1, words);
      i++;
      j = 0;
    }
    

  }
  rewind(fp);
  puts("转换输出:");
  for (i = 0; i < size1; i++)
  {
    fputs(string[i], stdout);
    fputs(string[i], fp);
  }
  free(string);
  if (fclose(fp) != 0)
    fputs("错误: 关闭文件失败\n", stderr);
  puts("完成");

  system("pause");
  return 0;
}

 

b.VLA变长数组版

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SLEN 81
#define LEN 256


void s_string(int n, int m, char str[n][m]);

int main()
{
  FILE *fp;
  char name[SLEN];
  char ch[LEN];
  char num;
  int size1 = 0, size2 = 0;
  int i, j;
  i = 0;
  j = 0;

  puts("请输入文件名:");
  scanf("%s", name);
  if ((fp = fopen(name, "r+")) == NULL)
  {
    fprintf(stderr, "错误: 无法打开 %s\n", name);
    exit(EXIT_FAILURE);
  }

  while ((num = getc(fp)) != '\n')
    size2++;
  size2++;
  size2++;
  rewind(fp);
  while (fgets(ch, LEN, fp) != NULL)
    size1++;
  rewind(fp);

  char string[size1][size2];

  while ((num = getc(fp)) != EOF)
  {
    if (num == '\n' || num == '\0')
      continue;
    switch (num)
    {
    case '0':
      num = ' ';
      break;
    case '1':
      num = '.';
      break;
    case '2':
      num = '\'';
      break;
    case '3':
      num = ':';
      break;
    case '4':
      num = '~';
      break;
    case '5':
      num = '*';
      break;
    case '6':
      num = '=';
      break;
    case '8':
      num = '%';
      break;
    case '9':
      num = '#';
      break;
    default:
      break;
    }
    string[i][j] = num;
    j++;
    if (j == size2-2)
    {
      string[i][j] = '\n';
      j++;
      string[i][j] = '\0';
      i++;
      j = 0;
    }

  }
  rewind(fp);
  puts("转换输出:");
  for (i = 0; i < size1; i++)
  {
    fputs(string[i], stdout);
    fputs(string[i], fp);
  }

  if (fclose(fp) != 0)
    fputs("错误: 关闭文件失败\n", stderr);
  puts("完成");


  return 0;
}

14

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SLEN 81
#define SIZE1 20
#define SIZE2 32
//消除失真
void compare(int str[][SIZE2], int n);

int main()
{
  errno_t err;
  FILE *fp;
  char name[SLEN];
  char num;
  int number[SIZE1][SIZE2];
  char string[SIZE1][SIZE2];
  int i, j, a, b;
  i = 0;
  j = 0;
  a = 0;
  b = 0;

  puts("请输入文件名:");
  scanf_s("%s", name, SLEN);
  if ((err = fopen_s(&fp, name, "r+")) != 0)
  {
    fprintf(stderr, "错误: 无法打开 %s\n", name);
    exit(EXIT_FAILURE);
  }
  while ((num = getc(fp)) != EOF)
  {
    if (num == '\n' || num == '\0')
      continue;
    sscanf_s(&num, "%d", &number[a][b]);
    b++;
    if (b == SIZE2 - 2)
    {
      b = 0;
      a++;
      if (a == SIZE1)
        break;
    }
  }

  puts("失真处理:");
  compare(number, SIZE1);
  rewind(fp);
  
  for (a = 0; a < SIZE1; a++)
  {
    for (b = 0; b < SIZE2-2; b++)
    {
      switch (number[i][j])
      {
      case 0:
        num = ' ';
        break;
      case 1:
        num = '.';
        break;
      case 2:
        num = '\'';
        break;
      case 3:
        num = ':';
        break;
      case 4:
        num = '~';
        break;
      case 5:
        num = '*';
        break;
      case 6:
        num = '=';
        break;
      case 8:
        num = '%';
        break;
      case 9:
        num = '#';
        break;
      default:
        break;
      }
      string[i][j] = num;
      j++;
      if (j == 30)
      {
        string[i][j] = '\0';
        i++;
        j = 0;
      }

    }
  }


    
  rewind(fp);
  puts("转换输出:");
  for (i = 0; i < SIZE1; i++)
  {
    fputs(string[i], stdout);
    putchar('\n');
    fputs(string[i], fp);
    fputs("\n", fp);
  }

  if (fclose(fp) != 0)
    fputs("错误: 关闭文件失败\n", stderr);
  puts("完成");

  system("pause");
  return 0;
}

void compare(int str[][SIZE2], int n)
{
  int i, j;
  int tot;
  double num;

  for (i = 0; i < n; i++)
  {
    for (j = 0; j < SIZE2-2; j++)
    {

      if (i == 0 && j == 0)
      {
        tot = 0;
        if (str[i][j] - str[i + 1][j] > 1 || str[i][j] - str[i + 1][j] < -1)
          tot++;
        if (str[i][j] - str[i][j + 1] > 1 || str[i][j] - str[i][j + 1] < -1)
          tot++;
        if (tot == 2)
        {
          num = (double)(str[i + 1][j] + str[i][j + 1]) / 2.0;
          str[i][j] = num - (int)num >= 0.5 ? (int)num + 1 : (int)num;
        }
      }
      else if (i == 0 && j > 0 && j < SIZE2 - 3)
      {
        tot = 0;
        if (str[i][j] - str[i][j - 1] > 1 || str[i][j] - str[i][j - 1] < -1)
          tot++;
        if (str[i][j] - str[i + 1][j] > 1 || str[i][j] - str[i + 1][j] < -1)
          tot++;
        if (str[i][j] - str[i][j + 1] > 1 || str[i][j] - str[i][j + 1] < -1)
          tot++;
        if (tot == 3)
        {
          num = (double)(str[i][j - 1] + str[i + 1][j] + str[i][j + 1]) / 3.0;
          str[i][j] = num - (int)num >= 0.5 ? (int)num + 1 : (int)num;
        }
      }
      else if (i == 0 && j == SIZE2 - 3)
      {
        tot = 0;
        if (str[i][j] - str[i][j - 1] > 1 || str[i][j] - str[i][j - 1] < -1)
          tot++;
        if (str[i][j] - str[i + 1][j] > 1 || str[i][j] - str[i + 1][j] < -1)
          tot++;
        if (tot == 2)
        {
          num = (double)(str[i + 1][j] + str[i][j - 1]) / 2.0;
          str[i][j] = num - (int)num >= 0.5 ? (int)num + 1 : (int)num;
        }
      }
      else if (i > 0 && j > 0 && i < n - 1 && j < SIZE2 - 3)
      {
        tot = 0;
        if (str[i][j] - str[i][j - 1] > 1 || str[i][j] - str[i][j - 1] < -1)
          tot++;
        if (str[i][j] - str[i + 1][j] > 1 || str[i][j] - str[i + 1][j] < -1)
          tot++;
        if (str[i][j] - str[i - 1][j] > 1 || str[i][j] - str[i - 1][j] < -1)
          tot++;
        if (str[i][j] - str[i][j + 1] > 1 || str[i][j] - str[i][j + 1] < -1)
          tot++;
        if (tot == 4)
        {
          num = (double)(str[i][j - 1] + str[i + 1][j] + str[i][j + 1] + str[i - 1][j]) / 4.0;
          str[i][j] = num - (int)num >= 0.5 ? (int)num + 1 : (int)num;
        }
      }
      else if (i > 0 && j == 0)
      {
        tot = 0;
        if (str[i][j] - str[i - 1][j] > 1 || str[i][j] - str[i - 1][j] < -1)
          tot++;
        if (str[i][j] - str[i + 1][j] > 1 || str[i][j] - str[i + 1][j] < -1)
          tot++;
        if (str[i][j] - str[i][j + 1] > 1 || str[i][j] - str[i][j + 1] < -1)
          tot++;
        if (tot == 3)
        {
          num = (double)(str[i][j + 1] + str[i + 1][j] + str[i - 1][j]) / 3.0;
          str[i][j] = num - (int)num >= 0.5 ? (int)num + 1 : (int)num;
        }
      }
      else if (i == n - 1 && j == 0)
      {
        tot = 0;
        if (str[i][j] - str[i - 1][j] > 1 || str[i][j] - str[i - 1][j] < -1)
          tot++;
        if (str[i][j] - str[i][j + 1] > 1 || str[i][j] - str[i][j + 1] < -1)
          tot++;
        if (tot == 2)
        {
          num = (double)(str[i][j + 1] + str[i - 1][j]) / 2.0;
          str[i][j] = num - (int)num >= 0.5 ? (int)num + 1 : (int)num;
        }
      }
      else if (i == n - 1 && j > 0 && j < SIZE2 - 3)
      {
        tot = 0;
        if (str[i][j] - str[i][j - 1] > 1 || str[i][j] - str[i][j - 1] < -1)
          tot++;
        if (str[i][j] - str[i - 1][j] > 1 || str[i][j] - str[i - 1][j] < -1)
          tot++;
        if (str[i][j] - str[i][j + 1] > 1 || str[i][j] - str[i][j + 1] < -1)
          tot++;
        if (tot == 3)
        {
          num = (double)(str[i][j - 1] + str[i][j + 1] + str[i - 1][j]) / 3.0;
          str[i][j] = num - (int)num >= 0.5 ? (int)num + 1 : (int)num;
        }
      }
      else if (i == n - 1 && j == SIZE2 - 3)
      {
        tot = 0;
        if (str[i][j] - str[i - 1][j] > 1 || str[i][j] - str[i - 1][j] < -1)
          tot++;
        if (str[i][j] - str[i][j - 1] > 1 || str[i][j] - str[i][j - 1] < -1)
          tot++;
        if (tot == 2)
        {
          num = (double)(str[i][j - 1] + str[i - 1][j]) / 2.0;
          str[i][j] = num - (int)num >= 0.5 ? (int)num + 1 : (int)num;
        }
      }
      else if (i > 0 && i < n - 1 && j == SIZE2 - 3)
      {
        tot = 0;
        if (str[i][j] - str[i - 1][j] > 1 || str[i][j] - str[i - 1][j] < -1)
          tot++;
        if (str[i][j] - str[i][j - 1] > 1 || str[i][j] - str[i][j - 1] < -1)
          tot++;
        if (str[i][j] - str[i + 1][j] > 1 || str[i][j] - str[i + 1][j] < -1)
          tot++;
        if (tot == 3)
        {
          num = (double)(str[i][j - 1] + str[i + 1][j] + str[i - 1][j]) / 3.0;
          str[i][j] = num - (int)num >= 0.5 ? (int)num + 1 : (int)num;
        }
      }
      
      printf("%d", str[i][j]);
      if (j == 29)
        putchar('\n');
    }
  }
}

 

 

 

 

 

版权说明:
点赞

发表评论

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

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

微信扫一扫打赏