用c语言编写一个智能家居监测系统,包含菜单功能,环境监测功能,温湿度监测功能等基础数据的程序
时间: 2024-03-19 15:43:59 浏览: 36
好的,以下是一个基本的智能家居监测系统程序,包含菜单功能、环境监测功能、温湿度监测功能等基础数据:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义传感器数据结构体
typedef struct {
int temperature;
int humidity;
int light;
int motion;
} SensorData;
// 定义传感器数据获取函数
SensorData get_sensor_data() {
SensorData data;
// 模拟传感器数据
data.temperature = rand() % 20 + 20;
data.humidity = rand() % 30 + 50;
data.light = rand() % 2;
data.motion = rand() % 2;
return data;
}
// 显示传感器数据函数
void display_sensor_data(SensorData data) {
// 显示传感器数据
printf("Temperature: %d°C\n", data.temperature);
printf("Humidity: %d%%\n", data.humidity);
printf("Light: %s\n", data.light ? "On" : "Off");
printf("Motion: %s\n", data.motion ? "Detected" : "Not detected");
}
int main() {
int choice;
SensorData data;
do {
// 显示菜单
printf("Smart Home Monitoring System\n");
printf("1. Environment Monitoring\n");
printf("2. Temperature and Humidity Monitoring\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// 获取环境传感器数据
printf("Getting environment sensor data...\n");
data.light = rand() % 2;
data.motion = rand() % 2;
display_sensor_data(data);
break;
case 2:
// 获取温湿度传感器数据
printf("Getting temperature and humidity sensor data...\n");
data = get_sensor_data();
display_sensor_data(data);
break;
case 3:
// 退出程序
printf("Exiting...\n");
break;
default:
// 处理无效选择
printf("Invalid choice, please try again...\n");
break;
}
} while (choice != 3);
return 0;
}
```
在这个程序中,我们使用了菜单功能来让用户选择需要监测的功能,包括环境监测和温湿度监测。在 `get_sensor_data()` 函数中,我们模拟了传感器数据的获取过程,使用 `rand()` 函数来随机生成温湿度数据。在 `main()` 函数中,我们使用了一个 `do-while` 循环来让用户反复进行选择,直到用户选择退出程序。在每个选择分支中,我们调用相应的函数来获取传感器数据,并使用 `display_sensor_data()` 函数来在终端中显示传感器数据。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)