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);
}
}
c++初学(电梯实验加强版)的更多相关文章
- c++初学(电梯实验)
模拟电梯载人实验 Elevator.h class Elevator{public: Elevator(); ~Elevator(); void getNowNum(); ...
- 基于单层决策树的AdaBoost算法原理+python实现
这里整理一下实验课实现的基于单层决策树的弱分类器的AdaBoost算法. 由于是初学,实验课在找资料的时候看到别人的代码中有太多英文的缩写,不容易看懂,而且还要同时看代码实现的细节.算法的原理什么的, ...
- 第三次实验计算分段函数 第四次计算分段函数和循环NEW 第五次分支+循环加强版 实验报告
一.实验题目,设计思路,实现方法 第四次分支+循环 加强版 (2-2计算个人所得税,2-7 装睡,2-8计算天数) 设计思路:2-2 用if-else的语句,与计算分段函数的题类似的做法:2-7 运用 ...
- 微型计算机系统实验总结(学习性实验:IO地址译码,可编程并行接口8255,交通灯控制实验 + 自主设计实验:汽车信号灯控制系统,电风扇控制器,洗衣机控制系统,霓虹灯,电梯控制系统)
实验配套软件: https://download.csdn.net/download/qq_39932172/11221584 实验指导用书: 教师版: https://download.csdn.n ...
- PHP基础班初学心得:脑洞实验-JS变量存储函数与return的一些问题
本人刚参加PHP基础班培训第一天,由于之前毫无基础,分享的心得可能不规范,方法也许也"旁门左道",不能保证质量,只作自己总结学习,也希望能帮助到同样是初学者的朋友们,共同进步. 在 ...
- 201771030115-牛莉梅 实验一 软件工程准备-<初学《构建之法--现代软件工程》的疑问>
项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/xbsf/nwnu2020SE 这个作业要求链接 https://www.cnblogs.com/nwnu- ...
- SQL学习(1)初学实验:SQL Server基本配置及基本操作
网络配置.远程连接配置: 防火墙设置: SQL Server的默认端口号是1433. 网络配置: SQLServer Configuration Manager中的客户端协议,众多IP中随便选一个,比 ...
- 初学 Java Web 开发,请远离各种框架,从 Servlet 开发
Web框架是开发者在使用某种语言编写Web应用服务端时关于架构的最佳实践.很多Web框架是从实际的Web项目抽取出来的,仅和Web的请求和响应处 理有关,形成一个基础,在开发别的应用项目的时候则可以从 ...
- 初学javaweb,远离各自框架
OSCHINA 软件库有一个分类--Web框架,该分类中包含多种编程语言的将近500个项目. Web框架是开发者在使用某种语言编写Web应用服务端时关于架构的最佳实践.很多Web框架是从实际的Web项 ...
随机推荐
- 集合问题 离线+并查集 HDU 3938
题目大意:给你n个点,m条边,q个询问,每条边有一个val,每次询问也询问一个val,定义:这样条件的两个点(u,v),使得u->v的的价值就是所有的通路中的的最长的边最短.问满足这样的点对有几 ...
- usb调试
修改文件:/home/mxy/code/v1/kernel-3.10/drivers/power/mediatek/battery_common.c //bool AutoDebug=true;//x ...
- BAT54C 二极管是如何工作的?
这是一个多电源供电的电路:Vcc是正常供电电源(如5V,由市电变换得到),电压大于(Vcc1-Vf),正常供电时二极管不导通:Vcc1是电池供电电源,当Vcc撤掉时,DD1(上边的二极管)导通,由Vc ...
- ntopng汉化记录
对应版本为 ntopng-1.2.0_r8116.tgz 1.
- mysql Explain 性能分析关键字
EXPLAIN 输出格式select_typetabletypepossible_keyskeykey_lenrowsExtra MySQL 提供了一个 EXPLAIN 命令, 它可以对 SELECT ...
- java fx example
http://www.java2s.com/Tutorials/Java/JavaFX/1500__JavaFX_WebEngine.htm
- HTML基本
a标签的四种状态 a:link; /* 未访问的链接 */ a:visited; /* 已访问的链接 */ a:hover; /* 当有鼠标悬停在链接上 */ a:hover 必须位于 a:lin ...
- mysql 触发器学习
1. 一个简单的例子 1.1. 创建表: create table t(s1 integer); 1.2. 触发器: delimiter | create trigger t_trigger befo ...
- 酷炫的方块状散点3D
http://threejs.org/examples/webgl_particles_random.html
- Spring ---annotation (重点)--Resource, Component 重要!!!
beans.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...