用C语言写一个在visualstudio中能运行的可以根据消费多少进行抽奖的函数,奖品是打折和玩具,再写一个能够实现对小吃进行套餐华的函数
时间: 2024-03-28 07:37:39 浏览: 16
以下是一个用C语言在Visual Studio中能够运行的抽奖函数,根据消费金额进行抽奖,奖品为打折和玩具:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 抽奖函数,money为消费金额
void lottery(int money) {
int discount = 0; // 折扣
char *toy = NULL; // 玩具
// 根据消费金额计算折扣和玩具
if (money >= 100 && money < 500) {
discount = 10;
toy = "小熊";
} else if (money >= 500 && money < 1000) {
discount = 20;
toy = "玩具车";
} else if (money >= 1000) {
discount = 30;
toy = "遥控飞机";
}
// 输出折扣和玩具信息
if (discount > 0) {
printf("恭喜您获得了%d%%的折扣,您可以享受更优惠的价格!\n", discount);
} else {
printf("很遗憾,您没有获得折扣,下次再接再厉!\n");
}
if (toy != NULL) {
printf("恭喜您获得了%s,祝您玩得开心!\n", toy);
} else {
printf("很遗憾,您没有获得玩具,下次再接再厉!\n");
}
}
int main() {
int money;
// 输入消费金额
printf("请输入您的消费金额:");
scanf("%d", &money);
// 调用抽奖函数
lottery(money);
return 0;
}
```
以下是一个用C语言在Visual Studio中能够运行的对小吃进行套餐化的函数:
```c
#include <stdio.h>
// 套餐化函数,price为小吃单价,num为购买数量
void combo(float price, int num) {
float total = price * num; // 总价
int combo_num = num / 3; // 套餐数量
float combo_price = combo_num * 8.0; // 套餐价格
// 输出购买信息
printf("您购买了%d份小吃,共计%.2f元。\n", num, total);
// 判断是否可以组成套餐
if (combo_num > 0) {
printf("恭喜您获得了%d个套餐,共计%.2f元,您可以享受更优惠的价格!\n", combo_num, combo_price);
printf("请您将%d份小吃分为%d组,每组3份,剩余%d份单价购买即可。\n", num, combo_num, num % 3);
} else {
printf("很遗憾,您没有获得套餐,下次再接再厉!\n");
}
}
int main() {
float price;
int num;
// 输入小吃单价和购买数量
printf("请输入小吃单价和购买数量(格式:单价 数量):");
scanf("%f %d", &price, &num);
// 调用套餐化函数
combo(price, num);
return 0;
}
```