C Primer Plus(第五版) 第十四章 第八题

2024-03-17 11:18

本文主要是介绍C Primer Plus(第五版) 第十四章 第八题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!



删除操作还没有做好,有些小地方不知道如何处理了,可能需要看一些开源项目来理解如何进行字符串操作。

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

#define MAX_char 20
#define NUMBER 12//number of seat

struct seat
{
 int number;//0~11
 int used;//0~1
 char First[MAX_char];//20
 char Last[MAX_char];//20
};
struct airplane
{
 struct seat seats[NUMBER];
};

char menu();
void init(struct airplane *a);
void show_empty(struct airplane *a);
int cal_empty(struct airplane *a);
void show_alp(struct airplane a);
void add_customer(struct airplane *a,int pos);
void delet_seat(struct airplane *a);
int b[12]={0,1,2,3,4,5,6,7,8,9,10,11};//seats

int cmp1(const void *a,const void *b)
{
 return strcmp(((struct seat*)a)->First,((struct seat *)b)->First);
}

void eatline()
{
 while(getchar()!='\n')
  continue;
}
int get_int(void)
{
 int a;
 char ch;
 while(scanf("%d",&a)!=1||a<0||a>11)
 {
  while((ch=getchar())!='\n')
   putchar(ch);
  puts(" is not correct input");
  puts("Please input the integer in the scope 0~11.");
 }
 eatline();
 return a;
}

int main(int argc, char **argv)
{
 //printf("hello world\n");
 char choose;
 struct airplane Planes;
 FILE *in;
 int i=0;//0~11
 int filecount=0;
 
 init(&Planes);//initial
 if((in=fopen("seats.dat","a+b"))==NULL)
 {
  fputs("Can't open the planes.dat",stderr);
  exit(1);
 }
 //rewind(in);
 while(i<NUMBER&&fread(&(Planes.seats[i]),sizeof(struct seat),1,in)==1)
 {
  if(i==0) puts("Here is current state of seats in airplane:");
  
  if(Planes.seats[i].used==1) printf("Number:%-2d Used:%-2d Customer %-10s %-10s\n",Planes.seats[i].number,Planes.seats[i].used,Planes.seats[i].First,Planes.seats[i].Last);
  i++;
 }
 filecount=i;
 while(1)
 {
  choose=menu();
  if(choose=='a') cal_empty(&Planes);
  if(choose=='b') show_empty(&Planes);
  if(choose=='c') show_alp(Planes);
  if(choose=='d')
  {
   add_customer(&Planes,i);
   //printf("%s %s No:%d Used:%d\n",Planes.seats[i].First,Planes.seats[i].Last,Planes.seats[i].number,Planes.seats[i].used);   
   
   fwrite(&(Planes.seats[i]),sizeof(struct seat),1,in);
   i++;
  }
  if(choose=='e') delet_seat(&Planes);
  if(choose=='f') { break;}
  //printf("you choose the choice:%c\n",choose);
  puts("\n");
 }
 /*if(i>0)
 {
  int index;
  puts("Here is the list of your plane:");
  for(index=0;index<12;index++)
   printf("%s %s No:%d Used:%d\n",Planes.seats[index].First,Planes.seats[index].Last,Planes.seats[index].number,Planes.seats[index].used);
  fwrite(&(Planes.seats[filecount]),sizeof(struct seat),i-filecount,in);
 }*/
 fclose(in);
 return 0;
}
void add_customer(struct airplane *a,int pos)
{
 int remain=cal_empty(a);
 show_empty(a);
 int sign_end=0;//0 or 1
 puts("Which seat do you want to choose? ");
 struct seat save;
 save.number=get_int();
 puts("Please input your First name");
 gets(save.First);
 puts("Please input your Last name");
 gets(save.Last);
 puts("Do you want to save the information? Please press 'y' to confirm.");
 if(tolower(getchar())=='y')
 { save.used=1;
  //int pos=save.number;
  a->seats[pos].number=save.number;
  strcpy(a->seats[pos].First,save.First);
  strcpy(a->seats[pos].Last,save.Last);
  a->seats[pos].used=save.used;
 }
 else
  save.used=0;
 eatline();
 //printf("%s %s No:%d Used:%d\n",save.First,save.Last,save.number,save.used);
}
void delet_seat(struct airplane *a)
{
 int pos,i=0;
 show_alp(*a);
 puts("which postion do you want to delete?");
 scanf("%d",&pos);
 eatline();
 while(i<NUMBER)
 {
  if(a->seats[i].number==pos)
   {a->seats[i].used=0;break;}
  i++;
 }
 
}
void init(struct airplane *a)
{
 int i=0;
 
 while(i<NUMBER)
 {
  a->seats[i].number=i;
  a->seats[i].used=0;
  strcpy(a->seats[i].First," ");
  strcpy(a->seats[i].Last," ");
  //printf("%s %s number: %d used:%d\n",a->seats[i].Last,a->seats[i].First,a->seats[i].number,a->seats[i].used);
  i++;
 }
}
void show_alp(struct airplane a)
{
 qsort(a.seats,12,sizeof(struct seat),cmp1);
 int i=0;
 while(i<NUMBER)
 {
  if(a.seats[i].used==1)
   printf("%-10s %-10s NO:%-.2d\n",a.seats[i].First,a.seats[i].Last,a.seats[i].number);
  i++;
 }
 
}
int cal_empty(struct airplane *a)
{
 int i=0;
 int count=0;
 while(i<NUMBER)
 {
  if(a->seats[i].used==0)
   count++;
  i++;
 }
 printf("The empty seats in this plane:%d\n",count);
 return count;
}
void show_empty(struct airplane *a)
{
 int i=0;
 while(i<NUMBER)
 {
  if(i==0) puts("The Empty Number of this plane: ");
  if(a->seats[i].used==1) b[a->seats[i].number]=-1;
  i++;
 }
 for(i=0;i<NUMBER;i++)
  if(b[i]!=-1) printf("%d ",b[i]);
  
 int b[12]={0,1,2,3,4,5,6,7,8,9,10,11};
 printf("\n");
}
char menu()
{
 char ans;
 puts("To choose a function,enter its letter label:");
 puts("a)Show number of empty seats");
 puts("b)Show list of empty");
 puts("c)Show alphabetical list of seats");
 puts("d)Assign a customer to a seat assignment");
 puts("e)Delete a seat assignment");
 puts("f)Quit");
 ans=tolower(getchar());
 eatline();
 while(strchr("abcdef",ans)==NULL)
 {
  puts("Please input the one of 'a,b,c,d,e,f'");
  ans=tolower(getchar());
  eatline();
 }
 return ans;
}

这篇关于C Primer Plus(第五版) 第十四章 第八题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/818793

相关文章

MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析

《MyBatis-Plus中Service接口的lambdaUpdate用法及实例分析》本文将详细讲解MyBatis-Plus中的lambdaUpdate用法,并提供丰富的案例来帮助读者更好地理解和应... 目录深入探索MyBATis-Plus中Service接口的lambdaUpdate用法及示例案例背景

MyBatis-Plus中静态工具Db的多种用法及实例分析

《MyBatis-Plus中静态工具Db的多种用法及实例分析》本文将详细讲解MyBatis-Plus中静态工具Db的各种用法,并结合具体案例进行演示和说明,具有很好的参考价值,希望对大家有所帮助,如有... 目录MyBATis-Plus中静态工具Db的多种用法及实例案例背景使用静态工具Db进行数据库操作插入

C++ Primer 标准库vector示例详解

《C++Primer标准库vector示例详解》该文章主要介绍了C++标准库中的vector类型,包括其定义、初始化、成员函数以及常见操作,文章详细解释了如何使用vector来存储和操作对象集合,... 目录3.3标准库Vector定义和初始化vector对象通列表初始化vector对象创建指定数量的元素值

springboot3.4和mybatis plus的版本问题的解决

《springboot3.4和mybatisplus的版本问题的解决》本文主要介绍了springboot3.4和mybatisplus的版本问题的解决,主要由于SpringBoot3.4与MyBat... 报错1:spring-boot-starter/3.4.0/spring-boot-starter-

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

SpringBoot基于MyBatis-Plus实现Lambda Query查询的示例代码

《SpringBoot基于MyBatis-Plus实现LambdaQuery查询的示例代码》MyBatis-Plus是MyBatis的增强工具,简化了数据库操作,并提高了开发效率,它提供了多种查询方... 目录引言基础环境配置依赖配置(Maven)application.yml 配置表结构设计demo_st

解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题

《解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题》本文主要讲述了在使用MyBatis和MyBatis-Plus时遇到的绑定异常... 目录myBATis-plus-boot-starpythonter与mybatis-spring-b

Spring Boot 中整合 MyBatis-Plus详细步骤(最新推荐)

《SpringBoot中整合MyBatis-Plus详细步骤(最新推荐)》本文详细介绍了如何在SpringBoot项目中整合MyBatis-Plus,包括整合步骤、基本CRUD操作、分页查询、批... 目录一、整合步骤1. 创建 Spring Boot 项目2. 配置项目依赖3. 配置数据源4. 创建实体类

【C++ Primer Plus习题】13.4

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "port.h"int main() {Port p1;Port p2("Abc", "Bcc", 30);std::cout <<