Untitled

 avatar
unknown
plain_text
2 years ago
6.8 kB
4
Indexable
/* 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];

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();
}

void list_all_routes(){
	int i;
	for (i = 0; i < num_routes; i++) {
			printf("%s: from %s to %s\n", routes[i].name, stops[routes[i].first_stop].name, stops[routes[i].last_stop].name);
		}
}

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;
}

void list_routes() {
	Route r;
    char reverse[20];
	if (readsNextWord(r.name) != 1){
		if (readsNextWord(reverse) != 1 && is_inverso_valid(reverse)) 
			list_all_routes();
			/*imprime as paragens por ordem inversa*/
		else if (!is_inverso_valid(reverse)) 
			printf("incorrect sort option.\n");
			/*imprime pela ordem normal*/ 
	else 
		list_all_routes();
	}
}

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;
}

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_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 add_route(char *arg) {
    char name[MAX_ROUTE_NAME_LENGTH];
    int first_stop, last_stop;
    
    if (strlen(arg) == 0) {
        printf("Invalid route name.\n");
        return;
    }

    if (num_routes == MAX_ROUTES) {
        printf("Maximum number of routes reached.\n");
        return;
    }

    first_stop = num_stops;
    last_stop = num_stops;

    routes[num_routes].first_stop = first_stop;
    routes[num_routes].last_stop = last_stop;

    strcpy(routes[num_routes].name, name);

    num_routes++;

    printf("Route added successfully.\n");
}

void list_stops() { /*TODO: adicionar aqui o caso em que insere latitude e longitude*/
    Stop s;
	char latitude[100];
	char longitude[100];
	int number_of_args;
	number_of_args = sscanf(input_list, "%s %lf %lf", s.name, &s.latitude, &s.longitude);
	if (number_of_args == 0){
	 	printf("%s\n", s.name);
		printf("ai entrou\n");
		list_all_stops();
	}
	if (number_of_args == 1){ /*se só der um argumento -> apenas dá a latitude e longitude*/
		if (findStop(s.name) == ERROR)
			printf("%s: no such stop.\n", s.name);
		else
			printf("%f %f\n", stops[findStop(s.name)].latitude, stops[findStop(s.name)].longitude);
	}
	if (number_of_args == 3){ 
		printf("%s\n", latitude);
		printf("%s\n", longitude);
		/*cria nova paragem com os dados fornecidos*/ 
		strcpy(stops[num_stops].name, s.name);
		stops[num_stops].latitude = s.latitude;
		stops[num_stops].longitude = s.longitude;		
	}
}

void addsLinks() {
	Link l;
    char cost[100];
    char duration[100];

	readsNextWord(l.career);
	readsNextWord(l.origin_stop);
	readsNextWord(l.destination_stop);
    readsNextWord(cost);
    l.cost = atof(cost);
    readsNextWord(duration);
    l.duration = atof(duration); 

	if (findRoute(l.career) == ERROR)
		printf("%s: no such line.\n", l.career);
	else 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() {
	int c;
	while ((c = getchar()) != EOF) {
		switch (c) {
		case 'q': 
			return 0;
		case 'c': list_routes();
			break;
		case 'p': list_stops();
			break;
		case 'l': addsLinks();
			break;
		case 'i': listsConnections();
			break;

		}
	}

	return 0;
}

int main() {
	char cmd;

	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);
		memmove(input_list, input_list + 1, strlen(input_list));
		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;
}
Editor is loading...