struct问题
本帖最后由 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;
char lanme;
};
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 = {0};
int index;
char ch;
for (index = 0; index < SIZE; index++) //座位编号
plane.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.seat_book == 0)
//printf(" %d", num.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.seat_book == 0)
{
printf(" %d", list.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.fname, alp.fname) > 0)
{
temp = alp;
alp = alp;
alp = temp;
}
}
}
for (k = 0; k < SIZE; k++)
{
if (alp.seat_book == 0)
printf("The %d seat is empty.\n", alp.seat_num);
else
printf("seat number:%d, name: %s %s.\n", alp.seat_num, alp.fname, alp.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.fname);
puts("Enter your lname:");
gets(assign.lanme);
assign.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 = temp;
delete.seat_num = num;
//printf("%d\n", delete.seat_num);
}
为什么我输入e后,然后原来结构体里面的内容就会跳到下一个结构体数组当中? 求助一下
页:
[1]