Repository navigation
Expand file tree
/
Copy pathWEIGHT.CPP
More file actions
152 lines (139 loc) · 3 KB
/
Copy pathWEIGHT.CPP
File metadata and controls
152 lines (139 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#define MAX 999
#include <time.h>
int main()
{
int c,x;
do{
//clrscr();
std::cout << "*+*+*+*+*+*+*+*+*+*+*+*+*+**+*+*+*+*+*+*+*+*+*+*"<<endl;
cout << " ELEVATOR PROBLEM WITH WEIGHT USING QUEUE"<<endl;
cout << "*+*+*+*+*+*+*+*+*+*+*+*+*+**+*+*+*+*+*+*+*+*+*+*"<<endl<<endl;
int floors = 0;
//Keep track of current floor
int CurrentFloor;
//to end all loops
x=0;
int req_time;
//total number of floors
cout << "Total Number of Floors: ";
cin >> floors;
a: cout << "Lift Resting at Floor No.: ";
cin >> CurrentFloor;
if(CurrentFloor>floors)
{
cout<<"Enter correct floor"<<endl;
goto a;
}
randomize();
//CurrentFloor=random(floors);
cout << "Total time required for travel floors (in Seconds): ";
cin >> req_time;
int i=0;
int nextFloor=0;
int call=0;
int rear = 0;
int front=0;
int front1=0;
int rear1=0;
int rear2=0;
int front2=0;
int temp=0;
int ttime = 0;
int w=0;
int f=0;
int wt=0;
int calls[MAX]; //que
int weight[MAX]; //que
int rweight[MAX]; //que
//Simulate people calling elevator
cout<<"Total number of calls: ";
cin>>call;
cout<<"Max. People Allowed in Lift: ";
cin>>w;
if(call>0)
{calls[front]=CurrentFloor;
f=CurrentFloor;
}
/*cout<<"weight sequence"<<endl;
for (i=rear1;i<call;i++)
{
cin>>weight[i];
rear1++;
}
*/
for(i=0;i<call;i++)
{
calls[rear++]= random(floors);
weight[rear1++]= random(floors+4);
rweight[rear2++]= random(floors+5);
}
if(weight[front]>w)
weight[front]=w;
cout << endl <<"Floors has been qued! Processing Requests. . . ." << endl;
cout<< "Lift at: "<< CurrentFloor << " With " <<weight[front] << " Persons"<< endl;
for (i=0;i<rear;i++)
cout<<" >> " << calls[i];
cout<<endl<< "Persons on Each Floor";
for (i=0;i<rear1;i++)
cout<<" >> " << weight[i];
rweight[0]=0;
cout<<endl<< "Persons Will Leave The Lift on Each Floor";
for (i=0;i<rear1;i++)
cout<<" >> " << rweight[i];
i=front;
wt=weight[front];
while(i<rear)
{
if(call==1)
nextFloor=calls[front];
else
nextFloor=calls[front];
wt = wt - rweight[front2];
front2++;
wt = wt + weight[front1];
if(wt>w)
{
weight[front1]=wt-w;
wt=wt - weight[front1];
call++;
calls[rear++]=nextFloor;
weight[rear1++]=weight[front1];
rweight[rear2++]=random(w);
front1++;
}
else
{
weight[i]=0;
front1++;
if(wt<0)
wt=0;
}
temp= req_time * (nextFloor-CurrentFloor);
if(temp<0)
temp = temp * -1;
ttime = ttime + temp;
cout<<endl<<"Floor: "<<calls[i]<<" Current Weight: "<<wt<< " Time: "<<ttime<<" Person left: "<<weight[i];
front++;
CurrentFloor=calls[i];
i++;
}
cout<<endl<<endl<<"===============================================";
if(front==rear)
{
cout<< endl <<"Floor Sequence: "<< f;
for (i=0;i<rear;i++)
cout<<" >> " << calls[i];
cout << endl << endl << "Total Time: "<<ttime <<" Seconds \tRemaining Persons in lift: " <<wt << endl <<"Total No. Calls Processed: "<<call;
}
else
cout<< endl <<"something went wrong";
cout<< endl <<"===============================================";
c=1;
cout << endl <<"press any key to exit.... OR 0 to repeat" << endl;
cin >> c;
}while(c==0);
return x;
}