|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 hickttye 于 2019-8-3 15:55 编辑
#include <stdio.h>
#define LEN 10
#define SIZE 12
struct seat {
int seat_num;
int seat_book;
char fname[LEN];
char lanme[LEN];
};
char meun(void);
void show_num(struct seat num[]);
void show_list(struct seat list[]);
void show_alp(struct seat alp[]);
void assign_seat(struct seat assign[]);
void delete_seat(struct seat *delete);
int main(void)
{
struct seat plane[SIZE] = {0};
int index;
char ch;
for (index = 0; index < SIZE; index++) //座位编号
plane[index].seat_num = index;
ch = meun();
while (ch != 'f')
{
switch (ch)
{
case 'a':
show_num(plane);
break;
case 'b':
show_list(plane);
break;
case 'c':
show_alp(plane);
break;
case 'd':
assign_seat(plane);
break;
case 'e':
delete_seat(&plane);
break;
}
ch = meun();
}
getchar();
return 0;
}
char meun(void)
{
char ch;
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");
ch = getchar();
getchar();
return ch;
}
void show_num(struct seat num[])
{
int index,count = 0;
for (index = 0; index < SIZE; index++)
{
//printf("The empty seat number is ");
if (num[index].seat_book == 0)
//printf(" %d", num[index].seat_num);
count++;
}
if (count == 0)
printf("There is no empty seat.\n");
else if (count == 1)
printf("There is 1 empty seat.\n");
else
printf("There are %d empty seat.\n", count);
}
void show_list(struct seat list[])
{
int index, count = 0;
printf("The empty seat number is ");
for (index = 0; index < SIZE; index++)
{
if (list[index].seat_book == 0)
{
printf(" %d", list[index].seat_num);
count++;
}
}
putchar('\n');
if (count == 0)
printf("There is no empty seat.\n");
}
void show_alp(struct seat alp[])
{
int i, j, k;
struct seat temp;
for (i = 0; i < SIZE - 1; i++)
{
for (j = i + 1; j < SIZE; j++)
{
if (strcmp(alp[i].fname, alp[j].fname) > 0)
{
temp = alp[i];
alp[i] = alp[j];
alp[j] = temp;
}
}
}
for (k = 0; k < SIZE; k++)
{
if (alp[k].seat_book == 0)
printf("The %d seat is empty.\n", alp[k].seat_num);
else
printf("seat number:%d, name: %s %s.\n", alp[k].seat_num, alp[k].fname, alp[k].lanme);
}
}
void assign_seat(struct seat assign[])
{
int num;
puts("Enter the number what you assign:");
scanf("%d", &num);
getchar();
puts("Enter your fname:");
gets(assign[num].fname);
puts("Enter your lname:");
gets(assign[num].lanme);
assign[num].seat_book = 1;
}
void delete_seat(struct seat *delete) //最重要是这里删除不掉结构里面的内容,例如:按e执行,输入5,原来结构数组5里面的内容,就会移植到结构数组6中
{
int num;
struct seat temp = {0,0,"",""};
puts("Enter the number what you delete:");
scanf("%d", &num);
getchar();
delete[num] = temp;
delete[num].seat_num = num;
//printf("%d\n", delete[num].seat_num);
}
为什么我输入e后,然后原来结构体里面的内容就会跳到下一个结构体数组当中? |
|