- /*
- The Colossus Airlines fleet consists of one plane with a seating capacity of 12. It makes one flight daily. Write a seating reservation program with the following features:
- The program uses an array of 12 structures. Each structure should hold a seat identification number, a marker that indicates whether the seat is assigned, the last name of the seat holder, and the first name of the seat holder.
- The program displays the following menu:
- To choose a function, enter its letter label:
- a) Show number of empty seats
- b) Show list of empty seats
- c) Show alphabetical list of seats
- d) Assign a customer to a seat assignment
- e) Delete a seat assignment
- f) Quit
- The program successfully executes the promises of its menu. Choices d) and e) require additional input, and each should enable the user to abort an entry.
- After executing a particular function, the program shows the menu again, except for choice f).
- Data is saved in a file between runs. When the program is restarted, it first loads in the data, if any, from the file.
- */
- #include <stdio.h>
- #include <stdlib.h> //for exit()
- #include <string.h> // for strchr()
- #define SEAT 12 // for the number of seat
- #define MAX 30 // for the name array
- #define DATAFILE "data.bat" // for data store
- struct seat{
- int identi;
- char marker;
- char lname[MAX];
- char fname[MAX];
- };
- struct seat airline[SEAT]={
- {1,'N',"null","null"},
- {2,'N',"null","null"},
- {3,'N',"null","null"},
- {4,'N',"null","null"},
- {5,'N',"null","null"},
- {6,'N',"null","null"},
- {7,'N',"null","null"},
- {8,'N',"null","null"},
- {9,'N',"null","null"},
- {10,'N',"null","null"},
- {11,'N',"null","null"},
- {12,'N',"null","null"},
- };
- void menu(void);
- void flush(void); // clean up keyboard buffer
- void initial(void);
- void deal_choice(char);
- void show_number_empty(void);
- void show_list_empty(void);
- void order_seat(void);
- void unorder_seat(void);
- void show_list_alph(void);
- int main(void)
- {
- char choice;
- while(menu(),scanf("%c",&choice)==1 && choice!='f')
- {
- flush();
- initial();
- deal_choice(choice);
- }
- }
- void menu(void)
- {
- puts("=============================================");
- puts("To choose a function,enter its letter label:");
- puts("a) Show number of empty seats");
- puts("b) Show list of empty seats");
- 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");
- puts("=============================================");
- }
- void flush(void)
- {
- while(getchar()!='\\n')
- continue;
- }
- void deal_choice(char ch)
- {
- switch(ch)
- {
- case 'a' : show_number_empty();
- break;
- case 'b' : show_list_empty();
- break;
- case 'c' : show_list_alph();
- break;
- case 'd' : order_seat();
- break;
- case 'e' : unorder_seat();
- break;
- case 'f' : exit(1);
- break;
- default : break;
- }
- }
- void initial(void)
- {
- FILE *fp;
- struct seat temp;
- int i;
- if((fp=fopen(DATAFILE,"ab+"))==NULL)
- {
- fprintf(stderr,"Can't open file %s.\\n",DATAFILE);
- exit(2);
- }
- rewind(fp);
- if(fread(&temp,sizeof(struct seat),1,fp)!=1)
- fwrite(airline,sizeof(struct seat),SEAT,fp); // run programm the first time
- // for(i=0;i<SEAT;i++)
- // {
- // printf("%d========",i);
- // fread(&temp,sizeof(struct seat),1,fp);
- // printf("%d\\t%c\\n",temp.identi,temp.marker);
- // }
- fclose(fp);
- }
- void show_number_empty(void)
- {
- int i;
- int ct; // to count empty seat
- FILE *fp;
- struct seat temp;
- if((fp=fopen(DATAFILE,"rb"))==NULL)
- {
- fprintf(stderr,"Can't open file %s.\\n",DATAFILE);
- exit(2);
- }
- rewind(fp);
- for(ct=0,i=0;i<SEAT;i++)
- {
- fread(&temp,sizeof(struct seat),1,fp);
- if(temp.marker=='N')
- ct++;
- }
- puts("\\n*********************************************");
- if(ct>0)
- printf("\\nNow have %d seats have not ordered.\\n\\n",ct);
- else
- printf("\\nI'm so sorry,all seats has been ordered.\\n\\n");
- puts("*********************************************\\n");
- fclose(fp);
- }
- void show_list_empty(void)
- {
- int i,j;
- FILE *fp;
- struct seat temp;
- if((fp=fopen(DATAFILE,"r+b"))==NULL)
- {
- fprintf(stderr,"Can't open file %s.\\n",DATAFILE);
- exit(3);
- }
- rewind(fp);
- puts("\\n*********************************************\\n");
- for(j=0,i=0;i<SEAT;i++)
- {
- fread(&temp,sizeof(struct seat),1,fp);
- if(temp.marker=='N')
- {
- printf("SEAT %-5d ======>\\tN\\n",temp.identi);
- j++;
- }
- }
- if(j==0)
- {
- printf("I'm so sorry,there is not seat\\n");
- }
- puts("\\n*********************************************\\n");
- fclose(fp);
- }
- void order_seat(void)
- {
- int i;
- int ticket;
- char ch;
- FILE *fp;
- struct seat temp;
- if((fp=fopen(DATAFILE,"rw+b"))==NULL)
- {
- fprintf(stderr,"Can't open file %s.\\n",DATAFILE);
- exit(4);
- }
- rewind(fp);
- printf("Input the seat number you want to order(1 to %d)\\n",SEAT);
- while(scanf("%d",&ticket)==1)
- {
- if(ticket<1 || ticket>12)
- {
- printf("Invalid input(1-%d)",SEAT);
- continue;
- }
- for(i=0;i<SEAT;i++)
- {
- fread(&temp,sizeof(struct seat),1,fp);
- if(ticket==temp.identi && temp.marker=='N')
- {
- printf("SEAT %d have not been ordered,are you sure(y/n)?\\n",temp.identi);
- flush();
- scanf("%c",&ch);
- flush();
- if(strchr("Yy",ch))
- {
- temp.marker='Y';
- puts("Enter your first name.");
- gets(temp.fname);
- puts("Enter your last name.");
- gets(temp.lname);
- fseek(fp,-sizeof(struct seat),SEEK_CUR);
- fwrite(&temp,sizeof(struct seat),1,fp);
- printf("\\nSEAT %d order successfull %s%s\\n\\n",ticket,temp.fname,temp.lname);
- fclose(fp);
- return;
- }
- else
- {
- fclose(fp);
- flush();
- return;
- }
- }
- }
- rewind(fp);
- printf("SEAT %d has already been ordered,choice others.\\n",ticket);
- }
- }
- void unorder_seat(void)
- {
- int i;
- char ch;
- FILE *fp;
- struct seat temp;
- char fname[14];
- char lname[14];
- if((fp=fopen(DATAFILE,"rw+b"))==NULL)
- {
- fprintf(stderr,"Can't open file %s.\\n",DATAFILE);
- exit(5);
- }
- rewind(fp);
- puts("Input your firstname.");
- gets(fname);
- puts("Input your last name.");
- gets(lname);
- for(i=0;i<SEAT;i++)
- {
- fread(&temp,sizeof(struct seat),1,fp);
- if(strcmp(temp.fname,fname)==0 && strcmp(temp.lname,lname)==0)
- {
- printf("%s%s you have ordered seat %d,are you sure unordered?(y/n)",fname,lname,temp.identi);
- scanf("%c",&ch);
- if(strchr("Yy",ch))
- {
- temp.marker='N';
- strcpy(temp.fname,"null");
- strcpy(temp.lname,"null");
- fseek(fp,-sizeof(struct seat),SEEK_CUR);
- fwrite(&temp,sizeof(struct seat),1,fp);
- printf("\\nSEAT %d unorder successfully %s%s\\n\\n",temp.identi,fname,lname);
- fclose(fp);
- flush();
- return;
- }
- else
- {
- flush();
- fclose(fp);
- return;
- }
- }
- }
- printf("\\n%s%s you have not ordered any seat.\\n\\n",fname,lname);
- }
- void show_list_alph(void)
- {
- int i,j,k;
- FILE *fp;
- char fullname[2*MAX];
- struct seat temp;
- struct seat passenger[SEAT];
- if((fp=fopen(DATAFILE,"rb"))==NULL)
- {
- fprintf(stderr,"Can't open file %s.\\n",DATAFILE);
- exit(6);
- }
- for(j=0,i=0;i<SEAT;i++)
- {
- fread(&temp,sizeof(struct seat),1,fp);
- if(strcmp(temp.fname,"null")!=0 && strcmp(temp.lname,"null")!=0)
- passenger[j++]=temp;
- }
- fclose(fp);
- puts("\\n*********************************************\\n");
- if(j==0)
- {
- printf("\\nThere has nobody ordered any seat.\\n\\n");
- puts("\\n*********************************************\\n");
- return;
- }
- for(i=0;i<j;i++)
- for(k=i;k<j;k++)
- {
- if(strcmp(passenger[i].fname,passenger[k].fname)>0)
- {
- temp=passenger[i];
- passenger[i]=passenger[k];
- passenger[k]=temp;
- }
- if(strcmp(passenger[i].fname,passenger[k].fname)==0)
- if(strcmp(passenger[i].lname,passenger[k].lname)>0)
- {
- temp=passenger[i];
- passenger[i]=passenger[k];
- passenger[k]=temp;
- }
- }
- for(i=0;i<j;i++)
- {
- strcpy(fullname,passenger[i].fname);
- strcat(fullname,passenger[i].lname);
- printf("%-30s ordered SEAT %d\\n",fullname,passenger[i].identi);
- }
- puts("\\n*********************************************\\n");
- }
- //该片段来自于http://www.codesnippet.cn/detail/040920135595.html
来源: http://www.codesnippet.cn/detail/040920135595.html