Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. struct _element {
  7. char icao_code[5];
  8. char station_name[100];
  9. };
  10.  
  11. typedef struct _element element;
  12.  
  13. void insertion_sort(element* stations, int size) {
  14. // implement either insertion_sort or selection_sort
  15. if (size <= 1) {
  16. return;
  17. }
  18. insertion_sort(stations, size-1);
  19. printf("\n");
  20. char last[5];
  21. strcpy(last,stations[size-1].icao_code);
  22. char letzte[100];
  23. strcpy(letzte,stations[size-1].station_name);
  24. int j = size-2;
  25. while (j >= 0 && strcmp(stations[j].icao_code,last) > 0){
  26. stations[j+1] = stations[j];
  27. j--;
  28. }
  29. strcpy(stations[j+1].icao_code, last);
  30. strcpy(stations[j+1].station_name,letzte);
  31. }
  32. void swap(element* a, element* b){
  33. element t = *a;
  34. *a = *b;
  35. *b = t;
  36. }
  37.  
  38. void swaper(void *a, void *b, size_t size){
  39. void *temp = malloc(size);
  40. memcpy(temp,a,size);
  41. memcpy(a,b,size);
  42. memcpy(b,temp,size);
  43. free(temp);
  44. }
  45.  
  46. int comp (void *p, void *q){
  47. return strcmp(((struct _element *)p)->icao_code,((struct _element *)q)->icao_code);
  48. }
  49.  
  50. void insertion_sort2(void* data, int nitems, int size, int (*comp) (void *, void*)){
  51. char *carray = (char *)data;
  52. int j;
  53. for(int i = 0; i < nitems; i++){
  54. j = i;
  55. while(j > 0 && comp(&carray[j * size], &carray[(j - 1) * size]) < 0){
  56. swaper(&carray[j * size], &carray[(j - 1) * size], size);
  57. j--;
  58. }
  59. }
  60. };
  61.  
  62. void selection_sort(element* stations, int size) {}
  63.  
  64.  
  65. int partition(element * stations, int left, int right){
  66. char pivot[5];
  67. strcpy(pivot,stations[right].icao_code);
  68. int i = left - 1;
  69. for(int j = left; j <= right-1; j++){
  70. if(strcmp(pivot,stations[j].icao_code) > 0){
  71. i++;
  72. swap(&stations[i],&stations[j]);
  73. }
  74. }
  75. swap(&stations[i+1],&stations[right]);
  76.  
  77. return (i+1);
  78. }
  79.  
  80. void quick_sort(element* stations, int left, int right) {
  81. // implement either quick_sort or merge_sort
  82. if(left < right){
  83. int pi = partition(stations, left, right);
  84. quick_sort(stations,left,pi-1);
  85. quick_sort(stations,pi+1,right);
  86. }
  87. }
  88.  
  89.  
  90.  
  91. void merge_sort(element* stations, element* tmpStations, int left, int right) {
  92. // implement either quick_sort or merge_sort
  93. // tmpStations corresponds to the array B in the lecture slides
  94. }
  95.  
  96. void print_stations(element* stations, int size) {
  97. int i;
  98. for (i = 0; i < size; i++) {
  99. printf("%s : %s", stations[i].icao_code, stations[i].station_name);
  100. }
  101. }
  102.  
  103. int readfile(element* stations, int* size) {
  104. FILE * fp;
  105. char line[100]; // keeps one line of the file
  106.  
  107. // try to open the file; in case of an error exit the program
  108. *size = 0;
  109. fp = fopen("stations.csv", "r");
  110. if (!fp) {
  111. printf("Cannot open file stations.csv!\n");
  112. return 1;
  113. }
  114.  
  115. // read all weather stations from file and add them to the
  116. // hash table
  117. while (!feof(fp)) {
  118. // read one line and skip empty and comment lines
  119. fgets(line, 100, fp);
  120. if (line[0] == '#') continue;
  121. if (isspace(line[0])) continue;
  122.  
  123. // take the first 4 characters as key value
  124. strncpy(stations[*size].icao_code, line, 4);
  125. stations[*size].icao_code[4] = '\0';
  126.  
  127. // take the rest as value
  128. strcpy(stations[*size].station_name, strchr(line, ';')+1);
  129. *size += 1;
  130. }
  131.  
  132. // close the file
  133. fclose(fp);
  134.  
  135. return 0;
  136. }
  137.  
  138. int main(int argc, char** argv) {
  139. element* stations; // holds the station codes and names
  140. element* tmpStations; // holds temporary station codes and names for merge sort
  141. int size; // holds the number of valid entries in the array
  142.  
  143. stations = (element*)malloc(sizeof(element)*6000);
  144. tmpStations = (element*)malloc(sizeof(element)*6000);
  145.  
  146. // now lets try insertion sort
  147. //printf("Sorting with insertion sort:\n");
  148. // read the station data from file
  149. //readfile(stations, &size);
  150. // sort the station names
  151. //insertion_sort(stations, size);
  152. // print the result
  153. //print_stations(stations, size);
  154.  
  155. // now lets try selection sort
  156. //printf("Sorting with selection sort:\n\n");
  157. // read the station data from file
  158. //readfile(stations, &size);
  159. // sort the station names
  160. //selection_sort(stations, size);
  161. // print the result
  162. //print_stations(stations, size);
  163.  
  164. // now lets try merge sort
  165. //printf("Sorting with merge sort:\n\n");
  166. // read the station data from file
  167. //readfile(stations, &size);
  168. // sort the station names
  169. //merge_sort(stations, tmpStations, 0, size-1);
  170. // print the result
  171. //print_stations(stations, size);
  172.  
  173. // now lets try quick sort
  174. //printf("Sorting with quick sort:\n\n");
  175. // read the station data from file
  176. //readfile(stations, &size);
  177. // sort the station names
  178. //quick_sort(stations, 0, size-1);
  179. // print the result
  180. //print_stations(stations, size);
  181.  
  182.  
  183. // now lets try insertion sort with Pointer
  184. printf("Sorting with Insertion Sort Pointer:\n\n");
  185. readfile(stations, &size);
  186. insertion_sort2(stations,size, sizeof(element),comp);
  187. print_stations(stations,size);
  188.  
  189. free(stations);
  190. free(tmpStations);
  191.  
  192. return 0;
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement