Kodewick
学びはじめる →
基礎←→0 / 70

パン屋さんの注文

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

パン屋さんの注文の 1 行は struct Item で、名前・値段・個数を持ちます。item_total(item) を完成させて、その行の金額を計算しましょう。3 個以上買うと 1 個あたり 1 安くなります。main では 2 つの行(ベーグル 2 個とマフィン 3 個)を試します。

表示されるべき内容
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;
    // ここにコードを書く:qty が 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 でも実行できます
結果

実行すると例で試せます。よさそうなら提出しましょう。