用c语言写一些能够运用到快餐点餐系统的函数,要求有创意,
时间: 2024-03-21 09:40:06 浏览: 23
好的,以下是针对快餐点餐系统的一些创意函数:
1. 计算订单总价并输出
```c
#include <stdio.h>
float calculate_total(float price, int quantity)
{
return price * quantity;
}
void print_total(float total)
{
printf("Total price: %.2f\n", total);
}
```
该函数使用商品单价和数量计算订单总价,并输出结果。
2. 根据订单内容生成二维码
```c
#include <qrencode.h>
#include <stdio.h>
void generate_qr_code(char *order)
{
QRcode *qrcode = QRcode_encodeString(order, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
if (qrcode) {
for (int y = 0; y < qrcode->height; y++) {
for (int x = 0; x < qrcode->width; x++) {
putchar(qrcode->data[y * qrcode->width + x] ? '#' : ' ');
}
putchar('\n');
}
QRcode_free(qrcode);
}
}
```
该函数使用`qrencode`库生成订单内容的二维码,并输出到控制台。
3. 根据用户输入的选项查询菜单
```c
#include <stdio.h>
#include <string.h>
typedef struct {
char name[20];
float price;
} Item;
Item menu[] = {
{"Hamburger", 5.99},
{"Cheeseburger", 6.99},
{"Fries", 2.99},
{"Soda", 1.99},
{"Water", 0.99}
};
int menu_size = sizeof(menu) / sizeof(Item);
void search_menu(char *option)
{
for (int i = 0; i < menu_size; i++) {
if (strcmp(menu[i].name, option) == 0) {
printf("%s - $%.2f\n", menu[i].name, menu[i].price);
return;
}
}
printf("Invalid option: %s\n", option);
}
```
该函数使用结构体存储菜单信息,包括名称和价格。用户输入选项后,遍历菜单,查找对应的菜品并输出价格。
4. 根据用户输入的选项生成订单
```c
#include <stdio.h>
#include <string.h>
typedef struct {
char name[20];
float price;
} Item;
Item menu[] = {
{"Hamburger", 5.99},
{"Cheeseburger", 6.99},
{"Fries", 2.99},
{"Soda", 1.99},
{"Water", 0.99}
};
int menu_size = sizeof(menu) / sizeof(Item);
void create_order(char *options)
{
char *option = strtok(options, ",");
float total = 0;
while (option != NULL) {
for (int i = 0; i < menu_size; i++) {
if (strcmp(menu[i].name, option) == 0) {
total += menu[i].price;
break;
}
}
option = strtok(NULL, ",");
}
printf("Order total: $%.2f\n", total);
}
```
该函数使用结构体存储菜单信息,包括名称和价格。用户输入选项后,遍历菜单,查找对应的菜品并计算订单总价。
使用示例:
```c
int main()
{
// 计算订单总价
float total = calculate_total(5.99, 2);
print_total(total);
// 生成二维码
generate_qr_code("Order: Hamburger, Fries, Soda");
// 查询菜单
search_menu("Fries");
// 生成订单
create_order("Hamburger,Cheeseburger,Fries");
return 0;
}
```
输出:
```
Total price: 11.98
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Hamburger - $5.99
Order total: $18.97
```
希望这个代码能够帮到你!