只能给点提示,希望有用。一,用到随做猛机函数。二,调度段枝算法为FIFO和电梯调度。参考 *** 作系统。三,文件io用到#include
<fstream>头文纯燃桥件
/*建立一座两层楼,一部谈陵电梯的模拟世侍谨程序。为简化起见,每部电梯限乘一人,电梯每天在一楼关门等待模拟程序包括一个时钟,每天从零开始。模拟程序得调度器组件随机设置每一层第一个人到来的时间,当时钟的时间等于第一个人到来的时间时,模拟程序生成一个新到的人将该人放到这一层。然后这个人按下按钮,请求电梯开门。这个人的目的地楼层不能与他上电梯的楼层相同。
如果第一个人到达第一层,则可以在按下按钮、等待电梯开门之后立即进入电梯。如果第一个人在第2层,则电梯要升到第2层去接他。电梯从1层移到2层需要5秒。
电梯到达一层时,打开电梯门上的灯,并在搜基电梯内发出铃声,该层的按钮和电梯中表示该层的按钮复位,电梯门打开,乘客走出电梯(如果有到该层的乘客)。另一乘客(如果该层有人等待)进入电梯按下目的层按钮,电梯们关上。电梯移动前确定移动方向(两层很容易判断),为简单起见,电梯到达一层直到关门所花时间为0。
任何时间每层最多只能有一人等待,如果新到的人(不在电梯中)要到达该层时该层被占用,则一秒后才能安排新到达者。假设每隔5到20秒人们随机到达每层*/
#include <iostream >
#include <ctime >
#include <iomanip >
#include <string >
#include <stdlib.h>
#include <windows.h >
using namespace std
class person
{
public:
string sex//定义人员类
person()
void newset()
}
person::person()
{sex="man" }
void person::newset()
{
int i
srand((unsigned)time(NULL))
for(i=0i++)
{
if(rand()==1)
{sex="man" break }
if(rand()==2)
{sex="woman" break }
}
}
class elevator
{
public:
int now //定义电梯类
elevator()
}
elevator::elevator()
{now=1 }
class Time
{
public:
static void outtime()//定义时间类,用于输出电梯每次运行时的时间
{
SYSTEMTIME t
GetLocalTime(&t)
cout<<" //"<<t.wYear<<"."<<t.wMonth<<"."<<t.wDay<<". "<<t.wHour<<":"<<t.wMinute<<":"<<t.wSecond<<endl
}
}
void last(long n)//用于等待时输出.的函数
{
long i
i=n/500
for(i=0i<n/500i++)
{
cout<<'.' Sleep(500)
}
cout<<endl
}
void waiting()
{long m //模拟随机等待的时间函数
srand((unsigned)time(NULL))
cout<<"elevator waiting"
for(int i=0i<1000i++)
m=rand()%(20000-5000+1)+5000
last(m)
}
void output(long t,person &p,elevator &e) //电梯模拟函数
{
static n=1 //定义乘客的个数
if( t==1)//有人在一楼按铃
{
p.newset()
if(e.now!=1) //但是此时电梯停留在二楼
{
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrived floor 1."//输出有人到达一楼
Time::outtime() //输出此时的准确时间
cout<<"elevator running"
last(5000)
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") enters elevator." //输出乘客进入电梯
Time::outtime() //输出此时的准确时间
cout<<"elevator running"
last(5000)
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrives floor 2." //输入乘客到达二楼
Time::outtime() //输出此时的准确时间
e.now=2
n++
system("color 0C") waiting() system("color 0F")
}
else//此时电梯就是一楼
{
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrives floor 1." //输出有人到达一楼
Time::outtime() //输出此时的准确时间
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") enters elevator."<<endl
cout<<"elevator running"
last(5000)
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrives floor 2."
Time::outtime()
e.now=2
n++
system("color 0C") waiting() system("color 0F")
}
}
if(t==2)//有人在二楼按铃
{
if(e.now!=2)//但是电梯此时不在二楼
{
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrived floor 2."
Time::outtime()
cout<<"elevator running"
last(5000)
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") enters elevator."
Time::outtime()
cout<<"elevator running"
last(5000)
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrives floor 1."
Time::outtime()
e.now=1
n++
system("color 0C") waiting() system("color 0F")
}
else//此时电梯就是二楼
{
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrives floor 2."
Time::outtime()
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") enters elevator."
Time::outtime()
cout<<"elevator running"
last(5000)
cout<<"person "<<n<<"(a "<<setw(5)<<p.sex<<") arrives floor 1."
Time::outtime()
e.now=1
n++
system("color 0C") waiting() system("color 0F")
}
}
}
int main()
{
cout.fill('*')
cout<<setw(80)<<'*'
cout.fill(' ')
system("color 0C")
cout<<setw(50)<<"***欢迎使用电梯模拟程序***"<<endl //输出头菜单
cout.fill('*')
cout<<setw(80)<<'*'
cout.fill(' ')
int i
person p
elevator e
srand((unsigned)time(NULL)) //定义随机数种
waiting()
system("color 0F")
for(i=0i++)
output(rand(),p,e)//调用电梯模拟函数
return 0
}
首先你的排序函数是不对的,具体错在你的交换a[i]和a[small]的值上,下面是我改的代码:void SelectSort(int a[],int n)
{
int i,j,small
int temp//temp是需要的。
for(i=0i<n-1i++)
{
small=i//设第i个数据元素关键字最小
for(j=i+1j<nj++)//寻找关键字最小的数据元素
if(a[j]<a[small]) small=j//记住最小元素的下标
if(small!=i)//当前最小元素的下标不为i是交换位置
{
temp = a[i]//楼主错在这里
a[i]=a[small]
a[small]=temp
}
}
}
其次电梯调度部分写得很混乱,错误很多,这里先来指出楼主的错误。
void SCAN(int a[],int m,int direction){
int k=0,j=0,i
int temp[m]//存放磁道的调度顺序号
//int cur//临时变量,用于记录调度的起始磁道位置
if(direction==0)//磁头向磁道号增加的方向移动
{
for(k<mk++)
{
if(b[k]>=StartTrack)
{
//下面楼主开始犯错误了
//for(k==m-1k++)temp[k]=b[k]//楼主的源代码,这里他犯了2个错误
//1、是for循环的条件,他写的k==m-1,如果是这样的话,那么循环一次也不会进行。
//2、直接把b[k]上的值赋给了temp[k],注意此时k不是0...
//下面是修改后的代码
for(i=0k+1<mi++)
{
temp[i]=b[k+i]
}
//在这里程序是不完整的,电梯这里只是走到了序号最大的地点,楼主的想法是想直接转到else部分,其实他没有理解if else到底是什么意思。。。
}
else if(k==m-1)//楼主此处假设初始位置到顶了
{
direction=1//省略部分代码//这里不知道楼主省了什么代码,不过单是一个direction=1是无法完成算法的培枝,程序不会转到else部分
int n
for(n=ii>0i--,n++)
{
temp[n]=b[i]
}
}
}
}
else//磁头向磁道号减少的方向移动
{
for(k<mk++)
{
if(b[k]>StartTrack)
{
//这里楼主犯错了for(k==0k--)temp[k]=b[k]//省略部分代码//理由同上
for(i=0k>0i++){
temp[i]=b[k-1-i]//修改好的代码
}
//理由同上面相应部分
}
else if(k==m-1)
{
direction=0//省略哗芦部分代码
//配芦敏此处理由同前相应部分
}
}
}
}
总结一下,楼主对 交换2个参量的值,条件判断语句, for循环方面的知识需要加强。
下面贴出正确的电梯调度代码:
本人使用的C++写的,使用C语言的话,需要替换相应的代码,如cout 换成相应的 printf()
经测试可以通过。
#include<iostream>
int StartTrack=143
void SelectSort(int[],int)
void SCAN(int[],int,int)
void display(int[],int)
int get_Position(int[],int,int)
int scanUP(int[] , int , int[] , int , int )
int scanDOWN(int[] , int , int[] , int , int)
void main()
{
int a[10] = {11,99,200,176,56,33,188,210,34,46}
SelectSort( a , 10 )
SCAN( a , 10 , 1)
}
void SelectSort(int a[] , const int a_size)
{
int i,j,small
int temp
for(i=0i<a_size-1i++)
{
small=i//设第i个数据元素关键字最小
for( j = i + 1 j <a_size j++)//寻找关键字最小的数据元素
if( a[j] <a[small] ) small = j//记住最小元素的下标
if( small != i)//当前最小元素的下标不为i是交换位置
{
temp = a[i]
a[i] = a[small]
a[small] = temp
}
}
}
void display(int a[] ,const int a_size)
{
using namespace std
for (int i = 0i <a_size i++)
{
cout<<a[i]<<" "
}
cout<<"\n"
cin.get()
cin.get()
}
void SCAN(int a[] ,const int a_size , int direction)
{
int result_Array[10]
int channel_number = StartTrack
int current_pos = get_Position( a , a_size , channel_number )
if( direction == 0){
int pos = scanUP( a , a_size , result_Array , 0 , current_pos)
scanDOWN( a , a_size ,result_Array , pos , current_pos )
}
else {
int pos = scanDOWN( a , a_size , result_Array , 0 , current_pos)
scanUP( a , a_size ,result_Array , pos , current_pos )
}
display(result_Array , a_size)
}
/*
get_Position
This function return the position that the value
of this position in array a[] is larger than the StartTrack
*/
int get_Position(int a[] ,int a_size , int channel_number)
{
for(int i = 0 i <a_size i++ )
{
if( a[i] >channel_number) return i
}
return a_size
}
/*
Scan up
a[] is the array to be scaned.
a_size is the size of the array.
result[] is the array to store the result.
result_tear is the last position that the position of result[] is empty.
*/
int scanUP(int a[] , int a_size , int result[] , int result_tear , int start_position)
{
int i = 0
for( start_position + i <a_size i++)
{
result[ result_tear + i] = a [start_position + i]
}
return i + result_tear
}
/*
Scan down
a[] is the array to be scaned.
a_size is the size of the array.
result[] is the array to store the result.
result_tear is the last position that the position of result[] is empty.
*/
int scanDOWN(int a[] , int a_size , int result[] , int result_tear , int start_position)
{
int i = 0
for( start_position - i >0 i++)
{
result[i + result_tear] = a [start_position - 1 - i]
}
return i + result_tear
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)