C语言初学者,觉得这道题很有价值去研究,有几个点:
1、代码比这本书上其他的题稍微多点;
2、对于初学者而言不算太难,但是实现起来也不是那么容易。
3、几乎囊括了C语言的所有语法;
4、一个简单的排序算法;
5、结构和指针的使用:
个人认为这道题最精彩的地方在于:排序算法中指向结构的指针的运用
void show_booked(plane list[]) //选项c,显示按字母表顺序排列的订票人信息
{
plane * ptstr[12];
int i, j;
for (i=0;i<12;i++){
ptstr[i] = &list[i];
}
plane *temp;
for (i=0;i<11;i++){ //排序
for (j=i+1;j<12;j++){
if (strcmp(ptstr[i]->firstname, ptstr[j]->firstname) > 0){
temp = ptstr[i];
ptstr[i] = ptstr[j];
ptstr[j] = temp;
}
}
}
for (i=0;i<12;i++){
if (ptstr[i]->booked == 1){
printf("Seat No:%d book by %s.%s \n", (i+1), ptstr[i]->firstname, ptstr[i]->lastname);
}
}
}
show_booked(plane list[])中,构造plane * ptstr[12],排序时仅对指针排序,保留了原有的数据,是一种非常值得借鉴的处理方法。
下面给出这道题的题目和完整代码:
/*
巨人航空公司的机群由座位容量为12的飞机组成.
它每天飞行一个航班.
按照下面的功能,写一个座位预订程序:
A. 程序使用一个含12个结构的数组.
每个结构要包括一个用于标识座位的编号,一个表示座位是否分配出去的标记
座位预订人的姓和名
B. 程序显示下面的菜单:
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
C. 程序应能执行菜单所给出的功能.选择 d) 和 e) 需要额外的输入,
每一个选项都应当允许用户终止输入.
D. 当执行完一个特定的功能之后,程序再次显示菜单,除非选择了 f
E. 每次运行程序都把数据保存到一个文件中,当程序再次运行时,首先从文件中载入数据(如果有的话)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int id;
int booked;
char firstname[20];
char lastname[20];
} plane;
void show_menu(void); //打印菜单
void get_empty(plane list[]);//选项a,显示空余座位的数量
void show_empty(plane list[]);//选项b,显示空余座位列表
void show_booked(plane list[]);//选项c,显示按字母表顺序排列的订票人信息
void book_seat(plane list[]); //选项d,订票
void cancel_seat(plane list[]); //选项e,取消订票
int main()
{
plane list[12] = {};
show_menu();
char selected;
while ((selected = getchar()) != 'f'){
switch (selected)
{
case 'a':
get_empty(list);
break;
case 'b':
show_empty(list);
break;
case 'c':
show_booked(list);
break;
case 'd':
book_seat(list);
break;
case 'e':
cancel_seat(list);
break;
default:
break;
}
while (getchar() != '\n')
continue;
show_menu();
}
return 0;
}
void show_menu(void) //打印菜单
{
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");
}
void get_empty(plane list[]) //选项a,显示空余座位的数量
{
int sum, i;
for (i=0;i<12;i++){
if (list[i].booked == 0)
sum++;
}
printf("There are %d seats empty\n", sum);
}
void show_empty(plane list[]) //选项b,显示空余座位列表
{
int i;
printf("Empty list: ");
for (i=0;i<12;i++){
if (list[i].booked == 0){
printf("%d ", i+1);
}
}
printf("\n");
}
void show_booked(plane list[]) //选项c,显示按字母表顺序排列的订票人信息
{
plane * ptstr[12];
int i, j;
for (i=0;i<12;i++){
ptstr[i] = &list[i];
}
plane *temp;
for (i=0;i<11;i++){ //排序
for (j=i+1;j<12;j++){
if (strcmp(ptstr[i]->firstname, ptstr[j]->firstname) > 0){
temp = ptstr[i];
ptstr[i] = ptstr[j];
ptstr[j] = temp;
}
}
}
for (i=0;i<12;i++){
if (ptstr[i]->booked == 1){
printf("Seat No:%d book by %s.%s \n", (i+1), ptstr[i]->firstname, ptstr[i]->lastname);
}
}
}
void book_seat(plane list[]) //选项d,订票
{
int id;
char fname[20], lname[20];
show_empty(list);
printf("Please select list:");
scanf("%d", &id);
if (list[id-1].booked == 1){
printf("Error selected!\n");
return;
}
list[id-1].id = id;
printf("Please input your firstname lastname.");
scanf("%s %s", fname, lname);
strcpy(list[id-1].firstname, fname);
strcpy(list[id-1].lastname, lname);
list[id-1].booked == 1;
printf("Booked!\n");
}
void cancel_seat(plane list[]) //选项e,取消订票
{
show_booked(list);
int id;
printf("Please select the seat to cancel:");
scanf("%d", &id);
if (list[id-1].booked == 0){
printf("Error selected!\n");
return;
}
list[id-1].booked = 0;
printf("Successfully canceled!");
}