rendered paste body#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
struct TChel{
int floor;
int color;
};
struct TElevator{
int floor;
int count;
TChel C[6];
};
TChel queue[5][16];
int count[5];
TElevator lift;
char *string;
int moves, floor, moved, empty, destination, napr = 0, k = 3;
void chelovek(int x, int y, int color, int floor, int mode)
{
itoa(floor, string, 10);
setcolor(color);
circle(x+10,y+10,10);
line(x+10,y+20,x+10,y+30);
line(x+10,y+30,x,y+40);
line(x+10,y+30,x+20,y+40);
line(x,y+25,x+20,y+25);
putpixel(x+5,y+8,color);
putpixel(x+15,y+8,color);
line(x+5,y+15,x+15,y+15);
if (mode == 1)
outtextxy(x+3,y+45,'<-'+string); else
if (mode == 2) outtextxy(x+3,y+45,"'"); else
outtextxy(x+8,y+45,string);
}
void frame(int x1, int y1, int x2, int y2, int color)
{
setcolor(color);
line(x1,y1,x2,y1);
line(x2,y1,x2,y2);
line(x1,y1,x1,y2);
line(x1,y2,x2,y2);
if (empty == 1)
{
int poly[8];
poly[0] = x1;
poly[1] = y1;
poly[2] = x2;
poly[3] = y1;
poly[4] = x2;
poly[5] = y2;
poly[6] = x1;
poly[7] = y2;
setfillstyle(SOLID_FILL, 0);
fillpoly(4,poly);
}
}
void draw_lift()
{
int poly[8];
poly[0] = 3;
poly[1] = 3;
poly[2] = 198;
poly[3] = 3;
poly[4] = 198;
poly[5] = 478;
poly[6] = 3;
poly[7] = 478;
setcolor(0);
setfillstyle(SOLID_FILL, 0);
fillpoly(4,poly);
int i,j;
i = (5-(lift.floor))*96+5;
setcolor(10);
frame(5,i,195,i+86,10);
if (lift.count > 0) for (j = 0; j < lift.count; j++)
{
chelovek(14+j*30, i+15, lift.C[j].color, lift.C[j].floor, 0);
}
empty = 0;
}
void move_elevator(int floor)
{
int i, j;
frame(5,(5-(lift.floor))*96+5,195,(5-(lift.floor))*96+5+86,0);
if (floor > lift.floor)
{
for (i= (5-(lift.floor))*96+5; i >= (5-(floor))*96+5; i--)
{
setcolor(10);
frame(5,i,195,i+86,10);
if (lift.count > 0) for (j = 0; j < lift.count; j++)
{
chelovek(14+j*30, i+15, lift.C[j].color, lift.C[j].floor, 0);
}
delay(10);
frame(5,i,195,i+86,0);
if (lift.count > 0) for (j = 0; j < lift.count; j++)
{
chelovek(14+j*30, i+15, 0, lift.C[j].floor, 0);
}
}
} else
{
for (i= (5-(lift.floor))*96+5; i <= (5-(floor))*96+5; i++)
{
setcolor(10);
frame(5,i,195,i+86,10);
if (lift.count > 0) for (j = 0; j < lift.count; j++)
{
chelovek(14+j*30, i+15, lift.C[j].color, lift.C[j].floor, 0);
}
delay(5);
frame(5,i,195,i+86,0);
if (lift.count > 0) for (j = 0; j < lift.count; j++)
{
chelovek(14+j*30, i+15, 0, lift.C[j].floor, 0);
}
}
}
frame(5,i,195,i+86,10);
if (lift.count > 0) { for (j = 0; j < lift.count; j++)
{
chelovek(14+j*30, i+15, lift.C[j].color, lift.C[j].floor, 0);
} }
lift.floor = floor;
}
void del_queue(int floor, int index)
{
if (index < 9)
{
for (int i = index; i <= count[floor]; i++)
{
queue[floor][i] = queue[floor][i+1];
}
}
count[floor]--;
}
void del_liftq(int index)
{
if (index < 5)
{
for (int i = index; i <= 6; i++)
{
lift.C[i] = lift.C[i+1];
}
}
lift.count--;
}
void draw_people(int floor)
{
int i;
for (i = 0; i < 16; i++)
{
chelovek(220+25*i,floor*96+20, 0, 0, 2);
}
for (i = 0; i < count[floor]+1; i++)
{
chelovek(220+25*i,floor*96+20,queue[floor][i].color, queue[floor][i].floor,0);
}
}
void add_queue(int floor, int index)
{
lift.C[lift.count].floor = queue[floor][index].floor;
lift.C[lift.count].color = queue[floor][index].color;
del_queue(floor,index);
lift.count++;
}
void main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
outtextxy(0,20,"COURSE WORK 'ELEVATOR'");
outtextxy(0,40,"AUTHOR:");
outtextxy(0,50,"ALEXANDER TIMONOV");
outtextxy(0,60,"07POVT-2");
outtextxy(0,80,"2007-2008, SECOND TERM");
outtextxy(0,90,"ORENBURG STATE UNIVERSITY");
outtextxy(0,110,"PRESS ANY KEY...");
getch();
cleardevice();
setcolor(2);
line(200,96,639,96);
line(200,2*96,639,2*96);
line(200,3*96,639,3*96);
line(200,4*96,639,4*96);
line(200,97,639,97);
line(200,2*96+1,639,2*96+1);
line(200,3*96+1,639,3*96+1);
line(200,4*96+1,639,4*96+1);
frame(0,0,639,479,WHITE);
frame(0,0,200,479,WHITE);
randomize();
lift.floor = 5;
lift.count = 0;
draw_lift();
int z = 1;
floor = 0;
moves = 0;
moved = 0;
destination = 1;
queue[4][0].color = random(15)+1;
queue[4][0].floor = random(5)+1;
while (queue[4][0].floor == 1)
queue[4][0].floor = random(5)+1;
queue[4][1].color = random(15)+1;
queue[4][1].floor = random(5)+1;
while (queue[4][1].floor == 1)
queue[4][1].floor = random(5)+1;
queue[4][2].floor = random(5)+1;
while (queue[4][2].floor == 1)
queue[4][2].floor = random(5)+1;
count[4] = 2;
while (z == 1)
{
for (int i=0; i < lift.count; i++)
{
if (lift.C[i].floor == lift.floor)
{
del_liftq(i);
draw_lift();
delay(300);
i--;
}
}
if (lift.count == 0)
destination = 1;
for (i=0; i < 5; i++)
{
if (count[i] < 15)
{
queue[i][count[i]].color = random(15)+1;
if (i == 4)
{
queue[i][count[i]].floor = random(5)+1;
while (queue[i][count[i]].floor == 1)
queue[i][count[i]].floor = random(5)+1;
} else {
queue[i][count[i]].floor = 1; }
draw_people(i);
count[i]++;
}
for (int j = 0; j < 15; j++)
{
if ((destination == 1)&&(lift.floor > 1)&&(lift.floor == 5-i)&& (queue[i][0].floor == 1) && (lift.count < 6))
{
add_queue(i,j);
draw_people(i);
draw_lift();
delay(1000);
j--;
}
}
for (j = 0; j < count[4]; j++)
{
if ((lift.floor == 1)&&(lift.count < 6))
{
add_queue(4,j);
draw_people(4);
draw_lift();
delay(1000);
j--;
}
}
draw_lift();
}
{
if ((lift.C[0].floor == 1)&&(moves == 0))
{
destination = 1;
while (lift.floor != 1)
{
move_elevator(lift.floor-1);
for (int i=0; i < lift.count; i++)
{
if (lift.C[i].floor == lift.floor)
{
del_liftq(i);
draw_lift();
delay(300);
i--;
}
}
for (int j = 0; j < count[5-lift.floor]; j++)
{
if (lift.count < 6)
{
add_queue(5-lift.floor,j);
draw_people(5-lift.floor);
draw_lift();
delay(1000);
j--;
}
}
}
} else
{
if ((lift.C[0].floor > 1)&&(moves == 0))
{
int mf = 0;
for (int q = 0; q < 6; q++)
{
if (lift.C[q].floor > mf)
mf = lift.C[q].floor;
}
destination = mf;
while (lift.floor != destination)
{
move_elevator(lift.floor+1);
for (int i=0; i < lift.count; i++)
{
if (lift.C[i].floor == lift.floor)
{
del_liftq(i);
draw_lift();
delay(300);
i--;
}
}
}
destination = 1;
}
}
}
};
/* clean up */
getch();
closegraph();
}