c编程
Tofloor
poster avatar
huanhuachengfeng
deepin
2016-09-07 01:54
Author
本帖最后由 huanhuachengfeng 于 2016-9-6 17:56 编辑

/* 源码 */
#include
#include
#include
#include
#define LEN 14
#define SEATS 12
#define EMPTY 0
#define TAKEN 1
#define CONTINUE 1
#define DONE 0
struct planestats {
int seat_id;
int status;
char last[LEN];
char first[LEN];
};
int getmenu(void);
int getlet(const char *);
int openings(const struct planestats [], int);
void show_empties(const struct planestats [], int);
void list_assign(struct planestats *[], int);
void assign_seat(struct planestats [], int);
void delete_seat(struct planestats [], int);
void show_seats(const struct planestats [], int);
void sort(struct planestats *[], int);
void makelist(const struct planestats [], char *, int);
char * s_gets(char * st, int n);
int main(void)
{
struct planestats plane_1[SEATS], *ps[SEATS];
int choice;
int i;
FILE *fp;
size_t size = sizeof(struct planestats);
for ( i = 0; i < SEATS; i++)
ps = &plane_1;
if ((fp = fopen("air.dat", "rb")) == NULL )
for (i = 0; i < SEATS; i++)
{
plane_1.status = EMPTY;
plane_1.seat_id = i + 1;
}
else
{
fread(plane_1, size, SEATS, fp);
fclose(fp);
}
while ( (choice = getmenu() ) != 'q')
{
switch (choice)
{
case 'o': printf ("现在有 %d 个空座位.\n",
openings(plane_1, SEATS));
break;
case 'e': show_empties(plane_1, SEATS);
break;
case 'l': list_assign(ps, SEATS);
break;
case 'a': assign_seat(plane_1, SEATS);
break;
case 'd': delete_seat(plane_1, SEATS);
break;
default : puts("Switch trouble");
break;
}
}
if((fp = fopen("air.dat", "wb")) == NULL )
puts("Can't save data to file.");
else
{
fwrite(plane_1, size, SEATS, fp);
fclose(fp);
}
puts("再见!欢迎下次光临。");
return 0;
}
#define CHOICES 6
int getmenu(void)
{
const char *descript[CHOICES] = {
"显示有多少空座位",
"显示空座位列表",
"依字母顺序显示乘客列表",
"为客户分配指定座位",
"删除一个指定座位",
"退出"
};
const char labels[CHOICES + 1] = "oeladq";
int i;
puts("请选择功能, 输入他前面的字母");
for (i = 0; i < CHOICES; i++)
printf("%c) %s\n", labels, descript);
return getlet(labels);
}
int getlet(const char * s)
{char c;
c = getchar();
while (strchr(s, c) == NULL)
{
printf ("Enter a character in the list %s\n", s);
while( getchar() != '\n')
continue;
c = getchar();
}
while (getchar() != '\n')
continue;
return c;
}
int openings(const struct planestats pl[], int n)
{
int count = 0;
int seat;
for (seat = 0; seat < n; seat++)
if (pl[seat].status == EMPTY)
count++;
return count;
}
void show_empties(const struct planestats pl[], int n)
{
char seating[3* SEATS];
if ( openings(pl,n) == 0)
puts("All seats are assigned");
else
{
puts("下面的座位号是可获得的:");
makelist(pl, seating, EMPTY);
puts (seating) ;
}
}
void makelist(const struct planestats pl[], char * str, int kind)
{
int seat;
char temp[LEN];
str[0] = '\0';
for (seat = 0; seat < SEATS; seat++)
if (pl[seat].status == kind)
{
sprintf(temp," %d", pl[seat].seat_id);
strcat(str, temp);
}
}
void list_assign(struct planestats *ps[], int n)
{
int i;
if (openings(*ps, n) == SEATS)
puts("所有的座位都是空的.");
else
{
sort(ps, n);
for(i = 0; i < SEATS; i++)
if ( ps->status == TAKEN )
printf ("Seat %d: %s, %s\n",
ps->seat_id, ps->last, ps->first);
}
}
void assign_seat(struct planestats pl[], int n)
{
char list[3 * SEATS];
int seat, loop;
if (openings(pl,n) == 0)
puts("All seats are assigned.");
else
{
makelist(pl,list, EMPTY);
puts("您想从列表选择你想要的座位:");
puts (list) ;
do
{
while( scanf("%d", &seat) != 1)
{
scanf("%*s");
puts("Enter a number from this list:");
puts (list) ;
}
if (seat < 1 || seat > SEATS ||
pl[seat-1].status == TAKEN)
{
puts("Enter a number from this list:");
puts (list) ;
loop = CONTINUE;
}
else
loop = DONE;
} while (loop == CONTINUE);
while (getchar() != '\n')
continue;
puts("贵姓:");
s_gets (pl[seat - 1].first, LEN);
puts("名字:");
s_gets (pl[seat - 1].last, LEN);
printf("%s %s assigned to seat %d.\n",
pl[seat - 1].first, pl[seat - 1].last, seat);
puts("同意请输入a, 取消输入c.");
if (getlet("ac") == 'a')
{
pl[seat - 1].status = TAKEN;
puts("assenger assigned to seat.");
}
else
puts("assenger not assigned.");
}
}
void delete_seat(struct planestats pl[], int n)
{
int seat, loop;
char list[3 * SEATS];
if (openings(pl, n) == SEATS)
puts("所有座位都是空的.");
else
{
show_seats(pl, n);
makelist(pl, list, TAKEN);
puts("输入要取消的座位号:");
do
{
while( scanf("%d", &seat) != 1)
{
scanf("%*s");
puts("Enter a number from this list:");
puts (list) ;
}
if (seat < 1 || seat > SEATS ||
pl[seat-1].status == EMPTY)
{
puts("Enter a number from this list:");
puts (list) ;
loop = CONTINUE;
}
else
loop = DONE;
} while (loop == CONTINUE);
while (getchar() != '\n')
continue;
printf("%s %s to be canceled for seat %d.\n",
pl[seat - 1].first, pl[seat - 1].last, seat);
puts("删除请输入d, 终止请输入a.");
if ( getlet("da") == 'd')
{
pl[seat - 1].status = EMPTY;
puts ("assenger dropped.");
}
else
puts("assenger retained.");
}
}
void show_seats(const struct planestats pl[], int n)
{
int i;
puts("Seats currently taken:");
for (i = 0; i < SEATS; i++)
if (pl.status == TAKEN)
printf("Seat %d: %s, %s\n", pl.seat_id,
pl.last, pl.first);
}
void sort(struct planestats *array[], int limit)
{
int top, search;
struct planestats * temp;
for (top = 0; top < limit -1; top++)
for (search = top + 1; search < limit; search++)
if (strcmp(array[search]->last, array[top]->last) < 0)
{
temp = array[search];
array[search] = array[top];
array[top] = temp;
}
}
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'); // look for newline
if (find) // if the address is not NULL,
*find = '\0'; // place a null character there
else
while (getchar() != '\n')
continue; // dispose of rest of line
}
return ret_val;
}


Reply Favorite View the author
All Replies
avatar
moli3
deepin
2016-09-07 02:19
#1
你这是要干啥?!
Reply View the author
avatar
huanhuachengfeng
deepin
2016-09-07 02:34
#2
本帖最后由 huanhuachengfeng 于 2016-9-6 18:55 编辑

自己试试ba
Reply View the author