الگوریتم آسانسور در ++C
دوشنبه 13 اردیبهشت 1389 5:43 PM
#include <stdio.h>
#include <conio.h>
#include <dos.h>
void module(void);
void stair(void);
void open(int str);
void close(int str);
void run(int to);
void oblong(int length,int width);
void key(void);
void time(int time);
int lsearch(int no);
int from=0,up[]={-1,-1,-1,-1,-1,-1};
void main()
{
stair();
module();
for(;;)
key();
return;
}//*********************END OF MAIN
void stair(void)
{
int i,j;
for(i=1;i<6;i++)
{
gotoxy(10,i*5);
for(j=0;j<15;j++)
printf("%c",219);
}
return;
}//********************END OF STAIR
void module(void)
{
gotoxy(15,21);
oblong(5,4);
return;
}//*******************END OF MODULE
void open(int str)
{
int i;
time(1000);
for(i=0;i<2;i++)
{
gotoxy(17,25-(str*5)-3+i);
printf("%c",0);
}
time(1000);
for(i=0;i<2;i++)
{
gotoxy(16,25-(str*5)-3+i);
printf("%c",0);
gotoxy(18,25-(str*5)-3+i);
printf("%c",0);
}
time(1000);
return;
}//***********************END OF OPEN
void close(int str)
{
int i;
time(1000);
for(i=0;i<2;i++)
{
gotoxy(16,25-(str*5)-3+i);
printf("%c",219);
gotoxy(18,25-(str*5)-3+i);
printf("%c",219);
}
time(1000);
for(i=0;i<2;i++)
{
gotoxy(17,25-(str*5)-3+i);
printf("%c",219);
}
time(1000);
return;
}//*************************END OF CLOSE
void key(void)
{
char key;
int z;
if(kbhit())
{
key=getch();
if(key-48>=0&&key-48<=4&&(lsearch(key-48))==5&&(key-48)!=from)
for(z=0;z<5;z++)
{
if(up[z]==-1)
{
up[z]=key-48;
run(key-48);
break;
}
}
}
return;
}//***********************END OF KEY
void oblong(int length,int width)
{
int i,j;
for(i=0;i<width;i++)
{
for(j=0;j<length;j++)
if(i>0&&i<width-1&&j>0&&j<length-1)
printf("%c",0);
else
printf("%c",219);
gotoxy(wherex()-length,wherey()+1);
}
return;
}//********************END OF OBLONG
void time(int time)
{
char key;
int k,z;
for(k=0;k<(time/10);k++)
{
if(kbhit())
{
key=getch();
if(key-48>=0&&key-48<=4&&(lsearch(key-48))==5)
for(z=0;z<5;z++)
{
if(up[z]==-1)
{
up[z]=key-48;
break;
}
}
}
delay(10);
}
}//*********************END OF TIME
int lsearch(int no)
{
int i;
for(i=0;i<5;i++)
if(up[i]==no)
return i;
return 5;
}//*********************END OF LSEARCH
void run(int to)
{
int i,j=0,k;
if(to>from)
{
close(from);
for(k=to-from;k>0;k--)
{
for(i=25-(from*5)-4;i>25-((from+1)*5)-4;i--)
{
movetext(15,i,19,i+3,15,i-1);
movetext(1,5,5,5,15,i+3);
j++;
if(j%5==0)
movetext(15,25,19,25,15,i+3);
time(500);
}
from++;
if(k==1&&legsrch(from)!=5)
k++;
if(k==1)
{
open(from);
up[lsearch(from)]=-1;
if(lemsrch(from)!=5)
run(up[lemsrch(from)]);
}
else if(up[lsearch(from)]==from)
{
open(from);
close(from);
up[lsearch(from)]=-1;
}
}
}
if(to<from)
{
close(from);
for(k=from-to;k>0;k--)
{
for(i=25-(from*5)-4;i<25-((from-1)*5)-4;i++)
{
movetext(15,i,19,i+3,15,i+1);
movetext(1,5,5,5,15,i);
j++;
if(j%5==0)
movetext(15,25,19,25,15,i);
time(500);
}
from--;
if(k==1&&lemsrch(from)!=5)
k++;
if(k==1)
{
open(from);
up[lsearch(from)]=-1;
if(legsrch(from)!=5)
run(up[legsrch(from)]);
}
else if(up[lsearch(from)]==from)
{
open(from);
close(from);
up[lsearch(from)]=-1;
}
}
}
return;
}//************************END OF RUN
int legsrch(int no)
{
int i;
for(i=0;i<5;i++)
if(up[i]>no)
return i;
return 5;
}//***********************END OF LEGSRCH
int lemsrch(int no)
{
int i;
for(i=0;i<5;i++)
if(up[i]<no&&up[i]>=0)
return i;
return 5;
}//***********************END OF LEGSRCH