您的位置:首页 > 其它

课程设计 电梯运行仿真

2015-12-25 09:07 232 查看
<pre class="cpp" name="code">/*
* Copyright (c)2015,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:项目.cbp
* 作    者:纪冬雪
* 完成日期:2015年12月25日
* 版 本 号:v1.0

* 问题描述:设计一个模拟电梯运行的程序
  

  

* 输入描述:无
* 程序输出:测试数据
*/

#include "elevator.h"

void last(long n)           //用于等待时输出.的函数
{
long i;
i=n/500;
for(i=0; i<n/500; i++)
{
cout<<'.';
Sleep(500);
}
cout<<endl;
}
void waiting()
{
long m;
//模拟随机等待的时间函数

srand((unsigned)time(NULL));//得到随机数
cout<<"elevator waiting";
for(int i=0; i<1000; i++)
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 1C");
waiting();
system("color 6F");
}
}
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 1C");
waiting();
system("color 3C");
}
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 3C");
waiting();
system("color 18");
}
}
}
}

int main()
{
cout.fill('*');
cout<<setw(80)<<'*';
cout.fill(' ');
system("color 7A");//前面是背景颜色,后面是字体颜色。
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=0;; i++)
output(rand(),p,e);                                     //调用电梯模拟函数
return 0;
}

#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=0;; i++)
{
if(rand()==1)//rand()系统获取随机数
{
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;//以一个SYSTEMTIME结构体变量来构造
GetLocalTime(& t);//获取当前的时间
cout<<"                   //"<<t.wYear<<"."<<t.wMonth<<"."<<t.wDay<<".

"<<t.wHour<<":"<<t.wMinute<<":"<<t.wSecond<<endl;
}
};

运行结果:



可以改变颜色的方法:



总结:

通过设计一个电梯运行的程序,更能了解电梯的运行,锻炼了自己的思考分析能力。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: