神秘博客

C Primer Plus [第六版]第13章复习题答案

4

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

int main(int argc, char *argv[])
{
  double num;
  long pos;
  double tot = 0;
  int i = 0, j;
  FILE *fp;
  errno_t err;
  char name[80];

  if (argc == 1)
  {
    puts("请输入文件名: ");
    scanf_s("%s", name, 80);
    if ((err = fopen_s(&fp, name, "wb+")) != 0)
    {
      fprintf(stderr, "无法打开或创建 %s 文件\n", name);
      exit(EXIT_FAILURE);
    }
  }
  else if (argc == 2)
  {
    if ((err = fopen_s(&fp, argv[1], "wb+")) != 0)
    {
      fprintf(stderr, "无法打开或创建 %s 文件\n", name);
      exit(EXIT_FAILURE);
    }
  }
  else
  {
    fprintf(stderr, "用法: %s 文件名\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  
  puts("请输入浮点数: ");
  while (scanf_s("%lf", &num) == 1)
  {
    i++;
    fwrite(&num, sizeof(double), 1, fp);
    tot += num;
    puts("请输入下一个浮点数[q退出]: ");
  }
  if (i == 0)
  {
    fputs("你没有输入内容, 再见.\n", stderr);
    if (fclose(fp) != 0)
      fputs("关闭文件失败\n", stderr);
    exit(EXIT_FAILURE);
  }
  else
  {
    puts("你输入的内容: ");
    for (j = 0; j < i; j++)
    {
      pos = (long)j * sizeof(double);
      fseek(fp, pos, SEEK_SET);
      fread(&num, sizeof(double), 1, fp);
      printf("%g\n", num);
    }

    printf("\n平均值为: %g / %d = %g\n", tot, i, (double)tot / i);

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

  
  return 0;

}

5

方法一

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

int find(char ch, char *str, int n);

int main(int argc,char *argv[])
{
  errno_t err;
  FILE *fp;
  char ch = 0;
  char words[256];
  int i, judge;

  if (argc != 3 || argv[1][1] != '\0')
  {
    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 (ch != EOF)
    {
      
      for (i = 0; (ch = getc(fp)) != EOF && ch != '\n'; i++)
      {
        words[i] = ch;
      }
      words[i] = '\0';
      judge = find(argv[1][0], words, 256);
      if (judge > 0)
        puts(words);
    }
  }

  if (fclose(fp) != 0)
    fputs("关闭文件失败.", stderr);

  puts("完成");

  
  return 0;

}

int find(char ch, char *str, int n)
{
  int i, judge = 0;

  for (i = 0; str[i] != '\0'; i++)
  {
    if (str[i] == ch)
      judge++;
  }

  return judge;
}

方法二

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

int find(char ch, char *str);

int main(int argc, char *argv[])
{
  errno_t err;
  FILE *fp;
  char ch = 0;
  char words[256];

  if (argc != 3 || argv[1][1] != '\0')
  {
    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, 256, fp) != NULL)
    {
      if (find(argv[1][0], words))
        fputs(words, stdout);
    }
  }

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


  return 0;

}

int find(char ch, char *str)
{
  int judge = 0;

  while (*str != '\0')
  {
    if (*str == ch)
      judge++;
    str++;
  }

  return judge;
}

 

 

版权说明:
点赞

发表评论

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

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

微信扫一扫打赏