给你一个简单的程序。只是关于第三个功能:Show alphabetical list of seats,说实话,我不知道到底想要什么。有关部分,你自己添上吧。
用了一个结构体数组,没用链表,当然如果改成用链表实现更好些,改动里面的部分代码就可以了。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct seats{
char id; //编号
char tag; //是否售出的标记,0:售出;1:未售出
char first_name[100];
char last_name[100];
};
struct airplane{
int empty; //空座数量
struct seats *head; //座位链表头指针
};
struct airplane example;
struct seats more_seats[12];
void fun1();
void fun2();
void fun3();
void fun4();
void fun5();
void show_empty_number(struct airplane);
void show_empty_list(struct airplane);
void assign_customer_seat(char*,char*,struct airplane*);
void delete_seat_assign(char,struct airplane*);
void msg()
{
printf("################################################\n");
printf("To choose a function, enter its letter label:\n");
printf("a) Show number of empty seats\n");
printf("b) Show list of empty seats\n");
printf("c) Show alphabetical list of seats\n");
printf("d) Assign a customer to a seat assignment\n");
printf("e) Delete a seat assignment\n");
printf("f) Quit\n");
printf("################################################\n");
}
void gogogo()
{
char ch=0,id=0;
char first_name[100]="\0";
char last_name[100]="\0";
printf("\n");
while(ch!='f')
{
printf(">>");
ch=getch();
if(ch==10){
printf("\n");
continue;
}
putchar(ch);
printf("\n");
switch(ch){
case 'a': fun1(); break;
case 'b': fun2(); break;
case 'c': fun3(); break;
case 'd': fun4(); break;
case 'e': fun5(); break;
case 'f': break;
}
}
}
void fun1()
{
show_empty_number(example);
}
void fun2()
{
show_empty_list(example);
}
void fun3()
{
printf("In fact, I do not know what you want to do exactly. Sorry.\n");
}
void fun4()
{
char first_name[100]="\0";
char last_name[100]="\0";
printf("Input the first name: ");
scanf("%s",first_name);
printf("Input the last name: ");
scanf("%s",last_name);
assign_customer_seat(first_name,last_name,&example);
free(first_name);
free(last_name);
}
void fun5()
{
int id=0;
if(example.empty==12)
{
printf("Sorry. There is no assignment.\n");
return ;
}
printf("Input the seat's id: ");
scanf("%d",&id);
getchar();
delete_seat_assign(id,&example);
}
void show_empty_number(struct airplane air)
{
printf("Empty number: %d\n",air.empty);
}
void show_empty_list(struct airplane air)
{
struct seats *tmp;
int i=0,j=0;
if(air.head!=NULL)
tmp=air.head;
else return ;
while(i<12)
{
if((tmp+i)->tag==0)
{
printf("%d ",(tmp+i)->id);
j++;
if(j==air.empty)
break;
}
i++;
}
printf("\n");
}
void assign_customer_seat(char* first_name,char *last_name,struct airplane *air)
{
struct seats *tmp,newseat;
int i=0,j=0,newid=0;
if(air->empty==0)
{
printf("Sorry, seats already full. Please try soon.\n");
return ;
}
newid=air->head->id;
tmp=air->head;
while(i<12)
{
if((tmp+i)->tag==0)
{
printf("assign id: %d\n",i);
(tmp+i)->tag=1;
strcpy((tmp+i)->first_name,first_name);
strcpy((tmp+i)->last_name,last_name);
break;
}
i++;
}
air->empty-=1;
}
void delete_seat_assign(char del_id,struct airplane *air)
{
struct seats *tmp;
char i=0;
tmp=air->head;
while(i<12)
{
if(((tmp+i)->id==del_id)&&((tmp+i)->tag==1))
{
(tmp+i)->tag=0;
free((tmp+i)->first_name);
free((tmp+i)->last_name);
air->empty++;
printf("ok\n");
return ;
}
if((tmp+i)->id==del_id&&(tmp+i)->tag==0)
{
printf("This seats not assigned.\n");
return ;
}
i++;
}
}
int main()
{
char i=0;
for(i=0;i<12;i++)
{more_seats[i].id=i+1;
more_seats[i].tag=0;
}
example.empty=12;
example.head=more_seats;
msg();
gogogo();
return 0;
}