close

Вход

Забыли?

вход по аккаунту

?

09. ТСПП (дод Г)

код для вставкиСкачать
??????? ?
???????? Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Elevator;
namespace consoleApp
{
class Program // ???? Program
{
static void Main(string[] args)
{
Building theBuilding = new Building(); //????????? ???????
Console.SetCursorPosition(1, 24);
Console.WriteLine(); //????????? ?? ??????? ????, ??? ????? ?? ???????
while (true)
{
theBuilding.master_tick(); //??????
theBuilding.record_floor_reqs();
} } }}
ClassLibrary1.dll using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Elevator
{
public enum Direction { UP = 0, DOWN = 1, STOP = 2 }
public class Building
{
public static void clear_line()
{
for (int i = 0; i < 79; i++) Console.Write(" ");
}
private bool[,] floor_request; //????? ??????? ????????
Elevator[] car_list; //??????? - ?????
//???????????
public Building()
{
Console.Clear(); // ??????? ??????
car_list = new Elevator[4]; // ????????? ?????? 4? ??????
floor_request = new bool[2, 20]; // 2 ??????????? 20 ????????
for (int i = 0; i < 4; i++) ???????? ?????
{
car_list[i] = new Elevator(this, i); //???? Elevator
}
for (int i = 0; i < 20; i++) // ??? ??????? ???????
{
Console.SetCursorPosition(3, 20 - i); // ??????? ????? ??????? ?? ?????
Console.Write((i + 1));
if (i < 10) Console.Write(" ");
floor_request[(int)Direction.UP, i] = false; // ??????? ?? ?? ????
floor_request[(int)Direction.DOWN, i] = false;
}
}
public void master_tick() // ????????
{
int j;
show_floor_reqs(); // ??????? ?????? ????????
for (j = 0; j < 4; j++) // ??? ??????? ?????
{
Elevator el = (Elevator)car_list[j];
el.car_tick1(); // ???? ????? ????? ?????
}
for (j = 0; j < 4; j++)
{
Elevator el = (Elevator)car_list[j];
el.car_tick2(); ???????? ?????? ??????? ?????
}
}
public bool get_floor_req(Direction dir, int floor) // ????????? ????, ?? ? ??????
{
return floor_request[(int)dir, floor];
}
public void set_floor_req(Direction dir, int floor, bool updown) // ?????????? ?????
{
floor_request[(int)dir, floor] = updown;
}
public int get_cars_floor(int carNo) // ????? ???????
{
return car_list[carNo].get_floor();
}
public Direction get_cars_dir(int carNo) // ?????????? ???????????
{
return car_list[carNo].get_direction();
}
public void show_floor_reqs() ????? ???????
{
for (int j = 0; j < 20; j++)
{
Console.SetCursorPosition(7, 20 - j);
if (floor_request[(int)Direction.UP, j]) Console.Write("^"); // ??????? ?????
else Console.Write(" ");
Console.SetCursorPosition(7 + 3, 20 - j);
if (floor_request[(int)Direction.DOWN, j]) Console.Write("v"); // ??????? ????
else Console.Write(" ");
}
}
public void record_floor_reqs() // ????????? ??????? ? ???????
{ // ??? ?? ?????????? ?????? ? ????? ?? ???????
char[] ustring = new char[80];
int iFloor;
char chDirection;
Console.SetCursorPosition(1, 22); // ??? ??????
Console.Write("????i?? \"!\" ??? ?????? ?i??? ??? ENTER ??? ???????????: ");
string en = Console.ReadLine(); //?????????? ?????
if (en != "!") return; //???? ?? ! ?? ???? ????????????
Console.SetCursorPosition(1, 22);
Building.clear_line();
Console.SetCursorPosition(1, 22);
Console.Write("?? ????? ?? ????i? ");
iFloor = int.Parse(Console.ReadLine()); //// ???????? ??????
Console.Write("? ????? ??????????i ?????? ????????(u/d): ");
string dir = Console.ReadLine(); // ???????? ???????????
chDirection = dir[0]; // ???????? ??????
if (chDirection == 'u') floor_request[(int)Direction.UP, iFloor - 1] = true; // ????? ?????
if (chDirection == 'd') floor_request[(int)Direction.DOWN, iFloor - 1] = true; // ????? ????
Console.SetCursorPosition(1, 22); // ?????? ?????? ?????
Building.clear_line();
Console.SetCursorPosition(1, 23);
Building.clear_line();
Console.SetCursorPosition(1, 24);
Building.clear_line();
}
}
}// ?????????? ????? Building
namespace Elevator // ?????????? ????? Elevator
{
public class Elevator
{
private Building ptrBuilding;
private int car_number;
private int current_floor;
private int old_floor;
private Direction current_dir;
private bool[] destination;
private int loading_timer;
private int unloading_timer;
public Elevator(Building ptrB, int nc) // ???????????
{
ptrBuilding = ptrB;
car_number = nc;
current_floor = 0; // ?????? ? 0
old_floor = 0; // ?????'????? ?????? ??????
destination = new bool[20];
current_dir = Direction.STOP; // ???????? ?????? ?? ?????
for (int j = 0; j < 20; j++)
{
destination[j] = false; // ???????? ?? ?? ????????? ??????
}
loading_timer = 0; // ?? ?? ???????? ???????
unloading_timer = 0; // ?? ?? ???????? ???????
}
public void car_tick1() // ???????? 1 ??? ??????? ?????
{
car_display(); // ???????
dests_display(); // ??????? ???????
if (loading_timer > 0) --loading_timer; //??????? ???? ???????
if (unloading_timer > 0) --unloading_timer; // ??????? ???? ???????
decide(); // ????????? ???????
}
private void decide() // ????????? ???????
{
int j;
//????? ????????? ?????? ?? ????? ??????? ?? ??????????
bool destins_above, destins_below; //??????? ??????
bool requests_above = false, requests_below = false; // ??????
int nearlest_higher_req = 0;
int nearlest_lower_req = 0;
bool car_between_up, car_between_dn;
bool car_opposite_up, car_opposite_dn;
int ofloor; // ??????
Direction odir; // ???????????
// ?????????? ?? ?? ?? ???? ?????? ?? ??????
if ((current_floor == 20 - 1 && current_dir == Direction.UP) ||
(current_floor == 0 && current_dir == Direction.DOWN)) current_dir = Direction.STOP;
//???? ???? ?????????? - ???, ?????? ???????
if (destination[current_floor] == true)
{
destination[current_floor] = false; // ??????? ?? ????????
if (unloading_timer == 0) // ???????
unloading_timer = 3;
return;
}
// ???????? ??????? ?? ????
if (ptrBuilding.get_floor_req(Direction.UP, current_floor) && current_dir != Direction.DOWN)
{
current_dir = Direction.UP; // ???? ???? ???????
ptrBuilding.set_floor_req(current_dir, current_floor, false);
if (loading_timer == 0) // ???????
loading_timer = 3;
return;
}
// ???????? ?????????
if (ptrBuilding.get_floor_req(Direction.DOWN, current_floor) && current_dir != Direction.UP)
{
current_dir = Direction.DOWN; // ???? ???? ???????
// ??????? ?? ? ??????? ???????????
ptrBuilding.set_floor_req(current_dir, current_floor, false);
if (loading_timer == 0) // ???????
loading_timer = 3;
return;
}
// ????????? ????? ????????? ?? ?? (??????? ????????)
destins_above = false;
destins_below = false;
for (j = current_floor + 1; j < 20; j++) // ????????? ??????? ????????
{
if (destination[j])
destins_above = true;
if (ptrBuilding.get_floor_req(Direction.UP, j) || ptrBuilding.get_floor_req(Direction.DOWN, j))
{
requests_above = true;
if (nearlest_higher_req == 0)
nearlest_higher_req = j;
}
}
for (j = current_floor - 1; j >= 0; j--) // ????????? ?????? ????????
{
if (destination[j])
destins_below = true;
if (ptrBuilding.get_floor_req(Direction.UP, j) || ptrBuilding.get_floor_req(Direction.DOWN, j))
{
requests_below = true;
if (nearlest_lower_req == 0)11111111111111111111111111111111111111111111111
nearlest_lower_req = j;
} }
// ???? ????? ??????? - ????????
if (!destins_above && !requests_above && !destins_below && !requests_below)
{ current_dir = Direction.STOP;
return;
}// ???? ? ?????????? - ?????? ??????
if (destins_above && (current_dir == Direction.STOP || current_dir == Direction.UP))
{ current_dir = Direction.UP;
return;
} if (destins_below && (current_dir == Direction.STOP || current_dir == Direction.DOWN))
{
current_dir = Direction.DOWN;
return;
}
// ????????? ???? ?????
car_between_dn = false;
car_between_up = false;
car_opposite_dn = false;
car_opposite_up = false;
for (j = 0; j < 4; j++) // ?????????? ?????? ????
{ if (j != car_number) // ???? ?? ?? ??? ????
{ ofloor = ptrBuilding.get_cars_floor(j); // ?????????? ???? ??????
odir = ptrBuilding.get_cars_dir(j); // ?? ???????????
// ???? ??? ????? ?? ?? ?????
if ((odir == Direction.UP || odir == Direction.STOP) && requests_above)
}
if ((current_dir == Direction.UP || current_dir == Direction.STOP) &&
requests_above && !car_between_up && !car_opposite_dn)
{ current_dir = Direction.UP;
return;
}
if ((current_dir == Direction.DOWN || current_dir == Direction.STOP) &&
requests_below && !car_between_dn && !car_opposite_up)
{ current_dir = Direction.DOWN;
return; // ??????????
}
current_dir = Direction.STOP;
} // ?????? ?????? decide
private void dests_display() // ??????? ??????? ???????
{
for (int j = 0; j < 20; j++)
{
Console.SetCursorPosition(7 - 2 + (car_number + 1) * 7, 20 - j);
if (destination[j])
Console.Write("*"); // ??????? - ?????????? ???????
else
Console.Write(" "); // ??????
} }
private void car_display() // ????? ?????? ?????
{
Console.SetCursorPosition(7 + (car_number + 1) * 7, 20 - old_floor);
Console.Write(" "); // ?????? ?? ?????? ???????
Console.SetCursorPosition(7 - 1 + (car_number + 1) * 7, 20 - current_floor);
switch (loading_timer) // ???????????? ?????
{ case 3:
Console.Write("\x01W W "); //???? ? ?????????? ???????
break; // ???????? ?????
case 2:
Console.Write(" W\x01W " ); // ???????? ? ??????
get_distinations(); ???????? ???????? ??????
break;
case 1:
Console.Write(" WWW "); ??????? ????? ??? ????????
break;
case 0:
Console.Write(" WWW "); ????? ???????
break;
} Console.SetCursorPosition(7 + (car_number + 1) * 7, 20 - current_floor);
switch (unloading_timer) // ???????? ?????
{ case 3:
Console.Write("W\x01W "); // ????? ????????, ???????? ? ??????
break;
case 2:
Console.Write("W W\x01"); // ????? ???????? ???????? ??????
break;
case 1:
Console.Write("WWW "); // ????? ??????? ???????? ?????
break;
case 0:
Console.Write("WWW "); ????? ???????
break;
}
old_floor = current_floor; // ????????
} public void car_tick2() // ???????? 2 ??? ??????? ?????
{ move(); ????? (???? ????????)
} private void move()
{ if (loading_timer > 0 || unloading_timer > 0) // ???? ??????? ?? ???????
return;
if (current_dir == Direction.UP) // ???? ????? - ?????
current_floor++;
else
if (current_dir == Direction.DOWN) // ???? ???? - ????
current_floor--;
} public int get_floor() // ????????? ??????? ???????
{ return current_floor;
} public Direction get_direction() // ????????? ??????? ????????????
{
return current_dir;
}
public void get_distinations() // ???????
{
int dest_floor; // ?????? ??????????
Console.SetCursorPosition(1, 22); // ???????? ??????? ????????
Building.clear_line();
Console.SetCursorPosition(1, 22);
Console.Write("?i?? " + (car_number + 1) + " ????????? ?? ??????i " + (current_floor + 1) +
"\n ????i?? ????? ??????? ??? ??????????(??? 0 ??? ???i?????? ?????)");
for (int j = 1; j < 20; j++)
{
Console.SetCursorPosition(1, 24);
Console.Write("???? ??????????" + j + ": ");
dest_floor = int.Parse(Console.ReadLine());
Console.SetCursorPosition(1, 24);
Building.clear_line(); // ?????? ????? ???????
if (dest_floor == 0) // ???? ????? ?????? ???????
{ //?????? 3 ?????? ???????
Console.SetCursorPosition(1, 22);
Building.clear_line();
Console.SetCursorPosition(1, 23);
Building.clear_line();
Console.SetCursorPosition(1, 24);
Building.clear_line();
return;
} --dest_floor; // ???????? ? 0 ? ?? ? 1
if (dest_floor == current_floor) // ??????? ????? ??????
{
j--; // ?????? ????
continue;
}
if (j == 1 && current_dir == Direction.STOP) // ???????? ?????
current_dir = (dest_floor < current_floor) ? Direction.DOWN : Direction.UP;
destination[dest_floor] = true;
dests_display(); ??????? ??????? ??????????
}
}
}
}
44
Документ
Категория
Без категории
Просмотров
13
Размер файла
35 Кб
Теги
тспп, дод
1/--страниц
Пожаловаться на содержимое документа