浙江理工大学信息学院
面向对象程序设计实验报告
实验名称:类的定义与使用 学时安排:3
实验类别:设计性实验 实验要求:1人1组
姓名:杨正龙 学号:2020329621193
 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄
一、实验目的
1)掌握类的概念、类的定义格式、类与结构的关系、类的成员属性和类的封装性;
2)掌握类对象的定义;
3)理解类的成员的访问控制的含义,公有、私有和保护成员的区别;
4)掌握构造函数和析构函数的含义与作用、定义方式和实现,能够根据要求正确定义和重载构造函数。能够根据给定的要求定义类并实现类的成员函数;
二、实验原理介绍
通过建立类及对象,用类的成员函数和对象访问类的成员;
利用建立类的构造函数,完成类的成员的初始化工作;
三、实验设备介绍
软件需求: Visual Studio C++或Codeblocks或Dev C++或其他C++ IDE
硬件需求: 能够流畅运行C++ IDE的计算机一台。
四、实验内容
编写一个程序,模拟电梯的功能。功能接口包括电梯上行按钮、下行按钮、楼层选择和电梯在行驶过程中的楼层显示。
要求:
1.由用户选择按上行按钮还是下行按钮,选择操作后再由用户输入要进入的楼层,进而电梯开始运行,显示所到的每一楼层层数。
2.如果是上行,则选择输入的楼层好不能比当前楼层号小,否则应给出不合法提示。
3. 如果是下行,则选择输入的楼层好不能比当前楼层号大,否则应给出不合法提示。
4.电梯一旦开始运作就会始终运行,直到窗口关闭。
5.电梯在经过不同楼层时,最好每个楼层的显示之间能有延迟,最终停靠的楼层的输出形式能更加醒目。如果可以,在电梯最初开始运行时,能在电梯由内部显示当前日期(提示:实现这些功能时,需要调用系统api,实现时间显示功能可以使用Date类)。
五 程序清单
data.h:
#pragma once
#include <iostream>
#include<ctime>
#include<cstdlib>
#include<string>
#include<cstdio>
using namespace std;
class CDate {
public:
CDate(int dd, int mm = 1, int yy = 1999);
CDate();
CDate(const CDate& other);
~CDate();
CDate addYear(int n);
CDate addMonth(int n);
CDate addDay(int n);
string format(string df);
int getDay() const;
int getMonth() const;
int getYear() const;
bool isEquals(CDate date) const;
public:
int* d, * m, * y;
int maxDay[2][13] =
{ {0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31} };
const string dfs = "ddd";
const string dfl = "DDD";
};
elevator.h:
#pragma once
#include <iostream>
#include<ctime>
#include<cstdlib>
#include<string>
#include<cstdio>
#include<windows.h>
#define MS 100
using namespace std;
class Elevator {
private:
int now_floor, want_floor, choice, total_floors;
void up(int want_floor);
void down(int want_floor);
void inputWant_floor();
void inputChoice();
void outsideopen();//电梯外开门动画
void outsideclose();//电梯外关门动画
void insideopen();//电梯内开门动画
void insideclose();//电梯内关门动画
void waitgame();//等待动画
public:
Elevator(int now_floorr, int total_floorss);
void control();
void display();
};
data.cpp:
#include"data.h"
CDate::CDate(int dd, int mm, int yy) :dfs("ddd"), dfl("DDD") //初始化
{
if ((yy >= 1000 && yy <= 9999) && (mm >= 1 && mm <= 12) && (dd >= 1 && dd
<= maxDay[(yy / 4 == 0 && yy / 100 != 0) || (yy / 400 == 0)][mm]))
{
y = new int(yy);
m = new int(mm);
d = new int(dd);
}
else throw "create CDate object error";
}
CDate::CDate() :dfs("ddd"), dfl("DDD") //初始化
{
time_t now;
time(&now);
struct tm* t_now;
t_now = localtime(&now);
y = new int(t_now->tm_year + 1900);
m = new int(t_now->tm_mon + 1);
d = new int(t_now->tm_mday);
}
CDate::CDate(const CDate& other)
{
cout << "CDate::CDate(const CDate &other)" << endl;
y = new int(*other.y);
m = new int(*other.m);
d = new int(*other.d);
}
CDate::~CDate()
{
cout << "CDate::~CDate()" << endl;
delete y;
delete m;
delete d;
}
CDate CDate::addYear(int n) //加 n 年
{
*y += n;
return *this;
}
CDate CDate::addMonth(int n) //加 n 月
{
*m += n;
if (*m > 12) {
*m = *m % 12;
(*y)++;
}
return *this;
}
CDate CDate::addDay(int n) //加 n 天
{
*d += n;
int dd = maxDay[(*y / 4 == 0 && *y / 100 != 0) || (*y / 400 == 0)][*m];
if (*d > dd)
{
*d = *d % dd;
(*m)++;
}
return *this;
}
string CDate::format(string df)
{
char c_df[20];
if (df == dfs)
{
sprintf(c_df, "%d-%d-%d", *y, *m, *d);
return string(c_df);
}
if (df == dfl)
{
sprintf(c_df, "%d 年%d 月%d 日", *y, *m, *d);
return string(c_df);
}
return string("");
}
int CDate::getDay() const
{
return *d;
}
int CDate::getMonth() const
{
return *m;
}
int CDate::getYear() const
{
return *y;
}
bool CDate::isEquals(CDate date) const
{
if ((*y == *date.y) && (*m == *date.m) && (*d == *date.d))
return true;
else return false;
}
function.cpp:
#include"elevator.h"
void display();
void Exit();
Elevator::Elevator(int now_floorr, int
total_floorss) :want_floor(0), choice(0)
{
total_floors = total_floorss;
now_floor = now_floorr;
cout << "该电梯一共" << total_floors << "层,欢迎您使用!" <<
endl << endl;
}
void Elevator::display() {
cout << "┌───────────┬──────────┐" << endl;
cout << "│ │ │ " << endl;
cout << "│ │ │ " << endl;
cout << "│ │ │ " << endl;
cout << "│ │ │ ┌───┐ " << endl;
cout << "│ │ │ │ ▲│ 1.上升 " << endl;
cout << "│ │ │ │ ▼│ 2.下降 " << endl;
cout << "│ │ │ └───┘ " << endl;
cout << "│ │ │ " << endl;
cout << "│ │ │ " << endl;
cout << "│ │ │ " << endl;
cout << "│ │ │ " << endl;
cout << "└───────────┴──────────┘" << endl;
}
void Elevator::inputChoice()
{
cin >> choice;
}
void Elevator::inputWant_floor()
{
cin >> want_floor;
}
void Elevator::down(int want_floor)
{
for (now_floor; want_floor < now_floor; now_floor--) {
system("cls");
cout << "┌───────────┬──────────┬───────┐" << endl;
cout << "│ │ │ ┌───┐ │ " << endl;
cout << "│ │ │ │ " << now_floor << " │ │ " << endl;
cout << "│ │ │ │ ▼│ │ " << endl;
cout << "│ │ │ └───┘ │ " << endl;
cout << "│ │ │┌─────┐│ " << endl;
cout << "│ │ ││ ⑨⑩││ " << endl;
cout << "│ │ ││ ⑦⑧││ " << endl;
cout << "│ │ ││ ⑤⑥││ " << endl;
cout << "│ │ ││ ③④││ " << endl;
cout << "│ │ ││ ①②││ " << endl;
cout << "│ │ │└─────┘│ " << endl;
cout << "└───────────┴──────────┴───────┘" << endl;
Sleep(1000); //等待1秒
}
system("cls");
cout << "┌───────────┬──────────┬───────┐" << endl;
cout << "│ │ │ ┌───┐ │ " << endl;
cout << "│ │ │ │ " << want_floor << " │ │ " << endl;
cout << "│ │ │ │ ▼│ │ " << endl;
cout << "│ │ │ └───┘ │ " << endl;
cout << "│ │ │┌─────┐│ " << endl;
cout << "│ │ ││ ⑨⑩││ " << endl;
cout << "│ │ ││ ⑦⑧││ " << endl;
cout << "│ │ ││ ⑤⑥││ " << endl;
cout << "│ │ ││ ③④││ " << endl;
cout << "│ │ ││ ①②││ " << endl;
cout << "│ │ │└─────┘│ " << endl;
cout << "└───────────┴──────────┴───────┘" << endl;
Sleep(1000); //等待1秒
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "第" << want_floor << "层到了!" << endl << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE);
Sleep(1000);
system("cls");
insideopen();
outsideclose();
system("cls");
display();
control();
}
void Elevator::up(int want_floor)
{
for (now_floor; now_floor < want_floor; now_floor++) {
system("cls");
cout << "┌───────────┬──────────┬───────┐" << endl;
cout << "│ │ │ ┌───┐ │ " << endl;
cout << "│ │ │ │ " << now_floor << " │ │ " << endl;
cout << "│ │ │ │ ▲│ │ " << endl;
cout << "│ │ │ └───┘ │ " << endl;
cout << "│ │ │┌─────┐│ " << endl;
cout << "│ │ ││ ⑨⑩││ " << endl;
cout << "│ │ ││ ⑦⑧││ " << endl;
cout << "│ │ ││ ⑤⑥││ " << endl;
cout << "│ │ ││ ③④││ " << endl;
cout << "│ │ ││ ①②││ " << endl;
cout << "│ │ │└─────┘│ " << endl;
cout << "└───────────┴──────────┴───────┘" << endl;
Sleep(1000); //等待1秒
}
system("cls");
cout << "┌───────────┬──────────┬───────┐" << endl;
cout << "│ │ │ ┌───┐ │ " << endl;
cout << "│ │ │ │ " << want_floor << " │ │ " << endl;
cout << "│ │ │ │ ▲│ │ " << endl;
cout << "│ │ │ └───┘ │ " << endl;
cout << "│ │ │┌─────┐│ " << endl;
cout << "│ │ ││ ⑨⑩││ " << endl;
cout << "│ │ ││ ⑦⑧││ " << endl;
cout << "│ │ ││ ⑤⑥││ " << endl;
cout << "│ │ ││ ③④││ " << endl;
cout << "│ │ ││ ①②││ " << endl;
cout << "│ │ │└─────┘│ " << endl;
cout << "└───────────┴──────────┴───────┘" << endl;
Sleep(1000); //等待1秒
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED);
cout << "第" << want_floor << "层到了!" << endl << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),
FOREGROUND_INTENSITY | FOREGROUND_RED |
FOREGROUND_GREEN | FOREGROUND_BLUE);
Sleep(1000);
system("cls");
insideopen();
outsideclose();
system("cls");
display();
control();
}
void Elevator::control()
{
inputChoice();
if (choice == 1) {
outsideopen();
system("cls");
insideclose();
waitgame();
cout << "请输入您想去的楼层:" << endl;
inputWant_floor();
if (want_floor > 0 && want_floor<total_floors + 1 && want_floor>now_floor) {
up(want_floor);
display();
}
else if (want_floor<0 || want_floor>total_floors) {
cout << "该楼层不存在" << endl;
Sleep(1000);
system("cls");
insideopen();
outsideclose();
system("cls");
display();
control();
}
else if (want_floor == now_floor) {
cout << "您已经在该楼层" << endl;
Sleep(1000);
system("cls");
insideopen();
outsideclose();
system("cls");
display();
control();
}
else {
cout << " 您 不 能 上 行 !" << endl;
Sleep(1000);
system("cls");
in