#include <stdio.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
printf("test");
return 0;
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
printf("%d", numberOfStops);
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/* To Simulate a Public Bus System
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
*/
/*
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
*/
/*
To Process passengers who arrive
Read a line from the input file;
Parse the line to create a waiting_passenger node;
Add the waiting_passenger to the waiting line at that stop;
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
To Process passengers who arrive
Read a line from the input file;
Parse the line to create a waiting_passenger node;
Add the waiting_passenger to the waiting line at that stop;
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
To Process passengers who arrive
While (store file position with fgetpos()) &&
(fscanf matches three formatted digits) &&
(the first matched digit == the current time_step);
Add the passenger to the waiting structure;
If (you're at the end of the file (fscanf returns 1))
Set the end_of_file flag
Reset the file position (fsetpos());
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Build the bus stops;
Create the bus and initialize it at stop #0;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
To Process passengers who arrive
While (store file position with fgetpos()) &&
(fscanf matches three formatted digits) &&
(the first matched digit == the current time_step);
Add the passenger to the waiting structure;
If (you're at the end of the file (fscanf returns 1))
Set the end_of_file flag;
Reset the file position (fsetpos());
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Build the bus stops;
Create the bus and initialize it at stop #0;
Run the simulation
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
To build the bus stops
create an array of bus stops;
To Process passengers who arrive
While (store file position with fgetpos()) &&
(fscanf matches three formatted digits) &&
(the first matched digit == the current time_step);
Add the passenger to the waiting structure;
If (you're at the end of the file (fscanf returns 1))
Set the end_of_file flag;
Reset the file position (fsetpos());
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Build the bus stops;
Create the bus and initialize it at stop #0;
Open a log file;
Run the simulation
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
To build the bus stops
create an array of bus stops;
To Process passengers who arrive
While (store file position with fgetpos()) &&
(fscanf matches three formatted digits) &&
(the first matched digit == the current time_step);
Add the passenger to the waiting structure;
If (you're at the end of the file (fscanf returns 1))
Set the end_of_file flag;
Reset the file position (fsetpos());
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Build the bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Step through the simulation;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To build the bus stops
create an array of bus stops;
To Process passengers who arrive
While (store file position with fgetpos()) &&
(fscanf matches three formatted digits) &&
(the first matched digit == the current time_step);
Add the passenger to the waiting structure;
If (you're at the end of the file (fscanf returns 1))
Set the end_of_file flag;
Reset the file position (fsetpos());
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Step through the simulation;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To Process passengers who arrive
While (store file position with fgetpos()) &&
(fscanf matches three formatted digits) &&
(the first matched digit == the current time_step);
Add the passenger to the waiting structure;
If (you're at the end of the file (fscanf returns 1))
Set the end_of_file flag;
Reset the file position (fsetpos());
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Step through the simulation;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
While (store file position with fgetpos()) &&
(fscanf matches three formatted digits) &&
(the first matched digit == the current time_step);
Add the passenger to the waiting structure;
If (you're at the end of the file (fscanf returns 1))
Set the end_of_file flag;
Reset the file position (fsetpos());
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
While (store file position with fgetpos()) &&
(fscanf matches three formatted digits) &&
(the first matched digit == the current time_step);
Add the passenger to the waiting structure;
If (you're at the end of the file (fscanf returns 1))
Set the end_of_file flag;
Reset the file position (fsetpos());
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
While (store file position with fgetpos()) &&
(fscanf matches three formatted digits) &&
(the first matched digit == the current time_step);
Add the passenger to the waiting structure;
If (you're at the end of the file (fscanf returns 1))
Set the end_of_file flag;
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
// Insert the passenger onto the bus
// Remove the passenger from the waiting list
To insert the passenger onto the bus
cur_waiting = head_waiting;
cur_waiting->next = head_bus;
// http://briandk.com/screenshots/MovingPassengersFromWaitingToBus-20111114-174433.png
// http://briandk.com/screenshots/MoveOnePassengerFromWaitingToBus-20111114-175217.png
// http://briandk.com/screenshots/MoveOnePassengerFromWaitingToBus2-20111114-180202.png
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_length = Compute transit length
wait_time = Compute wait time
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_length = Compute transit length
wait_time = Compute wait time
get_journey_length <- function(origin, destination, total.stops) {
if (origin < destination) {
journey <- destination - origin
} else {
journey <- destination + (total.stops - origin)
}
return(journey)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
Open the passenger list;
Get the number of bus stops to simulate;
Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_length = Compute transit length
wait_time = Compute wait time
get_journey_length <- function(origin, destination, total.stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
Free any remaining dynamically allocated memory;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
struct bus_stop *stops = malloc(numberOfStops * sizeof(struct bus_stop));
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
Free any remaining dynamically allocated memory;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
struct bus_stop *stops = malloc(numberOfStops * sizeof(struct bus_stop));
struct bus *bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
Create the bus and initialize it at stop #0;
Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
Free any remaining dynamically allocated memory;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
struct bus_stop *stops = malloc(numberOfStops * sizeof(struct bus_stop));
struct bus *bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
int current_time = 1;
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
Free any remaining dynamically allocated memory;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
int current_time = 1;
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
Free any remaining dynamically allocated memory;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
current_time = 1;
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
printf("Current timestep is: %d", current_time);
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
Open a log file;
Create a system tracker;
Step through the simulation;
Free any remaining dynamically allocated memory;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
current_time = 1;
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file && waiting_passengers > 0 && riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file || waiting_passengers > 0 || riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
}
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file || waiting_passengers > 0 || riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->total_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file || waiting_passengers > 0 || riding passengers > 0)
Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
To print the state of the system
Print the current time step;
Print the current bus stop;
Print the current number of riders;
For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->total_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void PrintSystemState() {
printf("*** TIME STEP = %d ***\n", current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void RunSimulator() {
// while (!feof(passengerFile) ||
// bus->number_of_passengers > 0 ||
// tracker->waiting_passengers > 0
// ) {
PrintSystemState();
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file || waiting_passengers > 0 || riding passengers > 0)
# Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
# To print the state of the system
# Print the current time step;
# Print the current bus stop;
# Print the current number of riders;
# For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->total_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void PrintSystemState() {
printf("*** TIME STEP = %d ***\n", current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival == current_time)) {;
printf("Add the passenger to the waiting structure\n");
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void RunSimulator() {
// while (!feof(passengerFile) ||
// bus->number_of_passengers > 0 ||
// tracker->waiting_passengers > 0
// ) {
PrintSystemState();
ProcessArrivingPassengers();
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file || waiting_passengers > 0 || riding passengers > 0)
# Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
# To print the state of the system
# Print the current time step;
# Print the current bus stop;
# Print the current number of riders;
# For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->total_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void PrintSystemState() {
printf("*** TIME STEP = %d ***\n", current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival == current_time)) {;
printf("Add the passenger to the waiting structure\n");
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void RunSimulator() {
// while (!feof(passengerFile) ||
// bus->number_of_passengers > 0 ||
// tracker->waiting_passengers > 0
// ) {
PrintSystemState();
ProcessArrivingPassengers();
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
TearDown();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file || waiting_passengers > 0 || riding passengers > 0)
# Print the state of the system;
Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
# To print the state of the system
# Print the current time step;
# Print the current bus stop;
# Print the current number of riders;
# For each bus stop in numberOfStops, print the passengers waiting;
To Process passengers who arrive
int origin;
int destination;
int arrival;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
While ((!feof(file)) && (arrival == current_time));
Add the passenger to the waiting structure;
prev_pos = store file position with fgetpos()
current_line = fscanf("%d %d %d", )
If (!feof(file))
Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->total_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void PrintSystemState() {
printf("*** TIME STEP = %d ***\n", current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival == current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
new_passenger = malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = &new_passenger;
tracker->waiting_passengers++;
/*
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
*/
}
void RunSimulator() {
// while (!feof(passengerFile) ||
// bus->number_of_passengers > 0 ||
// tracker->waiting_passengers > 0
// ) {
PrintSystemState();
ProcessArrivingPassengers();
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
TearDown();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file || waiting_passengers > 0 || riding passengers > 0)
# Print the state of the system;
# Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
# To print the state of the system
# Print the current time step;
# Print the current bus stop;
# Print the current number of riders;
# For each bus stop in numberOfStops, print the passengers waiting;
# To Process passengers who arrive
# int origin;
# int destination;
# int arrival;
#
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# While ((!feof(file)) && (arrival == current_time));
# Add the passenger to the waiting structure;
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# If (!feof(file))
# Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->total_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void PrintSystemState() {
printf("*** TIME STEP = %d ***\n", current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival == current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = (struct passenger*) &new_passenger;
tracker->waiting_passengers++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture stats
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void RunSimulator() {
// while (!feof(passengerFile) ||
// bus->number_of_passengers > 0 ||
// tracker->waiting_passengers > 0
// ) {
PrintSystemState();
ProcessArrivingPassengers();
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
TearDown();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file || waiting_passengers > 0 || riding passengers > 0)
# Print the state of the system;
# Process passengers who arrive at stops during that timestep;
Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
# To print the state of the system
# Print the current time step;
# Print the current bus stop;
# Print the current number of riders;
# For each bus stop in numberOfStops, print the passengers waiting;
# To Process passengers who arrive
# int origin;
# int destination;
# int arrival;
#
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# While ((!feof(file)) && (arrival == current_time));
# Add the passenger to the waiting structure;
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# If (!feof(file))
# Reset the file position (fsetpos());
To add a passenger to the waiting structure;
Create new_passenger structure;
Compute passenger stats;
new_passenger->next = head;
head = &new_passenger;
increment the waiting passengers;
To unload passengers
cur = head
prev = head
while (cur != null)
if (cur->dest == cur_stop) {
// capture stats
if (cur == head)
head = cur->next
prev->next = cur->next
free(cur)
cur = prev->next
} else {
prev = cur
cur = cur->next
}
// http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->total_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void PrintSystemState() {
printf("*** TIME STEP = %d ***\n", current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival == current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = (struct passenger*) &new_passenger;
tracker->waiting_passengers++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture stats
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void RunSimulator() {
// while (!feof(passengerFile) ||
// bus->number_of_passengers > 0 ||
// tracker->waiting_passengers > 0
// ) {
PrintSystemState();
ProcessArrivingPassengers();
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
TearDown();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file || waiting_passengers > 0 || riding passengers > 0)
# Print the state of the system;
# Process passengers who arrive at stops during that timestep;
# Unload the bus of passengers who need to get off;
Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
# To print the state of the system
# Print the current time step;
# Print the current bus stop;
# Print the current number of riders;
# For each bus stop in numberOfStops, print the passengers waiting;
# To Process passengers who arrive
# int origin;
# int destination;
# int arrival;
#
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# While ((!feof(file)) && (arrival == current_time));
# Add the passenger to the waiting structure;
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# If (!feof(file))
# Reset the file position (fsetpos());
# To add a passenger to the waiting structure;
# Create new_passenger structure;
# Compute passenger stats;
# new_passenger->next = head;
# head = &new_passenger;
# increment the waiting passengers;
# To unload passengers
# cur = head
# prev = head
# while (cur != null)
# if (cur->dest == cur_stop) {
# // capture stats
# if (cur == head)
# head = cur->next
# prev->next = cur->next
# free(cur)
# cur = prev->next
# } else {
# prev = cur
# cur = cur->next
# }
# // http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
To Load waiting passengers from a stop
if (head_waiting != NULL) {
temp_ptr = head_waiting->next;
head_waiting->next = head_bus;
head_bus = head_waiting;
head_waiting = temp_ptr;
}
// Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->total_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void PrintSystemState() {
printf("*** TIME STEP = %d ***\n", current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival == current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = (struct passenger*) &new_passenger;
tracker->waiting_passengers++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture stats
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
}
}
void RunSimulator() {
// while (!feof(passengerFile) ||
// bus->number_of_passengers > 0 ||
// tracker->waiting_passengers > 0
// ) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
TearDown();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file || waiting_passengers > 0 || riding passengers > 0)
# Print the state of the system;
# Process passengers who arrive at stops during that timestep;
# Unload the bus of passengers who need to get off;
# Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
# To print the state of the system
# Print the current time step;
# Print the current bus stop;
# Print the current number of riders;
# For each bus stop in numberOfStops, print the passengers waiting;
# To Process passengers who arrive
# int origin;
# int destination;
# int arrival;
#
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# While ((!feof(file)) && (arrival == current_time));
# Add the passenger to the waiting structure;
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# If (!feof(file))
# Reset the file position (fsetpos());
# To add a passenger to the waiting structure;
# Create new_passenger structure;
# Compute passenger stats;
# new_passenger->next = head;
# head = &new_passenger;
# increment the waiting passengers;
# To unload passengers
# cur = head
# prev = head
# while (cur != null)
# if (cur->dest == cur_stop) {
# // capture stats
# if (cur == head)
# head = cur->next
# prev->next = cur->next
# free(cur)
# cur = prev->next
# } else {
# prev = cur
# cur = cur->next
# }
# // http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
# To Load waiting passengers from a stop
# if (head_waiting != NULL) {
# temp_ptr = head_waiting->next;
# head_waiting->next = head_bus;
# head_bus = head_waiting;
# head_waiting = temp_ptr;
# }
# // Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->total_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void PrintSystemState() {
printf("*** TIME STEP = %d ***\n", current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival == current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = (struct passenger*) &new_passenger;
tracker->waiting_passengers++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
}
}
void RunSimulator() {
// while (!feof(passengerFile) ||
// bus->number_of_passengers > 0 ||
// tracker->waiting_passengers > 0
// ) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
TearDown();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file || waiting_passengers > 0 || riding passengers > 0)
# Print the state of the system;
# Process passengers who arrive at stops during that timestep;
# Unload the bus of passengers who need to get off;
# Load the bus with passengers at that stop;
Move the bus to the next stop;
Print the summary information;
# To print the state of the system
# Print the current time step;
# Print the current bus stop;
# Print the current number of riders;
# For each bus stop in numberOfStops, print the passengers waiting;
# To Process passengers who arrive
# int origin;
# int destination;
# int arrival;
#
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# While ((!feof(file)) && (arrival == current_time));
# Add the passenger to the waiting structure;
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# If (!feof(file))
# Reset the file position (fsetpos());
# To add a passenger to the waiting structure;
# Create new_passenger structure;
# Compute passenger stats;
# new_passenger->next = head;
# head = &new_passenger;
# increment the waiting passengers;
# To unload passengers
# cur = head
# prev = head
# while (cur != null)
# if (cur->dest == cur_stop) {
# // capture stats
# if (cur == head)
# head = cur->next
# prev->next = cur->next
# free(cur)
# cur = prev->next
# } else {
# prev = cur
# cur = cur->next
# }
# // http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
# To Load waiting passengers from a stop
# if (head_waiting != NULL) {
# temp_ptr = head_waiting->next;
# head_waiting->next = head_bus;
# head_bus = head_waiting;
# head_waiting = temp_ptr;
# }
# // Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
To move the bus to the next stop
current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->total_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void PrintSystemState() {
printf("*** TIME STEP = %d ***\n", current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival == current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = (struct passenger*) &new_passenger;
tracker->waiting_passengers++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
}
}
void RunSimulator() {
// while (!feof(passengerFile) ||
// bus->number_of_passengers > 0 ||
// tracker->waiting_passengers > 0
// ) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (current_stop + 1) % number_of_stops;
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
TearDown();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file || waiting_passengers > 0 || riding passengers > 0)
# Print the state of the system;
# Process passengers who arrive at stops during that timestep;
# Unload the bus of passengers who need to get off;
# Load the bus with passengers at that stop;
# Move the bus to the next stop;
Print the summary information;
# To print the state of the system
# Print the current time step;
# Print the current bus stop;
# Print the current number of riders;
# For each bus stop in numberOfStops, print the passengers waiting;
# To Process passengers who arrive
# int origin;
# int destination;
# int arrival;
#
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# While ((!feof(file)) && (arrival == current_time));
# Add the passenger to the waiting structure;
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# If (!feof(file))
# Reset the file position (fsetpos());
# To add a passenger to the waiting structure;
# Create new_passenger structure;
# Compute passenger stats;
# new_passenger->next = head;
# head = &new_passenger;
# increment the waiting passengers;
# To unload passengers
# cur = head
# prev = head
# while (cur != null)
# if (cur->dest == cur_stop) {
# // capture stats
# if (cur == head)
# head = cur->next
# prev->next = cur->next
# free(cur)
# cur = prev->next
# } else {
# prev = cur
# cur = cur->next
# }
# // http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
# To Load waiting passengers from a stop
# if (head_waiting != NULL) {
# temp_ptr = head_waiting->next;
# head_waiting->next = head_bus;
# head_bus = head_waiting;
# head_waiting = temp_ptr;
# }
# // Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
# To move the bus to the next stop
# current_stop = (current_stop + 1) % number_of_stops;
To compute passenger stats
transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
get_journey_length <- function(origin, destination, total_stops) {
return(
(destination + (total.stops - origin)) %% total.stops
)
}
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->total_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void PrintSystemState() {
printf("*** TIME STEP = %d ***\n", current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival == current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = (struct passenger*) &new_passenger;
tracker->waiting_passengers++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
}
}
void RunSimulator() {
// while (!feof(passengerFile) ||
// bus->number_of_passengers > 0 ||
// tracker->waiting_passengers > 0
// ) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (current_stop + 1) % number_of_stops;
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
TearDown();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file || waiting_passengers > 0 || riding passengers > 0)
# Print the state of the system;
# Process passengers who arrive at stops during that timestep;
# Unload the bus of passengers who need to get off;
# Load the bus with passengers at that stop;
# Move the bus to the next stop;
Print the summary information;
# To print the state of the system
# Print the current time step;
# Print the current bus stop;
# Print the current number of riders;
# For each bus stop in numberOfStops, print the passengers waiting;
# To Process passengers who arrive
# int origin;
# int destination;
# int arrival;
#
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# While ((!feof(file)) && (arrival == current_time));
# Add the passenger to the waiting structure;
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# If (!feof(file))
# Reset the file position (fsetpos());
# To add a passenger to the waiting structure;
# Create new_passenger structure;
# Compute passenger stats;
# new_passenger->next = head;
# head = &new_passenger;
# increment the waiting passengers;
# To unload passengers
# cur = head
# prev = head
# while (cur != null)
# if (cur->dest == cur_stop) {
# // capture stats
# if (cur == head)
# head = cur->next
# prev->next = cur->next
# free(cur)
# cur = prev->next
# } else {
# prev = cur
# cur = cur->next
# }
# // http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
# To Load waiting passengers from a stop
# if (head_waiting != NULL) {
# temp_ptr = head_waiting->next;
# head_waiting->next = head_bus;
# head_bus = head_waiting;
# head_waiting = temp_ptr;
# }
# // Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
# To move the bus to the next stop
# current_stop = (current_stop + 1) % number_of_stops;
# To compute passenger stats
# transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
# wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
# get_journey_length <- function(origin, destination, total_stops) {
# return(
# (destination + (total.stops - origin)) %% total.stops
# )
# }
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->total_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void PrintSystemState() {
printf("*** TIME STEP = %d ***\n", current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival == current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = (struct passenger*) &new_passenger;
tracker->waiting_passengers++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
}
}
void RunSimulator() {
// while (!feof(passengerFile) ||
// bus->number_of_passengers > 0 ||
// tracker->waiting_passengers > 0
// ) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
tracker->total_time++;
}
void PrintSummaryInformation() {
printf("Total sim time: %.0f\n", tracker->total_time);
printf("Total passengers: %.0f\n", tracker->total_passengers);
printf("Passenger throughput: %.2f\n", tracker->total_passengers / tracker->total_time);
printf("Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
printf("Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
/*
To Simulate a Public Bus System
# Open the passenger list;
# Get the number of bus stops to simulate;
# Create an array of bus stops;
# Create the bus and initialize it at stop #0;
# Create a timestep and initialize it to 1;
# Open a log file;
# Create a system tracker;
Step through the simulation;
Tear-down: Free any remaining dynamically allocated memory and close open file-pointers;
To step through the simulation
While (it's not end_of_file || waiting_passengers > 0 || riding passengers > 0)
# Print the state of the system;
# Process passengers who arrive at stops during that timestep;
# Unload the bus of passengers who need to get off;
# Load the bus with passengers at that stop;
# Move the bus to the next stop;
Print the summary information;
# To print the state of the system
# Print the current time step;
# Print the current bus stop;
# Print the current number of riders;
# For each bus stop in numberOfStops, print the passengers waiting;
# To Process passengers who arrive
# int origin;
# int destination;
# int arrival;
#
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# While ((!feof(file)) && (arrival == current_time));
# Add the passenger to the waiting structure;
# prev_pos = store file position with fgetpos()
# current_line = fscanf("%d %d %d", )
#
# If (!feof(file))
# Reset the file position (fsetpos());
# To add a passenger to the waiting structure;
# Create new_passenger structure;
# Compute passenger stats;
# new_passenger->next = head;
# head = &new_passenger;
# increment the waiting passengers;
# To unload passengers
# cur = head
# prev = head
# while (cur != null)
# if (cur->dest == cur_stop) {
# // capture stats
# if (cur == head)
# head = cur->next
# prev->next = cur->next
# free(cur)
# cur = prev->next
# } else {
# prev = cur
# cur = cur->next
# }
# // http://DrDoane.com/screenshots/skitched-20111114-124924.jpg
# To Load waiting passengers from a stop
# if (head_waiting != NULL) {
# temp_ptr = head_waiting->next;
# head_waiting->next = head_bus;
# head_bus = head_waiting;
# head_waiting = temp_ptr;
# }
# // Proof-by-skitch http://BrianDK.com/screenshots/MovePassengerFromWaitingToBus2-20111117-150539.png
# To move the bus to the next stop
# current_stop = (current_stop + 1) % number_of_stops;
# To compute passenger stats
# transit_time = get_journey_length(waiting_stop, destination_stop, number_of_stops)
# wait_time = get_journey_length(current_bus_position, waiting_stop, number_of_stops)
# get_journey_length <- function(origin, destination, total_stops) {
# return(
# (destination + (total.stops - origin)) %% total.stops
# )
# }
*/
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
current_time = 1;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->total_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void PrintSystemState() {
printf("*** TIME STEP = %d ***\n", current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival == current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = (struct passenger*) &new_passenger;
tracker->waiting_passengers++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
}
}
void RunSimulator() {
while (tracker->total_time < 10 ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
current_time = current_time + 1;
printf("\n\nCURRENT TIME: %d\n\n", current_time);
tracker->total_time++;
}
}
void PrintSummaryInformation() {
printf("Total sim time: %.0f\n", tracker->total_time);
printf("Total passengers: %.0f\n", tracker->total_passengers);
printf("Passenger throughput: %.2f\n", tracker->total_passengers / tracker->total_time);
printf("Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
printf("Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void PrintSystemState() {
printf("*** TIME STEP = %.0f ***\n", tracker->current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival ==tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = (struct passenger*) &new_passenger;
tracker->waiting_passengers++;
printf("\n\nADDED ARRIVING PASSENGER!\n\n");
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
}
}
void RunSimulator() {
while (tracker->current_time < 10 ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
printf("\n\nCURRENT TIME: %.0f\n\n", tracker->current_time);
tracker->current_time++;
}
}
void PrintSummaryInformation() {
printf("Total sim time: %.0f\n", tracker->current_time);
printf("Total passengers: %.0f\n", tracker->total_passengers);
printf("Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
printf("Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
printf("Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void PrintSystemState() {
printf("*** TIME STEP = %.0f ***\n", tracker->current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival ==tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = (struct passenger*) &new_passenger;
tracker->waiting_passengers++;
printf("\n\nADDED ARRIVING PASSENGER!\n\n");
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
}
}
void RunSimulator() {
while (tracker->current_time < 10 ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
printf("\n\nCURRENT TIME: %.0f\n\n", tracker->current_time);
tracker->current_time++;
}
}
void PrintSummaryInformation() {
printf("Total sim time: %.0f\n", tracker->current_time);
printf("Total passengers: %.0f\n", tracker->total_passengers);
printf("Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
printf("Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
printf("Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
void RunSimulator() {
while (tracker->current_time < 10 ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
printf("\n\nCURRENT TIME: %.0f\n\n", tracker->current_time);
tracker->current_time++;
}
}
void PrintSystemState() {
printf("*** TIME STEP = %.0f ***\n", tracker->current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival ==tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = (struct passenger*) &new_passenger;
tracker->waiting_passengers++;
printf("\n\nADDED ARRIVING PASSENGER!\n\n");
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
}
}
void PrintSummaryInformation() {
printf("Total sim time: %.0f\n", tracker->current_time);
printf("Total passengers: %.0f\n", tracker->total_passengers);
printf("Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
printf("Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
printf("Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (tracker->current_time < 10 ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
printf("\n\nCURRENT TIME: %.0f\n\n", tracker->current_time);
tracker->current_time++;
}
}
void PrintSystemState() {
printf("*** TIME STEP = %.0f ***\n", tracker->current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
while ((!feof(passengerFile)) && (arrival ==tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = (struct passenger*) &new_passenger;
tracker->waiting_passengers++;
printf("\n\nADDED ARRIVING PASSENGER!\n\n");
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
}
}
void PrintSummaryInformation() {
printf("Total sim time: %.0f\n", tracker->current_time);
printf("Total passengers: %.0f\n", tracker->total_passengers);
printf("Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
printf("Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
printf("Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (tracker->current_time < 10 ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
printf("\n\nCURRENT TIME: %.0f\n\n", tracker->current_time);
tracker->current_time++;
}
}
void PrintSystemState() {
printf("*** TIME STEP = %.0f ***\n", tracker->current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
while ((!feof(passengerFile)) && (arrival == tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = (struct passenger*) &new_passenger;
tracker->waiting_passengers++;
printf("\n\nADDED ARRIVING PASSENGER!\n\n");
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
}
}
void PrintSummaryInformation() {
printf("Total sim time: %.0f\n", tracker->current_time);
printf("Total passengers: %.0f\n", tracker->total_passengers);
printf("Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
printf("Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
printf("Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (tracker->current_time < 10 ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
printf("\n\nCURRENT TIME: %.0f\n\n", tracker->current_time);
tracker->current_time++;
}
}
void PrintSystemState() {
printf("*** TIME STEP = %.0f ***\n", tracker->current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
while ((!feof(passengerFile)) && (arrival == tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = (struct passenger*) &new_passenger;
tracker->waiting_passengers++;
printf("\n\nADDED ARRIVING PASSENGER!\n\n");
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
// Update the system to reflect loading the passenger on the bus
tracker->waiting_passengers--;
stops[bus->current_stop].num_waiting--;
bus->number_of_passengers++;
}
}
void PrintSummaryInformation() {
printf("Total sim time: %.0f\n", tracker->current_time);
printf("Total passengers: %.0f\n", tracker->total_passengers);
printf("Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
printf("Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
printf("Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (tracker->current_time < 10 ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
printf("\n\nCURRENT TIME: %.0f\n\n", tracker->current_time);
tracker->current_time++;
}
}
void PrintSystemState() {
printf("*** TIME STEP = %.0f ***\n", tracker->current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
while ((!feof(passengerFile)) && (arrival == tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger;
new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = new_passenger;
// Update the system to reflect newly arrived passenger
tracker->waiting_passengers++;
stops[origin].num_waiting++;
printf("\n\nADDED ARRIVING PASSENGER!\n\n");
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
prev->next = cur->next;
free(cur);
cur = prev->next;
} else {
prev = cur;
cur = cur->next;
}
}
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
// Update the system to reflect loading the passenger on the bus
tracker->waiting_passengers--;
stops[bus->current_stop].num_waiting--;
bus->number_of_passengers++;
}
}
void PrintSummaryInformation() {
printf("Total sim time: %.0f\n", tracker->current_time);
printf("Total passengers: %.0f\n", tracker->total_passengers);
printf("Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
printf("Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
printf("Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (tracker->current_time < 10 ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
printf("\n\nCURRENT TIME: %.0f\n\n", tracker->current_time);
tracker->current_time++;
}
}
void PrintSystemState() {
printf("*** TIME STEP = %.0f ***\n", tracker->current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
while ((!feof(passengerFile)) && (arrival == tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger;
new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = new_passenger;
// Update the system to reflect newly arrived passenger
tracker->waiting_passengers++;
stops[origin].num_waiting++;
printf("\n\nADDED ARRIVING PASSENGER!\n\n");
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) { // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
free(cur);
bus->number_of_passengers--;
cur = bus->riders;
prev = bus->riders;
} else {
prev->next = cur->next;
free(cur);
bus->number_of_passengers--;
cur = prev->next;
}
} else { // If this branch executes, the current passenger isn't getting off
prev = cur;
cur = cur->next;
}
}
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
// Update the system to reflect loading the passenger on the bus
tracker->waiting_passengers--;
stops[bus->current_stop].num_waiting--;
bus->number_of_passengers++;
}
}
void PrintSummaryInformation() {
printf("Total sim time: %.0f\n", tracker->current_time);
printf("Total passengers: %.0f\n", tracker->total_passengers);
printf("Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
printf("Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
printf("Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 0;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (tracker->current_time < 10 ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
tracker->current_time++;
}
}
void PrintSystemState() {
printf("*** TIME STEP = %.0f ***\n", tracker->current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
while ((!feof(passengerFile)) && (arrival == tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger;
new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = new_passenger;
// Update the system to reflect newly arrived passenger
tracker->waiting_passengers++;
stops[origin].num_waiting++;
printf("\n\nADDED ARRIVING PASSENGER!\n\n");
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) { // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
free(cur);
bus->number_of_passengers--;
cur = bus->riders;
prev = bus->riders;
} else {
prev->next = cur->next;
free(cur);
bus->number_of_passengers--;
cur = prev->next;
}
} else { // If this branch executes, the current passenger isn't getting off
prev = cur;
cur = cur->next;
}
} // Proof-by-skitch: http://briandk.com/screenshots/UnloadPassengersFromBus-20111120-211338.png
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
// Update the system to reflect loading the passenger on the bus
tracker->waiting_passengers--;
stops[bus->current_stop].num_waiting--;
bus->number_of_passengers++;
}
}
void PrintSummaryInformation() {
printf("Total sim time: %.0f\n", tracker->current_time);
printf("Total passengers: %.0f\n", tracker->total_passengers);
printf("Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
printf("Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
printf("Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 1;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (!feof(passengerFile) ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
tracker->current_time++;
}
}
void PrintSystemState() {
printf("*** TIME STEP = %.0f ***\n", tracker->current_time);
printf("Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
printf("Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
while ((!feof(passengerFile)) && (arrival == tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger;
new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = new_passenger;
// Update the system to reflect newly arrived passenger
tracker->waiting_passengers++;
stops[origin].num_waiting++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) { // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
free(cur);
bus->number_of_passengers--;
cur = bus->riders;
prev = bus->riders;
} else {
prev->next = cur->next;
free(cur);
bus->number_of_passengers--;
cur = prev->next;
}
} else { // If this branch executes, the current passenger isn't getting off
prev = cur;
cur = cur->next;
}
} // Proof-by-skitch: http://briandk.com/screenshots/UnloadPassengersFromBus-20111120-211338.png
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
// Update the system to reflect loading the passenger on the bus
tracker->waiting_passengers--;
stops[bus->current_stop].num_waiting--;
bus->number_of_passengers++;
}
}
void PrintSummaryInformation() {
printf("Total sim time: %.0f\n", tracker->current_time);
printf("Total passengers: %.0f\n", tracker->total_passengers);
printf("Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
printf("Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
printf("Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 1;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (!feof(passengerFile) ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
tracker->current_time++;
}
}
void PrintSystemState() {
fprintf(logFile, "*** TIME STEP = %.0f ***\n", tracker->current_time);
fprintf(logFile, "Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
fprintf(logFile, "Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fpos_t prev_pos;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
while ((!feof(passengerFile)) && (arrival == tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger;
new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = new_passenger;
// Update the system to reflect newly arrived passenger
tracker->waiting_passengers++;
stops[origin].num_waiting++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) { // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
free(cur);
bus->number_of_passengers--;
cur = bus->riders;
prev = bus->riders;
} else {
prev->next = cur->next;
free(cur);
bus->number_of_passengers--;
cur = prev->next;
}
} else { // If this branch executes, the current passenger isn't getting off
prev = cur;
cur = cur->next;
}
} // Proof-by-skitch: http://briandk.com/screenshots/UnloadPassengersFromBus-20111120-211338.png
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
// Update the system to reflect loading the passenger on the bus
tracker->waiting_passengers--;
stops[bus->current_stop].num_waiting--;
bus->number_of_passengers++;
}
}
void PrintSummaryInformation() {
fprintf(logFile, "Total sim time: %.0f\n", tracker->current_time);
fprintf(logFile, "Total passengers: %.0f\n", tracker->total_passengers);
fprintf(logFile, "Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
fprintf(logFile, "Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
fprintf(logFile, "Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 1;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (!feof(passengerFile) ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
tracker->current_time++;
}
}
void PrintSystemState() {
fprintf(logFile, "*** TIME STEP = %.0f ***\n", tracker->current_time);
fprintf(logFile, "Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
fprintf(logFile, "Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
while ((!feof(passengerFile)) && (arrival == tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger;
new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = new_passenger;
// Update the system to reflect newly arrived passenger
tracker->waiting_passengers++;
stops[origin].num_waiting++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) { // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
free(cur);
bus->number_of_passengers--;
cur = bus->riders;
prev = bus->riders;
} else {
prev->next = cur->next;
free(cur);
bus->number_of_passengers--;
cur = prev->next;
}
} else { // If this branch executes, the current passenger isn't getting off
prev = cur;
cur = cur->next;
}
} // Proof-by-skitch: http://briandk.com/screenshots/UnloadPassengersFromBus-20111120-211338.png
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
// Update the system to reflect loading the passenger on the bus
tracker->waiting_passengers--;
stops[bus->current_stop].num_waiting--;
bus->number_of_passengers++;
}
}
void PrintSummaryInformation() {
fprintf(logFile, "Total sim time: %.0f\n", tracker->current_time);
fprintf(logFile, "Total passengers: %.0f\n", tracker->total_passengers);
fprintf(logFile, "Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
fprintf(logFile, "Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
fprintf(logFile, "Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w+");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 1;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (!feof(passengerFile) ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
tracker->current_time++;
}
}
void PrintSystemState() {
fprintf(logFile, "*** TIME STEP = %.0f ***\n", tracker->current_time);
fprintf(logFile, "Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
fprintf(logFile, "Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
while ((!feof(passengerFile)) && (arrival == tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger;
new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = new_passenger;
// Update the system to reflect newly arrived passenger
tracker->waiting_passengers++;
stops[origin].num_waiting++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) { // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
free(cur);
bus->number_of_passengers--;
cur = bus->riders;
prev = bus->riders;
} else {
prev->next = cur->next;
free(cur);
bus->number_of_passengers--;
cur = prev->next;
}
} else { // If this branch executes, the current passenger isn't getting off
prev = cur;
cur = cur->next;
}
} // Proof-by-skitch: http://briandk.com/screenshots/UnloadPassengersFromBus-20111120-211338.png
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
// Update the system to reflect loading the passenger on the bus
tracker->waiting_passengers--;
stops[bus->current_stop].num_waiting--;
bus->number_of_passengers++;
}
}
void PrintSummaryInformation() {
fprintf(logFile, "Total sim time: %.0f\n", tracker->current_time);
fprintf(logFile, "Total passengers: %.0f\n", tracker->total_passengers);
fprintf(logFile, "Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
fprintf(logFile, "Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
fprintf(logFile, "Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w+");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 1;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (!feof(passengerFile) ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
tracker->current_time++;
}
}
void PrintSystemState() {
fprintf(logFile, "*** TIME STEP = %.0f ***\n", tracker->current_time);
fprintf(logFile, "Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
fprintf(logFile, "Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
while ((!feof(passengerFile)) && (arrival == tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &origin, &destination, &arrival);
}
if (!feof(passengerFile)) {
printf("\n\nResetting File Position to: %d\tCurrent Time: %.fl", (int) prev_pos, tracker->current_time);
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger;
new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = new_passenger;
// Update the system to reflect newly arrived passenger
tracker->waiting_passengers++;
stops[origin].num_waiting++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) { // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
free(cur);
bus->number_of_passengers--;
cur = bus->riders;
prev = bus->riders;
} else {
prev->next = cur->next;
free(cur);
bus->number_of_passengers--;
cur = prev->next;
}
} else { // If this branch executes, the current passenger isn't getting off
prev = cur;
cur = cur->next;
}
} // Proof-by-skitch: http://briandk.com/screenshots/UnloadPassengersFromBus-20111120-211338.png
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
// Update the system to reflect loading the passenger on the bus
tracker->waiting_passengers--;
stops[bus->current_stop].num_waiting--;
bus->number_of_passengers++;
}
}
void PrintSummaryInformation() {
fprintf(logFile, "Total sim time: %.0f\n", tracker->current_time);
fprintf(logFile, "Total passengers: %.0f\n", tracker->total_passengers);
fprintf(logFile, "Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
fprintf(logFile, "Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
fprintf(logFile, "Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w+");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 1;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (!feof(passengerFile) ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
tracker->current_time++;
}
}
void PrintSystemState() {
fprintf(logFile, "*** TIME STEP = %.0f ***\n", tracker->current_time);
fprintf(logFile, "Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
fprintf(logFile, "Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
while ((!feof(passengerFile)) && (arrival == tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
}
if (!feof(passengerFile)) {
// printf("\n\nResetting File Position to: %d\tCurrent Time: %.fl", (int) prev_pos, tracker->current_time);
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger;
new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = new_passenger;
// Update the system to reflect newly arrived passenger
tracker->waiting_passengers++;
stops[origin].num_waiting++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) { // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
free(cur);
bus->number_of_passengers--;
cur = bus->riders;
prev = bus->riders;
} else {
prev->next = cur->next;
free(cur);
bus->number_of_passengers--;
cur = prev->next;
}
} else { // If this branch executes, the current passenger isn't getting off
prev = cur;
cur = cur->next;
}
} // Proof-by-skitch: http://briandk.com/screenshots/UnloadPassengersFromBus-20111120-211338.png
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
// Update the system to reflect loading the passenger on the bus
tracker->waiting_passengers--;
stops[bus->current_stop].num_waiting--;
bus->number_of_passengers++;
}
}
void PrintSummaryInformation() {
fprintf(logFile, "Total sim time: %.0f\n", tracker->current_time);
fprintf(logFile, "Total passengers: %.0f\n", tracker->total_passengers);
fprintf(logFile, "Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
fprintf(logFile, "Average passenger time: %.2f\n", tracker->total_journey_length / tracker->total_passengers);
fprintf(logFile, "Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w+");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 1;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (!feof(passengerFile) ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
tracker->current_time++;
}
}
void PrintSystemState() {
fprintf(logFile, "*** TIME STEP = %.0f ***\n", tracker->current_time);
fprintf(logFile, "Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
fprintf(logFile, "Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
while ((!feof(passengerFile)) && (arrival == tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
}
if (!feof(passengerFile)) {
// printf("\n\nResetting File Position to: %d\tCurrent Time: %.fl", (int) prev_pos, tracker->current_time);
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger;
new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = new_passenger;
// Update the system to reflect newly arrived passenger
tracker->waiting_passengers++;
stops[origin].num_waiting++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) { // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
free(cur);
bus->number_of_passengers--;
cur = bus->riders;
prev = bus->riders;
} else {
prev->next = cur->next;
free(cur);
bus->number_of_passengers--;
cur = prev->next;
}
} else { // If this branch executes, the current passenger isn't getting off
prev = cur;
cur = cur->next;
}
} // Proof-by-skitch: http://briandk.com/screenshots/UnloadPassengersFromBus-20111120-211338.png
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
if (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
// Update the system to reflect loading the passenger on the bus
tracker->waiting_passengers--;
stops[bus->current_stop].num_waiting--;
bus->number_of_passengers++;
}
}
void PrintSummaryInformation() {
fprintf(logFile, "Total sim time: %.0f\n", tracker->current_time);
fprintf(logFile, "Total passengers: %.0f\n", tracker->total_passengers);
fprintf(logFile, "Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
fprintf(logFile, "Average passenger time: %.2f\n", (tracker->total_journey_length + tracker->total_wait_time) / tracker->total_passengers);
fprintf(logFile, "Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator();
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator() {
passengerFile = fopen("passenger.small.txt", "r");
logFile = fopen("output.txt", "w+");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 1;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (!feof(passengerFile) ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
tracker->current_time++;
}
}
void PrintSystemState() {
fprintf(logFile, "*** TIME STEP = %.0f ***\n", tracker->current_time);
fprintf(logFile, "Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
fprintf(logFile, "Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
while ((!feof(passengerFile)) && (arrival == tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger;
new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = new_passenger;
// Update the system to reflect newly arrived passenger
tracker->waiting_passengers++;
stops[origin].num_waiting++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) { // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
free(cur);
bus->number_of_passengers--;
cur = bus->riders;
prev = bus->riders;
} else {
prev->next = cur->next;
free(cur);
bus->number_of_passengers--;
cur = prev->next;
}
} else { // If this branch executes, the current passenger isn't getting off
prev = cur;
cur = cur->next;
}
} // Proof-by-skitch: http://briandk.com/screenshots/UnloadPassengersFromBus-20111120-211338.png
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
while (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
// Update the system to reflect loading the passenger on the bus
tracker->waiting_passengers--;
stops[bus->current_stop].num_waiting--;
bus->number_of_passengers++;
}
}
void PrintSummaryInformation() {
fprintf(logFile, "Total sim time: %.0f\n", tracker->current_time);
fprintf(logFile, "Total passengers: %.0f\n", tracker->total_passengers);
fprintf(logFile, "Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
fprintf(logFile, "Average passenger time: %.2f\n", (tracker->total_journey_length + tracker->total_wait_time) / tracker->total_passengers);
fprintf(logFile, "Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"
int main (int argc, char const *argv[]) {
InitializeSimulator(argv[1], argv[2]);
RunSimulator();
PrintSummaryInformation();
TearDown();
return 0;
}
void InitializeSimulator(char const *input, char const *output) {
passengerFile = fopen(input, "r");
logFile = fopen(output, "w+");
numberOfStops = GetNumberOfStops(passengerFile);
stops = malloc(numberOfStops * sizeof(struct bus_stop));
bus = malloc(sizeof(struct bus));
bus->current_stop = 0;
bus->number_of_passengers = 0;
bus->riders = NULL;
tracker = malloc(sizeof(struct system_tracker));
int i = 0;
for (i = 0; i < numberOfStops; i++) {
stops[i].num_waiting = 0;
stops[i].waiting = NULL;
}
tracker->current_time = 1;
tracker->total_passengers = 0;
tracker->total_wait_time = 0;
tracker->total_journey_length = 0;
tracker->waiting_passengers = 0;
}
int GetNumberOfStops (FILE *file) {
char number_of_stops[100];
int output;
fgets(number_of_stops, 100, file);
output = atoi(number_of_stops);
return(output);
}
void RunSimulator() {
while (!feof(passengerFile) ||
bus->number_of_passengers > 0 ||
tracker->waiting_passengers > 0
) {
PrintSystemState();
ProcessArrivingPassengers();
UnloadPassengers();
LoadWaitingPassengers(&stops[bus->current_stop]);
bus->current_stop = (bus->current_stop + 1) % numberOfStops;
tracker->current_time++;
}
}
void PrintSystemState() {
fprintf(logFile, "*** TIME STEP = %.0f ***\n", tracker->current_time);
fprintf(logFile, "Bus: Bus Stop %d, passengers %d\n", bus->current_stop, bus->number_of_passengers);
int i = 0;
for (i = 0; i < numberOfStops; i++) {
fprintf(logFile, "Bus Stop %d: waiting %d\n", i, stops[i].num_waiting);
}
}
void ProcessArrivingPassengers() {
int origin = 0;
int destination = 0;
int arrival = 0;
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
while ((!feof(passengerFile)) && (arrival == tracker->current_time)) {;
AddArrivingPassengerToWaiting(origin, destination);
fgetpos(passengerFile, &prev_pos);
fscanf(passengerFile, "%d %d %d", &arrival, &origin, &destination);
}
if (!feof(passengerFile)) {
fsetpos(passengerFile, &prev_pos);
}
}
void AddArrivingPassengerToWaiting(int origin, int destination) {
struct passenger *new_passenger;
new_passenger = (struct passenger*) malloc(sizeof(struct passenger));
new_passenger->origin = origin;
new_passenger->destination = destination;
new_passenger->transit_time = (destination - origin + numberOfStops) % numberOfStops;
new_passenger->wait_time = (origin - bus->current_stop + numberOfStops) % numberOfStops;
new_passenger->next = stops[origin].waiting;
stops[origin].waiting = new_passenger;
// Update the system to reflect newly arrived passenger
tracker->waiting_passengers++;
stops[origin].num_waiting++;
}
void UnloadPassengers() {
struct passenger *cur;
struct passenger *prev;
cur = bus->riders;
prev = bus->riders;
while (cur != NULL) {
if (cur->destination == bus->current_stop) {
// capture passenger stats
tracker->total_journey_length += cur->transit_time;
tracker->total_wait_time += cur->wait_time;
tracker->total_passengers +=1;
if (cur == bus->riders) { // if the current passenger is the head of the riders linked list,
bus->riders = cur->next; // make the next passenger the new head of the riders linked list
free(cur);
bus->number_of_passengers--;
cur = bus->riders;
prev = bus->riders;
} else {
prev->next = cur->next;
free(cur);
bus->number_of_passengers--;
cur = prev->next;
}
} else { // If this branch executes, the current passenger isn't getting off
prev = cur;
cur = cur->next;
}
} // Proof-by-skitch: http://briandk.com/screenshots/UnloadPassengersFromBus-20111120-211338.png
}
void LoadWaitingPassengers(struct bus_stop *current_stop) {
struct passenger *temp_ptr;
while (current_stop->waiting != NULL) {
temp_ptr = current_stop->waiting->next;
current_stop->waiting->next = bus->riders;
bus->riders = current_stop->waiting;
current_stop->waiting = temp_ptr;
// Update the system to reflect loading the passenger on the bus
tracker->waiting_passengers--;
stops[bus->current_stop].num_waiting--;
bus->number_of_passengers++;
}
}
void PrintSummaryInformation() {
fprintf(logFile, "Total sim time: %.0f\n", tracker->current_time);
fprintf(logFile, "Total passengers: %.0f\n", tracker->total_passengers);
fprintf(logFile, "Passenger throughput: %.2f\n", tracker->total_passengers / tracker->current_time);
fprintf(logFile, "Average passenger time: %.2f\n", (tracker->total_journey_length + tracker->total_wait_time) / tracker->total_passengers);
fprintf(logFile, "Average wait time: %.2f\n", tracker->total_wait_time / tracker->total_passengers);
}
void TearDown() {
fclose(passengerFile);
fclose(logFile);
}
|