2025 COS2611 ASSIGNMENT 1
SOLUTION
#include <iostream>
#include <fstream>
#include <queue>
#include <string>
#include <vector>
#include <iomanip>
#include <sstream>
using namespace std;
// Passenger structure
struct Passenger {
char destination; // 'S', 'L', or 'C'
int id; // Passenger ID (e.g., 1, 2, 3...)
int boardingTime; // Time required to board
int remainingTime; // Remaining boarding time
int arrivalTime; // Time when the passenger arrived
Passenger(char dest, int passId, int boarding, int arrival) :
destination(dest), id(passId), boardingTime(boarding),
remainingTime(boarding), arrivalTime(arrival) {}
};
// Structure to keep track of taxi state
struct TaxiState {
int capacity; // Current number of passengers (max 5)
Passenger* boardingPassenger; // Pointer to the currently boarding passenger
TaxiState() : capacity(5), boardingPassenger(nullptr) {}
};
// Structure to maintain passenger counters for each type
struct PassengerCounters {
int sCount;
int lCount;
int cCount;
PassengerCounters() : sCount(0), lCount(0), cCount(0) {}
};
, // Parse a line from the input file
bool parseInputLine(const string& line, int& timeCount, char& route, int& boardingTime) {
stringstream ss(line);
string item;
// Parse time count
if (!getline(ss, item, ',')) return false;
timeCount = stoi(item);
// Parse route
if (!getline(ss, item, ',')) return false;
route = item[0];
// Parse boarding time
if (!getline(ss, item)) return false;
boardingTime = stoi(item);
return true;
}
// Print the table header
void printTableHeader() {
cout << "+------+------+-------+-------+-------+-------+-------+-------+-------+-------+-------+" << endl;
cout << "| T | next | S | L | C | WQS | WQL | WQC | CS | CL | CC |" << endl;
cout << "+------+------+-------+-------+-------+-------+-------+-------+-------+-------+-------+" << endl;
}
// Get string representation of a passenger
string passengerToString(const Passenger* p) {
if (!p) return "-";
stringstream ss;
ss << p->destination << p->id << "(" << p->remainingTime << ")";
return ss.str();
}
// Print a row in the table
void printTableRow(int time, char nextPassenger,
Passenger* queueS,
Passenger* queueL,
Passenger* queueC,
queue<Passenger*>& waitingQS,
queue<Passenger*>& waitingQL,
queue<Passenger*>& waitingQC,
const TaxiState& taxiS,
SOLUTION
#include <iostream>
#include <fstream>
#include <queue>
#include <string>
#include <vector>
#include <iomanip>
#include <sstream>
using namespace std;
// Passenger structure
struct Passenger {
char destination; // 'S', 'L', or 'C'
int id; // Passenger ID (e.g., 1, 2, 3...)
int boardingTime; // Time required to board
int remainingTime; // Remaining boarding time
int arrivalTime; // Time when the passenger arrived
Passenger(char dest, int passId, int boarding, int arrival) :
destination(dest), id(passId), boardingTime(boarding),
remainingTime(boarding), arrivalTime(arrival) {}
};
// Structure to keep track of taxi state
struct TaxiState {
int capacity; // Current number of passengers (max 5)
Passenger* boardingPassenger; // Pointer to the currently boarding passenger
TaxiState() : capacity(5), boardingPassenger(nullptr) {}
};
// Structure to maintain passenger counters for each type
struct PassengerCounters {
int sCount;
int lCount;
int cCount;
PassengerCounters() : sCount(0), lCount(0), cCount(0) {}
};
, // Parse a line from the input file
bool parseInputLine(const string& line, int& timeCount, char& route, int& boardingTime) {
stringstream ss(line);
string item;
// Parse time count
if (!getline(ss, item, ',')) return false;
timeCount = stoi(item);
// Parse route
if (!getline(ss, item, ',')) return false;
route = item[0];
// Parse boarding time
if (!getline(ss, item)) return false;
boardingTime = stoi(item);
return true;
}
// Print the table header
void printTableHeader() {
cout << "+------+------+-------+-------+-------+-------+-------+-------+-------+-------+-------+" << endl;
cout << "| T | next | S | L | C | WQS | WQL | WQC | CS | CL | CC |" << endl;
cout << "+------+------+-------+-------+-------+-------+-------+-------+-------+-------+-------+" << endl;
}
// Get string representation of a passenger
string passengerToString(const Passenger* p) {
if (!p) return "-";
stringstream ss;
ss << p->destination << p->id << "(" << p->remainingTime << ")";
return ss.str();
}
// Print a row in the table
void printTableRow(int time, char nextPassenger,
Passenger* queueS,
Passenger* queueL,
Passenger* queueC,
queue<Passenger*>& waitingQS,
queue<Passenger*>& waitingQL,
queue<Passenger*>& waitingQC,
const TaxiState& taxiS,