Question: / / Online C + + compiler to run C + + program online #include #include #include #include #include #include / / For time functions

// Online C++ compiler to run C++ program online
#include
#include
#include
#include
#include
#include // For time functions
#include
class CustomerTask {
public:
int taskID;
std::string taskName;
int arrivalTime;
int waitTime;
int startTime;
int periodTime;
std::string priority;
CustomerTask(int id, std::string name, int arrival, int period, std::string p)
: taskID(id), taskName(name), arrivalTime(arrival), waitTime(0), periodTime(period), priority(p), startTime(-1){
if (priority == "Important"){
this->arrivalTime -=2;
} else if (priority == "VIP"){
this->arrivalTime -=5;
}
}
std::string getFormattedTime(int minutes){
int hours = minutes /60;
int mins = minutes %60;
std::stringstream ss;
ss << std::setw(2)<< std::setfill('0')<< hours <<":"<< std::setw(2)<< std::setfill('0')<< mins;
return ss.str();
}
void printTaskInfo(){
std::cout << "Task ID: "<< taskID <<", Task Name: "<< taskName <<", Arrival Time: "<< getFormattedTime(arrivalTime)
<<", Wait Time: "<< waitTime <<" minutes, Start Time: "<<((startTime !=-1)? getFormattedTime(startTime) : "Not served")
<<", Period Time: "<< periodTime <<" minutes, Priority: "<< priority << std::endl;
}
};
class Compare {
public:
bool operator()(CustomerTask& t1, CustomerTask& t2){
return t1.startTime > t2.startTime;
}
};
class Bank {
private:
std::priority_queue, Compare> tasks;
public:
void addTask(){
int id, period;
std::string name, priority, arrivalTimeStr;
std::cout << "Enter task ID: ";
std::cin >> id;
std::cout << "Enter task name: ";
std::cin >> name;
std::cout << "Enter arrival time (format HH:MM): ";
std::cin >> arrivalTimeStr;
std::cout << "Enter period time: ";
std::cin >> period;
std::cout << "Enter priority (Normal/Important/VIP): ";
std::cin >> priority;
// Parse the input time in HH:MM format
int hours, minutes;
if (sscanf(arrivalTimeStr.c_str(),"%d:%d", &hours, &minutes)!=2|| hours <0|| hours >=24|| minutes <0|| minutes >=60){
std::cout << "Invalid arrival time format. Please use HH:MM.
";
return;
}
int arrivalTime = hours *60+ minutes;
if (arrivalTime <480|| arrivalTime >=840){
std::cout << "Invalid arrival time. Must be between 8 AM and 2 PM (exclusive).
";
return;
}
CustomerTask task(id, name, arrivalTime, period, priority);
tasks.push(task);
}
void addNTasks(int n){
std::vector newTasks;
for (int i =0; i < n; ++i){
int id = i +1;
std::string name = "Task" + std::to_string(id);
int arrival = rand()%361+480; // Random time between 8 AM and 2 PM
int period = rand()%61; // Random time between 0 and 60 minutes
std::string priority =(rand()%3==0)? "Important" : ((rand()%5==0)? "VIP" : "Normal");
CustomerTask task(id, name, arrival, period, priority);
newTasks.push_back(task);
}
std::sort(newTasks.begin(), newTasks.end(), Compare());
for (auto& task : newTasks){
tasks.push(task);
}
}
void rearrangeQueue(){
std::vector tempTasks;
while (!tasks.empty()){
CustomerTask task = tasks.top();
tasks.pop();
task.waitTime +=5;
task.startTime = task.arrivalTime + task.waitTime;
tempTasks.push_back(task);
}
for (auto& task : tempTasks){
tasks.push(task);
}
}
void printTaskInfo(){
std::priority_queue, Compare> tempTasks = tasks;
while (!tempTasks.empty()){
CustomerTask task = tempTasks.top();
tempTasks.pop();
task.printTaskInfo();
}
}
int totalWaitTime(){
int total =0;
std::priority_queue, Compare> tempTasks = tasks;
while (!tempTasks.empty()){
CustomerTask task = tempTasks.top();
tempTasks.pop();
total += task.waitTime;
}
return total;
}
int timeNeededToFinishAllTasks(){
int total =0;
std::priority_queue, Compare> tempTasks = tasks;
while (!tempTasks.empty()){
CustomerTask task = tempTasks.top();
tempTasks.pop();
total += task.periodTime;
} int tasksNotCompleted(){
int count =0;
std::priority_queue, Compare> tempTasks = tasks;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!