#include <iostream> 
#include <fstream>
#include <cstdlib>     // to clear the screen 
#include <cmath>
#include <string>
using namespace std; 

#ifndef NULL
#define NULL 0
#endif

int MAX_FLOOR=10;

struct passenger
{
	int id,waitingtime,timestamp;
	float source,destination;
    passenger * next;	

};

struct node   /*this is the linked list that contains source and destination of passengers 
in the elevator*/
{
	int data;
	node *next;
	char kind;
	int id;
	int servicetime;
	int arrivaltime;
};
struct elevator
{
	float current;
	string elevstat;
	int load;
	node * elev;// pointer to head of linked list of elevator.
};


  

// functions declarations
void insertpdata(int id,int timestamp,float source, float destination, passenger *& head);

void insert_up(int timestamp ,int id ,float source , float destination,node *&head);

void insert_down(int timestamp ,int id ,float source , float destination,node *&head);

int programInterface();

int countpeople(passenger *&head);
int countserved(node *&head);

void removepassenger(passenger *&head);

bool Valid_requests (int simulation_time,passenger *&head1,passenger *&head2);

bool loadCheck (elevator &E);

void elevator_status(elevator &E,node *&Ehead);

string passenger_direction(passenger *&head);

bool DirectionCheck (elevator &E, passenger *& head);

int DistanceCheck (elevator &E1, elevator &E2, passenger *& head );

void movelevator (elevator &E ,node * head);

void changeload(elevator &E,node *head);

void movenode(node *&head1 , node *&served, int simulationtime);
//**********************************************************************************
void printAll(node*head);

//**********************************************************************************

int main() 
{
  int interface1=programInterface();
  int simulationtime=22;
	
 fstream input("c:\\input.txt",ios::in); //reading from input file
  passenger p;
  passenger *phead=NULL; 
  passenger *Valid=NULL;
  node *Served=NULL;

node *head1 = NULL;  //represents the begining of the linked list of elevator 1


node *head2 = NULL;  //represents the begining of the linked list of elevator 2


  elevator E1,E2;
  E1.current=0;
  E2.current=0;
  E1.load=0;
  E2.load=0;
  E1.elevstat="IDLE";
  E2.elevstat="IDLE";
	while ((input>>p.id)&& (p.id!=-1))
	  {
	   input>>p.timestamp;
       input>>p.source;
       input>>p.destination;
      insertpdata( p.id, p.timestamp, p.source,  p.destination,  phead);
	}
			input.close();
    
  int countarrived= countpeople(phead);  // how many passengers will come during simulation
     while (Valid_requests (simulationtime,phead,Valid) || (countarrived!=countserved(Served)))
   {      passenger *p=Valid;
	  while (p!=NULL)
	  {
        elevator_status(E1,head1);
        elevator_status(E2,head2);
if (((E1.elevstat=="IDLE")&&(E2.elevstat=="IDLE")) && ((E1.current==0)&&(E2.current==0)) )
{
	if (DirectionCheck (E1,p)&&(loadCheck (E1)))
	  {   
			 if (head1!=NULL )
			 {
				 if ((passenger_direction(p))=="MOVUP")

				  insert_up (p->timestamp,p->id,p->source,p->destination,head1);
				 else 
			
					insert_down (p->timestamp,p->id,p->source,p->destination,head1);
			 }
			 else if (p->source == E1.current)
			 {
			   E1.elevstat=passenger_direction(p);

				 if ((passenger_direction(p))=="MOVUP")

				  insert_up (p->timestamp,p->id,p->source,p->destination,head1);
				 else 
			
					insert_down (p->timestamp,p->id,p->source,p->destination,head1);
			 
			 }
		 }


}
else
{
        	if (DirectionCheck (E1,p) &&  DirectionCheck (E2,p))
	  {      
	     if  ((DistanceCheck (E1,E2,p)==1) && (loadCheck (E1)))
		 {   
			 if (head1!=NULL )
			 {
				 if ((passenger_direction(p))=="MOVUP")

				  insert_up (p->timestamp,p->id,p->source,p->destination,head1);
				 else 
			
					insert_down (p->timestamp,p->id,p->source,p->destination,head1);
			 }
			 else if (p->source == E1.current)
			 {
			   E1.elevstat=passenger_direction(p);

				 if ((passenger_direction(p))=="MOVUP")

				  insert_up (p->timestamp,p->id,p->source,p->destination,head1);
				 else 
			
					insert_down (p->timestamp,p->id,p->source,p->destination,head1);
			 
			 }
		 }
				 else if (loadCheck (E2))
				 {
		     	 if (head2!=NULL )
			 {
				 if ((passenger_direction(p))=="MOVUP")

				  insert_up (p->timestamp,p->id,p->source,p->destination,head2);
				 else 
			
					insert_down (p->timestamp,p->id,p->source,p->destination,head2);
			 }
			 else if (p->source == E2.current)
			 {
			   E1.elevstat=passenger_direction(p);

				 if ((passenger_direction(p))=="MOVUP")

				  insert_up (p->timestamp,p->id,p->source,p->destination,head2);
				 else 
			
					insert_down (p->timestamp,p->id,p->source,p->destination,head2);
			 
			 }
				 }
            
		}
	  else if (DirectionCheck (E1,p)&&(loadCheck (E1)))
	  {   
			 if (head1!=NULL )
			 {
				 if ((passenger_direction(p))=="MOVUP")

				  insert_up (p->timestamp,p->id,p->source,p->destination,head1);
				 else 
			
					insert_down (p->timestamp,p->id,p->source,p->destination,head1);
			 }
			 else if (p->source == E1.current)
			 {
			   E1.elevstat=passenger_direction(p);

				 if ((passenger_direction(p))=="MOVUP")

				  insert_up (p->timestamp,p->id,p->source,p->destination,head1);
				 else 
			
					insert_down (p->timestamp,p->id,p->source,p->destination,head1);
			 
			 }
		 }
	  else if (DirectionCheck (E2,p)&&(loadCheck (E2)))
	  {
		     	 if (head2!=NULL )
			 {
				 if ((passenger_direction(p))=="MOVUP")

				  insert_up (p->timestamp,p->id,p->source,p->destination,head2);
				 else 
			
					insert_down (p->timestamp,p->id,p->source,p->destination,head2);
			 }
			 else if (p->source == E2.current)
			 {
			   E1.elevstat=passenger_direction(p);

				 if ((passenger_direction(p))=="MOVUP")

				  insert_up (p->timestamp,p->id,p->source,p->destination,head2);
				 else 
			
					insert_down (p->timestamp,p->id,p->source,p->destination,head2);
			 
			 }
				 }
       else ; //do nothing 
	  }

	   p=p->next;
	  }
 movelevator(E1,head1);

 movelevator(E2,head2);
if(head1!=NULL )
{
 if( (E1.current ==head1->data) && (loadCheck (E1)) && DirectionCheck (E1,p) )
{
	changeload(E1,head1);
    movenode(head1,Served,simulationtime);
}
}
if(head2!=NULL )
{ 
if( E2.current ==head2->data && (loadCheck (E2)) && DirectionCheck (E2,p))
{
	changeload(E2,head2);
    movenode(head2,Served,simulationtime);
}

}

 /*if ((E1.elevstat =="STOPUP") || (E1.elevstat =="STOPDWN"))

     changeload(E1,head1);
  if ((E2.elevstat =="STOPUP") || (E2.elevstat =="STOPDWN"))
     changeload(E2,head2);
     if(interface1==1)
		{
			 upDateScreen();
		}
	  
*/
  	 printAll(head1);
	   simulationtime++;
   }

 return 0;
}

 //******************************************************************************************

void insert_up(int timestamp ,int id ,float source , float destination,node *&head)
{
node *newnode_source =new node; //insert node contain  source of passenger
node *newnode_dest =new node;   //insert node contain  destination of passenger
node*ptr= head;    // node pointer to search for the  palace of el node i will insert
node *pre = new node;
pre->next=ptr;   // follow the ptr pointer but lags it by one
if(head==NULL)     // insert request in the linked list when it was empty
	{
	    head=newnode_source;
 		newnode_source->data =source;
		newnode_source->arrivaltime = timestamp;
	    newnode_source->next=newnode_dest;
		newnode_dest->data=destination;
		newnode_source->kind='s';
		newnode_source->id=id;
		newnode_dest->kind='d' ;
		newnode_dest ->id = id ;
		newnode_dest->next=NULL;
	}

else

{
	if(ptr->data > source)
	{
	  newnode_source ->data = source ;
	  newnode_source ->kind = 's';
	  newnode_source ->id=id;
	  newnode_source ->next = head;
	  head = newnode_source ;


	}
else
	{

		while(ptr->next !=NULL && ptr->data <= source )
		{
			ptr=ptr->next;
			pre=pre->next;
		}
if (ptr->next ==NULL && ptr->data <= source )

       {    ptr->next=newnode_source;
			newnode_source->data = source;
			newnode_source->kind ='s';
			newnode_source ->id=id;
			newnode_source->next = NULL;
		}
else
{
		pre->next=newnode_source;
		newnode_source->data = source;
		pre=pre->next;
		newnode_source->kind='s';
		newnode_source ->id= id;
		newnode_source->next = ptr;
}		
		
		
	}
while( ptr->next !=NULL && destination >= ptr->data )
		{
			ptr=ptr->next;
			pre=pre->next;
		}
if (ptr->next ==NULL && destination >= ptr->data)
		{
			ptr->next=newnode_dest;
			newnode_dest->data = destination;
			newnode_dest->kind ='d';
			newnode_dest->id= id;
			newnode_dest->next = NULL;
		}
else
		{
			pre->next=newnode_dest;
			newnode_dest->data = destination;
			newnode_dest->kind ='d';
			newnode_dest->id=id;
			newnode_dest->next = ptr;
		}
	}

}
 //**********************************************************************************************
void insert_down(int timestamp ,int id ,float source , float destination,node *&head)
{
node *newnode_source =new node; //insert node contain  source of passenger
node *newnode_dest =new node;   //insert node contain  destination of passenger
node*ptr= head;    // node pointer to search for the  palace of el node i will insert
node *pre = new node;
pre->next=ptr;   // follow the ptr pointer but lags it by one
if(head==NULL)     // insert request in the linked list when it was empty
	{
	    head=newnode_source;
 		newnode_source->data =source;	
	    newnode_source->next=newnode_dest;
		newnode_dest->data=destination;
		newnode_source->kind='s';
		newnode_source->id=id;
		newnode_dest->kind='d' ;
		newnode_dest ->id = id ;
		newnode_dest->next=NULL;
	}


else

{
	if(ptr->data < source)
	{
	  newnode_source ->data = source ;
	  newnode_source ->kind = 's';
	  newnode_source ->id = id;
	  newnode_source ->next = head;
	  head = newnode_source ;


	}

else
	{

		while(ptr->next !=NULL && ptr->data >= source )
		{
			ptr=ptr->next;
			pre=pre->next;
		}
if (ptr->next ==NULL && ptr->data >= source)
{
            ptr->next=newnode_source;
			newnode_source->data = source;
			newnode_source->kind ='s';
			newnode_source ->id= id;
			newnode_source->next = NULL;
}
else 
{
	    pre->next=newnode_source;
		newnode_source->data = source;
		pre=pre->next;
		newnode_source->kind='s';
		newnode_source ->id = id;
		newnode_source->next = ptr;
	
}
}
while( ptr->next !=NULL && destination <= ptr->data )
		{
			ptr=ptr->next;
			pre=pre->next;
		}
if (ptr->next ==NULL && destination <= ptr->data)
		{
			ptr->next=newnode_dest;
			newnode_dest->data = destination;
			newnode_dest->kind ='d';
			newnode_dest->id =id ;
			newnode_dest->next = NULL;
		}
else
		{
			pre->next=newnode_dest;
			newnode_dest->data = destination;
			newnode_dest->kind ='d';
			newnode_dest->id = id ;
			newnode_dest->next = ptr;
		}
	}

}
//*******************************************************************************************

  int programInterface()
{
	cout<<"Press 1 to run interactive mode ,press any key to run silent mode\n";
	int n;
	cin>>n;
	return n;
}

//********************************************************************************************

  int countpeople(passenger *&head) //##phead mara w el served mara
{
	passenger *p=head;
   int count=0;
   while (p!=NULL)
	  {
	   count=count+1;
       p=p->next;
   }
   return count;
}
//********************************************************************************************

  int countserved(node *&head) //##phead mara w el served mara
{
	node *p=head;
   int count=0;
   while (p!=NULL)
	  {
	   count=count+1;
       p=p->next;
   }
   return count;
}
//********************************************************************************************
 bool Valid_requests (int simulation_time,passenger *&head1,passenger *&head2)// wa7da valid w wa7da phead
 {
	 passenger *p=head1;
  if (p!=NULL)
  {
	  while ((p != NULL) && (p->timestamp <= simulation_time) )
	  {  
          
		  insertpdata(p->id,p->timestamp,p->source,p->destination,head2);

		  passenger*q=p->next;
		 removepassenger(head1);
		  p = q;
	  }
		
	  if (head2!=NULL)
	  return true;
  else
	  return false;
  }
  else
	  return false;
 }
 //********************************************************************************************

 void elevator_status(elevator &E,node *&Ehead) // node of source and destination linked list
 {
     if (Ehead!=NULL)
	    {
		 if(E.current < Ehead->data)
			 E.elevstat="MOVUP";
		 else if (E.current > Ehead->data)
			 E.elevstat="MOVDWN";
		 else if ((Ehead->next->data<1) || (Ehead->next->data> MAX_FLOOR))
			 ;//do nothing
         else if ((E.current==Ehead->data) && (Ehead->data < Ehead->next->data))
	         E.elevstat="STOPUP";
		 else if ((E.current==Ehead->data) && (Ehead->data > Ehead->next->data))
             E.elevstat="STOPDWN";
		 /*else if (E.load==0)
	         E.elevstat="IDLE";
	    */
		  }
	/*else 
	      E.elevstat="IDLE";
	 */
}
 //********************************************************************************************

 /*this function insert the passengers data in a linked list 
the insertion is done after the last entered node
*/

void insertpdata(int id,int timestamp,float source, float destination, passenger *& head)
{   
	passenger *newpassenger = new passenger;
    if (head==NULL)   // assuming that the list is empty
	{
		head=newpassenger;
	               newpassenger->id=id;
                   newpassenger->timestamp=timestamp;
                   newpassenger->source=source;
                   newpassenger->destination=destination;
	               newpassenger->next=NULL;
	}
	else
	{  
	    passenger *ptr=head;   // when the list has nodes in it
		while (ptr->next!=NULL)
		{
			ptr=ptr->next;
		}
		ptr->next=newpassenger;
	               newpassenger->id=id;
                   newpassenger->timestamp=timestamp;
                   newpassenger->source=source;
                   newpassenger->destination=destination;
	               newpassenger->next=NULL;
	}

}


//********************************************************************************************

bool DirectionCheck (elevator &E, passenger *& head)// hena il passenger list heya beta3et il valid
{
	passenger *ptr=head;
	if (ptr!=NULL)
	{
	 if ((ptr->destination > ptr->source) && ((E.elevstat == "MOVUP")||(E.elevstat == "STOPUP")))
	{ 
		if  (E.current < ptr->source)
			return true;
		else
			return false;
	}
else if ((ptr->destination < ptr->source) && ((E.elevstat == "MOVDWN")||(E.elevstat == "STOPDWN")))
	{
		if  (E.current > ptr->source)
			return true;
		else 
			return false;
	}
    else if(E.elevstat == "IDLE")
	{
			if (E.current > ptr->source)
			{
			E.elevstat = "MOVDWN";
		    return true;
			}
         	else if(E.current < ptr->source)
			{
			 E.elevstat = "MOVUP";
		     return true;
			}
	             else if (E.current == ptr->source)
				 {
					 if (passenger_direction(head)=="MOVUP")
			 E.elevstat= "STOPUP";
					 else
             E.elevstat= "STOPDWN";
			 return true;
				 }
	}
	else 
		return true;
	}
else
return false;
}
//****************************************************************************************

int DistanceCheck (elevator &E1, elevator &E2, passenger *& head )
{
    passenger *ptr=head;
	if (ptr!=NULL)
	{	                          
	 if ((fabs(ptr->source - E1.current)) <= fabs((ptr->source - E2.current)))
		return 1; // insert at E1
	else 
		return 2; // insert at E2
	}
	else 
		return NULL;
}

//****************************************************************************************

bool loadCheck (elevator &E)
{
	if (E.load <8)
		return true;
	else
		return false;
}
//****************************************************************************************
void removepassenger(passenger *&head)
{ 
  passenger  * temp1=head;

 if(temp1!=NULL)
   
{
   head=head->next; 
   delete temp1;
   temp1=head;
}
}

 //**************************************************************************************

/* this function takes the passenger id and then when it reach it ,it gives back either up or 
down */ 

  string passenger_direction(passenger *&head)
{
passenger *p=head;
 if (p!=NULL)
	{
	
		int direction= (p->destination) - (p->source)	;
		 if(direction>0)
			 return "MOVUP";
		 else if (direction<0)
			 return "MOVDWN";
		 else 
			 ;
			
	}
else	return NULL;
}




//**************************************************************************************

void printAll(node*head)  // this function is merely for testing the reading process
{
	node*p = head;

	while(p != NULL)
	{
		cout << "[ " <<p->data<<" ]"<<endl;
	
		p = p->next;
	}

	cout << "*";
}

//**********************************************************************************************
/*
void upDateScreen()
cout<<"\n\n                      Waiting Passengers ";
cout<<"\n\n                      ================== "; 
cout<<"\n\nP#            Time            Src Floor            Dst Floor ";
cout<<"\n\n------------------------------------------------------------ ";

// 7agat ketera bete7sal

cout<<"\n\n************************************************************ ";
cout<<"                           Elevators
cout<<"                      ================== "<< endl<<endl; 
cout<<"\n\nE#       Current Floor        Direction         Load Passengers";
cout<<"\n\n------------------------------------------------------------ ";


// 7agat ketera tany
cout<<"\n\n************************************************************ ";
cout<<"Last Actions"<<endl;


cout<<"\n\n************************************************************ ";

cout<<"                      Simulation Time:"<<simulationtime<<endl;

cout<<"            Press any key to advance simulation "<<endl;
    system("pause");	
    system("cls");// to clear screen


*/
//*********************************************************************************************
void movelevator (elevator &E ,node * head)  //takes the linked list of source and destinations of elevators
{  
	if (E.current<=MAX_FLOOR || E.current>=0)
	{
		if (E.elevstat== "MOVUP")
	{   
			E.current=E.current+0.5;
		    if ((head!=NULL) && (E.current ==head->data) )
		    E.elevstat="STOPUP";

	}
    else if( E.elevstat== "MOVDWN")
	{
		E.current=E.current-0.5;
		if ((head!=NULL) && (E.current ==head->data) )
		E.elevstat="STOPDWN";
	}
	}
}

//********************************************************************************************

void changeload(elevator &E,node *head)
{
	int total=0;                  //total is the total load to be incremented or dicremented
node *ptr=head;
	if (ptr->data==E.current)
	{
		if(ptr->kind=='s')
			total++;
		else 
			total--;
	}
	E.load=E.load+total;
}
////*********************************************************
void movenode(node *&head , node *&served , int simulationtime)
{
	node * temp=head;
	head=head->next;
	temp->next=served;
	served=temp;
	served->servicetime=simulationtime;
}
///************************

this code represents a code designed for two elevators and eventually i need to calculate the average waiting time for each passenger , the thing is that i'm having a problem to organize what
comes before what in the sequence of something like choosing the elevator and then moving it , etc...

the first thing you need to know that the elevator only take passengers with same direction.
plz help ,the code is a mess ask for details to understand better but plz soon.

Edited 7 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

Comments

Well that's one big code you've posted there, nobody can help you on that quickly, unless you clearly mark out a piece of code that is not working correctly or state your problem in much simpler manner or just about anything that makes one focus on some part of the code. It would take sometime and a really good heart for someone to completely understand your code and solve your problem.

Learn to use code tags

Comments
Well said!
This article has been dead for over six months. Start a new discussion instead.