文件名称:大作业任务说明
文件大小:1.21MB
文件格式:PPT
更新时间:2017-05-31 14:02:29
大作业
#include <time h> #define STOP TIME 5 从开始停靠到开始运行时间为5s #define INTERVAL TIME 5 电梯正常运行一层的时间为5s #define MAXFLOOR 9 最大楼层为9 #define IDLE 1 定义表示状态的三个常量 #define RUN 2 #define STOP 3 #define UP 1 电梯运行方向 #define DOWN 1 #define NONE 0 int state IDLE; 初始化电梯状态为停靠状态 int current floor 1; 当前的楼层 int dest floor 0; 目标楼层 int inside[MAXFLOOR+1] {0}; 定义数组inside用来存放电梯内部输入的数据 : 第一个元素即a[0]不用来存放数据 int outside up[MAXFLOOR+1] {0}; 定义数组outside up用来存放电梯外部输入的数据 : 第一个元素即a[0]不用来存放数据 当输入Q W E R T Y U I时表示8层到1层有上行呼叫请求 int outside down[MAXFLOOR+1] {0}; 定义数组outside down用来存放电梯外部输入的数据 : 第一个元素即a[0]不用来存放数据 当输入A S D F G H J K时表示9层到2层有下行呼叫请求 int go 0; 启动按钮 clock t total start 0 total finish 0; clock t current time; 当前时间 clock t input time; 定义变量input time用来存放输入的时间 clock t start 0; int direction NONE; 电梯的上下行方向 int up count 0 down count 0 stop count 0; int count 0; int pre floor 1; int flag 0; 表示向上运行 在cartoon函数中用到 #include<stdlib h> #include<stdio h> #include<winsock h> #include<winbase h> #include<windows h> void getInput ; void control ; void print ; void state trans ; void cartoon ; void time count int ; extern int checkup int int int[] ; extern int checkdown int int int[] ; void change char ; main { 创建线程1:读入请求 DWORD ThreadID 1; 创建线程(对应于函数getInput 用于接收电梯输入; HANDLE hRead CreateThread NULL 0 LPTHREAD START ROUTINE getInput NULL 0 &ThreadID ; 打印提示信息 printf " n" " ^^欢迎使用电梯模拟系统^^ n" " n" "从键盘输入 n" "敲击键1 2 3 4 5 6 7 8 9表示电梯内有乘客按目标按钮 对应相应目标楼层 n" "敲击键Q W E R T Y U I表示8层到1层有上行呼叫请求 n" "敲击键A S D F G H J K表示9层到2层有下行呼叫请求 n" "敲击键M表示go有信号 n" "敲击N显示欢迎帮助界面 n" "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ n" ; while 1 { state trans ; 根据自动机模型确定电梯此刻的状态 print ; control ; 用来得到目标楼层 并改变电梯的运动方向 time count 5 ; 推进时间 } return 0; } 输入数据的函数 void getInput { char request; while 1 { scanf "%c" &request ; if request "M"&&state RUN go 1; else if request> "1"&&request< "9" inside[request "0"] 1; switch request { case "I": outside up[1] 1;break; case "U": outside up[2] 1;break; case "Y": outside up[3] 1;break; case "T": outside up[4] 1;break; case "R": outside up[5] 1;break; case "E": outside up[6] 1;break; case "W": outside up[7] 1;break; case "Q": outside up[8] 1;break; case "A": outside down[9] 1;break; case "S": outside down[8] 1;break; case "D": outside down[7] 1;break; case "F": outside down[6] 1;break; case "G": outside down[5] 1;break; case "H": outside down[4] 1;break; case "J": outside down[3] 1;break; case "K": outside down[2] 1;break; } } } 确定状态的函数 void state trans { clock t finish; switch state { case IDLE: 电梯无运行方向 if dest floor current floor { state STOP; start clock ; } else if dest floor 0 { 找到目标 state RUN; start clock ; } else if 0 dest floor 没找到目标 direction NONE; break; case RUN: 电梯在运行状态 finish clock ; if finish start CLOCKS PER SEC> INTERVAL TIME { up count down count 0; if UP direction { flag 0; current floor++; start clock ; count 0; } else{ flag 1; current floor ; start clock ; count 0; } } else if UP direction up count++; else down count++; if dest floor current floor&&up count 0&&down count 0 { 目标楼层为当前楼层 inside[dest floor] outside up[dest floor] 0; if outside up[current floor] { if UP direction flag 1; else flag 0; } state STOP; start clock ; } break; case STOP: 电梯停靠状态 if go { state RUN; start clock ; go 0; } else { finish clock ; if finish start CLOCKS PER SEC> STOP TIME { if 0 dest floor state IDLE; else if dest floor current floor { state IDLE; outside up[current floor] outside down[current floor] 0; } else { state RUN; start clock ; } stop count 0; } else stop count++; } break; } } 控制函数 void control { int floor i j; 到达目标楼层确定下一目标楼层 情形1:电梯向上运行或无运行状态 if direction UP { current floor up count 0 current floor:current floor+1 ; floor checkup current floor MAXFLOOR outside up ; if floor< MAXFLOOR 比当前楼层高的楼层有上行请求 dest floor floor; 无高层上行请求 从最高层搜索下行请求 else{ floor checkdown MAXFLOOR 1 outside down ; if floor> 1 dest floor floor; 无下行请求 从最低层搜索上行请求 else{ floor checkup 1 MAXFLOOR outside up ; if floor<current floor dest floor floor; } } } 情形2:电梯向下运行 else if direction DOWN { current floor down count 0 current floor:current floor+1 ; floor checkdown current floor 1 outside down ; if floor> 1 比当前楼层低的楼层有下行请求 dest floor floor; 无低层下行请求 从最低层搜索上行请求 else{ floor checkup 1 MAXFLOOR outside up ; if floor< MAXFLOOR dest floor floor; 无上行请求 从最高层搜索下行请求 else{ floor checkdown MAXFLOOR 1 outside down ; if floor>current floor dest floor floor; } } } if direction NONE { for i current floor;i< 9&&outside up[i] 0&&outside down[i] 0&&inside[i] 0;i++ ; for j current floor;j> 1&&outside up[j] 0&&outside down[j] 0&&inside[j] 0;j ; if i 10&&j 0 { 上下都找到了 则进行比较 floor i current floor > current floor j j:i ; dest floor floor; } else if i 10&&j 0 只有下行的找到了 dest floor j; else if j 0&&i 10 只有上行的找到了 dest floor i; } if dest floor>current floor direction UP; else if dest floor<current floor direction DOWN; else direction NONE; } 上行请求的函数 int checkup int a int b int queue[] { int i; for i a;i< b;i++ if queue[i] || inside[i] return i ; return 10; } 下行请求的函数 int checkdown int a int b int queue[] { int i; for i a;i> b;i if queue[i] || inside[i] return i ; return 0; } void time count int n { time t start finish; start clock ; finish clock ; while finish start CLOCKS PER SEC<n { finish clock ; } } 打印结果的函数 void print void { printf "电梯的当前楼层为%d t" current floor+up count+down count ; if state IDLE&&state STOP printf "目标楼层是第%d层 t" dest floor ; if direction NONE printf "电梯没有运动 n" ; else if state RUN if direction UP printf "电梯正在向上运动 n" ; else printf "电梯正在向下运动 n" ; if state RUN printf "当前状态为RUN n" ; else if state IDLE printf "当前状态为IDLE n" ; else printf "当前状态为STOP n" ; }">#include <time h> #define STOP TIME 5 从开始停靠到开始运行时间为5s #define INTERVAL TIME 5 电梯正常运行一层的时间为5s #define MAXFLOOR 9 最大楼层为9 #define IDLE 1 定义表示状态的三个常量 #define RUN 2 #define STOP 3 #define UP 1 电梯运行方 [更多]