扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:风之韵 来源:yesky 2007年11月16日
关键字:
void CLiftsimulationView::OnTimer(UINT nIDEvent) //主要在这里处理所有得逻辑 { int i=1; int j=1; int a; int flag1=0; CString str1,str2; CString showtime,sen,m,h; DWORD dwTime; if (nIDEvent==1) //整个系统来驱动,这个是最小得时间单位 {if (this->m_elevator.bStart =true) //电梯在仿真 { //对所有乘客循环查询,类似于消息循环,这是电梯核心程序得入口 for(i=1;i<=this->n;i++) {if (this->m_passenger[i].bInLift ==false && this->m_passenger[i].bStart ==true && this->m_elevator.bStop==true && this->m_elevator.bIsEmptyOperation==true) { //轮询各个乘客得请求 //只有当电梯停止而且乘客在楼层上得时候,才能进行发信号 if (this->m_passenger[i].bSignal ==true) {//初始化乘客 srand(::GetTickCount()); a =rand()%10+1; //产生一个1到10得随机数 this->m_passenger[i].iToFloor =a; //该乘客要去得层数 if (a==this->m_passenger[i].iAtFloor) //楼层一样得话就放弃此次信号 if (a ==10) this->m_passenger[i].iAtFloor=a-1; else this->m_passenger[i].iAtFloor=a+1; str1.Format("%d",this->m_passenger[i].iToFloor); str2.Format("%d",this->m_passenger[i].number ); this->m_passenger[i].flag =str2+"--"+str1; this->m_passenger[i].bSignal =false; //关闭她得信号标志,给别得乘客一个机会 this->m_passenger[i].bInLift=true; //进入电梯了(虚拟) //初始化电梯,为空载运行作准备 this->m_elevator.iToFloor =this->m_passenger[i].iAtFloor; this->m_elevator.iPassengers =1; this->m_elevator.m_passengers.Add(this->m_passenger[i]); this->m_elevator.bStop =false; //表明电梯再运行 this->iEmptySrcFloor =this->m_elevator.iAtFloor; this->iEmptyDesFloor =this->m_elevator.iToFloor ; this->SetDlgItemText(IDC_STATIC18,(LPCTSTR)(str2+"--"+str1)); //完成空载任务 this->itimes[4]++; if (this->itimes[4]==1) SetTimer(4,2000,0); //电梯空载去接乘客 break; //每次接一个 } } } //判断是否结束仿真 for(i=1;i<=this->n;i++) { if (this->m_passenger[i].bStart ==true) flag1=1; } if (flag1==0) //结束仿真 { KillTimer(1); this->m_elevator.bStart =false; return; } //防止没有乘客发信号,系统进入死循环 for(i=1;i<=this->n;i++) { if (this->m_passenger[i].bInLift ==false && this->m_elevator.bStop ==true && this->m_elevator.bStart ==true) { this->m_passenger[i].bSignal =true; //应该是随机 break; } } } else { KillTimer(1); //如果电梯运行标志为false,则停止仿真 } dwTime=::GetTickCount() -this->ElapseTime ; dwTime=dwTime/1000; sen.Format("%d",dwTime%60); h.Format("%d",dwTime/3600); m.Format("%d",(dwTime/60)%60); showtime=h+" : "+m+" : "+sen; this->SetDlgItemText(IDC_STATIC16,showtime); } else if (nIDEvent==4) //电梯空载运行(去接乘客) { if (this->iEmptySrcFloor ==this->iEmptyDesFloor ) //到达目的地了 { if (this->iEmptySrcFloor !=1 && this->iEmptySrcFloor !=10 ) { this->DrawColor(iEmptySrcFloor,1); this->DrawColor(iEmptySrcFloor,-1); } //因为到站了,所以要把电梯设置为接收信号状态 this->m_elevator.bStop =true; //电梯不运行了 this->m_elevator.bIsEmptyOperation =false; //为满载作准备 this->m_elevator.iAtFloor =this->m_elevator.iToFloor ; this->m_elevator.iToFloor=this->m_elevator.m_passengers[0].iToFloor; KillTimer(4); this->itimes[4]=0; this->iSrcFloor =this->m_elevator.iAtFloor; this->iDesFloor =this->m_elevator.iToFloor ; this->m_elevator.bStop =false; //电梯启动 this->itimes[2]++; if (this->itimes[2]==1) SetTimer(2,2000 ,0); //启动乘客上电梯 } if (iEmptySrcFloor <iEmptyDesFloor) { this->DrawColor(iEmptySrcFloor,1); iEmptySrcFloor++; } else { this->DrawColor(iEmptySrcFloor,-1); iEmptySrcFloor--; } } else if(nIDEvent==2) //电梯每隔3秒,电梯是载客运行,还有一种情况是无客运行 {if (iSrcFloor <iDesFloor) { this->DrawColor(iSrcFloor,1); iSrcFloor++; } else if (iSrcFloor >iDesFloor) { this->DrawColor(iSrcFloor,-1); iSrcFloor--; } if (iSrcFloor ==iDesFloor) //到达目的地了 { this->DrawColor(iSrcFloor,1); this->DrawColor(iSrcFloor,-1); //因为到站了,所以要把电梯设置为接收信号状态 this->m_elevator.bStop =true; this->m_elevator.iAtFloor =this->m_elevator.iToFloor ; //进行卸载乘客得操作 srand(::GetTickCount()); a =rand()%10+1; //产生一个1到10得随机数 this->m_elevator.m_passengers[0].iAtFloor =this->m_elevator.m_passengers[0].iToFloor ; this->m_elevator.m_passengers[0].iToFloor = a; //该乘客要去得层数 this->m_elevator.m_passengers[0].iLifts++; if (this->m_elevator.m_passengers[0].iLifts ==this->l ) this->m_elevator.m_passengers[0].bStart =false; this->m_elevator.m_passengers[0].bInLift =false; //电梯外面了 this->m_elevator.m_passengers.RemoveAll(); this->m_elevator.iPassengers --; //为下一次空载作准备 this->m_elevator.bStop =true; this->m_elevator.bIsEmptyOperation =true; KillTimer(2); this->itimes[2]=0; } } CFormView::OnTimer(nIDEvent); } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者