关于c语言编程实现超市购物

img


编程实现超市购物,需要其构建三个情景,分别是在家,在超市,交易支付,自己实在百思不得其解,自己c语言比较弱,只能说通过编码再去一步一步推一下,希望可以得到帮助,谢谢!

gpt的回答

#include <stdio.h>

int main() {
    int walletAmount = 0;
    int choice, quantity;
    int itemPrices[] = {10, 20, 30, 40}; // 商品价格数组,可以根据需要进行修改

    printf("请输入您携带的金额数目:");
    scanf("%d", &walletAmount);
    printf("\n");

    while (1) {
        printf("请选择商品 (1-4),输入0结束购物:");
        scanf("%d", &choice);

        if (choice == 0) {
            break;
        } else if (choice < 1 || choice > 4) {
            printf("无效的选项,请重新选择!\n\n");
            continue;
        }

        printf("请输入购买数量:");
        scanf("%d", &quantity);

        if (quantity <= 0) {
            printf("购买数量必须大于0,请重新选择!\n\n");
            continue;
        }

        int totalPrice = itemPrices[choice - 1] * quantity;
        if (totalPrice > walletAmount) {
            printf("金额不足!购买失败。\n\n");
        } else {
            walletAmount -= totalPrice;
            printf("购买成功!剩余金额:%d\n\n", walletAmount);
        }
    }

    printf("感谢您的购物!祝您生活愉快!\n");

    return 0;
}