编写一个程序10.现有一购物车列表如下: carts=[{name:床, price:1999, num:1}, {name:枕头, price:10,num:2}, {name:被子, price:20, num:1}] 要求通过用户输入总资产,例如:3000,然后显示购物车列表,计算购物车商
时间: 2023-03-26 20:02:38 浏览: 83
品的总价值和剩余资产。以下是程序的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct cart {
char name[20];
int price;
int num;
};
int main() {
struct cart carts[3] = {
{"床", 1999, 1},
{"枕头", 10, 2},
{"被子", 20, 1}
};
int total_money, remain_money, total_price = ;
printf("请输入您的总资产:");
scanf("%d", &total_money);
remain_money = total_money;
printf("您的购物车列表如下:\n");
for (int i = ; i < 3; i++) {
printf("%s\t%d元\t%d个\n", carts[i].name, carts[i].price, carts[i].num);
total_price += carts[i].price * carts[i].num;
}
printf("购物车商品总价值为:%d元\n", total_price);
remain_money -= total_price;
printf("剩余资产为:%d元\n", remain_money);
return ;
}
阅读全文