Untitled

 avatar
unknown
plain_text
2 years ago
9.5 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 stop */
typedef struct {

	char name[MAX_STOP_NAME_LENGTH];

	double latitude;

	double longitude;

} Stop;


/* Structure representing a route*/
typedef struct {

	char name[MAX_ROUTE_NAME_LENGTH];

	char first_stop[MAX_STOP_NAME_LENGTH];

	int route_num_stops;

	double total_cost;
	 
	double total_duration;

	char last_stop[MAX_STOP_NAME_LENGTH];

} Route;



/* Structure representing a link*/
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;



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++) {
			if (routes[i].route_num_stops == 0)
				printf("%s %d %.2f %.2f\n", routes[i].name, routes[i].route_num_stops, routes[i].total_cost, routes[i].total_duration);
			else 
				printf("%s %s %s %d %.2f %.2f\n", routes[i].name, routes[i].first_stop, routes[i].last_stop , routes[i].route_num_stops, routes[i].total_cost, routes[i].total_duration);
		}

}

void list_all_routes_reverse(){

        int i;
        for (i = num_routes - 1; i >= 0; i--) {
            printf("%s %s %.2f %.2f\n", routes[i].name, routes[i].first_stop, stops[findStop(routes[i].name)].latitude, stops[findStop(routes[i].name)].longitude);
        }
}


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) == 0)
			return i;
	}

	return ERROR;

}


void list_routes() {

	Route r;

	int number_of_args, i, counter = 0;

	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*/
			for (i = 0; i < num_links; i++) {
				if (strcmp(r.name, links[i].career) == 0) {
					if (counter == 0){
						printf("%s, %s", links[i].origin_stop, links[i].destination_stop);
						counter++;
					} else {
						printf(", %s", links[i].destination_stop);
						counter++;
					}
				}
			}
			printf("\n");
		}
		
	}

	if (number_of_args == 3 && is_inverso_valid(reverse) )
		list_all_routes(); 

	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);
			return;
		}
		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 addLinkToStart(Link link){
	int i;
	if (num_links == MAX_LINKS)
		return;
	for (i = num_links - 1; i >= 0; i--) {
            links[i + 1] = links[i];
	}
	strcpy(links[0].origin_stop, link.origin_stop);
	strcpy(links[0].destination_stop, link.destination_stop);
	strcpy(links[0].career, link.career);
	links[0].cost = link.cost;
	links[0].duration = link.duration;
	num_links++;
}

void addsLinks() {
	Link l;
	int num_to_add, index;

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

	if (findRoute(l.career) == ERROR){
		printf("%s: no such line.\n", l.career);
		return ;
	}

	if (findStop(l.origin_stop) == ERROR){
		printf("%s: no such stop.\n", l.origin_stop);
		return ;
	}

	if (findStop(l.destination_stop) == ERROR){
		printf("%s: no such stop.\n", l.destination_stop);
		return ;
	}

	index = findRoute(l.career);
	
	/*No caso das paragens da ligação, origem ou destino, não correspoderem a um dos extremos da carreira.*/
	if ((strcmp(l.origin_stop, routes[findRoute(l.career)].first_stop) == 0 && strcmp(l.destination_stop, routes[findRoute(l.career)].last_stop) != 0) && routes[findRoute(l.career)].route_num_stops != 0) {
		printf("link cannot be associated with bus line.\n"); 
		return ;
	}
	
	if (strcmp(l.destination_stop, routes[findRoute(l.career)].last_stop) == 0 && strcmp(l.origin_stop, routes[findRoute(l.career)].first_stop) != 0 && routes[findRoute(l.career)].route_num_stops != 0) {
		printf("link cannot be associated with bus line.\n"); 
		return ;
	}	

	if (l.cost < 0 || l.duration < 0){ 
		printf("negative cost or duration.\n");
		return ;
	}

	else {
			num_to_add = 1;
			if (strcmp(routes[findRoute(l.career)].first_stop, l.origin_stop) != 0 && strcmp(routes[findRoute(l.career)].last_stop, l.destination_stop) != 0 && routes[findRoute(l.career)].route_num_stops == 0) {
				strcpy(routes[index].first_stop, l.origin_stop);
				strcpy(routes[index].last_stop, l.origin_stop);
				num_to_add = 2;
			} 

			if (strcmp(routes[findRoute(l.career)].first_stop, l.origin_stop) == 0 && strcmp(routes[findRoute(l.career)].last_stop, l.destination_stop) != 0 && routes[findRoute(l.career)].route_num_stops != 0) {
				strcpy(routes[index].last_stop, l.origin_stop);
			}				

			if (strcmp(routes[findRoute(l.career)].last_stop, l.origin_stop) == 0){
				strcpy(routes[findRoute(l.career)].last_stop, l.destination_stop);
			}
			
			if ((strcmp(routes[findRoute(l.career)].first_stop, l.destination_stop)) == 0 ){
				strcpy(routes[index].first_stop, l.origin_stop);
				addLinkToStart(l);
				routes[findRoute(l.career)].total_cost += l.cost;
				routes[findRoute(l.career)].total_duration += l.duration;
				routes[findRoute(l.career)].route_num_stops += num_to_add;
				return;
			}

			strcpy(links[num_links].origin_stop, l.origin_stop);
			strcpy(links[num_links].destination_stop, l.destination_stop);
			strcpy(links[num_links].career, l.career);
			

			links[num_links].cost = l.cost;
			links[num_links].duration = l.duration;
			num_links++;
			
			routes[findRoute(l.career)].total_cost += l.cost;
			routes[findRoute(l.career)].total_duration += l.duration;
			routes[findRoute(l.career)].route_num_stops += num_to_add;
		}
}





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;

}
Editor is loading...