-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem4.cpp
More file actions
235 lines (210 loc) · 6.14 KB
/
problem4.cpp
File metadata and controls
235 lines (210 loc) · 6.14 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// problem 4 , Mahmoud hosni and Mahmoud Hesham
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
template <typename T>
class MaxHeap{
private:
T* heap;
int capacity;
int heapSize;
void heapify(int index) {
int parent = (index - 1) / 2;
if (index <= 0 || parent < 0) {
return;
}
if (heap[index] > heap[parent]) {
swap(heap[index], heap[parent]);
heapify(parent);
}
}
void heapifyDown(int index) {
int leftChild = 2 * index + 1;
int rightChild = 2 * index + 2;
if (leftChild >= heapSize) return;
int largest = index;
if (leftChild < heapSize && heap[leftChild] > heap[largest])
largest = leftChild;
if (rightChild < heapSize && heap[rightChild] > heap[largest])
largest = rightChild;
if (largest != index) {
swap(heap[index], heap[largest]);
heapifyDown(largest);
}
}
public:
MaxHeap(int capacity = 100) {
this->capacity = capacity;
heap = new T[capacity];
heapSize = 0;
}
~MaxHeap() {
delete[] heap;
}
void insert(const T &value) {
cout << "Inserting: " << value << endl;
if (heapSize == capacity) {
capacity *= 2;
T* newHeap = new T[capacity];
for (int i = 0; i < heapSize; i++) {
newHeap[i] = heap[i];
}
delete[] heap;
heap = newHeap;
}
heap[heapSize] = value;
heapify(heapSize);
heapSize++;
print_heap();
}
T extract_max() {
if (heapSize == 0) {
throw runtime_error("Heap is empty");
}
T maxValue = heap[0];
heap[0] = heap[heapSize - 1];
heapSize--;
if (heapSize > 0) {
heapifyDown(0);
}
return maxValue;
}
T peek() {
if (heapSize == 0) {
throw runtime_error("Heap is empty");
}
return heap[0];
}
void print_heap() {
cout << "Heap: [ ";
for (int i = 0; i < heapSize; i++) {
cout << heap[i];
if (i < heapSize - 1) {
cout << ", ";
}
}
cout << " ]" << endl;
}
};
class Patient {
string name;
int severity, arrivalTime;
public:
// Add default constructor
Patient() : name(""), severity(0), arrivalTime(0) {}
Patient(string name, int severity, int arrivalTime) {
this->name = name;
this->severity = severity;
this->arrivalTime = arrivalTime;
}
bool operator>(const Patient &other) const{
if(severity == other.severity)
return arrivalTime < other.arrivalTime;
return severity > other.severity;
}
friend ostream &operator<<(ostream &os, const Patient &patient) {
os << patient.name;
return os;
}
};
class EmergencyRoom {
private:
MaxHeap<Patient> patients;
public:
void addPatient(const string &name, int severity, int arrivalTime) {
if (name.empty() || severity <= 0 || arrivalTime <= 0) {
cout << "Invalid input" << endl;
return;
}
Patient patient(name, severity, arrivalTime);
patients.insert(patient);
}
Patient treat_patient() {
return patients.extract_max();
}
Patient peek_patient() {
return patients.peek();
}
void printPatients() {
patients.print_heap();
}
};
int main() {
EmergencyRoom emergency_room;
int choice;
cout << "Emergency Room Priority Queue System\n";
cout << "===================================\n";
while (true) {
cout << "\n1. Add new patient\n"
<< "2. Treat next patient\n"
<< "3. View next patient\n"
<< "4. Display all patients\n"
<< "5. Add sample data (10 patients)\n"
<< "0. Exit\n"
<< "Enter your choice: ";
cin >> choice;
switch (choice) {
case 0: {
cout << "Exiting program...\n";
return 0;
}
case 1: {
string name;
int severity;
int time;
cout << "Enter patient name: ";
cin >> name;
cout << "Enter severity (1-100): ";
cin >> severity;
cout << "Enter arrival time: ";
cin >> time;
emergency_room.addPatient(name, severity, time);
cout << "Patient " << name << " added successfully.\n";
break;
}
case 2: {
try {
Patient p = emergency_room.treat_patient();
cout << "Treating patient: " << p << endl;
} catch (const runtime_error& e) {
cout << "No patients in queue.\n";
}
break;
}
case 3: {
try {
Patient patient = emergency_room.peek_patient();
cout << "Next patient to be treated: " << patient << endl;
} catch (const runtime_error& e) {
cout << "No patients in queue.\n";
}
break;
}
case 4: {
cout << "Current queue: ";
emergency_room.printPatients();
break;
}
case 5: {
cout << "Adding sample data from file...\n";
ifstream file("patients.txt");
if (!file) {
cout << "Error: Could not open patients.txt\n";
break;
}
string name;
int severity;
int time;
while (file >> name >> severity >> time) {
emergency_room.addPatient(name, severity, time);
cout << endl;
}
file.close();
break;
}
default:
cout << "Invalid choice. Please try again.\n";
}
}
}