神秘博客

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

1

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

int days_s(char *st);

struct month {
  char month_a[20];
  char month_b[3];
  int days;
  int month_c;
};

struct month months[12] = {
  { "january","jan",31,1 },
{ "february","feb",28,2 },
{ "march","mar",31,3 },
{ "april","apr",30,4 },
{ "may","may",31,5 },
{ "june","jun",30,6 },
{ "july","jul",31,7 },
{ "august","aug",31,8 },
{ "september","sep",30,9 },
{ "october","oct",31,10 },
{ "november","nov",30,11 },
{ "december","dec",31,12 }

};



int main()
{
  int i;
  int count = 0;
  char ch[20];

  bk:puts("请输入月份");
  scanf_s("%s", ch, 20);
  for (i = 0; i < 12; i++)
  {
    if (!strcmp(months[i].month_a, ch))
      count++;
  }
  if (count == 0)
  {
    puts("输入错误, 您输入的不是正确的月份.");
    goto bk;
  }
  printf("january - %s 月总计 %d 天.\n", ch, days_s(ch));

  system("pause");
  return 0;
}
int days_s(char *st)
{
  int i;
  int count = 0;


  for (i = 0; i < 12; i++)
  {
    count += months[i].days;
    if (!strcmp(months[i].month_a, st))
      break;
  }

  return count;
}

2

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

void days_s(void);

struct month {
  char month_a[20];
  char month_b[3];
  int days;
  int month_c;
};

struct month months[12] = {
  { "january","jan",31,1 },
{ "february","feb",28,2 },
{ "march","mar",31,3 },
{ "april","apr",30,4 },
{ "may","may",31,5 },
{ "june","jun",30,6 },
{ "july","jul",31,7 },
{ "august","aug",31,8 },
{ "september","sep",30,9 },
{ "october","oct",31,10 },
{ "november","nov",30,11 },
{ "december","dec",31,12 }

};



int main()
{
  days_s();

  system("pause");
  return 0;
}
void days_s(void)
{
  int i;
  int tot = 0;
  int count = 0;
  char ch[20];
  int day;
  int month_s;
  int year;

bk1:puts("请输入日: ");
  while (scanf_s("%d", &day) != 1)
  {
    puts("输入错误, 请输入1-31:");
  }
  if (day < 1 || day>31)
  {
    puts("错误: 请输入1-31:");
    goto bk1;
  }
bk2:puts("请输入月份:");
  scanf_s("%s", ch, 20);
  for (i = 0; i < 12; i++)
  {
    if (!strcmp(months[i].month_a, ch))
      tot = 1;
    else if (!strcmp(months[i].month_b, ch))
      tot = 2;
    else if (ch[0] >= '0'&&ch[0] <= '9')
    {
      sscanf_s(ch, "%d", &month_s);
      tot = 3;
    }
  }
  if (tot == 0)
  {
    puts("错误: 请输入1-12月, 英文月份, 或者英文月份缩写.");
    goto bk2;
  }
  puts("请输入年份: ");
  scanf_s("%d", &year);

  for (i = 0; i < 12; i++)
  {
    if (tot == 1)
    {
      if (!strcmp(months[i].month_a, ch))
        break;
    }
    else if (tot == 2)
    {
      if (!strcmp(months[i].month_b, ch))
        break;
    }
    else if (tot == 3)
    {
      if (months[i].month_c == month_s)
        break;
    }
    count += months[i].days;
  }
  count += day;
  if (tot == 1)
  {
    printf("%d / january 月 1 日 - %d / %s 月 %d 日 总计 %d 天.\n", year, year, ch, day, count);
  }
  else if (tot == 2)
  {
    printf("%d / jan 月 1 日 - %d / %s 月 %d 日 总计 %d 天.\n", year, year, ch, day, count);
  }
  else if (tot == 3)
  {
    printf("%d / 1 月 1 日 - %d / %d 月 %d 日 总计 %d 天.\n", year, year, month_s, day, count);
  }


}

3

#include <stdio.h>
#include <string.h>
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 100
//输入
char *s_gets(char *st, int n);
//以标题首字母升序排序
void title_s(struct book *pst[], int n);
//以价格升序排序
void money(struct book *pst[], int n);

struct book {
  char title[MAXTITL];
  char author[MAXAUTL];
  float value;
};

int main()
{
  struct book library[MAXBKS];
  struct book *pst1[MAXBKS];
  struct book *pst2[MAXBKS];
  int count = 0;
  int index;

  printf("请输入书名: \n");
  printf("按一行开始处按[enter]停止.\n");
  while (count < MAXBKS&&s_gets(library[count].title, MAXTITL) != NULL && library[count].title[0] != '\0')
  {
    printf("现在输入作者: \n");
    s_gets(library[count].author, MAXAUTL);
    puts("现在输入价格: ");
    scanf_s("%f", &library[count].value);
    pst1[count] = &library[count];
    pst2[count] = &library[count];
    count++;
    while (getchar() != '\n')
      continue;
    if (count < MAXBKS)
      puts("输入下一个标题: ");
  }
  if (count > 0)
  {
    puts("书籍列表[输入升序]:");
    for (index = 0; index < count; index++)
      printf("%s by %s: $%.2f\n", library[index].title, library[index].author, library[index].value);
    title_s(pst1, count);
    money(pst2, count);
  }
  else
    puts("没有书? 太糟糕了.");

  system("pause");
  return 0;
}

void money(struct book *pst[], int n)
{
  int i, j;
  struct book *temp;

  for (i = 0; i < n; i++)
  {
    for (j = i; j < n; j++)
    {
      if (pst[i]->value > pst[j]->value)
      {
        temp = pst[i];
        pst[i] = pst[j];
        pst[j] = temp;
      }

    }
  }
  puts("书籍列表[价格升序]:");
  for (i = 0; i < n; i++)
    printf("%s by %s: $%.2f\n", pst[i]->title, pst[i]->author, pst[i]->value);
}

void title_s(struct book *pst[], int n)
{
  int i, j;
  struct book *temp;

  for (i = 0; i < n; i++)
  {
    for (j = i; j < n; j++)
    {
      if (pst[i]->title[0] > pst[j]->title[0])
      {
        temp = pst[i];
        pst[i] = pst[j];
        pst[j] = temp;
      }
        
    }
  }
  puts("书籍列表[首字母升序]:");
  for (i = 0; i < n; i++)
    printf("%s by %s: $%.2f\n", pst[i]->title, pst[i]->author, pst[i]->value);
}
char *s_gets(char *st, int n)
{
  char *ret_val;

  ret_val = fgets(st, n, stdin);

  if (ret_val)
  {
    while (*st != '\n'&&*st != '\0')
      st++;
    if (*st == '\n')
      *st = '\0';
    else
      while (getchar() != '\n')
        continue;
  }

  return ret_val;
}

4

a

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

struct namect
{
  char name_a[20];
  char name_b[20];
  char name_c[10];
};

typedef struct s_name
{
  int num;
  struct namect name;
} name;

void show(name *pst[], int n);

//输入
char *s_gets(char *st, int n);

int main()
{
  name list[5];
  name *ps1[5];
  int i = 0;

  puts("请输入名: ");
  while (s_gets(list[i].name.name_a, 20) != NULL && list[i].name.name_a[0] != '\0')
  {
    puts("请输入中间名[按Enter跳过]: ");
    s_gets(list[i].name.name_b, 20);
    puts("请输入姓: ");
    scanf_s("%s", list[i].name.name_c, 10);
    puts("请输入社会保险号:");
    scanf_s("%d", &list[i].num);
    ps1[i] = &list[i];
    i++;
    while (getchar() != '\n')
      continue;
    if (i == 5)
      break;
    puts("请输入名[按Enter退出]: ");
  }
  if (i > 0)
    show(ps1, i);
  else
    puts("没有输入内容, 结束.");
  
  system("pause");
  return 0;
}

void show(name *pst[], int n)
{
  int i;

  for (i = 0; i < n; i++)
  {
    if (pst[i]->name.name_b[0] == '\0')
      printf("%s, %s -- %d\n", pst[i]->name.name_a, pst[i]->name.name_c, pst[i]->num);
    else
      printf("%s, %s %c. -- %d\n", pst[i]->name.name_a, pst[i]->name.name_b, pst[i]->name.name_c[0], pst[i]->num);
  }
}

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

b

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

struct namect
{
  char name_a[20];
  char name_b[20];
  char name_c[10];
};

typedef struct s_name
{
  int num;
  struct namect name;
} name;
//打印内容
void show(char *st1, char *st2, char *st3, int n);

//输入
char *s_gets(char *st, int n);

int main()
{
  name list[5];
  int i = 0;
  int j;

  puts("请输入名: ");
  while (s_gets(list[i].name.name_a, 20) != NULL && list[i].name.name_a[0] != '\0')
  {
    puts("请输入中间名[按Enter跳过]: ");
    s_gets(list[i].name.name_b, 20);
    puts("请输入姓: ");
    scanf_s("%s", list[i].name.name_c, 10);
    puts("请输入社会保险号:");
    scanf_s("%d", &list[i].num);
    i++;
    while (getchar() != '\n')
      continue;
    if (i == 5)
      break;
    puts("请输入名[按Enter退出]: ");
  }
  if (i > 0)
    for (j = 0; j < i; j++)
      show(list[j].name.name_a, list[j].name.name_b, list[j].name.name_c, list[j].num);
  else
    puts("没有输入内容, 结束.");
  
  system("pause");
  return 0;
}

void show(char *st1, char *st2, char *st3, int n)
{

  if (st2[0] == '\0')
    printf("%s, %s -- %d\n", st1, st3, n);
  else
    printf("%s, %s %c. -- %d\n", st1, st2, st3[0], n);
}

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

5

#include <stdio.h>
#define CSIZE 4
struct name
{
  char name_a[20];
  char name_b[10];
};

struct student
{
  struct name namect;
  float grade[3];
  float average;
};

//输入
char *s_gets(char *st, int n);
//计算个人平均分
void Average(struct student pst[], int n);
//计算班级平均分
void Avergae_s(struct student pst[], int n);

int main()
{
  struct student list[CSIZE];
  int i = 0;
  int j;

  puts("请输入姓: ");
  while (s_gets(list[i].namect.name_b, 10) != NULL && list[i].namect.name_b[0] != '\0')
  {
    puts("请输入名: ");
    scanf_s("%s", list[i].namect.name_a, 20);
    puts("请输入三组分数: ");
    scanf_s("%f%f%f", &list[i].grade[0], &list[i].grade[1], &list[i].grade[2]);
    i++;
    while (getchar() != '\n')
      continue;
    if (i == CSIZE)
      break;
    puts("请输入姓[空行退出]: ");

  }

  if (i > 0)
  {
    Average(list, i);
    for (j = 0; j < i; j++)
    {
      printf("姓名: %s, %s \t分数: %g %g %g \t平均分: %g\n", list[j].namect.name_a, list[j].namect.name_b, list[j].grade[0],
        list[j].grade[1], list[j].grade[2], list[j].average);
    }
    Avergae_s(list, i);
  }
  else
    puts("错误: 您没有输入内容.");

  system("pause");
  return 0;
}

void Avergae_s(struct student pst[], int n)
{
  float tot = 0;
  int i;

  for (i = 0; i < n; i++)
  {
    tot += (pst[i].grade[0] + pst[i].grade[1] + pst[i].grade[2]) / 3;
  }
  printf("班级平均分: %g\n", tot / n);
}

void Average(struct student pst[], int n)
{
  int i;

  for (i = 0; i < n; i++)
  {
    pst[i].average = (pst[i].grade[0] + pst[i].grade[1] + pst[i].grade[2]) / 3;
  }
}

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

  ret_val = fgets(st, n, stdin);

  if (ret_val)
  {
    while (*st != '\n'&&*st != '\0')
      st++;
    if (*st == '\n')
      *st = '\0';
    else
      while (getchar() != '\n')
        continue;
  }

  return ret_val;
}

6

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

struct data
{
  int number; //球员号
  char name_b[20];  //名
  char name_a[10];  //姓
  int num_a;  //球员上场次数
  int num_b;  //击中数
  int num_c;  //走垒数
  int num_d;  //打点RBI
  float total; //安打率
};

int main()
{
  errno_t err;
  FILE *fp;
  char name[81];
  struct data list[19] = { 
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  { 0,'\0','\0',0,0,0,0,0 },
  };
  int i;
  struct data temp;


  puts("请输入需要打开的文件名: ");
  scanf_s("%s", name, 81);
  if ((err = fopen_s(&fp, name, "r")) != 0)
  {
    fprintf(stderr, "错误: 无法打开 %s 文件\n", name);
    exit(EXIT_FAILURE);
  }
  while (!feof(fp))
  {
    fscanf_s(fp, "%d", &temp.number);
    fscanf_s(fp, "%s", temp.name_b, 20);
    fscanf_s(fp, "%s", temp.name_a, 10);
    fscanf_s(fp, "%d", &temp.num_a);
    fscanf_s(fp, "%d", &temp.num_b);
    fscanf_s(fp, "%d", &temp.num_c);
    fscanf_s(fp, "%d", &temp.num_d);
    
    if (list[temp.number].name_b[0] == '\0')
      list[temp.number] = temp;
    else
    {
      list[temp.number].num_a += temp.num_a;
      list[temp.number].num_b += temp.num_b;
      list[temp.number].num_c += temp.num_c;
      list[temp.number].num_d += temp.num_d;
    }
  }
  puts("球员号 名        姓        上场次数 击中数 走垒数 打点[RBI] 安打率 ");
  for (i = 0; i < 19; i++)
  {
    if (list[i].num_b != 0)
      list[i].total = (float)list[i].num_b / (float)list[i].num_a;
    else
      list[i].total = 0;
    printf("%-6d %-9s %-9s %-8d %-6d %-6d %-9d %g\n", list[i].number, list[i].name_b, list[i].name_a, list[i].num_a,
      list[i].num_b, list[i].num_c, list[i].num_d, list[i].total);
  }

  fclose(fp);

  system("pause");
  return 0;
}

7

//在文件中保存结构中的内容
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 10


struct book {
  int Title;
  char title[MAXTITL];
  char author[MAXAUTL];
  float value;
  bool delete;
};

//输入
char *s_gets(char *st, int n);
//显示菜单
int menu(void);
//删除图书
int s_delete(struct book pst[], int n);

int main()
{
  struct book library[MAXBKS];  //结构数组
  int count = 0;
  int filecount;
  errno_t err;
  FILE *pbooks;
  int size = sizeof(struct book);  //结构大小
  int num;  //菜单选项
  int i, j = 0;

  while ((err = fopen_s(&pbooks, "book.dat", "r+b")) != 0)  //因为r+b模式无法创建文件
  {
    if ((err = fopen_s(&pbooks, "book.dat", "wb")) != 0)  //所以当无法打开文件的时候以wb模式创建一个新文件然后以r+b模式打开
    {
      fputs("无法打开 book.dat 文件\n", stderr);
      exit(0);
    }
    fclose(pbooks);
    
  }
    
    
  
  while ((num = menu()) != 4)
  {
    rewind(pbooks);
    while (getchar() != '\n')
      continue;
    if (num == 1)
    {
      count = 0;
      while (count < MAXBKS&&fread(&library[count], size, 1, pbooks) == 1)
      {
        if (count == 0)
          puts("book.dat 当前内容: ");
        printf("%d. %s by %s: $%.2f\n", library[count].Title, library[count].title, library[count].author, library[count].value);
        count++;
      }
      if (count == 0)
      {
        fputs("错误: 当前系统没有内容\n", stderr);
        continue;
      }
      
        
    }
    
    else if (num == 2)
    {
      count = 0;
      while (count < MAXBKS&&fread(&library[count], size, 1, pbooks) == 1)
      {
        if (count == 0)
          puts("book.dat 当前内容: ");
        printf("%d. %s by %s: $%.2f\n", library[count].Title, library[count].title, library[count].author, library[count].value);
        count++;
      }
      
      if (count == MAXBKS)
      {
        fputs("错误: 当前系统已满请管理图书删除内容后重新添加\n", stderr);
        continue;
      }
      filecount = count;
      puts("请添加新书标题.");
      puts("在一行开始按[Enter]退出.");
      while (count < MAXBKS&&s_gets(library[count].title, MAXTITL) != NULL && library[count].title[0] != '\0')
      {
        puts("现在输入作者:");
        s_gets(library[count].author, MAXAUTL);
        puts("现在输入值:");
        scanf_s("%f", &library[count].value);
        library[count].delete = true;
        library[count].Title = count + 1;
        while (getchar() != '\n')
          continue;
        count++;

        if (count < MAXBKS)
          puts("输入下一个标题:");
      }
      if (count==filecount)
      {
        fputs("错误: 您没有输入内容, 返回\n", stderr);
        continue;
      }
      fwrite(&library[filecount], size, count - filecount, pbooks);
      
    }

    else if (num == 3)
    {
      count = 0;
      while (count < MAXBKS&&fread(&library[count], size, 1, pbooks) == 1)
      {
        if (count == 0)
          puts("book.dat 当前内容: ");
        printf("%d. %s by %s: $%.2f\n", library[count].Title, library[count].title, library[count].author, library[count].value);
        count++;
      }
      if (count == 0)
      {
        fputs("错误: 当前系统没有内容\n", stderr);
        continue;
      }
      
        
      if (s_delete(library, count) == 0)
      {
        fputs("错误: 您没有任何操作, 返回\n", stderr);
        continue;
      }
      puts("提示: 开始删除写入...");
      fclose(pbooks);
      if ((err = fopen_s(&pbooks, "book.dat", "w+b")) != 0)  
      {
        fputs("无法打开 book.dat 文件\n", stderr);
        exit(0);
      }
      for (i = 0; i < count; i++)
      {
        if (library[i].delete)
        {
          library[i].Title = j + 1;
          fwrite(&library[i], size, 1, pbooks);
          j++;
        }
      }
      puts("提示: 操作完成");
    }
    
  }
  

  puts("再见");
  fclose(pbooks);

  system("pause");
  return 0;
}

int s_delete(struct book pst[], int n)
{
  int i = 0;
  int delete_a;
  char del_a[] = "删除";
  char del_b[] = "储存";

  puts("请输入你要删除的图书, 重复选择可以切换删除与保留模式:");
  while (scanf_s("%d", &delete_a) == 1 && (delete_a > 0 && delete_a <= n))
  {
    if (pst[delete_a - 1].delete)
      pst[delete_a - 1].delete = false;
    else
      pst[delete_a - 1].delete = true;
    for (i = 0; i < n; i++)
    {
      if (pst[i].delete)
        printf("%d. %s by %s: $%.2f %s\n", pst[i].Title, pst[i].title, pst[i].author, pst[i].value, del_b);
      else
        printf("%d. %s by %s: $%.2f %s\n", pst[i].Title, pst[i].title, pst[i].author, pst[i].value, del_a);
    }
    puts("请继续输入你要删除的图书, 重复选择可以切换删除与保留模式[q退出]:");
  }
  while (getchar() != '\n')
    continue;

  return i;
}

int menu(void)
{
  int num;


  
  puts("*********图书管理系统*********");
  puts("1. 显示图书 \t2. 添加图书");
  puts("3. 删除图书 \t4. 退出系统");
  puts("******************************");
  bk:while (scanf_s("%d", &num) != 1)
  {
    fputs("错误: 请输入1-4\n", stderr);
    while (getchar() != '\n')
      continue;
  }
  if (num < 1 || num>4)
  {
    fputs("错误: 请输入1-4\n", stderr);
    while (getchar() != '\n')
      continue;
    goto bk;
  }

  return num;
}

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

8

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 12


struct seat
{
  int number;  //座位号
  bool empty;   //座位使用
  char name_a[20];  //名
  char name_b[10];  //姓
};

//显示菜单
char menu(void);
//抛弃多余输入字符
void d_stdin(void);
//按字母顺序打印座位列表
void show(struct seat *pst[], int n);
//给客户分配座位
int Seat_set(struct seat *pst[], int n);
//删除座位
int d_Seat(struct seat *pst[], int n);
//输入
char *s_gets(char *st, int n);

int main()
{
  int i;
  int filecount;
  int count;
  errno_t err;
  FILE *fp;
  char ch;
  int size = sizeof(struct seat);
  struct seat list[SIZE] = {
  { 1,true,'\0','\0' },
  { 2,true,'\0','\0' },
  { 3,true,'\0','\0' },
  { 4,true,'\0','\0' },
  { 5,true,'\0','\0' },
  { 6,true,'\0','\0' },
  { 7,true,'\0','\0' },
  { 8,true,'\0','\0' },
  { 9,true,'\0','\0' },
  { 10,true,'\0','\0' },
  { 11,true,'\0','\0' },
  { 12,true,'\0','\0' },
  };
  struct seat *pst1[SIZE];
  struct seat *pst2[SIZE];

  for (i = 0; i < SIZE; i++)
  {
    pst1[i] = &list[i];
    pst2[i] = &list[i];
  }

  while ((err = fopen_s(&fp, "data.bat", "r+b")) != 0)
  {
    if ((err = fopen_s(&fp, "data.bat", "wb")) != 0)
    {
      fputs("错误: 无法创建数据文件 data.bat \n", stderr);
      exit(0);
    }
    fwrite(&list[0], size, SIZE, fp);
    fclose(fp);
  }

  while ((ch = menu()) != 'f')
  {
    filecount = 0;
    rewind(fp);
    count = 0;
    switch (ch)
    {
    case 'a':
      while (filecount < SIZE&&fread(&list[filecount], size, 1, fp) == 1)
        filecount++;

      for (i = 0, count = 0; i < SIZE; i++)
      {
        if (list[i].empty)
          count++;
      }
      printf("查询结果: 目前空闲座位余数为 %d\n\n", count);
      break;
    case 'b':
      while (filecount < SIZE&&fread(&list[filecount], size, 1, fp) == 1)
        filecount++;
      printf("查询结果: \n目前空闲座位列表\n");
      for (i = 0; i < SIZE; i++)
      {
        if (list[i].empty)
          printf("%-2d 空闲\n", list[i].number);
        else
          printf("%-2d 占有\n", list[i].number);
      }
      putchar('\n');
      break;
    case 'c':
      while (filecount < SIZE&&fread(&list[filecount], size, 1, fp) == 1)
        filecount++;
      show(pst1, SIZE);
      break;
    case 'd':
      while (filecount < SIZE&&fread(&list[filecount], size, 1, fp) == 1)
        filecount++;
      printf("提示: 目前可以预定的座位\n");
      for (i = 0; i < SIZE; i++)
      {
        if (list[i].empty)
          printf("%-2d 空闲\n", list[i].number);
      }
      if (Seat_set(pst2, SIZE) == 0)
      {
        fputs("错误: 您没有进行任何操作, 返回菜单 \n\n", stderr);
        break;
      }
      fclose(fp);
      puts("提示: 正在修改写入数据库");
      if ((err = fopen_s(&fp, "data.bat", "w+b")) != 0)
      {
        fputs("错误: 无法打开数据文件 data.bat \n", stderr);
        exit(0);
      }
      fwrite(&list[0], size, SIZE, fp);
      puts("提示: 写入完成\n");
      break;
    case 'e':
      while (filecount < SIZE&&fread(&list[filecount], size, 1, fp) == 1)
        filecount++;
      for (i = 0; i < SIZE; i++)
      {
        if (!list[i].empty)
        {
          count++;
          printf("%-2d %-15s %-10s 占有\n", list[i].number, list[i].name_a, list[i].name_b);
        }
      }
      if (count == 0)
      {
        fputs("错误: 当前系统座位全部为空闲状态, 返回菜单 \n\n", stderr);
        break;
      }
      if (d_Seat(pst2, SIZE) == 0)
      {
        fputs("错误: 您没有进行任何操作, 返回菜单 \n\n", stderr);
        break;
      }
      fclose(fp);
      puts("提示: 正在修改写入数据库");
      if ((err = fopen_s(&fp, "data.bat", "w+b")) != 0)
      {
        fputs("错误: 无法打开数据文件 data.bat \n", stderr);
        exit(0);
      }
      fwrite(&list[0], size, SIZE, fp);
      puts("提示: 写入完成\n");

      break;
    default:
      break;
    }
    d_stdin();
  }
  
  puts("完成");
  
  fclose(fp);
  system("pause");
  return 0;
}

//给客户分配座位
int Seat_set(struct seat *pst[], int n)
{
  int i;
  int num;
  int count = 0;
  int j = 0;
  
  puts("请输入需要预定的座位: ");
  while (scanf_s("%d", &num) == 1)
  {
    
    if (num < 1 || num > 12)
    {
      fputs("错误: 请输入 1 - 12 空闲的座位\n", stderr);
      d_stdin();
      continue;
    }
    num = num - 1;
    if (!pst[num]->empty)
    {
      fputs("错误: 请输入 1 - 12 空闲的座位\n", stderr);
      fprintf(stderr, "%d 座位已被占用.\n", pst[num]->number);
      d_stdin();
      continue;
    }
    d_stdin();
    puts("请输入名: ");
    while (s_gets(pst[num]->name_a, 20) && pst[num]->name_a[0] == '\0')
      fputs("错误: 名不能为空.\n", stderr);
    
    puts("请输入姓: ");
    while (s_gets(pst[num]->name_b, 10) && pst[num]->name_b[0] == '\0')
      fputs("错误: 姓不能为空.\n", stderr);
    pst[num]->empty = false;
    j++;
    for (i = 0; i < SIZE; i++)
    {
      if (pst[i]->empty)
      {
        printf("%-2d 空闲\n", pst[i]->number);
        count++;
      }	
    }
    if (count == 0)
    {
      fputs("提示: 目前已经没有可预定的座位, 返回菜单\n", stderr);
      break;
    }
    else
    {
      puts("提示: 还有以上座位可以预定 \n请输入下一个座位[q退出]: ");
    }
  }

  putchar('\n');

  return j;
}

//删除座位
int d_Seat(struct seat *pst[], int n)
{
  int i;
  int num;
  int count = 0;
  int j = 0;

  puts("请输入你要删除的座位: ");
  while (scanf_s("%d", &num) == 1)
  {

    if (num < 1 || num > 12)
    {
      fputs("错误: 请输入 1 - 12 占有的座位\n", stderr);
      d_stdin();
      continue;
    }
    num = num - 1;
    if (pst[num]->empty)
    {
      fputs("错误: 请输入 1 - 12 占有的座位\n", stderr);
      fprintf(stderr, "%d 座位是空闲状态.\n", pst[num]->number);
      d_stdin();
      continue;
    }
    d_stdin();
    pst[num]->empty = true;
    *pst[num]->name_a = '\0';
    *pst[num]->name_b = '\0';
    j++;
    for (i = 0; i < SIZE; i++)
    {
      if (!pst[i]->empty)
      {
        count++;
        printf("%-2d %-15s %-10s 占有\n", pst[i]->number, pst[i]->name_a, pst[i]->name_b);
      }
    }
    if (count == 0)
    {
      fputs("错误: 当前系统座位全部为空闲状态, 返回菜单 \n\n", stderr);
      break;
    }
    else
      puts("提示: 还有以上座位可以删除 \n请输入下一个座位[q退出]: ");

  }
  putchar('\n');


  return j;
}

//按字母顺序打印座位列表
void show(struct seat *pst[], int n)
{
  int i, j;
  struct seat *temp;
  int count = 0;

  for (i = 0; i < n; i++)
  {
    for (j = i; j < n; j++)
    {
      if (pst[i]->name_a[0] > pst[j]->name_a[0])
      {
        temp = pst[i];
        pst[i] = pst[j];
        pst[j] = temp;
      }
    }
  }

  for (i = 0; i < n; i++)
  {
    if (!pst[i]->empty)
    {
      printf("%-2d %-15s %-10s\n", pst[i]->number, pst[i]->name_a, pst[i]->name_b);
      count++;
    }
      
  }
  if (count == 0)
    puts("提示: 当前系统座位全部为空闲状态.");
  putchar('\n');
}

//显示菜单
char menu(void)
{
  char ch;

  puts("****巨人航空座位预定管理系统****");
  puts("a. 显示空闲座位余数");
  puts("b. 显示空闲座位列表");
  puts("c. 显示按字母排列的座位列表");
  puts("d. 给客户分配座位");
  puts("e. 管理删除已分配的座位");
  puts("f. 退出");
  puts("********************************");
bk:while (scanf_s("%c", &ch, 1) != 1)
{
  fputs("错误: 请输入 a - f \n", stderr);
  d_stdin();
}
   if (ch < 'a' || ch > 'f')
   {
     fputs("错误: 请输入 a - f \n", stderr);
     d_stdin();
     goto bk;
   }

   return ch;

}

//抛弃多余字符
void d_stdin(void)
{
  while (getchar() != '\n')
    continue;
}

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

9

确认座位分配不知道是干嘛的没有写

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 12


struct seat
{
  int number;  //座位号
  bool empty;   //座位使用
  char name_a[20];  //名
  char name_b[10];  //姓
};

//显示顶层菜单
int top_menu(void);
//显示菜单
char menu(char st[][20], int n);
//抛弃多余输入字符
void d_stdin(void);
//按字母顺序打印座位列表
void show(struct seat *pst[], int n);
//给客户分配座位
int Seat_set(struct seat *pst[], int n);
//删除座位
int d_Seat(struct seat *pst[], int n);
//输入
char *s_gets(char *st, int n);

int main()
{
  int i;
  int filecount;
  int count;
  int n_num;
  errno_t err;
  FILE *fp;
  char ch;
  char name[4][15] =
  {
    "data1.dat",
    "data2.dat",
    "data3.dat",
    "data4.dat",
  };
  int size = sizeof(struct seat);
  struct seat list[SIZE] = {
  { 1,true,'\0','\0' },
  { 2,true,'\0','\0' },
  { 3,true,'\0','\0' },
  { 4,true,'\0','\0' },
  { 5,true,'\0','\0' },
  { 6,true,'\0','\0' },
  { 7,true,'\0','\0' },
  { 8,true,'\0','\0' },
  { 9,true,'\0','\0' },
  { 10,true,'\0','\0' },
  { 11,true,'\0','\0' },
  { 12,true,'\0','\0' },
  };
  struct seat *pst1[SIZE];
  struct seat *pst2[SIZE];
  char plane[4][20] = { "航班102","航班311","航班444","航班519" };



  for (i = 0; i < SIZE; i++)
  {
    pst1[i] = &list[i];
    pst2[i] = &list[i];
  }

  while ((n_num = top_menu()) != 5)
  {
    d_stdin();
    while ((err = fopen_s(&fp, name[n_num - 1], "r+b")) != 0)
    {
      if ((err = fopen_s(&fp, name[n_num - 1], "wb")) != 0)
      {
        fprintf(stderr, "错误: 无法打开数据文件 %s \n", name[n_num - 1]);
        exit(0);
      }
      for (i = 0; i < SIZE; i++)
      {
        list[i].empty = true;
        *list[i].name_a = '\0';
        *list[i].name_b = '\0';
      }
      fwrite(&list[0], size, SIZE, fp);
      fclose(fp);
    }

    while ((ch = menu(plane, n_num - 1)) != 'f')
    {
      filecount = 0;
      rewind(fp);
      count = 0;
      while (filecount < SIZE&&fread(&list[filecount], size, 1, fp) == 1)
        filecount++;
      switch (ch)
      {
      case 'a':
        for (i = 0, count = 0; i < SIZE; i++)
        {
          if (list[i].empty)
            count++;
        }
        printf("查询结果: 目前空闲座位余数为 %d\n\n", count);
        break;
      case 'b':
        printf("查询结果: \n目前空闲座位列表\n");
        for (i = 0; i < SIZE; i++)
        {
          if (list[i].empty)
            printf("%-2d 空闲\n", list[i].number);
          else
            printf("%-2d 占有\n", list[i].number);
        }
        putchar('\n');
        break;
      case 'c':
        show(pst1, SIZE);
        break;
      case 'd':
        printf("提示: 目前可以预定的座位\n");
        for (i = 0; i < SIZE; i++)
        {
          if (list[i].empty)
            printf("%-2d 空闲\n", list[i].number);
        }
        if (Seat_set(pst2, SIZE) == 0)
        {
          fputs("错误: 您没有进行任何操作, 返回菜单 \n\n", stderr);
          break;
        }
        fclose(fp);
        puts("提示: 正在修改写入数据库");
        if ((err = fopen_s(&fp, name[n_num - 1], "w+b")) != 0)
        {
          fprintf(stderr, "错误: 无法打开数据文件 %s \n", name[n_num - 1]);
          exit(0);
        }
        fwrite(&list[0], size, SIZE, fp);
        puts("提示: 写入完成\n");
        break;
      case 'e':
        for (i = 0; i < SIZE; i++)
        {
          if (!list[i].empty)
          {
            count++;
            printf("%-2d %-15s %-10s 占有\n", list[i].number, list[i].name_a, list[i].name_b);
          }
        }
        if (count == 0)
        {
          fputs("错误: 当前系统座位全部为空闲状态, 返回菜单 \n\n", stderr);
          break;
        }
        if (d_Seat(pst2, SIZE) == 0)
        {
          fputs("错误: 您没有进行任何操作, 返回菜单 \n\n", stderr);
          break;
        }
        fclose(fp);
        puts("提示: 正在修改写入数据库");
        if ((err = fopen_s(&fp, name[n_num - 1], "w+b")) != 0)
        {
          fprintf(stderr, "错误: 无法打开数据文件 %s \n", name[n_num - 1]);
          exit(0);
        }
        fwrite(&list[0], size, SIZE, fp);
        puts("提示: 写入完成\n");

        break;
      default:
        break;
      }
      d_stdin();
    }
    fclose(fp);
  }
  
  
  puts("完成");
  
  fclose(fp);
  system("pause");
  return 0;
}

//给客户分配座位
int Seat_set(struct seat *pst[], int n)
{
  int i;
  int num;
  int count = 0;
  int j = 0;
  
  puts("请输入需要预定的座位: ");
  while (scanf_s("%d", &num) == 1)
  {
    
    if (num < 1 || num > 12)
    {
      fputs("错误: 请输入 1 - 12 空闲的座位\n", stderr);
      d_stdin();
      continue;
    }
    num = num - 1;
    if (!pst[num]->empty)
    {
      fputs("错误: 请输入 1 - 12 空闲的座位\n", stderr);
      fprintf(stderr, "%d 座位已被占用.\n", pst[num]->number);
      d_stdin();
      continue;
    }
    d_stdin();
    puts("请输入名: ");
    while (s_gets(pst[num]->name_a, 20) && pst[num]->name_a[0] == '\0')
      fputs("错误: 名不能为空.\n", stderr);
    
    puts("请输入姓: ");
    while (s_gets(pst[num]->name_b, 10) && pst[num]->name_b[0] == '\0')
      fputs("错误: 姓不能为空.\n", stderr);
    pst[num]->empty = false;
    j++;
    for (i = 0; i < SIZE; i++)
    {
      if (pst[i]->empty)
      {
        printf("%-2d 空闲\n", pst[i]->number);
        count++;
      }	
    }
    if (count == 0)
    {
      fputs("提示: 目前已经没有可预定的座位, 返回菜单\n", stderr);
      break;
    }
    else
    {
      puts("提示: 还有以上座位可以预定 \n请输入下一个座位[q退出]: ");
    }
  }

  putchar('\n');

  return j;
}

//删除座位
int d_Seat(struct seat *pst[], int n)
{
  int i;
  int num;
  int count = 0;
  int j = 0;

  puts("请输入你要删除的座位: ");
  while (scanf_s("%d", &num) == 1)
  {

    if (num < 1 || num > 12)
    {
      fputs("错误: 请输入 1 - 12 占有的座位\n", stderr);
      d_stdin();
      continue;
    }
    num = num - 1;
    if (pst[num]->empty)
    {
      fputs("错误: 请输入 1 - 12 占有的座位\n", stderr);
      fprintf(stderr, "%d 座位是空闲状态.\n", pst[num]->number);
      d_stdin();
      continue;
    }
    d_stdin();
    pst[num]->empty = true;
    *pst[num]->name_a = '\0';
    *pst[num]->name_b = '\0';
    j++;
    for (i = 0; i < SIZE; i++)
    {
      if (!pst[i]->empty)
      {
        count++;
        printf("%-2d %-15s %-10s 占有\n", pst[i]->number, pst[i]->name_a, pst[i]->name_b);
      }
    }
    if (count == 0)
    {
      fputs("错误: 当前系统座位全部为空闲状态, 返回菜单 \n\n", stderr);
      break;
    }
    else
      puts("提示: 还有以上座位可以删除 \n请输入下一个座位[q退出]: ");

  }
  putchar('\n');


  return j;
}

//按字母顺序打印座位列表
void show(struct seat *pst[], int n)
{
  int i, j;
  struct seat *temp;
  int count = 0;

  for (i = 0; i < n; i++)
  {
    for (j = i; j < n; j++)
    {
      if (pst[i]->name_a[0] > pst[j]->name_a[0])
      {
        temp = pst[i];
        pst[i] = pst[j];
        pst[j] = temp;
      }
    }
  }

  for (i = 0; i < n; i++)
  {
    if (!pst[i]->empty)
    {
      printf("%-2d %-15s %-10s\n", pst[i]->number, pst[i]->name_a, pst[i]->name_b);
      count++;
    }
      
  }
  if (count == 0)
    puts("提示: 当前系统座位全部为空闲状态.");
  putchar('\n');
}

//显示顶层菜单
int top_menu(void)
{
  int num;

  puts("****巨人航空座位预定管理系统****");
  puts("1. 航班102 \t2. 航班311");
  puts("3. 航班444 \t4. 航班519");
  puts("5. 退出");
  puts("********************************");
bk:while (scanf_s("%d", &num) != 1)
{
  fputs("错误: 请输入 1 - 5 \n", stderr);
  d_stdin();
}
   if (num < 1 || num > 5)
   {
     fputs("错误: 请输入 1 - 5 \n", stderr);
     d_stdin();
     goto bk;
   }

   return num;
}

//显示菜单
char menu(char st[][20], int n)
{
  char ch;

  puts("****巨人航空座位预定管理系统****");
  printf("当前航班: %s\n", st[n]);
  puts("a. 显示空闲座位余数");
  puts("b. 显示空闲座位列表");
  puts("c. 显示按字母排列的座位列表");
  puts("d. 给客户分配座位");
  puts("e. 管理删除已分配的座位");
  puts("f. 返回上一层");
  puts("********************************");
bk:while (scanf_s("%c", &ch, 1) != 1)
{
  fputs("错误: 请输入 a - f \n", stderr);
  d_stdin();
}
   if (ch < 'a' || ch > 'f')
   {
     fputs("错误: 请输入 a - f \n", stderr);
     d_stdin();
     goto bk;
   }

   return ch;

}

//抛弃多余字符
void d_stdin(void)
{
  while (getchar() != '\n')
    continue;
}

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

10

#include <stdio.h>

//相加
void sum_1(double x, double y);
//相乘
void sum_2(double x, double y);
typedef void(*fp)(double x, double y);

int main()
{
  double x = 3.6;
  double y = 8.4;
  int num;
  fp fpun[2] = { sum_1,sum_2 };

  puts("1. 相加 \t2. 相乘");
  scanf_s("%d", &num);
  switch (num)
  {
  case 1:
    fpun[0](x, y);
    break;
  case 2:
    fpun[1](x, y);
    break;
  default:
    puts("输入错误");
    break;
  }

  system("pause");
  return 0;
}

void sum_1(double x, double y)
{
  printf("%g + %g = %g\n", x, y, x + y);
}
void sum_2(double x, double y)
{
  printf("%g x %g = %g\n", x, y, x * y);
}

11

#include <stdio.h>
#include <math.h>
#define SIZE 100

//数组赋值
void value(double *ps, int n);
//计算
void transform(double *ps1, double *ps2, int n, double(*fp)(double x));
//显示
void show(double *ps, int n, int m);
//平方
double square(double x);
//立方
double cube(double x);

int main()
{
  double num_a[SIZE];
  double num_b[SIZE];

  value(num_b, SIZE);
  transform(num_a, num_b, SIZE, sin);
  puts("sin: ");
  show(num_a, SIZE, 5);
  transform(num_a, num_b, SIZE, cos);
  puts("cos: ");
  show(num_a, SIZE, 5);
  transform(num_a, num_b, SIZE, square);
  puts("square: ");
  show(num_a, SIZE, 8);
  transform(num_a, num_b, SIZE, cube);
  puts("cube: ");
  show(num_a, SIZE, 9);

  system("pause");
  return 0;
}

double cube(double x)
{
  return x * x * x;
}

double square(double x)
{
  return x * x;
}

void value(double *ps, int n)
{
  int i;

  for (i = 0; i < n; i++)
    ps[i] = i;
}

void transform(double *ps1, double *ps2, int n, double(*fp)(double x))
{
  int i;

  for (i = 0; i < n; i++)
    ps1[i] = (*fp)(ps2[i]);
}

void show(double *ps, int n, int m)
{
  int i;

  if (m == 5)
  {
    for (i = 0; i < n; i++)
    {
      if (i % 10 == 0 && i != 0)
        putchar('\n');
      printf("%5.2f ", ps[i]);
    }
  }
  else if (m == 8)
  {
    for (i = 0; i < n; i++)
    {
      if (i % 10 == 0 && i != 0)
        putchar('\n');
      printf("%8.2f ", ps[i]);
    }
  }
  else if (m == 9)
  {
    for (i = 0; i < n; i++)
    {
      if (i % 10 == 0 && i != 0)
        putchar('\n');
      printf("%9.2f ", ps[i]);
    }
  }
  
    

  putchar('\n');
}

 

 

版权说明:
点赞

发表评论

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

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

微信扫一扫打赏