C语言模拟加油站自动加油机系统
#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\n// 初始化函数\nvoid init() {\n // L0~L7 依次点亮,再依次熄灭\n for (int i = 0; i <= 7; i++) {\n printf("L%d 点亮\n", i);\n }\n for (int i = 7; i >= 0; i--) {\n printf("L%d 熄灭\n", i);\n }\n\n // 发送当前油价信息\n printf("92# : 7.8 95# : 8.2 98# : 8.9\n");\n}\n\n// 按键处理函数\nvoid handleKeyPress(int key) {\n static int oilType = 0; // 0表示未选择油号,1表示92#,2表示95#,3表示98#\n static int startTimestamp = 0; // 加油开始时间戳\n static int oilAmount = 0; // 加油量\n\n // 根据按键选择油号\n if (key == 0) {\n oilType = 1;\n printf("oil=92#\n");\n } else if (key == 1) {\n oilType = 2;\n printf("oil=95#\n");\n } else if (key == 2) {\n oilType = 3;\n printf("oil=98#\n");\n }\n\n // 加油键按下\n if (key == 5) {\n startTimestamp = time(NULL); // 记录开始时间戳\n printf("L7 点亮\n");\n }\n\n // 停止键按下\n if (key == 6) {\n int endTimestamp = time(NULL); // 记录结束时间戳\n int duration = endTimestamp - startTimestamp; // 计算加油时长\n oilAmount = duration; // 油量 = 时长\n int price = 0;\n if (oilType == 1) {\n price = 7.8;\n } else if (oilType == 2) {\n price = 8.2;\n } else if (oilType == 3) {\n price = 8.9;\n }\n int cost = oilAmount * price; // 总费用 = 油量 * 单价\n printf("oil=%d total=%d price=%d cost=%d\n", oilType, oilAmount, price, cost);\n }\n\n // 付费键按下\n if (key == 7) {\n printf("所有 LED 点亮 2 秒钟\n");\n printf("pay ok\n");\n oilType = 0;\n startTimestamp = 0;\n oilAmount = 0;\n }\n}\n\n// 温控函数\nvoid temperatureControl(float temperature) {\n // 发送当前环境温度\n printf("temp=%.1f\n", temperature);\n\n // 若温度超过40度,发送预警信息\n if (temperature > 40) {\n printf("warn:temp=%.1f!!\n", temperature);\n }\n}\n\nint main() {\n init();\n\n int key; // 按键值\n float temperature; // 环境温度\n\n while (1) {\n printf("请输入按键值:");\n scanf("%d", &key);\n handleKeyPress(key);\n\n printf("请输入环境温度:");\n scanf("%f", &temperature);\n temperatureControl(temperature);\n }\n\n return 0;\n}\n
原文地址: https://www.cveoy.top/t/topic/pDMN 著作权归作者所有。请勿转载和采集!