面包店订单上的一行是一个 struct Item:名字、单价、个数。补好 item_total(item),算这一行多少钱:买 3 个或更多,每个便宜 1 元。main 会算两行:2 个 bagel、3 个 muffin。
bagel: 8 muffin: 12
#include <stdio.h>
struct Item {
const char *name;
int price;
int qty;
};
int item_total(struct Item item) {
int price = item.price;
// 在这里写:个数不少于 3,price 就减 1
return price * item.qty;
}
int main(void) {
struct Item bagels = {"bagel", 4, 2};
struct Item muffins = {"muffin", 5, 3};
printf("%s: %d\n", bagels.name, item_total(bagels));
printf("%s: %d\n", muffins.name, item_total(muffins));
return 0;
}
写好了先点「运行」试试示例,没问题再点「提交」。