c++初学(电梯实验加强版)
Elevator.h
class Elevator{
public:
Elevator();
~Elevator();
void getNowNum();
void SetToNum(int);
void SetNowNum(int);
void RUN();
void set_choice(int);
int choice;
private:
int const MaxWeight=300;
int NowNum;
int ToNum;
};
class Person{
public:
int from;
int to;
int weight;
};
class Temp{
public:
int p_num; //电梯需要停靠楼层
int p_count;//第几位乘客
int weight;//乘客体重
int ud;//乘客进入或者出去
};
class ElevatorFactory :public Elevator{
private:
Person p[10];
Temp t[20];
int max_weight;
int count;
int now_weight;
public:
ElevatorFactory(int w);
void run();
void set_count(int);
void set_p_info();
void turn();
void sort();
int del(int p_del, int T);
};
Elevator.cpp
#include<iostream>
#include<Windows.h>
#include"Elevator.h"
using namespace std;
Elevator::Elevator()
{
NowNum = 1;
ToNum = 10;
}
Elevator::~Elevator()
{
}
void Elevator::SetNowNum(int n)
{
NowNum = n;
}
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);
}
void Elevator::SetToNum(int a)
{
ToNum = a;
}
void Elevator::RUN()
{
int i;
if (choice == 1){
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;
}
}
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;
}
}
}
ElevatorFactory::ElevatorFactory(int w)
{
now_weight = 0;
count = 0;
max_weight = w;
choice = 0;
}
void ElevatorFactory::set_count(int c)
{
count = c;
}
void Elevator::set_choice(int c)
{
choice = c;
}
void ElevatorFactory::set_p_info()
{
int i;
for (i = 0; i < count; i++)
{
cin>>p[i].from;
cin>>p[i].to;
cin>>p[i].weight;
}
}
void ElevatorFactory::turn()
{
int i=0,j=0;
for (i = 0; i < count; i++)
{
t[j].p_num = p[i].from;
t[j].p_count = i + 1;
t[j].ud = 1;
t[j].weight = p[i].weight;
j++;
t[j].p_num = p[i].to;
t[j].p_count = i + 1;
t[j].ud = 0;
t[j].weight = p[i].weight;
j++;
}
}
void ElevatorFactory::sort()
{
int i, j,k;
Temp temp;
for (i = 0; i < count * 2 - 1; i++)
{
k = i;
for (j = i + 1; j < count * 2; j++)
{
if (choice == 1){
if (t[k].p_num > t[j].p_num)
k = j;
}
else
{
if (t[k].p_num < t[j].p_num)
k = j;
}
}
if (k != i)
{
temp.p_num = t[k].p_num;
temp.p_count = t[k].p_count;
temp.weight = t[k].weight;
temp.ud = t[k].ud;
t[k].p_num = t[i].p_num;
t[k].p_count = t[i].p_count;
t[k].weight = t[i].weight;
t[k].ud = t[i].ud;
t[i].p_num = temp.p_num;
t[i].p_count = temp.p_count;
t[i].weight = temp.weight;
t[i].ud = temp.ud;
}
}
}
int ElevatorFactory::del(int p_del,int T)
{
for (int i = p_del; i < T; i++)
{
if (t[i].p_count == p_del)
{
for (int j = i; j < T; j++)
{
t[j].p_num = t[j+1].p_num;
t[j].p_count = t[j+1].p_count;
t[j].weight = t[j+1].weight;
t[j].ud = t[j+1].ud;
}
}
}
T--;
return T;
}
void ElevatorFactory::run()
{
int T = count * 2;
int i;
turn();
sort();
for (i = 0; i < T; i++)
{
SetToNum(t[i].p_num);
RUN();
if (t[i].ud == 1) //上电梯
{
HANDLE consolehwnd;
consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(consolehwnd, 10);
cout << "第" << t[i].p_count << "位乘客上电梯咯,电梯开动,啦啦啦~啦啦啦~~" << endl;
now_weight += t[i].weight;
if (now_weight > max_weight)
{
cout << "咦,开动不了,电梯超载了,第" <<t[i].p_count<<"位乘客不开心的走下了电梯"<< endl;
T=del(t[i].p_count,T);
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
else{ //下电梯
HANDLE consolehwnd;
consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(consolehwnd, 10);
cout << "第" << t[i].p_count << "位乘客下电梯咯,先生慢走,啦啦啦~啦啦啦~~" << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
now_weight -= t[i].weight;
}
SetNowNum(t[i].p_num);
}
}
demo.cpp
#include<iostream>
#include<Windows.h>
#include<stdio.h>
#include"Elevator.h"
using namespace std;
int main()
{
SYSTEMTIME time;
GetLocalTime(&time);
printf("%4d年%02d月%02d日 %02d:%02d:%02d 星期%1d\n", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wDayOfWeek);
int choice, to,num, i, now, count;
cout << "欢迎乘坐电梯\n" << endl;
int w;
cout << "请输入电梯最大载重:";
cin >> w;
ElevatorFactory e1(w);
while (1)
{
e1.getNowNum();
cout<<"输入你的选择:1-上 2-下 3-退出";
cin >> choice;
if (choice == 3) return 0;
e1.set_choice(choice);
cout << "请输入乘客数量:";
cin >> count;
e1.set_count(count);
cout << "请输入" << count << "位乘客当前楼层,目的楼层,体重" << endl;
e1.set_p_info();
e1.run();
}
system("pause");
return 0;
}
#include<iostream>
#include<Windows.h>
#include"Elevator.h"
using namespace std;
Elevator::Elevator()
{
NowNum = 1;
ToNum = 10;
}
Elevator::~Elevator()
{
}
void Elevator::SetNowNum(int n)
{
NowNum = n;
}
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);
}
void Elevator::SetToNum(int a)
{
ToNum = a;
}
void Elevator::RUN()
{
int i;
if (choice == 1){
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;
}
}
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;
}
}
}
ElevatorFactory::ElevatorFactory(int w)
{
now_weight = 0;
count = 0;
max_weight = w;
choice = 0;
}
void ElevatorFactory::set_count(int c)
{
count = c;
}
void Elevator::set_choice(int c)
{
choice = c;
}
void ElevatorFactory::set_p_info()
{
int i;
for (i = 0; i < count; i++)
{
cin>>p[i].from;
cin>>p[i].to;
cin>>p[i].weight;
}
}
void ElevatorFactory::turn()
{
int i=0,j=0;
for (i = 0; i < count; i++)
{
t[j].p_num = p[i].from;
t[j].p_count = i + 1;
t[j].ud = 1;
t[j].weight = p[i].weight;
j++;
t[j].p_num = p[i].to;
t[j].p_count = i + 1;
t[j].ud = 0;
t[j].weight = p[i].weight;
j++;
}
}
void ElevatorFactory::sort()
{
int i, j,k;
Temp temp;
for (i = 0; i < count * 2 - 1; i++)
{
k = i;
for (j = i + 1; j < count * 2; j++)
{
if (choice == 1){
if (t[k].p_num > t[j].p_num)
k = j;
}
else
{
if (t[k].p_num < t[j].p_num)
k = j;
}
}
if (k != i)
{
temp.p_num = t[k].p_num;
temp.p_count = t[k].p_count;
temp.weight = t[k].weight;
temp.ud = t[k].ud;
t[k].p_num = t[i].p_num;
t[k].p_count = t[i].p_count;
t[k].weight = t[i].weight;
t[k].ud = t[i].ud;
t[i].p_num = temp.p_num;
t[i].p_count = temp.p_count;
t[i].weight = temp.weight;
t[i].ud = temp.ud;
}
}
}
int ElevatorFactory::del(int p_del,int T)
{
for (int i = p_del; i < T; i++)
{
if (t[i].p_count == p_del)
{
for (int j = i; j < T; j++)
{
t[j].p_num = t[j+1].p_num;
t[j].p_count = t[j+1].p_count;
t[j].weight = t[j+1].weight;
t[j].ud = t[j+1].ud;
}
}
}
T--;
return T;
}
void ElevatorFactory::run()
{
int T = count * 2;
int i;
turn();
sort();
for (i = 0; i < T; i++)
{
SetToNum(t[i].p_num);
RUN();
if (t[i].ud == 1) //上电梯
{
HANDLE consolehwnd;
consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(consolehwnd, 10);
cout << "第" << t[i].p_count << "位乘客上电梯咯,电梯开动,啦啦啦~啦啦啦~~" << endl;
now_weight += t[i].weight;
if (now_weight > max_weight)
{
cout << "咦,开动不了,电梯超载了,第" <<t[i].p_count<<"位乘客不开心的走下了电梯"<< endl;
T=del(t[i].p_count,T);
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
else{ //下电梯
HANDLE consolehwnd;
consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(consolehwnd, 10);
cout << "第" << t[i].p_count << "位乘客下电梯咯,先生慢走,啦啦啦~啦啦啦~~" << endl;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY
| FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
now_weight -= t[i].weight;
}
SetNowNum(t[i].p_num);
}
}