|
发表于 2020-12-18 23:38:47
|
显示全部楼层
本楼为最佳答案
 看起来简单,但是写了两个小时
复制到单个文件中就可以编译运行了
(输出格式没有调好)
- //关闭vs2019不安全函数-4996警告
- #pragma warning(disable : 4996)
- #include<stdio.h>
- //可食用物品
- struct item
- {
- int id;
- char* name;
- double cost;
- };
- //构造可食用物品
- struct item create_item(int id, char* name, double cost) {
- struct item i;
- i.id = id;
- i.name = name;
- i.cost = cost;
- return i;
- }
- //打印菜单
- void print_menu(char* title,struct item items[], int n) {
- printf("/***************%s***************/\n", title);
- printf("ID\t\t\tName\t\t\tCost\n");
- for (int i = 0; i < n; i++) {
- printf("%d\t\t\t%s\t\t\t%f\n", items[i].id, items[i].name, items[i].cost);
- }
- printf("/**********************************/\n");
- }
- //根据选择的id获取食物
- struct item get_item(struct item items[], int n, int id) {
- while (n--)
- if (items[n - 1].id == id)
- return items[n - 1];
- //id不存在的情况下防止出错
- struct item tmp;
- tmp.id = 0;
- tmp.name = "nothing";
- tmp.cost = 0;
- return tmp;
- }
- //主流程
- int main(char* v[], int v_n) {
- struct item foods[5];
- foods[0] = create_item(1, "Curry beef with rice", 40);
- foods[1] = create_item(2, "Sushi set meal", 65);
- foods[2] = create_item(3, "YangZhou fire rice", 45);
- foods[3] = create_item(4, "Sirloin Steak with Spaghetti", 72);
- foods[4] = create_item(5, "Chicken vegetable roll", 42);
- struct item drinks[3];
- drinks[0] = create_item(21, "Soft drink", 10);
- drinks[1] = create_item(22, "Red wine", 15);
- drinks[2] = create_item(23, "Beer", 15);
- struct item selected_items[100];
- int selected_amount = 0;
- //选择食物
- while (selected_amount<100) {
- print_menu("Food Menu", foods, 5);
- printf("\n");
- printf("Please Select Your Food ID:");
- int id;
- scanf("%d",&id);
- struct item selected_food = get_item(foods, 5, id);
- selected_items[selected_amount] = selected_food;
- selected_amount++;
- printf("You have selected %s\n", selected_food.name);
- printf("Do you need more food? 0-No 1-Yes\n");
- int flag;
- scanf("%d",&flag);
- printf("\n");
- if (flag == 0) break;
- }
- //选择饮料
- while (selected_amount < 100) {
- print_menu("Drink Menu", drinks, 3);
- printf("\n");
- printf("Please Select Your drink ID:");
- int id;
- scanf("%d", &id);
- struct item selected_drind = get_item(drinks, 5, id);
- selected_items[selected_amount] = selected_drind;
- selected_amount++;
- printf("You have selected %s\n", selected_drind.name);
- printf("Do you need more drink? 0-No 1-Yes\n");
- int flag;
- scanf("%d", &flag);
- printf("\n");
- if (flag == 0) break;
- }
- double total_cost = 0;
- for (int i = 0; i < selected_amount; i++) {
- total_cost += selected_items[i].cost;
- }
-
- printf("Your total price:%f", total_cost);
- return 0;
- }
复制代码 |
|