模拟电梯载人实验
Elevator.h
class Elevator{
public:
Elevator();
~Elevator();
void getNowNum();
void SetToNum(int);
void UP();
void DOWN();
private:
int NowNum;
int ToNum;
};
Elevator.cpp
#include<iostream>
#include<Windows.h>
#include"Elevator.h"
using namespace std;
Elevator::Elevator()
{
NowNum = 1;
ToNum = 10;
}
Elevator::~Elevator()
{
}
void Elevator::getNowNum()
{
HANDLE consolehwnd;
consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(consolehwnd, 10);
cout << "您现在在" << NowNum << "楼" << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout << "请选择:";
cout << "1.上升 2.下降 3.退出 " ;
}
void Elevator::SetToNum(int a)
{
ToNum = a;
}
void Elevator::UP()
{
int i;
if (ToNum < NowNum)
cout << "你不能上行" << endl;
else if (ToNum == NowNum)
cout << "亲,你就在这一层了,别调皮" << endl;
else
{
for ( i = NowNum+1; i <= ToNum & i<=10; i++)
{
cout << "现在到了第" << i << "层了,我们就快到了" << endl;
Sleep(500);
}
if (ToNum>10)
{
ToNum = i-1;
cout << "以上楼层正在建设中,您只能到这里了。" << endl;
NowNum = 10;
}
else
NowNum = ToNum;
}
}
void Elevator::DOWN()
{
int i;
if (ToNum > NowNum)
cout << "你不能下行" << endl;
else if (ToNum == NowNum)
cout << "亲,你就在这一层了,别调皮" << endl;
else
{
for ( i = NowNum - 1; i >= ToNum & i>=1; i--)
{
cout << "现在到了第" << i << "层了,我们就快到了" << endl;
Sleep(500);
}
if (ToNum<1)
{
ToNum = i+1;
cout << "地下室正在挖,您过两天才能下去。" << endl;
NowNum = 1;
}
else
NowNum = ToNum;
}
}
demo.cpp
#include<iostream>
#include<Windows.h>
#include"Elevator.h"
using namespace std;
int main()
{
Elevator e1;
int choice,to;
SYSTEMTIME time;
GetLocalTime(&time);
printf("%4d年%02d月%02d日 %02d:%02d:%02d 星期%1d
", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wDayOfWeek);
cout << "欢迎乘坐电梯
" << endl;
while (1)
{
e1.getNowNum();
cout<<"输入你的选择:";
cin >> choice;
e1.SetToNum(to);
switch(choice)
{
case 1:{
cout<<"输入你要去的楼层:"<<endl;
cin>>to;
e1.SetToNum(to);
e1.UP();break;
}
case 2:{
cout<<"输入你要去的楼层:"<<endl;
cin>>to;
e1.SetToNum(to);
e1.DOWN();break;
}
case 3:
return 0;
default:
cout<<"输入错误!"<<endl;
}
}
system("pause");
return 0;
}