用C语言写一个电梯运行程序
时间: 2023-12-04 13:03:40 浏览: 44
好的,以下是一个简单的电梯运行程序的 C 语言代码:
#include <stdio.h>
int main()
{
int current_floor = 1; // 当前楼层
int target_floor = 1; // 目标楼层
int direction = 0; // 运动方向(0为静止,1为上行,-1为下行)
while (1) {
printf("当前楼层:%d\n", current_floor);
printf("请输入目标楼层(1-10之间的整数):");
scanf("%d", &target_floor);
// 判断电梯行动方向
if (target_floor > current_floor) {
direction = 1;
} else if (target_floor < current_floor) {
direction = -1;
} else {
printf("您已经在目标楼层了!\n");
continue;
}
// 移动电梯
while (current_floor != target_floor) {
current_floor += direction;
printf("正在前往%d楼...\n", current_floor);
}
printf("电梯已到达%d楼!\n", current_floor);
}
return 0;
}
提供以上代码供参考,如有需要可以根据需求自行修改和完善。