本次作业是结对编程,是在团队项目之前的一个小型项目,旨在考验团队协作能力与锻炼个人编程能力,其实一开始我拿到这个题目还以为是一个很简单的问题,因为觉得电梯的运作方式很“傻瓜”,但是我的队友魏鑫磊()却并不是这么想的,在这里真的要感谢我的队友,磊姐想的太全面了!磊姐想到了这个项目最好应该是软硬件相结合,尤其是一些电梯控制超载,图形显示,传感器肯定是要有硬件实现,经过讨论,我才发现我想的真是太简单了。。。。(差距啊= =)但是由于客观条件不足(主要是我不会啊= =)最后我们决定将一开始的计划进行简化,抛弃了做界面的想法,实现基本功能就好。。
首先是需求分析:
1.4部电梯,每个电梯的信息不同,需要根据它的信息,做出与之对应的停靠、响应
2.如何能让电梯符合日常需求,做出合理控制
3.图形显示就先让它尽量用输出表述清晰
我们在模拟电梯的运行方式上达成了共识,因为只有四部电梯,所以把每个电梯的楼层用一个数组表示,通过上升下降实现对楼层控制,用该数组输出。接下来是显示问题,由于四部电梯停靠楼层,载人数都不一样,所以可以在输出显示中分别各自显示,运行中的电梯上下用箭头,停靠用下划线。但是我们还是遇到了难题,无法控制运行时间,每走一层应该是要有相应的变量进行控制才对,这样才是电梯啊。磊姐还是想用硬件表示,比如用中断显示电梯运行状态,但是客观条件实在不允许啊(其实主要还是我不会。。。)这时候我就想到了一个很傻瓜的方法,通过输入一个固定的指令来显示控制电梯的运行。解决这个问题,就可以动手运作。
总结:本次结对编程是一次很新鲜的尝试,是我之前从未有过的体验。在团队成员很少的情况下,只有两个人会磨合的更好,而且进入状态也很快。因为我的性格平时马马虎虎,大大咧咧,所以这次很庆幸我的队友是一位细心认真,一丝不苟的姑娘,如果满分是一百分,我要给磊姐九十九!(少一分怕他骄傲QAQ)很感谢磊姐在这次结对编程中给予我的帮助,也让我认识到了很多我平时没有发现的问题与短板。因为我平时编程习惯很不好,基本就是想到哪就写到哪,没有很清晰的思路,主要也是自己喜欢偷懒,坐不住,但是这次结对作业中,有队友的不断提点与督促,两个人共同理清思路,效果事半功倍。这也让我对接下来的团队编程更有信心,我相信我们小组六位成员一定会群策群力,共同进步!
PS:下面是我们程序的源代码与运行截图
#include<iostream>
#include<string.h>
#include<conio.h>
#include<windows.h>
using namespace std;
char status[4] = { '_',' ',' ',' ' };
int elevator[4] = { 5,8,13,20 };
char status_not[4] = { '_',' ',' ',' ' };
int flag_even = 0, flag_odd = 0, flag_all_ten = 0, flag_all_twenty = 0;
void display(int a)
{
HANDLE hOut;
COORD pos = { 0,0 }; /* 光标的起始位(第1列,第3行) 0是第1列 2是第3行*/
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
int i,j;
for (i = 0; i < 4; i++)
{
cout << elevator[i];
for (j = 0; j < 4; j++)
{
if (i == a)
cout << status[j];
else
cout << status_not[j];
}
}
}
void upstairs(int a)
{
char m;
HANDLE hOut;
COORD pos = { 0,6 };
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
cin >> m;
if (m = 'a')
{
elevator[a] = elevator[a] + 1;
status[0] = int(24);
display(a);
}
}
void downstairs(int a)
{
char m;
HANDLE hOut;
COORD pos = { 0,6 };
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
cin >> m;
if (m = 'a')
{
elevator[a] = elevator[a] - 1;
status[0] = int(25);
display(a);
}
}
void lift(int Nowfloor,int direction)
{
int a, b, c, d, min,floor,peoplecount=0;
char m;
a = Nowfloor - elevator[1] ;//偶数
b = Nowfloor - elevator[2] ;//全10
c = Nowfloor - elevator[3] ;//全20
d = Nowfloor - elevator[0] ;//奇数
if (direction == 2)
{
if (a > 0)
a = 25;
if (b > 0)
b = 25;
if (c > 0)
c = 25;
if (d > 0)
d = 25;
a = abs(a);
b = abs(b);
c = abs(c);
d = abs(d);
}
if (direction == 1)
{
if (a < 0)
a = 25;
if (b < 0)
b = 25;
if (c < 0)
c = 25;
if (d < 0)
d = 25;
}
if (b >= c)
min = c;
else
min = b;
if (b == 25 && c == 25)
min = 22;
if (a % 2 != 0)
{
if (d % 2 == 0)
{
if (min >= d)
min = d;
}
}
if (d % 2 != 0)
{
if (a % 2 == 0)
{
if (min >= a)
min = a;
}
}
if ( Nowfloor % 2 == 0)
{
if (min == a&&flag_even==0)
{
if (elevator[1] == Nowfloor)
{
if (direction == 1)
{
status[0] = int(24);
}
else
{
status[0] = int(25);
}
display(1);
}
for (; elevator[1] != Nowfloor;)
{
if (direction == 1)
upstairs(1);
else
downstairs(1);
}
HANDLE hOut;
COORD pos = { 0,7 }; /* 光标的起始位(第1列,第3行) 0是第1列 2是第3行*/
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
cout << "请输入人数,范围为0~20 ";
cin >> peoplecount;
peoplecount += peoplecount;
if (peoplecount > 20)
cout << "人数已满\n";
else
{
cout << "已到达当前楼层,请输入目的楼层,范围为0~20 ";
cin >> floor;
while (floor % 2 != 0)
{
HANDLE hOut;
COORD pos = { 0,8 }; /* 光标的起始位(第1列,第3行) 0是第1列 2是第3行*/
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
cout << " \n";
cout << "该电梯只在偶数层停靠\n";
cin >> floor;
}
for (; elevator[1] != floor;)
{
if (direction == 1)
upstairs(1);
else
downstairs(1);
}
status[0] = int(95);
display(1);
}
}
}
else if ( Nowfloor % 2 != 0)
{
if (min == d&&flag_odd == 0)
{
if (elevator[0] == Nowfloor)
{
if (direction == 1)
{
status[0] = int(24);
}
else
{
status[0] = int(25);
}
display(0);
}
for (; elevator[0] != Nowfloor;)
{
if (direction == 1)
upstairs(0);
else
downstairs(0);
}
HANDLE hOut;
COORD pos = { 0,7 }; /* 光标的起始位(第1列,第3行) 0是第1列 2是第3行*/
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
cout << "请输入人数,范围为0~10 ";
cin >> peoplecount;
peoplecount += peoplecount;
if (peoplecount > 10)
cout << "人数已满\n";
else
{
cout << "已到达当前楼层,请输入目的楼层,范围为0~20 ";
cin >> floor;
while (floor % 2 == 0)
{
HANDLE hOut;
COORD pos = { 0,8 }; /* 光标的起始位(第1列,第3行) 0是第1列 2是第3行*/
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
cout << " \n";
cout << "该电梯只在奇数层停靠\n";
cin >> floor;
}
for (; elevator[0] != floor;)
{
if (direction == 1)
upstairs(0);
else
downstairs(0);
}
status[0] = int(95);
display(0);
}
}
}
if (min == b&&flag_all_ten == 0)
{
if (elevator[2] == Nowfloor)
{
if (direction == 1)
{
status[0] = int(24);
}
else
{
status[0] = int(25);
}
display(2);
}
for (; elevator[2] != Nowfloor;)
{
if (direction == 1)
upstairs(2);
else
downstairs(2);
}
HANDLE hOut;
COORD pos = { 0,7 }; /* 光标的起始位(第1列,第3行) 0是第1列 2是第3行*/
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
cout << "请输入人数,范围为0~10 ";
cin >> peoplecount;
peoplecount += peoplecount;
if (peoplecount > 10)
cout << "人数已满\n";
else
{
cout << "已到达当前楼层,请输入目的楼层,范围为0~20 ";
cin >> floor;
for (; elevator[2] != floor;)
{
if (direction == 1)
upstairs(2);
else
downstairs(2);
}
status[0] = int(95);
display(2);
}
}
if (min == c&&flag_all_twenty == 0)
{
if (elevator[3] == Nowfloor)
{
if (direction == 1)
{
status[0] = int(24);
}
else
{
status[0] = int(25);
}
display(3);
}
for (; elevator[3] != Nowfloor;)
{
if (direction == 1)
upstairs(3);
else
downstairs(3);
}
HANDLE hOut;
COORD pos = { 0,7 }; /* 光标的起始位(第1列,第3行) 0是第1列 2是第3行*/
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
cout << "请输入人数,范围为0~20 ";
cin >> peoplecount;
peoplecount += peoplecount;
if (peoplecount > 20)
cout << "人数已满\n";
else
{
cout << "已到达当前楼层,请输入目的楼层,范围为0~20 ";
cin >> floor;
for (; elevator[3] != floor;)
{
if (direction == 1)
upstairs(3);
else
downstairs(3);
}
status[0] = int(95);
display(3);
}
}
if (min != 22)
{
HANDLE hOut;
COORD pos = { 0,11 }; /* 光标的起始位(第1列,第3行) 0是第1列 2是第3行*/
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
cout << "已到达目的楼层\n";
}
}
void menu()
{
int direction,floor,Nowfloor,flag;
int i,j;
for (i = 0; i < 4; i++)
{
cout << elevator[i];
for (j = 0; j < 4; j++)
{
cout << status[j];
}
}
while (1)
{
HANDLE hOut;
COORD pos = { 0,2 }; /* 光标的起始位(第1列,第3行) 0是第1列 2是第3行*/
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, pos);
cout << "\n1.UP 2.DOWN\n";
cout << "输入呼叫楼层,范围为0~20\n";
cin >> direction>>Nowfloor;
switch (direction)
{
case 1:
{
lift(Nowfloor,direction);
break;
}
case 2:
{
lift(Nowfloor,direction);
break;
}
}
}
}
int main()
{
menu();
return 1;
}
/*void even(int floor)
{
int i;
for (i = 0; i < 1000; i++)
{
if (flag_even == 1)
{
if (flag_all_ten == 1)
{
if (flag_all_twenty == 1)
continue;
else
elevator_all_twenty(floor);
}
else
elevator_all_ten(floor);
}
else
elevator_even(floor);
}
}
void odd(int floor)
{
int i;
for (i = 0; i < 1000; i++)
{
if (flag_all_twenty == 1)
{
if (flag_all_ten == 1)
{
if (flag_all_twenty == 1)
continue;
else
elevator_all_twenty(floor);
}
else
elevator_all_ten(floor);
}
else
elevator_odd(floor);
}
}
void elevator_even(int floor)
{
height=get_height();
passenger=get_passenger();
if (height <= 1600 && passenger <= 20)
{
}
}
void elevator_odd(int floor)
{
height = get_height();
passenger = get_passenger();
if (height <= 800 && passenger <= 10)
}
void elevator_all_ten(int floor)
{
height = get_height();
passenger = get_passenger();
if (height <= 800 && passenger <= 10)
}
void elevator_all_twenty(int floor)
{
height = get_height();
passenger = get_passenger();
if (height <= 2000 && passenger <= 20)
}*/