feat(项目十): 新增综合章节源码、章节正文与教材 docx
- textbook/main.c:单文件控制台程序(约 400 行、13 个函数),覆盖项目一~九知识点 - textbook/项目十-章节正文.md:章节正文源稿 - textbook/figs:8 张控制台运行结果截图(真实回显) - textbook/tools:截图与 docx 生成脚本 - 10.16C语言程序设计教材初稿 - 项目十.docx:按原稿样式生成的综合章节(28 页)
This commit is contained in:
1 parent
4c1034db80
commit
ffef53c22f
15 files changed
+2331
No files matched your search
+436
@@ -0,0 +1,436 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_DEVICES 8
|
||||
#define CONFIG_FILE "devices.cfg"
|
||||
#define LOG_FILE "smart_home.log"
|
||||
|
||||
/* 状态标志位:一个字节同时记录 4 种状态 */
|
||||
#define FLAG_ONLINE 0x01 /* bit0 在线:0000 0001 */
|
||||
#define FLAG_FAULT 0x02 /* bit1 故障:0000 0010 */
|
||||
#define FLAG_LOCKED 0x04 /* bit2 锁定:0000 0100 */
|
||||
#define FLAG_SAVING 0x08 /* bit3 节能:0000 1000 */
|
||||
|
||||
/* 设备档案:把一台设备的全部信息封装成一个整体 */
|
||||
typedef struct {
|
||||
int id; /* 设备编号 */
|
||||
char name[20]; /* 设备名称 */
|
||||
int type; /* 0灯 1空调 2门锁 3传感器 */
|
||||
int on; /* 0关闭 1开启 */
|
||||
int level; /* 亮度 0~100 或 温度 16~30 */
|
||||
unsigned char flags; /* 状态标志位 */
|
||||
} Device;
|
||||
|
||||
Device devices[MAX_DEVICES]; /* 设备台账:结构体数组 */
|
||||
int device_count = 0; /* 台账中的设备数量 */
|
||||
|
||||
void init_devices(void);
|
||||
void show_devices(void);
|
||||
int find_device(int id);
|
||||
void write_log(const char *text);
|
||||
void show_log(void);
|
||||
void control_device(void);
|
||||
void manage_flags(void);
|
||||
void run_scene(void);
|
||||
void calc_stats(int *on_count, int *avg_level,
|
||||
int *max_level, int *min_level);
|
||||
void show_report(void);
|
||||
void save_config(void);
|
||||
void load_config(void);
|
||||
void print_menu(void);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int choice;
|
||||
init_devices();
|
||||
write_log("系统启动,设备台账初始化完成");
|
||||
|
||||
do {
|
||||
print_menu();
|
||||
printf("请选择功能(0-8):");
|
||||
(void)scanf("%d", &choice);
|
||||
printf("\n");
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
show_devices();
|
||||
break;
|
||||
case 2:
|
||||
control_device();
|
||||
break;
|
||||
case 3:
|
||||
manage_flags();
|
||||
break;
|
||||
case 4:
|
||||
run_scene();
|
||||
break;
|
||||
case 5:
|
||||
show_report();
|
||||
break;
|
||||
case 6:
|
||||
save_config();
|
||||
break;
|
||||
case 7:
|
||||
load_config();
|
||||
break;
|
||||
case 8:
|
||||
show_log();
|
||||
break;
|
||||
case 0:
|
||||
write_log("用户退出系统");
|
||||
printf("感谢使用智联·悦居智能家居中控系统,再见!\n");
|
||||
break;
|
||||
default:
|
||||
printf("输入错误:请输入 0-8 之间的数字。\n");
|
||||
break;
|
||||
}
|
||||
} while (choice != 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 初始化设备台账 */
|
||||
void init_devices(void)
|
||||
{
|
||||
Device tpl[] = {
|
||||
{1, "客厅主灯", 0, 1, 80, FLAG_ONLINE},
|
||||
{2, "卧室壁灯", 0, 0, 0, FLAG_ONLINE},
|
||||
{3, "客厅空调", 1, 1, 26, FLAG_ONLINE},
|
||||
{4, "智能门锁", 2, 1, 0, FLAG_ONLINE | FLAG_LOCKED},
|
||||
{5, "温湿度传感器", 3, 1, 25, FLAG_ONLINE},
|
||||
{6, "烟感传感器", 3, 1, 0, FLAG_ONLINE | FLAG_FAULT}
|
||||
};
|
||||
int i;
|
||||
int n = (int)(sizeof(tpl) / sizeof(tpl[0]));
|
||||
|
||||
device_count = n;
|
||||
for (i = 0; i < n; i++) {
|
||||
devices[i] = tpl[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* 显示设备台账 */
|
||||
void show_devices(void)
|
||||
{
|
||||
int i;
|
||||
const char *type_name[4] = {"灯光", "空调", "门锁", "传感器"};
|
||||
|
||||
printf("-------------------- 设备台账 --------------------\n");
|
||||
for (i = 0; i < device_count; i++) {
|
||||
printf("[%d] %s | 类型:%s | 状态:%s | 参数:%d | 标志:0x%02X\n",
|
||||
devices[i].id, devices[i].name, type_name[devices[i].type],
|
||||
devices[i].on ? "开启" : "关闭",
|
||||
devices[i].level, devices[i].flags);
|
||||
}
|
||||
printf("--------------------------------------------------\n");
|
||||
printf("共 %d 台设备\n", device_count);
|
||||
}
|
||||
|
||||
/* 按编号查找设备,返回数组下标,找不到返回 -1 */
|
||||
int find_device(int id)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < device_count; i++) {
|
||||
if (devices[i].id == id) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 写运行日志(追加方式) */
|
||||
void write_log(const char *text)
|
||||
{
|
||||
FILE *fp = fopen(LOG_FILE, "a");
|
||||
if (fp == NULL) {
|
||||
printf("日志文件打开失败!\n");
|
||||
return;
|
||||
}
|
||||
fprintf(fp, "%s\n", text);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
/* 查看运行日志 */
|
||||
void show_log(void)
|
||||
{
|
||||
FILE *fp = fopen(LOG_FILE, "r");
|
||||
char line[128];
|
||||
int count = 0;
|
||||
|
||||
if (fp == NULL) {
|
||||
printf("暂无日志记录。\n");
|
||||
return;
|
||||
}
|
||||
printf("-------------------- 运行日志 --------------------\n");
|
||||
while (fgets(line, sizeof(line), fp) != NULL) {
|
||||
printf("%s", line);
|
||||
count++;
|
||||
}
|
||||
fclose(fp);
|
||||
printf("------------------ 共 %d 条记录 ------------------\n", count);
|
||||
}
|
||||
|
||||
/* 控制设备:开关与参数调节 */
|
||||
void control_device(void)
|
||||
{
|
||||
int id, idx, op, value;
|
||||
|
||||
printf("请输入设备编号:");
|
||||
(void)scanf("%d", &id);
|
||||
idx = find_device(id);
|
||||
if (idx < 0) {
|
||||
printf("查找失败:编号 %d 的设备不存在。\n", id);
|
||||
return;
|
||||
}
|
||||
printf("当前设备:%s,状态:%s,参数:%d\n",
|
||||
devices[idx].name, devices[idx].on ? "开启" : "关闭",
|
||||
devices[idx].level);
|
||||
printf("1.开启 2.关闭 3.调节参数\n请选择操作:");
|
||||
(void)scanf("%d", &op);
|
||||
|
||||
if (op == 1) {
|
||||
devices[idx].on = 1;
|
||||
printf("操作完成:%s 已开启。\n", devices[idx].name);
|
||||
} else if (op == 2) {
|
||||
devices[idx].on = 0;
|
||||
printf("操作完成:%s 已关闭。\n", devices[idx].name);
|
||||
} else if (op == 3) {
|
||||
printf("请输入参数值:");
|
||||
(void)scanf("%d", &value);
|
||||
if (devices[idx].type == 0 && (value < 0 || value > 100)) {
|
||||
printf("参数越界:灯光亮度应在 0~100 之间。\n");
|
||||
return;
|
||||
}
|
||||
if (devices[idx].type == 1 && (value < 16 || value > 30)) {
|
||||
printf("参数越界:空调温度应在 16~30 之间。\n");
|
||||
return;
|
||||
}
|
||||
devices[idx].level = value;
|
||||
printf("操作完成:%s 参数已设为 %d。\n", devices[idx].name, value);
|
||||
} else {
|
||||
printf("操作非法:请输入 1~3。\n");
|
||||
return;
|
||||
}
|
||||
write_log("执行设备控制操作");
|
||||
}
|
||||
|
||||
/* 状态标志位管理:置位、清零、取反、查询、循环移位 */
|
||||
void manage_flags(void)
|
||||
{
|
||||
int id, idx, op, bit;
|
||||
unsigned char mask;
|
||||
|
||||
printf("请输入设备编号:");
|
||||
(void)scanf("%d", &id);
|
||||
idx = find_device(id);
|
||||
if (idx < 0) {
|
||||
printf("查找失败:编号 %d 的设备不存在。\n", id);
|
||||
return;
|
||||
}
|
||||
printf("设备 %s 当前标志:0x%02X\n",
|
||||
devices[idx].name, devices[idx].flags);
|
||||
printf("1.置位 2.清零 3.取反 4.查询 5.循环左移\n请选择操作:");
|
||||
(void)scanf("%d", &op);
|
||||
|
||||
if (op == 1 || op == 2 || op == 3) {
|
||||
printf("请选择标志(1在线 2故障 3锁定 4节能):");
|
||||
(void)scanf("%d", &bit);
|
||||
if (bit < 1 || bit > 4) {
|
||||
printf("标志选择非法。\n");
|
||||
return;
|
||||
}
|
||||
mask = (unsigned char)(1 << (bit - 1));
|
||||
if (op == 1) {
|
||||
devices[idx].flags |= mask; /* 置位 */
|
||||
} else if (op == 2) {
|
||||
devices[idx].flags &= (unsigned char)~mask; /* 清零 */
|
||||
} else {
|
||||
devices[idx].flags ^= mask; /* 取反 */
|
||||
}
|
||||
printf("操作完成,新标志:0x%02X\n", devices[idx].flags);
|
||||
} else if (op == 4) {
|
||||
printf("在线:%s 故障:%s 锁定:%s 节能:%s\n",
|
||||
(devices[idx].flags & FLAG_ONLINE) ? "是" : "否",
|
||||
(devices[idx].flags & FLAG_FAULT) ? "是" : "否",
|
||||
(devices[idx].flags & FLAG_LOCKED) ? "是" : "否",
|
||||
(devices[idx].flags & FLAG_SAVING) ? "是" : "否");
|
||||
} else if (op == 5) {
|
||||
devices[idx].flags = (unsigned char)((devices[idx].flags << 1) |
|
||||
(devices[idx].flags >> 7));
|
||||
printf("循环左移完成,新标志:0x%02X\n", devices[idx].flags);
|
||||
} else {
|
||||
printf("操作非法:请输入 1~5。\n");
|
||||
return;
|
||||
}
|
||||
write_log("执行状态标志位操作");
|
||||
}
|
||||
|
||||
/* 场景联动:一次操作批量控制多台设备 */
|
||||
void run_scene(void)
|
||||
{
|
||||
int scene, i;
|
||||
|
||||
printf("1.回家模式 2.离家模式 3.睡眠模式 4.节能模式\n请选择场景:");
|
||||
(void)scanf("%d", &scene);
|
||||
if (scene < 1 || scene > 4) {
|
||||
printf("场景选择非法。\n");
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < device_count; i++) {
|
||||
if (scene == 1) { /* 回家:开灯、开空调、解锁 */
|
||||
if (devices[i].type == 0) {
|
||||
devices[i].on = 1;
|
||||
devices[i].level = 70;
|
||||
}
|
||||
if (devices[i].type == 1) {
|
||||
devices[i].on = 1;
|
||||
devices[i].level = 26;
|
||||
}
|
||||
if (devices[i].type == 2) {
|
||||
devices[i].flags &= (unsigned char)~FLAG_LOCKED;
|
||||
}
|
||||
} else if (scene == 2) { /* 离家:全关、上锁 */
|
||||
devices[i].on = 0;
|
||||
if (devices[i].type == 2) {
|
||||
devices[i].flags |= FLAG_LOCKED;
|
||||
}
|
||||
} else if (scene == 3) { /* 睡眠:关灯、空调 26、上锁 */
|
||||
if (devices[i].type == 0) {
|
||||
devices[i].on = 0;
|
||||
}
|
||||
if (devices[i].type == 1) {
|
||||
devices[i].on = 1;
|
||||
devices[i].level = 26;
|
||||
}
|
||||
if (devices[i].type == 2) {
|
||||
devices[i].flags |= FLAG_LOCKED;
|
||||
}
|
||||
} else { /* 节能:灯光降亮、置节能位 */
|
||||
if (devices[i].type == 0) {
|
||||
devices[i].level = 30;
|
||||
devices[i].flags |= FLAG_SAVING;
|
||||
}
|
||||
}
|
||||
}
|
||||
write_log("执行场景联动");
|
||||
printf("场景执行完成,可查看设备台账确认结果。\n");
|
||||
}
|
||||
|
||||
/* 统计计算:通过指针参数一次返回 4 个结果 */
|
||||
void calc_stats(int *on_count, int *avg_level,
|
||||
int *max_level, int *min_level)
|
||||
{
|
||||
int i, sum = 0, count = 0;
|
||||
|
||||
*on_count = 0;
|
||||
*avg_level = 0;
|
||||
*max_level = 0;
|
||||
*min_level = 100;
|
||||
for (i = 0; i < device_count; i++) {
|
||||
if (devices[i].on == 1) {
|
||||
(*on_count)++; /* 开机台数 */
|
||||
}
|
||||
if (devices[i].type == 0) { /* 只统计灯光亮度 */
|
||||
sum += devices[i].level;
|
||||
count++;
|
||||
if (devices[i].level > *max_level) {
|
||||
*max_level = devices[i].level;
|
||||
}
|
||||
if (devices[i].level < *min_level) {
|
||||
*min_level = devices[i].level;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count > 0) {
|
||||
*avg_level = sum / count;
|
||||
}
|
||||
}
|
||||
|
||||
/* 统计报表 */
|
||||
void show_report(void)
|
||||
{
|
||||
int on_count, avg_level, max_level, min_level;
|
||||
int type_count[4] = {0, 0, 0, 0};
|
||||
int i, online = 0;
|
||||
|
||||
calc_stats(&on_count, &avg_level, &max_level, &min_level);
|
||||
for (i = 0; i < device_count; i++) {
|
||||
type_count[devices[i].type]++;
|
||||
if (devices[i].flags & FLAG_ONLINE) {
|
||||
online++;
|
||||
}
|
||||
}
|
||||
printf("-------------------- 统计报表 --------------------\n");
|
||||
printf("设备总数:%d 台,开机:%d 台,在线:%d 台\n",
|
||||
device_count, on_count, online);
|
||||
printf("灯光 %d 台,空调 %d 台,门锁 %d 台,传感器 %d 台\n",
|
||||
type_count[0], type_count[1], type_count[2], type_count[3]);
|
||||
printf("灯光亮度:平均 %d,最高 %d,最低 %d\n",
|
||||
avg_level, max_level, min_level);
|
||||
printf("-------------------------------------------------\n");
|
||||
}
|
||||
|
||||
/* 保存配置到文本文件 */
|
||||
void save_config(void)
|
||||
{
|
||||
FILE *fp = fopen(CONFIG_FILE, "w");
|
||||
int i;
|
||||
|
||||
if (fp == NULL) {
|
||||
printf("配置文件打开失败,保存未完成!\n");
|
||||
return;
|
||||
}
|
||||
fprintf(fp, "%d\n", device_count);
|
||||
for (i = 0; i < device_count; i++) {
|
||||
fprintf(fp, "%d %s %d %d %d %d\n", devices[i].id, devices[i].name,
|
||||
devices[i].type, devices[i].on, devices[i].level,
|
||||
devices[i].flags);
|
||||
}
|
||||
fclose(fp);
|
||||
write_log("保存设备配置");
|
||||
printf("保存成功:%d 台设备已写入 %s。\n", device_count, CONFIG_FILE);
|
||||
}
|
||||
|
||||
/* 从文本文件加载配置 */
|
||||
void load_config(void)
|
||||
{
|
||||
FILE *fp = fopen(CONFIG_FILE, "r");
|
||||
int i, n;
|
||||
|
||||
if (fp == NULL) {
|
||||
printf("配置文件不存在,请先执行保存操作。\n");
|
||||
return;
|
||||
}
|
||||
if (fscanf(fp, "%d", &n) != 1 || n > MAX_DEVICES) {
|
||||
printf("配置文件格式错误,加载失败。\n");
|
||||
fclose(fp);
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < n; i++) {
|
||||
int flags_value;
|
||||
if (fscanf(fp, "%d %19s %d %d %d %d", &devices[i].id,
|
||||
devices[i].name, &devices[i].type, &devices[i].on,
|
||||
&devices[i].level, &flags_value) != 6) {
|
||||
break;
|
||||
}
|
||||
devices[i].flags = (unsigned char)flags_value;
|
||||
}
|
||||
device_count = i;
|
||||
fclose(fp);
|
||||
write_log("加载设备配置");
|
||||
printf("加载成功:已从 %s 读入 %d 台设备。\n",
|
||||
CONFIG_FILE, device_count);
|
||||
}
|
||||
|
||||
/* 主菜单 */
|
||||
void print_menu(void)
|
||||
{
|
||||
printf("\n========== 智联·悦居 智能家居中控系统 ==========\n");
|
||||
printf(" 1.查看设备台账 2.控制设备\n");
|
||||
printf(" 3.状态标志位管理 4.场景联动\n");
|
||||
printf(" 5.统计报表 6.保存配置\n");
|
||||
printf(" 7.加载配置 8.查看运行日志\n");
|
||||
printf(" 0.退出系统\n");
|
||||
printf("================================================\n");
|
||||
}
|
||||
Reference in new issue
Block a user