Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "rally.h"
- void free_array(char **array) {
- int i = 0;
- while (array[i] != NULL) {
- free(array[i]);
- i++;
- }
- free(array);
- }
- void delete_all_drivers(struct list *drivers_list) {
- if (drivers_list) {
- while(delete_driver(drivers_list));
- free(drivers_list);
- }
- }
- int delete_driver(struct list *drivers_list) {
- if (drivers_list->first == NULL) // List is empty
- return 0;
- if (drivers_list->first == drivers_list->last) { //Only one member in the list
- free(drivers_list->first->name);
- free(drivers_list->first->team);
- free(drivers_list->first);
- drivers_list->first = NULL;
- drivers_list->last = NULL;
- return 1;
- }
- else { // 2 or more members in the list
- struct driver *qfirst;
- struct driver *newhead;
- qfirst = drivers_list->first;
- newhead = drivers_list->first->next;
- free(qfirst->name);
- free(qfirst->team);
- free(qfirst);
- drivers_list->first = newhead;
- return 1;
- }
- return 0;
- }
- int print_drivers_list(struct list *drivers_list) {
- struct driver *current;
- current = drivers_list->first;
- if (current == NULL) {
- return(0);
- }
- while(current != NULL) {
- printf("Name: %s Team: %s Hours: %d Minutes: %d Seconds: %d\n", current->name, current->team, current->hours, current->minutes, current->seconds);
- current = current->next;
- }
- return 1;
- }
- int add_new_driver(struct list *drivers_list, char* name, char* team) {
- printf("Eka\n");
- struct driver *current;
- printf("Eka1\n");
- current = drivers_list->first;
- printf("Eka2\n");
- while(current != NULL) {
- if (strcmp(current->name, name) == 0) {
- printf("Eka3\n");
- printf("Toka\n");
- return 0;
- }
- current = current->next;
- }
- printf("Kolmas\n");
- struct driver *newdriver;
- newdriver = malloc(sizeof(struct driver));
- newdriver->name = malloc((strlen(name) + 1) * sizeof(char));
- strcpy(newdriver->name, name);
- newdriver->team = malloc((strlen(team) + 1) * sizeof(char));
- strcpy(newdriver->team, team);
- printf("Neljäs\n");
- newdriver->hours = 0;
- newdriver->minutes = 0;
- newdriver->seconds = 0;
- printf("Viides\n");
- if (drivers_list->first == NULL) {
- drivers_list->first = newdriver;
- drivers_list->last = newdriver;
- }
- else {
- drivers_list->last->next = newdriver;
- drivers_list->last = newdriver;
- }
- newdriver->next = NULL;
- return 1;
- }
- char** token_reader(char buffer[], char **array_1) {
- free_array(array_1);
- char **array;
- array = malloc(sizeof(char *));
- *array = NULL;
- int i = 0;
- char* token;
- token = strtok(buffer, " ");
- printf("1. token:%s\n", token);
- while(token != NULL) {
- array = realloc(array, (i + 1) * sizeof(char*));
- array[i] = malloc((strlen(token) + 1) * sizeof(char));
- strcpy(array[i], token);
- token = strtok(NULL, " ");
- i++;
- printf("%d. token:%s\n", i+1, token);
- }
- // array = realloc(array, (i + 1) * sizeof(char*));
- // array[i] = malloc((strlen("STOP") + 1) * sizeof(char));
- // strcpy(array[i], "STOP");
- return array;
- }
- int main() {
- struct list *drivers_list = calloc(1, sizeof(struct driver));
- char buffer[100] = {"\0"};
- char **array;
- array = malloc(sizeof(char *));
- *array = NULL;
- printf(" *** Welcome to Rally-Statistics Program ***\n\n1. Write: A Lastname Team, to add a new driver to database\n2. Write: U Lastname hours minutes seconds,"
- " to update the situation of a driver.\n *Note that 72 minutes is 1 hour and 12 minutes.\n3. Write: L, to print current situation of the rally.\n"
- "4. Write: W filename, to save Rally-statistics to file.\n5. Write: 0 filename, to download previous Rally-statistics.\n"
- "6. Write: exit, to quit.\n");
- while(1) {
- fflush(stdin);
- // char **array;
- // array = malloc(sizeof(char *));
- // *array = NULL;
- fgets(buffer, 100, stdin);
- printf("Buffer:%s", buffer);
- if (strcmp(buffer, "exit\n") == 0) {
- free_array(array);
- break;
- }
- array = token_reader(buffer, array);
- if (strcmp(array[0], "A") == 0) {
- printf("A-testi\n");
- if(add_new_driver(drivers_list, array[1], array[2]))
- printf("Driver %s added to database\n", array[1]);
- else
- printf("Adding failed = driver already exists\n");
- }
- if (strcmp(array[0], "L\n") == 0) {
- printf("Printti-testi\n");
- if(print_drivers_list(drivers_list))
- printf("Printing complete\n");
- else
- printf("List is empty\n");
- }
- // free_array(array);
- } //while
- delete_all_drivers(drivers_list);
- printf("Program closed, Have a nice day\n");
- return 0;
- } //main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement