//CPP 14.18-1
#include <stdio.h>
#include <string.h>
char * s_gets(char *st,int n);
int days(const char ar[]);
typedef struct
{
char name[10];
int days;
}month;
month months[12] =
{
{"January",31},
{"February",28},
{"March",31},
{"April",30},
{"May",31},
{"June",30},
{"July",31},
{"August",31},
{"september",30},
{"October",30},
{"Novemember",30},
{"December",31}
};
int main(void)
{
char input[10];
int total;
printf("请输入您要计算的月份名:");
while (s_gets(input,10) != NULL && input[0] != '\n')
{
printf("您输入的月份是: %s\n",input);
total = days(input);
if (total != -1)
printf("计算结果为:%d 天\n",total);
else
printf("您输入的月份名称不存在!\n");
printf("请输入下一个您要计算的月份名:");
}
printf("再见!\n");
return 0;
}
int days(const char ar[])
{
int index,total;
for (index = 0;index < 12 ; index++)
{
if (strcmp(months[index].name,ar) == 0)
break;
}
if (index == 12)
return -1;
else
{
int i = 0;
for (i = 0,total = 0;i < index ;i++ )
total += months[i].days;
}
return total;
}
char * s_gets(char *st,int n)
{
char * ret_val;
char * find;
ret_val = fgets(st,n,stdin);
if (ret_val)
{
find = strchr(st,'\n');
if (find)
*find = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
//CPP 14.18-8
#include <stdio.h>
#include <stdlib.h>
#define LEN 20
typedef struct _planestats {
int seat_id;
int status;
char last[LEN];
char first[LEN];
}Planestats;
char getmenu();
void show_empties(const Planestats seat [], int n);
void show_orderlist(const Planestats list [],int n);
void order_seat(Planestats ptro [],Planestats ptrp [], int * p_ct);
void order_del(Planestats ptro [],Planestats ptrp [], int * p_ct);
int main(void)
{
char choice;
int ct_order = 0; //记录用户已经定的座位数
int ct_empty; //记录剩余座位数
Planestats planeseat[12] = {{1,0,'\0','\0'},{2,0,'\0','\0'},{3,0,'\0','\0'},
{4,0,'\0','\0'},{5,0,'\0','\0'},{6,0,'\0','\0'},{7,0,'\0','\0'},{8,0,'\0','\0'},
{9,0,'\0','\0'},{10,0,'\0','\0'},{11,0,'\0','\0'},{12,0,'\0','\0'}}; //初始化座位信息
Planestats orderlist[12] = {{1,0,'\0','\0'},{2,0,'\0','\0'},{3,0,'\0','\0'},
{4,0,'\0','\0'},{5,0,'\0','\0'},{6,0,'\0','\0'},{7,0,'\0','\0'},{8,0,'\0','\0'},
{9,0,'\0','\0'},{10,0,'\0','\0'},{11,0,'\0','\0'},{12,0,'\0','\0'}}; //初始化座位信息
choice = getmenu();
while (choice != 'e')
{
switch (choice)
{
case 'a': //显示剩余座位信息
show_empties(planeseat,12);
break;
case 'b': //显示您当前预定座位的信息
show_orderlist(orderlist,ct_order);
break;
case 'c': //预定一个座位
order_seat(orderlist,planeseat,&ct_order);
break;
case 'd': //删除一个预定的座位
order_del(orderlist,planeseat,&ct_order);
break;
}
choice = getmenu();
}
printf("再见!\n");
return 0;
}
void order_del(Planestats ptro [],Planestats ptrp [], int * p_ct)
{
int id;
printf("请选择您要删除的座位编号:\n");
while (scanf("%d",&id) == 1 && (id > 0 && id < 12))
{
fflush(stdin);
if (ptrp[id - 1].status == 1)
{
(*p_ct)--;
ptrp[id - 1].status = 0;
ptro[id - 1].status = 0;
break;
}
else
printf("您未预定该座位,请重新选择已经预定的座位编号.\n");
}
}
void order_seat(Planestats ptro [],Planestats ptrp [], int * p_ct)
{
int id;
printf("请输入您要预定的座位编号:\n");
while (scanf("%d",&id) == 1 && (id >= 1 && id <= 12))
{
fflush(stdin);
if (ptrp[id - 1].status == 0)
{
(*p_ct)++;
ptrp[id - 1].status = 1;
ptro[id - 1].status = 1;
printf("请输入预订人的姓氏:\n");
scanf("%s",&ptro[id - 1].first);
printf("请输入预订人的名:\n");
scanf("%s",&ptro[id - 1].last);
break;
}
else
printf("该座位已经被预定,请预定其他座位.\n");
}
}
void show_orderlist(const Planestats list [],int n)
{
int index;
if (n == 0)
printf("您当前还没有预定座位!\n");
else
{
printf("您已经预定的座位信息:\n");
printf(" 座位编号 预订人姓名\n");
for (index = 0;index < 12 ;index++ )
if (list[index].status == 1)
printf(" %-8d %s%s\n",list[index].seat_id,list[index].first,list[index].last);
}
}
void show_empties(const Planestats seat [], int n)
{
int index;
int count = 0;
printf(" 未被预定的座位编号:\n");
for (index = 0;index < n; index++ )
{
if (seat[index].status == 0)
{
count++;
printf(" %d\t",seat[index].seat_id);
}
}
putchar('\n');
printf("当前一共剩余空座位数量为 %d\n",count);
}
char getmenu()
{
char choice;
puts("*************************************");
puts("请选择一个功能(输入对应的字母标签):");
puts("a).显示剩余座位信息");
puts("b).显示您当前预定座位的信息");
puts("c).预定一个座位");
puts("d).删除一个预定的座位");
puts("e).退出");
puts("*************************************");
choice = getchar();
while (choice < 'a' || choice > 'e')
{
printf("请输入a,b,c,d,e中的一个: \n");
fflush(stdin);
choice = getchar();
}
fflush(stdin);
return choice;
}