⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elevator.cpp

📁 此程序可实现可视界面下的电梯模拟运行
💻 CPP
字号:
// Elevator.cpp
//#include "msoftcon.h"
#include "Elevator.h"     //include class declarations
#include <stdio.h>     //include class declarations
#include <stdlib.h>
#include <conio.h>
#include <dos.h>


// --- Global variable definitions
Building building;


// --- Function definitions
void elevatorInitialization(Elevator* ep)
{
    ep->current_floor = 0;              //start at 0 (user's 1)
    ep->prev_floor = 0;                 //remember previous floor
    ep->current_dir = STOP;             //stationary at start
    for(int j=0; j<NUM_FLOORS; j++)     //occupants have not pushed any buttons yet
	ep->destination[j] = 0;
    ep->loading_timer = 0;              //not loading yet
    ep->unloading_timer = 0;            //not unloading yet
}
//电梯的初始化

void buildingInitialization()
{
    char str_var[BUF_LENGTH]; //string for floor numbers

    init_graphics();                  //initialize graphics
    clear_screen();                   //clear screen
    building.car_one = (struct Elevator*)malloc(sizeof(Elevator));
    elevatorInitialization(building.car_one);

    for(int j=0; j<NUM_FLOORS; j++)   //for each floor
    {
	set_cursor_pos(3, NUM_FLOORS-j);  //put floor number
	itoa(j+1, str_var, 10);           //on screen
	printf("%3s", str_var);

	building.floor_request[UP][j] = 0;  //no floor requests yet
	building.floor_request[DOWN][j] = 0;
    }
}
//大楼的初始化
void clear_botttom_three_line()
{
    set_cursor_pos(1,22); clear_line();
    set_cursor_pos(1,23); clear_line();
    set_cursor_pos(1,24); clear_line();
}
//清楚屏幕
void show_floor_reqs()  //display floor requests
{
    for(int j=0; j<NUM_FLOORS; j++)
    {
	set_cursor_pos(SPACING, NUM_FLOORS-j);
	printf("%c", (building.floor_request[UP][j]==1) ? 0x1E : ' ');

	set_cursor_pos(SPACING + 3, NUM_FLOORS-j);
	printf("%c", (building.floor_request[DOWN][j]==1) ? 0x1F : ' ');
    }
}
//显示楼层的请求
void get_user_floor_reqs() // get requests from riders outside car
{
    char ch = 'x';             //utility char for input
    int iFloor;                //floor from which request made
    char chDirection;          //'u' or 'd' for up or down
    char str_var[BUF_LENGTH]; //string for floor numbers

    set_cursor_pos(1,22);      //bottom of screen
    printf("%s", "Press [Enter] to call an elevator: ");
    while(1){
	if( !kbhit() )  // wait for keypress (must be CR)
	    return;

	ch = getch();
	if(ch=='\x1B')   // if escape key, end program
	    exit(0);

	if(ch==13)      // until it is a return
	    break;
    }

    set_cursor_pos(1,22); clear_line();  //clear old text
    set_cursor_pos(1,22);      //bottom of screen

    printf("Enter the floor you're on: ");
    scanf( "%s", str_var);
    iFloor = atoi(str_var);    //convert to integer
    scanf( "%c", &ch); // discard the following newline character

    printf( "Enter direction you want to go (u or d): ");
    scanf( "%c", &chDirection);
    if(chDirection=='u' || chDirection=='U')
	building.floor_request[UP][iFloor-1] = 1;  //up floor request
    if(chDirection=='d' || chDirection=='D')
	building.floor_request[DOWN][iFloor-1] = 1;  //down floor request

    clear_botttom_three_line();
}

void car_tick1(Elevator* ep) //tick 1 for the car
{
    car_display(ep);                    //display elevator box
    dests_display(ep);                  //display destinations
    if(ep->loading_timer)                 //count down load time
	-- ep->loading_timer;
    if(ep->unloading_timer)               //count down unload time
	-- ep->unloading_timer;
    decide(ep);                         //decide what to do
}

void car_tick2(Elevator* ep) //tick 2 for the car
{
    move(ep); //move car if appropriate
}

void car_display(Elevator* ep)         //display elevator image
{
    set_cursor_pos( 2 * SPACING, NUM_FLOORS-ep->prev_floor);
    printf("    ");  //erase old position
    set_cursor_pos( 2 * SPACING - 1, NUM_FLOORS-ep->current_floor);
    switch(ep->loading_timer)
    {
	case 3:
	    printf("%s", "  ] ");
	    break;
	case 2:
	    printf("%s", "  Ω] ");
	    get_destinations(ep);
	    break;
	case 1:
	    printf("%s", " [ ] ");
	    break;
	case 0:
	    printf("%s", " [ ] ");
	    break;
    }

    set_cursor_pos( 2 * SPACING, NUM_FLOORS-ep->current_floor);
    switch(ep->unloading_timer)
    {
	case 3:
	    printf("%s", " [Ω  ");
	    break;
	case 2:
	    printf("%s", " [   ");
	    break;                      //happy face on right
	case 1:
	    printf("%s", " [ ] ");   //closed door, no
	    break;                      //no happy face
	case 0:
	    if(!ep->loading_timer)
		printf("%s", " [ ] ");
	    break;
    }
    ep->prev_floor = ep->current_floor; //remember old floor
}

void dests_display(Elevator* ep) //display destinations
{
    for(int j=0; j<NUM_FLOORS; j++)
    {
	set_cursor_pos( 2 * SPACING-2, NUM_FLOORS-j);
	if( ep->destination[j] == 1 )
	    printf("→");
	else
	    printf(" ");
    }
}

void decide(Elevator* ep)               //decide what to do
{
    int j;
    //flags indicate if destinations or requests above/below us
    int destins_above, destins_below;    //destinations
    int requests_above, requests_below;  //requests

    //ensure we don't go too high or too low
    if( (ep->current_floor==NUM_FLOORS-1 && ep->current_dir==UP)
	|| (ep->current_floor==0 && ep->current_dir==DOWN) )
	ep->current_dir = STOP;

    //if there's a destination on this floor, unload passengers
    if( ep->destination[ep->current_floor]==1 )
    {
	ep->destination[ep->current_floor] = 0;  //erase destination
	if(!ep->unloading_timer)                //unload
	    ep->unloading_timer = LOAD_TIME;
	return;
    }

    //if there's an UP floor request on this floor,
    //and if we're going up or stopped, load passengers
    if( (building.floor_request[UP][ep->current_floor] && ep->current_dir != DOWN) )
    {
	ep->current_dir = UP;  //(in case it was STOP)
	//remove floor request for direction we're going
	building.floor_request[ep->current_dir][ep->current_floor] = 0;
	if( !ep->loading_timer) //load passengers
	    ep->loading_timer = LOAD_TIME;
	return;
    }

    // if there's a down floor request on this floor,
    // and if we're going down or stopped, load passengers
    if((building.floor_request[DOWN][ep->current_floor] && ep->current_dir != UP) )
    {
	ep->current_dir = DOWN;  //(in case it was STOP)
	//remove floor request for direction we're going
	building.floor_request[ep->current_dir][ep->current_floor] = 0;
	if( !ep->loading_timer )    //load passengers
	    ep->loading_timer = LOAD_TIME;
	return;
    }

    //check if there are other destinations or requests
    destins_above = destins_below = 0;
    requests_above = requests_below = 0;
    for(j=ep->current_floor+1; j<NUM_FLOORS; j++)
    {                                   //check floors above
	if( ep->destination[j] )                //if destinations
	    destins_above = 1;            //set flag
	if(building.floor_request[UP][j] || building.floor_request[DOWN][j] )
	    requests_above = 1;  //if requests
    }
    for(j=ep->current_floor-1; j>=0; j--)      //check floors below
    {
	if(ep->destination[j] )                 //if destinations
	    destins_below = 1;            //set flag
	if( building.floor_request[UP][j] || building.floor_request[DOWN][j] )
	    requests_below = 1;           // if requests, set flag
    }

    //if no requests or destinations above or below, stop
    if( !destins_above && !requests_above && !destins_below && !requests_below)
    {
	ep->current_dir = STOP;
	return;
    }

    // if destinations and we're stopped, or already going the right way,
    // go toward destinations
    if( destins_above && (ep->current_dir==STOP || ep->current_dir==UP) )
    {
	ep->current_dir = UP;
	return;
    }
    if( destins_below && (ep->current_dir==STOP || ep->current_dir==DOWN) )
    {
	ep->current_dir = DOWN;
	return;
    }

    //if we're going up or stopped, and there is an FR above us, then go up
    if( (ep->current_dir==UP || ep->current_dir==STOP) && requests_above )
    {
	ep->current_dir = UP;
	return;
    }

    //if we're going down or stopped, and there is an FR below, then go down
    if( (ep->current_dir==DOWN || ep->current_dir==STOP) && requests_below )
    {
	ep->current_dir = DOWN;
	return;
    }

    //if nothing else happening, stop
    ep->current_dir = STOP;
}


void move(Elevator* ep)
{
   if(ep->loading_timer || ep->unloading_timer)  ////if loading or unloading, don't move
      return;
   if(ep->current_dir==UP)        //if going up, go up
      ep->current_floor++;
   else if(ep->current_dir==DOWN)   //if going down, go down
      ep->current_floor--;
}

void get_destinations(Elevator* ep)          //stop, get destinations
{
    int     dest_floor;          //destination floor
    char    ch;
    char    str_var[BUF_LENGTH]; //string for floor numbers

    set_cursor_pos(1,22); clear_line();  //clear top line
    set_cursor_pos(1, 22);
    printf(" Car has stopped at floor %d", ep->current_floor+1);
    printf("\nEnter destination floors (0 when finished)");
    for(int j=1; j<NUM_FLOORS; j++)    //get floor requests
    {                               //maximum; usually fewer
	set_cursor_pos(1, 24);
	printf("Destination %d :", j);
	scanf( "%s", str_var);
	dest_floor = atoi(str_var);    //convert to integer
	scanf( "%c", &ch); // discard newline character
	set_cursor_pos(1,24); clear_line(); //clear old input line

	if(dest_floor==0) //if no more requests
	{
	    clear_botttom_three_line();
	    return;
	}
	--dest_floor;                   //start at 0, not 1
	if(dest_floor==ep->current_floor)   //chose this very floor
	    continue;        // so forget it

	//if we're stopped, first choice made sets direction
	if(ep->current_dir==STOP)
	    ep->current_dir = (dest_floor < ep->current_floor) ? DOWN : UP;
	ep->destination[dest_floor] = 1; //record selection
	dests_display(ep);                //display destinations
    }
}

int main()
{
    buildingInitialization();

    while(1)
    {
	show_floor_reqs();              //display floor requests
	car_tick1(building.car_one);    //send it time tick 1
	car_tick2(building.car_one);    //send it time tick 2

	delay(1000);
	get_user_floor_reqs(); //get floor requests from user
    }
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -