回复 12楼 心灯甚亮
这个偶的vc6.0编译.c文件通不过,定义变量需要在块的开头,然后.cpp文件还是木有通过,
system无法解析,偶这编译器无语了,加了头文件才是出来了,终于可以看到效果了,学习下!
Stay hungry , Stay foolish!
/* 要为一个餐厅做顾客点餐和买单的程序,要求要用函数、循环和指针。具体步骤是: 1 输入选项:食物有 fish($3), chips($2), hamburgers($3), cheese($4)(顾客只有点了hamburgers时才询问),饮料有 三种soft drinks($1), coffee/tea($1),税率为5%; 2 输出价钱选项有:total food(食物),total drink (饮料),total order(订餐总价),tax(税)、total owing(应收)、 Amount tendered(收钱),change(找零)。 */ #include <stdio.h> #include "utility.h" // 食品数据结构 struct food_t { int id; // 编码 char name[20]; // 名称 double price; // 价格 int bound; // 捆绑食品的编码 bool enabled; // 可用性 }; // 饮料数据结构 struct drink_t { int id; // 编码 char name[20]; // 名称 double price; // 价格 }; // 订单数据结构 struct order_t { int type; // 品类编码: 0-食品 1-饮料 int id; // 物品编码 int quantity; // 数量 }; static food_t foods[4] = { { 1, "fish", 3.0, 0, true }, { 2, "chips", 2.0, 0, true }, { 3, "hamburgers", 3.0, 0, true }, { 4, "cheese", 4.0, 3, false } }; static const drink_t drinks[3] = { { 1, "soft drinks", 1.0 }, { 2, "coffee", 1.0 }, { 3, "tea", 1.0 } }; static const double tax_rate = 0.05; // 税率 static order_t order_list[10]; // 购物清单,每单不超过10项 static int order_index = 0; // 购物当前序数 void initialization(void); int order_menu(const int type); void check_bound(void); void order(const int type, const int id); void display_account(void); void account(void); // 程序主入口 int main(void) { int choice; do { cls(); display_account(); printf_s("[1] food\n"); printf_s("[2] drink\n"); printf_s("[3] account\n"); printf_s("[0] exit\n"); printf_s("Choice: "); choice = gets_int(1); switch (choice) { case 1: case 2: order_menu(choice - 1); break; case 3: account(); break; default: break; } } while (choice != 0); return 0; } // 初始化系统 void initialization(void) { // 复位订单项目计数器 order_index = 0; // 恢复原始菜单,没有绑定项的食品,初始时不显示 for (int index = 0; index < sizeof(foods) / sizeof(food_t); ++index) { if (foods[index].bound > 0) { foods[index].enabled = false; } } } // 物品选择菜单 int order_menu(const int type) { int choice; do { cls(); display_account(); int index; switch (type) { case 0: for (index = 0; (index < sizeof(foods) / sizeof(food_t)) && foods[index].enabled; ++index) { printf_s("[%2d] %-20s $%-10.2f\n", foods[index].id, foods[index].name, foods[index].price); } break; case 1: for (index = 0; index < sizeof(drinks) / sizeof(drink_t); ++index) { printf_s("[%2d] %-20s $%-10.2f\n", drinks[index].id, drinks[index].name, drinks[index].price); } break; default: break; } printf_s("[ 0] return\n"); printf_s("Choice: "); choice = gets_int((index % 10) + 1); if ((choice > 0) && (choice <= index)) { order(type, choice); if (type == 0) { check_bound(); } } } while (choice != 0); return choice; } // 检查绑定食品 void check_bound(void) { for (int i = 0; i < sizeof(foods) / sizeof(food_t); ++i) { if (foods[i].bound > 0) { for (int index = 0; index < order_index; ++index) { if ((order_list[index].type == 0) && (order_list[index].id == foods[i].bound)) { foods[i].enabled = true; } } } } } // 输入物品数量,数量为零时取消当前物品 void order(const int type, const int id) { int quantity; putchar('\n'); printf_s("%-20s\n", (type == 0) ? foods[id - 1].name : drinks[id - 1].name); do { printf_s("Please input quantity: "); } while ((quantity = gets_int(4)) < 0); if (quantity > 0) { order_list[order_index].type = type; order_list[order_index].id = id; order_list[order_index].quantity = quantity; ++order_index; } } // 显示购物清单 void display_account(void) { for (int index = 0; index < order_index; ++index) { const char* name = (order_list[index].type == 0) ? foods[order_list[index].id - 1].name : drinks[order_list[index].id - 1].name; double price = (order_list[index].type == 0) ? foods[order_list[index].id - 1].price : drinks[order_list[index].id - 1].price; int quantity = order_list[index].quantity; printf_s("%02d. %-20s $%-10.2f %4d $%-10.2f \n", index + 1, name, price, quantity, price * quantity); } printf_s("\n\n"); } // 结算 void account(void) { cls(); display_account(); double total_food = 0.0; double total_drink = 0.0; for (int index = 0; index < order_index; ++index) { switch (order_list[index].type) { case 0: total_food += foods[order_list[index].id - 1].price * order_list[index].quantity; break; case 1: total_drink += drinks[order_list[index].id - 1].price * order_list[index].quantity; break; default: break; } } double total_order = total_food + total_drink; double tax = total_order * tax_rate; double total_owing = total_order + tax; printf_s(" Total food: $%.2f\n", total_food); printf_s("Total drink: $%.2f\n", total_drink); printf_s("Total order: $%.2f\n", total_order); printf_s(" Tax: $%.2f\n", tax); printf_s("Total owing: $%.2f\n", total_owing); putchar('\n'); printf_s("Now account? (Y/N)"); if (get_bool()) { double amount_tendered; do { printf_s("Input amount tendered: "); amount_tendered = gets_double(20); } while (amount_tendered < total_owing); putchar('\n'); printf_s("Amount tendered: $%.2f\n", amount_tendered); printf_s(" Change: $%.2f\n", amount_tendered - total_owing); Pause(); initialization(); } }
#ifndef _UTILTITY #define _UTILTITY // 清空控制台屏幕 void cls(void); // 暂停程序 void Pause(void); // 从控制台读入指定位数的整数,可含负号 int gets_int(const int length); // 从控制台读入指定位数的浮点数,可含负号 double gets_double(const int length); // 从控制台读入逻辑值,只接受y/n形式的输入 bool get_bool(void); #endif