daily pastebin goal
82%
SHARE
TWEET

Untitled

a guest Apr 21st, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "rally.h"
  5.  
  6. void free_array(char **array) {
  7.     int i = 0;
  8.  
  9.     while (array[i] != NULL) {
  10.         free(array[i]);
  11.         i++;
  12.     }
  13.  
  14.     free(array);
  15. }
  16.  
  17. void delete_all_drivers(struct list *drivers_list) {
  18.     if (drivers_list) {
  19.         while(delete_driver(drivers_list));
  20.         free(drivers_list);
  21.     }
  22. }
  23.  
  24. int delete_driver(struct list *drivers_list) {
  25.  
  26.     if (drivers_list->first == NULL)            // List is empty
  27.         return 0;
  28.  
  29.     if (drivers_list->first == drivers_list->last) {       //Only one member in the list
  30.  
  31.         free(drivers_list->first->name);
  32.         free(drivers_list->first->team);
  33.         free(drivers_list->first);
  34.         drivers_list->first = NULL;
  35.         drivers_list->last = NULL;
  36.  
  37.         return 1;
  38.     }
  39.  
  40.     else {                          // 2 or more members in the list              
  41.  
  42.         struct driver *qfirst;
  43.         struct driver *newhead;
  44.  
  45.         qfirst = drivers_list->first;
  46.         newhead = drivers_list->first->next;
  47.         free(qfirst->name);
  48.         free(qfirst->team);
  49.         free(qfirst);
  50.         drivers_list->first = newhead;
  51.  
  52.         return 1;
  53.     }
  54.  
  55.     return 0;
  56.    
  57. }
  58.  
  59.  
  60. int print_drivers_list(struct list *drivers_list) {
  61.     struct driver *current;
  62.     current = drivers_list->first;
  63.     if (current == NULL) {
  64.         return(0);
  65.     }
  66.     while(current != NULL) {
  67.         printf("Name: %s Team: %s Hours: %d Minutes: %d Seconds: %d\n", current->name, current->team, current->hours, current->minutes, current->seconds);
  68.         current = current->next;
  69.     }
  70.     return 1;
  71. }
  72.  
  73. int add_new_driver(struct list *drivers_list, char* name, char* team) {
  74.  
  75.     printf("Eka\n");
  76.     struct driver *current;
  77.     printf("Eka1\n");
  78.     current = drivers_list->first;
  79.     printf("Eka2\n");
  80.     while(current != NULL) {            
  81.         if (strcmp(current->name, name) == 0) {
  82.             printf("Eka3\n");
  83.             printf("Toka\n");
  84.             return 0;
  85.         }
  86.         current = current->next;
  87.  
  88.     }
  89.     printf("Kolmas\n");
  90.     struct driver *newdriver;
  91.  
  92.     newdriver = malloc(sizeof(struct driver));
  93.     newdriver->name = malloc((strlen(name) + 1) * sizeof(char));
  94.     strcpy(newdriver->name, name);
  95.     newdriver->team = malloc((strlen(team) + 1) * sizeof(char));
  96.     strcpy(newdriver->team, team);
  97.  
  98.     printf("Nelj√§s\n");
  99.     newdriver->hours = 0;
  100.     newdriver->minutes = 0;
  101.     newdriver->seconds = 0;
  102.     printf("Viides\n");
  103.  
  104.  
  105.     if (drivers_list->first == NULL) {
  106.         drivers_list->first = newdriver;
  107.         drivers_list->last = newdriver;
  108.     }
  109.  
  110.     else {
  111.         drivers_list->last->next = newdriver;
  112.         drivers_list->last = newdriver;
  113.     }
  114.  
  115.     newdriver->next = NULL;
  116.     return 1;
  117. }
  118.  
  119. char** token_reader(char buffer[], char **array_1) {
  120.     free_array(array_1);
  121.  
  122.     char **array;
  123.     array = malloc(sizeof(char *));
  124.     *array = NULL;
  125.  
  126.  
  127.     int i = 0;
  128.     char* token;
  129.  
  130.     token = strtok(buffer, " ");
  131.     printf("1. token:%s\n", token);
  132.  
  133.     while(token != NULL) {
  134.  
  135.         array = realloc(array, (i + 1) * sizeof(char*));
  136.         array[i] = malloc((strlen(token) + 1) * sizeof(char));
  137.         strcpy(array[i], token);
  138.         token = strtok(NULL, " ");
  139.         i++;
  140.         printf("%d. token:%s\n", i+1, token);
  141.         }
  142.  
  143.     // array = realloc(array, (i + 1) * sizeof(char*));
  144.     // array[i] = malloc((strlen("STOP") + 1) * sizeof(char));
  145.     // strcpy(array[i], "STOP");
  146.  
  147.     return array;
  148.  
  149. }
  150.  
  151. int main() {
  152.  
  153.     struct list *drivers_list = calloc(1, sizeof(struct driver));
  154.     char buffer[100] = {"\0"};
  155.    
  156.     char **array;
  157.     array = malloc(sizeof(char *));
  158.     *array = NULL;
  159.  
  160.     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,"
  161.         " 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" 
  162.         "4. Write: W filename, to save Rally-statistics to file.\n5. Write: 0 filename, to download previous Rally-statistics.\n"
  163.         "6. Write: exit, to quit.\n");
  164.  
  165.     while(1) {
  166.         fflush(stdin);
  167.  
  168.         // char **array;
  169.         // array = malloc(sizeof(char *));
  170.   //    *array = NULL;
  171.  
  172.  
  173.         fgets(buffer, 100, stdin);
  174.         printf("Buffer:%s", buffer);
  175.         if (strcmp(buffer, "exit\n") == 0)  {
  176.             free_array(array);
  177.             break;
  178.         }
  179.  
  180.         array = token_reader(buffer, array);
  181.        
  182.         if (strcmp(array[0], "A") == 0) {
  183.             printf("A-testi\n");
  184.             if(add_new_driver(drivers_list, array[1], array[2]))
  185.                 printf("Driver %s added to database\n", array[1]);
  186.             else
  187.                 printf("Adding failed = driver already exists\n");
  188.         }
  189.  
  190.         if (strcmp(array[0], "L\n") == 0) {
  191.             printf("Printti-testi\n");
  192.             if(print_drivers_list(drivers_list))
  193.                 printf("Printing complete\n");
  194.             else
  195.                 printf("List is empty\n");
  196.         }
  197.         // free_array(array);
  198.  
  199.     } //while
  200.  
  201.     delete_all_drivers(drivers_list);
  202.  
  203.     printf("Program closed, Have a nice day\n");
  204.     return 0;
  205. } //main
RAW Paste Data
Top