题目:
编写一个程序,模拟电梯的功能。功能接口包括电梯上行按钮、下行按钮、楼层选择和电梯在行驶过程中的楼层显示。 要求:
1.由用户选择按上行按钮还是下行按钮,选择操作后再由用户输入要进入的楼层,进而电梯开始运行,显示所到的每一楼层层数。
2.如果是上行,则选择输入的楼层号不能比当前楼层号小,否则应给出不合法提示。
3.如果是下行,则选择输入的楼层号不能比当前楼层号大,否则应给出不合法提示。
4.电梯一旦开始运作就会始终运行,直到窗口关闭。
5.电梯在经过不同楼层时,最好每个楼层的显示之间能有延迟,最终停靠的楼层的输出形式能更加醒目。如果可以,在电梯最初开始运行时,能在电梯由内部显示当前日期(提示:实现这些功能时,需要调用系统api,实现时间显示功能可以使用CDate类)。
我们将这部分功能分为五个部分:
1、图形界面
2、接受上下行
3、选择楼层
4、上下行函数
5、上下行动画
电梯类
首先定义电梯类,我们用getmove记录用户的上下行输入,用getfloor记录用户的目标楼层,同时用静态变量current_floor记录电梯当前楼层
class Elevator
{
public:
static int current_floor; //current_floor表示现在楼层
int getmove, getfloor; //getmove接受上下行变量,gerfloor接受目标楼层
void interface2(); //图形界面
void move_receive(); //接受上下行
void select_floor(); //选择楼层
void move_up(); //上行函数
void move_down(); //下行函数
void time1(); //时间函数
Elevator()
{
getmove = 1;
getfloor = 1;
z = 0;
}
~Elevator()
{ }
private:
void animation_up(); //上行动画
void animation_down(); //下行动画
int z; //运行次数
};
图形界面
图形界面就相当于电梯的显示屏,用来提示用户输入,同时我们通过调用时间函数实现时间输出
注:SYSTEMTIME类和GetLocalTime函数都在windows.h头文件下
void Elevator::interface2() //定义图形界面函数
{
z++;
if(z>1)
Sleep(2000);
system("cls");
cout << "欢迎使用智慧电梯系统" << endl;
time1();
cout << " --" << "1、上行" << "--" << endl;
cout << " --" << "2、下行" << "--" << endl;
cout << " --" << "3、结束" << "--" << endl;
}
void Elevator::time1()
{
SYSTEMTIME s;
GetLocalTime(&s);
cout << s.wYear << "年" << s.wMonth << "月" << s.wDay <<"日 ";
cout << s.wHour << ":" << s.wMinute << endl;
}
接受上下行
这部分接受用户的上下行输入,如果用户输入了1~3之外的数字,需要报错,并提示用户重新输入
void Elevator::move_receive() //定义接受上下行函数
{
while (1)
{
cout << "请输入方向" << endl;
cin >> getmove;
if (getmove >= 1 && getmove <= 3)
{
break;
}
else
cout << "输入错误!" << endl;
}
}
选择楼层
接受用户的目标楼层,判断调用上行函数还是下行函数,如果电梯不在用户的楼层,首先调用上下行函数让电梯来到用户楼层
void Elevator::select_floor() //定义接受楼层函数
{
cout << "请输入当前楼层:";
cin >> getfloor;
if (getfloor > current_floor)
{
move_up();
}
else if (getfloor < current_floor)
{
move_down();
}
else if (getfloor == current_floor);
cout << "请输入目标楼层:";
cin >> getfloor;
}
上下行函数
这两个函数是对题目中2、3条的实现,同时调用对应的动画
void Elevator::move_up() //定义上行函数
{
if (getfloor > 10 || getfloor < 1)
{
cout << "输入错误,本栋楼只有10层" << endl;
}
else if (current_floor < getfloor)
{
animation_up();
}
else if (current_floor >= getfloor)
{
cout << "本次电梯上行!" << endl;
};
}
void Elevator::move_down() //定义下行函数
{
if (getfloor > 10 || getfloor < 1)
{
cout << "输入错误,本栋楼只有10层" << endl;
}
else if (current_floor > getfloor)
{
animation_down();
}
else if (current_floor <= getfloor)
{
cout << "本次电梯下行!" << endl;
}
}
上下行动画
这个函数主要实现动画显示,以及电梯运行到了第几层,同时要注意题目要求延迟显示,我们通过sleep函数来实现,sleep(1000)代表延迟一秒
注:sleep函数在windows.h头文件下
void Elevator::animation_up() //定义上行动画
{
int i;
system("cls");
for (i = 0; current_floor < getfloor; current_floor++)
{
cout << " --第" << current_floor << "层--" << endl;
cout << " / \\ " << endl;
cout << " / | \\ " << endl;
cout << " | " << endl;
cout << " | " << endl;
Sleep(2000);
i++;
if (i % 2 == 0)
system("cls");
}
system("cls");
cout << "电梯到达第" << current_floor << "层" << endl ;
}
void Elevator::animation_down() //定义上行动画
{
int i;
system("cls");
for (i = 0; current_floor > getfloor; current_floor--)
{
cout << " --第" << current_floor << "层--" << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " \\ | / " << endl;
cout << " \\ / " << endl;
Sleep(2000);
i++;
if (i % 2 == 0)
system("cls");
}
system("cls");
cout << "电梯到达第" << current_floor << "层" << endl ;
}
以下为完整代码:
注:类中的静态变量要在类外初始化
#include<string>
#include<iostream>
#include<stdio.h>
#include<Windows.h>
using namespace std;
class Elevator
{
public:
static int current_floor; //current_floor表示现在楼层
int getmove, getfloor; //getmove接受上下行变量,gerfloor接受目标楼层
void interface2(); //图形界面
void move_receive(); //接受上下行
void select_floor(); //选择楼层
void move_up(); //上行函数
void move_down(); //下行函数
void time1(); //时间函数
Elevator()
{
getmove = 1;
getfloor = 1;
z = 0;
}
~Elevator()
{ }
private:
void animation_up(); //上行动画
void animation_down(); //下行动画
int z; //运行次数
};
int Elevator::current_floor = 1;
void Elevator::interface2() //定义图形界面函数
{
z++;
if(z>1)
Sleep(2000);
system("cls");
cout << "欢迎使用智慧电梯系统" << endl;
time1();
cout << " --" << "1、上行" << "--" << endl;
cout << " --" << "2、下行" << "--" << endl;
cout << " --" << "3、结束" << "--" << endl;
}
void Elevator::time1()
{
SYSTEMTIME s;
GetLocalTime(&s);
cout << s.wYear << "年" << s.wMonth << "月" << s.wDay <<"日 ";
cout << s.wHour << ":" << s.wMinute << endl;
}
void Elevator::move_receive() //定义接受上下行函数
{
while (1)
{
cout << "请输入方向" << endl;
cin >> getmove;
if (getmove >= 1 && getmove <= 3)
{
break;
}
else
cout << "输入错误!" << endl;
}
}
void Elevator::select_floor() //定义接受楼层函数
{
cout << "请输入当前楼层:";
cin >> getfloor;
if (getfloor > current_floor)
{
move_up();
}
else if (getfloor < current_floor)
{
move_down();
}
else if (getfloor == current_floor);
cout << "请输入目标楼层:";
cin >> getfloor;
}
void Elevator::move_up() //定义上行函数
{
if (getfloor > 10 || getfloor < 1)
{
cout << "输入错误,本栋楼只有10层" << endl;
}
else if (current_floor < getfloor)
{
animation_up();
}
else if (current_floor >= getfloor)
{
cout << "本次电梯上行!" << endl;
};
}
void Elevator::move_down() //定义下行函数
{
if (getfloor > 10 || getfloor < 1)
{
cout << "输入错误,本栋楼只有10层" << endl;
}
else if (current_floor > getfloor)
{
animation_down();
}
else if (current_floor <= getfloor)
{
cout << "本次电梯下行!" << endl;
}
}
void Elevator::animation_up() //定义上行动画
{
int i;
system("cls");
for (i = 0; current_floor < getfloor; current_floor++)
{
cout << " --第" << current_floor << "层--" << endl;
cout << " / \\ " << endl;
cout << " / | \\ " << endl;
cout << " | " << endl;
cout << " | " << endl;
Sleep(2000);
i++;
if (i % 2 == 0)
system("cls");
}
system("cls");
cout << "电梯到达第" << current_floor << "层" << endl ;
}
void Elevator::animation_down() //定义上行动画
{
int i;
system("cls");
for (i = 0; current_floor > getfloor; current_floor--)
{
cout << " --第" << current_floor << "层--" << endl;
cout << " | " << endl;
cout << " | " << endl;
cout << " \\ | / " << endl;
cout << " \\ / " << endl;
Sleep(2000);
i++;
if (i % 2 == 0)
system("cls");
}
system("cls");
cout << "电梯到达第" << current_floor << "层" << endl ;
}
int main()
{
Elevator elevator;
while (elevator.getmove != 3)
{
elevator.interface2(); //输出界面
elevator.move_receive(); //接受上下行键入
if (elevator.getmove != 3)
{
elevator.select_floor(); //接受楼层键入
if (elevator.getmove == 1)
{
elevator.move_up(); //上行
}
else if (elevator.getmove == 2)
{
elevator.move_down(); //下行
}
}
else
{
printf("程序已退出");
continue;
}
}
return 0;
}