济南在线 莱芜在线

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索

查看: 695|回复: 0

用AI写一段控制32层楼电梯运行的C语言程序。看看其分析过程,搞懂电梯运行的逻辑

[复制链接]
发表于 2025-2-13 07:53 来自手机 | 显示全部楼层 |阅读模式 | 来自山东
以下是一个用C语言实现的简单电梯控制程序,支持32层楼的基本操作:

```c
#include
#include
#include
#include
#include

#define TOTAL_FLOORS 32
#define SLEEP_TIME 500000  // 0.5秒/层

int current_floor = 1;
int direction = 0;  // 0:停止 1:上行 -1:下行
int requests[TOTAL_FLOORS] = {0};

// 设置非阻塞输入
void set_nonblocking(int enable) {
    static struct termios oldt, newt;
    if (enable) {
        tcgetattr(STDIN_FILENO, &oldt);
        newt = oldt;
        newt.c_lflag &= ~(ICANON  ECHO);
        tcsetattr(STDIN_FILENO, TCSANOW, &newt);
    } else {
        tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
    }
}

// 检查输入状态
int kbhit() {
    struct timeval tv = {0, 0};
    fd_set fds;
    FD_ZERO(&fds);
    FD_SET(STDIN_FILENO, &fds);
    return select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
}

// 显示电梯状态
void display_status() {
    printf("\r当前楼层: \033[1;32m%2d\033[0m  状态: ", current_floor);
    if (direction == 1) printf("");
    else if (direction == -1) printf("");
    else printf("");
   
    printf("  目标楼层: ");
    for (int i = 0; i < TOTAL_FLOORS; i++) {
        if (requests[i]) printf("\033[1;33m%d \033[0m", i+1);
    }
    fflush(stdout);
}

// 处理输入
void process_input() {
    char input[10];
    if (read(STDIN_FILENO, input, sizeof(input)) > 0) {
        int floor = atoi(input);
        if (floor >= 1 && floor <= TOTAL_FLOORS) {
            requests[floor-1] = 1;
            printf("\n新目标: %d 楼", floor);
        }
    }
}

// 更新电梯运行方向
void update_direction() {
    if (direction == 0) {
        // 寻找最近请求
        int up = -1, down = -1;
        for (int i = 0; i < TOTAL_FLOORS; i++) {
            if (requests[i]) {
                if (i+1 > current_floor && (up == -1  i+1 < up)) up = i+1;
                if (i+1 < current_floor && (down == -1  i+1 > down)) down = i+1;
            }
        }
        if (up != -1) direction = 1;
        else if (down != -1) direction = -1;
    }
}

// 处理电梯移动
void move_elevator() {
    if (direction == 1) {
        current_floor++;
        if (current_floor == TOTAL_FLOORS) direction = 0;
    } else if (direction == -1) {
        current_floor--;
        if (current_floor == 1) direction = 0;
    }
}

// 检查是否需要停靠
void check_stop() {
    if (requests[current_floor-1]) {
        printf("\n\033[1;34m到达 %d 楼,开门...\033[0m", current_floor);
        requests[current_floor-1] = 0;
        sleep(1);  // 开门等待时间
    }
}

int main() {
    set_nonblocking(1);
    printf("\033[2J\033[H");  // 清屏
    printf("电梯控制系统(1-32层)\n");
    printf("输入目标楼层后回车,Ctrl+C退出\n");

    while (1) {
        display_status();
        
        if (kbhit()) process_input();
        update_direction();
        move_elevator();
        check_stop();
        
        usleep(SLEEP_TIME);  // 移动间隔
    }

    set_nonblocking(0);
    return 0;
}
```

功能说明:
1. **实时状态显示:
   - 当前楼层(绿色显示)
   - 运行状态(表示上行,表示下行,表示停止)
   - 目标楼层(黄色显示)

2. 操作方式:
   - 直接输入楼层号并回车添加目标
   - Ctrl+C 退出程序

3. 运行逻辑:
   - 采用LOOK电梯调度算法
   - 优先处理当前运行方向上的请求
   - 自动调整运行方向
   - 到达目标楼层后开门1秒

4. 技术特点:
   - 非阻塞输入处理
   - ANSI转义码实现终端控制
   - 实时更新显示

### 编译运行(Linux/macOS):
```bash
gcc elevator.c -o elevator && ./elevator
```

该程序模拟了电梯的基本运行逻辑,包括请求处理、方向决策、楼层停靠等功能,可以通过终端实时观察电梯运行状态并交互操作。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

隐私声明|小黑屋|联系电话:0531-75627999|济南在线 莱芜在线 ( 鲁ICP备15020683-2号 )

GMT+8, 2025-4-16 22:40

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表