#include <stdio.h>
#include <string.h>
#include <time.h>
#define PASSWORD "20123456"
struct food_t {
char id[BUFSIZ];
char name[BUFSIZ];
int price;
int start_hour;
int end_hour;
};
static struct food_t foods[BUFSIZ] = {
/*
* 这是C语言初始化结构体的一种方式。就是对应每一个变量都赋值就可以了。如果不在这里写,放到main里面也是可以的
* 不过那样写,就会很乱。
* */
{.id = "A1", .name = "HAM AND EDD", .price = 20, .start_hour = 8, .end_hour = 10},
{.id = "A2", .name = "BACON AND CHEESE", .price = 10, .start_hour = 8, .end_hour = 10},
{.id = "A3", .name = "TUNA SALAD", .price = 15, .start_hour = 8, .end_hour = 10},
{.id = "A4", .name = "BEEF SOUP", .price = 25, .start_hour = 8, .end_hour = 10},
{.id = "B1", .name = "SPICY BEEF BARBECUE", .price = 20, .start_hour = 11, .end_hour = 13},
{.id = "B2", .name = "PORK BARBECUE", .price = 10, .start_hour = 11, .end_hour = 13},
{.id = "B3", .name = "OVEN CHICKEN BARBECUE", .price = 10, .start_hour = 11, .end_hour = 13},
{.id = "B4", .name = "PULLED BEEF BARBECUE BURGER", .price = 25, .start_hour = 11, .end_hour = 13},
{.id = "C1", .name = "SPICY PORK BARBECUE", .price = 20, .start_hour = 17, .end_hour = 20},
{.id = "C2", .name = "VEGETABLE PORK BARBECUE", .price = 10, .start_hour = 11, .end_hour = 13},
{.id = "C3", .name = "OVEN PORK BARBECUE", .price = 15, .start_hour = 11, .end_hour = 13},
{.id = "C4", .name = "PULLED PORK BARBECUE", .price = 25, .start_hour = 11, .end_hour = 13},
};
void show(struct food_t *food, int breakfast, int lunch, int dinner) {
printf("BREAKFAST (Available:8 am - 10 am)\n");
for (int i = 0; i < breakfast; i++) {
printf("%5s %-40s %5d\n", food[i].id, food[i].name, food[i].price);
}
printf("LUNCH (Available:11 am - 1 pm)\n");
for (int i = breakfast; i < breakfast + lunch; i++) {
printf("%5s %-40s %5d\n", food[i].id, food[i].name, food[i].price);
}
printf("DINNER (Available:5 pm - 8 pm)\n");
for (int i = breakfast + lunch; i < breakfast + lunch + dinner; i++) {
printf("%5s %-40s %5d\n", food[i].id, food[i].name, food[i].price);
}
}
struct food_t *select(struct food_t *food, int size, char *target) {
for (int i = 0; i < size; i++) {
if (strcmp(food[i].id, target) == 0) return &food[i];
}
return NULL;
}
int main() {
/*
* fgets()和scanf的用法是一样的,都是获取输入。
* 但是scanf会把回车留到缓冲区中,所以在输入字符串的时候,就容易出问题。
* 这里使用fgets就会把回车一起读进来。
* */
char input_password[BUFSIZ];
while (1) {
printf("Enter password: ");
fgets(input_password, BUFSIZ, stdin);
input_password[strlen(input_password) - 1] = '\0';
if (strcmp(PASSWORD, input_password) == 0) break;
printf("password is error\n");
}
printf("User name: ");
char name[BUFSIZ];
fgets(name, BUFSIZ, stdin);
name[strlen(name) - 1] = '\0';
show(foods, 4, 4, 4);
int num = 0;
while (1) {
printf("Customer Number: ");
scanf("%d", &num);
if (num >= 1 && num <= 6) break;
else {
printf("input number is 1 ~ 6\n");
}
}
fflush(stdin);
struct food_t *choices[num][16];
int choice_size[num];
for (int i = 0; i < num; i++) {
printf("Dish entry for customer %d: \n", i + 1);
int choice = 0;
while (1) {
char buff[BUFSIZ];
printf("Dish Code: ");
fgets(buff, BUFSIZ, stdin);
buff[strlen(buff) - 1] = '\0';
if (strcmp("F", buff) == 0) {
break;
}
struct food_t *target = select(foods, sizeof(foods) /
sizeof(struct food_t), buff);
if (target == NULL) {
printf("This dish code does not exist! Try again!, select = %s\n",
buff);
} else {
time_t rawtime;
struct tm *info;
time(&rawtime);
info = localtime(&rawtime);
if (info->tm_hour >= target->start_hour &&
info->tm_hour < target->end_hour) {
choices[i][choice++] = target;
} else {
printf("This dish is not available now!\n");
}
}
}
choice_size[i] = choice;
}
printf("============================\n");
printf("THANK YOU FOR DINING HERE!\n");
printf("============================\n");
printf("Here is your receipt:\n\n");
double total = 0;
for (int i = 0; i < num; i++) {
printf("Customer %d: \n", i + 1);
printf("Code Dish Price\n");
for (int j = 0; j < choice_size[i]; j++) {
printf("%5s %-40s %5d\n", choices[i][j]->id, choices[i][j]->name,
choices[i][j]->price);
total += choices[i][j]->price;
}
}
printf("============================\n");
printf("TOTAL COST: %.2f\n", total);
double receive = -1;
printf("Cash Received: ");
while (1) {
scanf("%lf", &receive);
if (receive >= total) break;
else {
printf("Change Given: ");
}
}
return 0;
}
09-09
1477
07-08
388