#include <stdio.h>
#include <string.h>
#define MaxSize 20
#define NAME_SIZE 41 //存放客户名,尺寸放大点,比如少数民族客户名
#define SIZE 10
struct guest_info{
char name[NAME_SIZE];
int sum;
char time[SIZE];
int number;
}GuestList[MaxSize];
void Insert(int *);
void Search(int);
void Update(int);
void Delete(int *);
void Show(int);
int main(void)
{
int i;
int count = 0;
do
{
printf("\n");
printf("1---插入(Insert)\n");
printf("2---查询(Search)\n");
printf("3---修改(Update)\n");
printf("4---删除(Delete)\n");
printf("5---显示(Show)\n");
printf("6---退出(Exit)\n");
scanf_s("%d", &i, SIZE); //scanf_s("%d", &i);
switch (i)
{
case 1:Insert(&count);
break;
case 2:Search(count);
break;
case 3:Update(count);
break;
case 4:Delete(&count);
break;
case 5:Show(count);
break;
case 6:
break;
default:printf("错误!请重选");
break;
}
} while (i != 6);
return 0;
}
void Insert(int * count)
{
int i, in_number;
if (*count == MaxSize)
{
printf("空间已满!");
return;
}
printf("请输入编号:");
scanf_s("%d", &in_number, SIZE); //scanf("%d", &in_number);
for (i = 0; i < *count; i++)
{
if (GuestList[i].number == in_number)
{
printf("已有相同的编号:");
return;
}
}
GuestList[i].number = in_number;
printf("请输入姓名:");
scanf_s("%s", GuestList[i].name, NAME_SIZE); //scanf("%s", GuestList[i].name);
printf("请输入人数:");
scanf_s("%d", &GuestList[i].sum, SIZE); //scanf("%d", &GuestList[i].sum);
printf("请输入用餐时间:");
scanf_s("%s", GuestList[i].time, SIZE); //scanf("%s", GuestList[i].time);
(*count)++;
}
void Search(int count)
{
int i, number, flag = 1;
printf("请输入要查询的编号: ");
scanf_s("%d", &number, 10); //scanf("%d", &number);
for (i = 0; i < count&&flag; i++)
{
if (GuestList[i].number == number)
{
printf("姓名:%s\n", GuestList[i].name);
printf("人数:%d\n", GuestList[i].sum);
printf("用餐时间:%s\n", GuestList[i].time);
flag = 0;
}
}
if (flag)
printf("没有查询到!!");
}
void Update(int count)
{
int i, number, flag = 1;
printf("请输入要修改数据的编号:");
scanf_s("%d", &number, SIZE); //scanf("%d", &number);
for (i = 0; i < count&&flag; i++)
{
if (GuestList[i].number == number)
{
printf("请输入人数:");
scanf_s("%d", &GuestList[i].sum, SIZE); //scanf("%d", &GuestList[i].sum);
printf("请输入用餐时间:");
scanf_s("%s", GuestList[i].time, SIZE); //scanf("%s", GuestList[i].time);
flag = 0;
}
}
if (flag)
printf("没有查询到可以修改的数据!!\n");
}
void Delete(int * count)
{
int i, j, number, flag = 1;
printf("请输入要删除数据的编号:");
scanf_s("%d", &number, SIZE); //scanf("%d", &number);
for (i = 0; i<*count&&flag; i++)
{
if (GuestList[i].number == number)
{
for (j = i; i<*count - 1; j++)
GuestList[j] = GuestList[j + 1];
flag = 0;
(*count)--;
}
}
if (flag)
printf("没有查询到可以删除的数据!!");
}
void Show(int count)
{
int i;
printf("\n");
printf(" 编号 姓名 人数 用餐时间\n");
for (i = 0; i<count; i++)
{
printf("%10d", GuestList[i].number);
printf("%12s", GuestList[i].name);
printf("%10d", GuestList[i].sum);
printf("%12s\n", GuestList[i].time);
}
}