/* João Pimentel - iaed-23 - ist198951 - project1 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ROUTE_NAME_LENGTH 21 /*Deviam ser 20?*/
#define MAX_STOP_NAME_LENGTH 50
#define MAX_ROUTES 200
#define MAX_STOPS 10000
#define MAX_LINKS 30000
#define BUFSIZE 8192
#define TRUE 1
#define FALSE 0
#define ERROR -1
/* Structure representing a route or circular row */
typedef struct {
char name[MAX_ROUTE_NAME_LENGTH];
int first_stop;
int last_stop;
} Route;
/* Structure representing a stop */
typedef struct {
char name[MAX_STOP_NAME_LENGTH];
double latitude;
double longitude;
} Stop;
/* Structure representing a link between two stops */
typedef struct {
char career[MAX_ROUTE_NAME_LENGTH];
char origin_stop[MAX_STOP_NAME_LENGTH];
char destination_stop[MAX_STOP_NAME_LENGTH];
double cost;
double duration;
} Link;
Route routes[MAX_ROUTES];
Stop stops[MAX_STOPS];
Link links[MAX_LINKS];
char input_list[BUFSIZE];
char cmd;
int num_routes = 0;
int num_stops = 0;
int num_links = 0;
/* Functions to read the input from the user */
int readsNextWord(char str[]) {
char c = getchar();
int i = 0;
while (c == ' ' || c == '\t')
c = getchar();
while (c != ' ' && c != '\t' && c != '\n') {
str[i++] = c;
c = getchar();
}
str[i] = '\0';
return (c == '\n');
}
void readsWordUntilEnd(char str[]) {
char c = getchar();
int i = 0;
while (c == ' ' || c == '\t')
c = getchar();
while (c != '\n') {
str[i++] = c;
c = getchar();
}
str[i] = '\0';
}
void readsUntilEndline() {
char c = getchar();
while (c != '\n')
c = getchar();
}
int findStop(char id[]) { /* If the names match, the function immediately returns the index of the current stop.*/
int i;
for (i = 0; i < num_stops; i++)
if (!strcmp(id, stops[i].name))
return i;
return ERROR;
}
void list_all_routes(){
int i;
for (i = 0; i < num_routes; i++) {
printf("%s %d %.2f %.2f\n", routes[i].name, routes[i].first_stop, stops[findStop(routes[i].name)].latitude, stops[findStop(routes[i].name)].longitude);
}
}
void list_all_routes_reverse(){
int i;
for (int i = num_routes - 1; i >= 0; i--) {
printf("%s %d %.2f %.2f\n", routes[i].name, routes[i].first_stop, stops[findStop(routes[i].name)].latitude, stops[findStop(routes[i].name)].longitude);
}
}
void list_route(char *name){
int i;
for (i = 0; i < MAX_ROUTES; i++) {
if (strcmp(routes[i].name, name) == 0) {
printf("%d", routes[i].first_stop);
}
}
}
int is_inverso_valid(char *str) {
return strcmp(str, "inv") == 0 ||
strcmp(str, "inve") == 0 ||
strcmp(str, "inver") == 0 ||
strcmp(str, "inverso") == 0;
}
int findRoute(char id[]) { /* If the names match, the function immediately returns the index of the current route.*/
int i;
for (i = 0; i < num_routes; i++)
if (!strcmp(id, routes[i].name))
return i;
return ERROR;
}
void list_routes() {
Route r;
int number_of_args;
char reverse[8];
number_of_args = sscanf(input_list, "%c %s %8s",&cmd, r.name, reverse);
if (number_of_args == 1)
list_all_routes();
if (number_of_args == 2){
if (num_routes == MAX_ROUTES) {
printf("Maximum number of routes reached.\n");
return;
}
if (findRoute(r.name) == ERROR) { /*Se não existir uma carreira com este nome,cria*/
strcpy(routes[num_routes].name, r.name);
num_routes++;
}
else{ /*Se existe, imprime o dessa carreira*/
printf("%d %d\n", routes[findRoute(r.name)].first_stop, routes[findRoute(r.name)].last_stop);
}
}
if (number_of_args == 3 && is_inverso_valid(reverse) )
list_all_routes(); /*todo: meter isto por ordem inversa*/
if (number_of_args == 3 && !is_inverso_valid(reverse))
printf("incorrect sort option.\n");
}
void list_all_stops() {
int i;
for (i = 0; i < num_stops; i++) {
printf("%s: %16.12f %16.12f %d\n", stops[i].name, stops[i].latitude, stops[i].longitude, num_routes);
}
}
void list_stops() {
Stop s;
int number_of_args;
number_of_args = sscanf(input_list, "%c %s %lf %lf",&cmd, s.name, &s.latitude, &s.longitude);
if (s.name[0] == '"')
number_of_args = sscanf(input_list, "%c \"%50[^\"]\" %lf %lf",&cmd, s.name, &s.latitude, &s.longitude);
if (number_of_args == 1){
list_all_stops();
}
if (number_of_args == 2){
if (findStop(s.name) == ERROR)
printf("%s: no such stop.\n", s.name);
else
printf("%16.12f %16.12f\n", stops[findStop(s.name)].latitude, stops[findStop(s.name)].longitude);
}
if (number_of_args == 4){
/*Creates a new stop if it is not created yet*/
if (findStop(s.name) != ERROR){
printf("%s: stop already exists.\n", s.name);
return;
}
if (num_stops == MAX_STOPS) {
printf("Maximum number of stops reached.\n");
return;
}
strcpy(stops[num_stops].name, s.name);
stops[num_stops].latitude = s.latitude;
stops[num_stops].longitude = s.longitude;
num_stops++;
}
}
void addsLinks() {
Link l;
sscanf(input_list, "%c %s %s %s %lf %lf",&cmd, l.career, l.origin_stop, l.destination_stop, &l.cost, &l.duration);
if (l.origin_stop[0] == '"')
sscanf(input_list, "%c %s \"%50[^\"]\" %s %lf %lf",&cmd, l.career, l.origin_stop, l.destination_stop, &l.cost, &l.duration);
if (l.destination_stop[0] == '"')
sscanf(input_list, "%c %s %s \"%50[^\"]\" %lf %lf",&cmd, l.career, l.origin_stop, l.destination_stop, &l.cost, &l.duration);
if (l.origin_stop[0] == '"' && l.destination_stop[0] == '"')
sscanf(input_list, "%c %s \"%50[^\"]\" \"%50[^\"]\" %lf %lf",&cmd, l.career, l.origin_stop, l.destination_stop, &l.cost, &l.duration);
printf("1st stop:%s 2nd stop:%s\n", l.origin_stop,l.destination_stop);
if (findRoute(l.career) == ERROR)
printf("%s: no such line.\n", l.career);
if (findStop(l.origin_stop) == ERROR)
printf("%s: no such stop.\n", l.origin_stop);
else if (findStop(l.destination_stop) == ERROR)
printf("%s: no such stop.\n", l.destination_stop);
else if (l.cost < 0 || l.duration < 0)
printf("negative cost or duration.");
else {
strcpy(links[num_links].career, l.career);
strcpy(links[num_links].origin_stop, l.origin_stop);
strcpy(links[num_links].destination_stop, l.destination_stop);
links[num_links].cost = l.cost;
links[num_links].duration = l.duration;
num_links++;
}
}
void listsConnections(){
/*TODO: NOT IMPLEMENTED YET*/
}
int main() {
while (fgets(input_list, BUFSIZE, stdin) != NULL) {
if (strlen(input_list) > 0 && input_list[strlen(input_list) - 1] == '\n') {
input_list[strlen(input_list) - 1] = '\0'; /* adiciona o 0 ao final da lista*/
}
sscanf(input_list, "%c", &cmd);
switch (cmd) {
case 'q':
return 0;
case 'c': list_routes();
break;
case 'p': list_stops();
break;
case 'l': addsLinks();
break;
case 'i': listsConnections();
break;
}
}
return 0;
}