Pair programming-Elevator scheduling 2

Source: Internet
Author: User

This time the teacher is trooped programming homework, so that I can learn about other people's programming habits and methods, so as to recognize their shortcomings, learn more useful things.

First, the topic requirements

Existing new office building, a total of 21 floors, a total of four elevators, all elevators basic parameters are shown in the following table:

Elevator number

Service Floors available

Maximum number of passengers

Maximum Load weight

1

All floors

10

Approx.

2

Single layer

10

Approx.

3

Double

20

Approx.

4

All floors

20

Kg

The terms of use are as follows:

1, the floor number is 0~20, of which No. 0 is the basement layer;

2. Elevators with floor restrictions are not docked in the response floor, such as a single double layer;

3, all elevators with unified button control

Please design and implement an elevator control program according to the above requirements, if there is a graphical display is better.

Second, demand analysis

1, a total of 4 elevators, a single double-decker docking and all floors are docked.

2. Select the nearest elevator by controlling some variables

3. Always enter the request elevator

Third, the function realization

1, elevator data structure:

typedef struct ELEVATOR{INT staus;//three states: stationary, upstream, down int type;//three types: Double, single, full int floor;//The number of floors to stop at this time int beixuyao;//whether this floor is called int flag;//Whether someone use elevator}b;

2. Initialization function:

A[0].staus = 0;
A[1].staus = 0;a[2].staus = 0;a[3].staus = 0;a[0].floor = -1;a[1].floor = 7;//Indicates the number of floors at which elevators are parked is 7 floor A[2].floor = 14;a[3].floor = 20;a[0].type = 2;//Elevator All floors are docked a[1].type = 1;//Second Elevator number floor are docked a[2].type = 0;//Third Elevator Double-level floors are docked a[3].type = 2;//Fourth elevators all floors are docked

4. Check the elevator status and calculate the number of floors that differ from the current user's elevator:

int c;if (A[x].staus = = 0)//At this time a[x] The elevator is in a stationary state {if (Y < Z) {c = z-y;} Else{c = Y-z;}} if (A[x].staus = = 1)//At this time a[x] elevator in the rising state {if (y > Z&&dir = = 1) {c = y-z;} else{printf ("This elevator cannot respond to user requests");}} if (A[x].staus = = 2)//At this time a[x] elevator in descending state {if (Y < z &&dir = = 0) {c = z-y;} else{printf ("This elevator cannot respond to user requests");}}

5. Judging the nearest elevator:

if (Flag1 = = 1) {if (K[j] < K[j + 1]) {printf ("closest to the user and eligible elevators are:%d\n", M[j]); a[m[j]].floor = Gofloor;} else{printf ("The nearest and eligible elevator for the user is:%d\n", M[j + 1]); A[m[j + 1]].floor = Gofloor;}} else if (Flag1 = = 2) {if (K[0] < k[1]) {if (K[0] < k[3]) {printf ("the nearest and qualified elevator for the user is: 0\n"); a[0].floor = Gofloor;        } else{   printf ("The nearest and eligible elevator to the user is: 3\n");   A[3].floor = Gofloor;} Else{if (K[1] < k[3]) {printf ("the most recent and qualified elevator to the User: 1\n"); a[1].floor = Gofloor;} else{printf ("The nearest and eligible elevator for the user is: 3\n"); a[3].floor = Gofloor;}}}

6. User input:

printf ("----------------------------------------------\ n");p rintf ("To use the elevator press button 1! \ n "); scanf ("%d ", &flag);//When someone presses the elevator if (flag = = 1) {printf (" * * * * * * * * * * * * * * * * * * \ n ");} printf ("User's Floor:"); scanf ("%d", &floor);//The number of floors of people who press elevators at this time printf ("Go upstairs please press 1, go downstairs please press 0:"); scanf ("%d", &dir);// The user request is to go upstairs or downstairs to mark printf ("Please select the floor to go:"), scanf ("%d", &gofloor);//The floor the user is going to

7. The user enters the elevator:

if (flag = = 1)//The user presses the elevator {if (dir = = 1)//user wants to go upstairs {if (floor% 2 = = 0)//user is on even floors {if (gofloor% 2 = = 1)//users are going to odd floors

8. Elevator status update:

K[FLAG1] = lookfor (i, Floor, a[i].floor); M[flag1] = I;flag1++;a[i].beixuyao = 0;printf ("Closest to the user and eligible elevators are:%d\n", M[j]); a[ M[j]].floor = Gofloor;

9. Display function:

else if (flag = = 0) {printf ("No elevator at this time");} printf ("       ----       ----       ----       ----\ n");p rintf (      "    |     |    | | | | | | | \ n ");p rintf ("%d%d%d%d  \ n ", A[0].floor, A[1].floor, A[2].floor, A[3].floor);p rintf ("      |    |     |    |     |    |     |    | \ n ");p rintf ("       ----       ----       ----       ----\ n ");p rintf (" \ n ");p rintf ("------------------------------- ---------------\ n ");}

10. Main function:

void Main () {while        (1) {           ...         }}

Four, commissioning analysis

1. Start the execution status:

2. User input:

3. The result of the elevator display:

Five. Summary

1. The whole elevator dispatching procedure: through the user's input to control the elevator, is the user to go upstairs to divide into: the user in the odd floor to even floor; the user to go downstairs is divided into: users in the even floor to the odd floor. In satisfying one of the conditions of the elevator to find the conditions, then the state of the elevator is divided into: static, Rise, fall, in these three states to find eligible elevators and calculated with the user's current elevator floor number of the difference, and then return to the main function to calculate which difference is the smallest elevator, then start the elevator, It will eventually be parked on the floor of the user's request. This program does not take the lift load weight into account, I and my teammates will continue to discuss, continue to update ing ...

2. The evaluation of teammates: The teacher has just finished decorating this topic, I really big head, to control the condition is really too much, for the weak programming ability of me, the task is really very arduous!! In my first conversation with the team Zhang Shurie How to start this programming, he gave me a lot of inspiration, that day there are many other students together to help us think of the general structure of the whole program, and eventually, finally clear ideas, although the final programming is cumbersome, not concise, But it's such a difficult program that I can do my own part of my own, thank my teammates, thank you for helping our classmates.

Teammate: Zhang Shurie blog Address: http://www.cnblogs.com/bjzsr/p/5370268.html

  

Pair programming-Elevator scheduling 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.