Kodewick
開始學 →
基礎←→0 / 70

麵包店的單子

#058基礎int f(struct Item item)約 4 分鐘

麵包店訂單上的一行是一個 struct Item:名字、單價、個數。補好 item_total(item),算這一行多少錢:買 3 個或更多,每個便宜 1 元。main 會算兩行:2 個 bagel、3 個 muffin。

應該顯示
bagel: 8
muffin: 12
main.c
#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;
}
Ctrl + Enter 也能執行
結果

寫好了先點「執行」試試示例,沒問題再點「提交」。