Guest User

Untitled

a guest
Jan 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <ctype.h>
  6.  
  7. #define KEY_MAX_LEN 10
  8. #define VALUE_MAX_LEN 64
  9.  
  10. /* Магические "+ 1" в объявлении чаровых массивов -- не магические, они под '\0' символ */
  11. typedef struct {
  12. char s[KEY_MAX_LEN + 1];
  13. char n[KEY_MAX_LEN + 1];
  14. char value[VALUE_MAX_LEN + 1];
  15. } data;
  16.  
  17. typedef struct {
  18. data * values;
  19. int sz, cap;
  20. } vector;
  21.  
  22. /* Создание вектора */
  23. int vectorCreate(vector * v) {
  24. if(v == NULL) return 1;
  25.  
  26. v->sz = 0;
  27. v->cap = 8;
  28. v->values = (data *) malloc(v->cap * sizeof(data));
  29.  
  30. if(v->values == NULL) return 1;
  31.  
  32. return 0;
  33. }
  34.  
  35. /* Добавление в вектор */
  36. int vectorAdd(vector * v, data val) {
  37. if(v == NULL) return 1;
  38.  
  39. if(v->sz == v->cap) {
  40. v->cap = 1 + (v->cap * 3) / 2;
  41.  
  42. v->values = (data *) realloc(v->values, v->cap * sizeof(data));
  43.  
  44. if(v->values == NULL) return 1;
  45. }
  46.  
  47. v->values[(v->sz)++] = val;
  48.  
  49. return 0;
  50. }
  51.  
  52. /* Очищаем вектор */
  53. void vectorErase(vector * v) {
  54. free(v->values);
  55. }
  56.  
  57. int main() {
  58. /* Статус окочания считывания */
  59. bool notEnded = true;
  60.  
  61. /* Структура для считывания данных */
  62. data newData;
  63.  
  64. /* Вектор для хранения данных */
  65. vector vectorWithData;
  66.  
  67. /* Инициализируем вектор */
  68. if(vectorCreate(&vectorWithData)) {
  69. perror("ERROR");
  70. exit(0);
  71. }
  72.  
  73. /* Массивы для сортировки */
  74. int count[10];
  75. int * oldIds = NULL;
  76. int * newIds = NULL;
  77.  
  78. /* Переменные для парсинга даты */
  79. int day, month, year;
  80.  
  81. while(notEnded) {
  82. /* Считаем две строчки */
  83. if(scanf("%10s%*1c%64[^\n]", newData.s, newData.value) != 2) {
  84. notEnded = false;
  85. continue;
  86. }
  87.  
  88. /* Выпарсиваем три числа */
  89. if(sscanf(newData.s, "%d.%d.%d", &day, &month, &year) != 3) {
  90. fprintf(stderr, "ERROR: Wrong key format\n");
  91. exit(0);
  92. }
  93.  
  94. /* Проверяем числа */
  95. if(day < 0 || day >= 100 || month < 0 || month >= 100 || year < 0 || year >= 10000) {
  96. fprintf(stderr, "ERROR: Wrong key format\n");
  97. exit(0);
  98. }
  99.  
  100. /* Записываем их в строчку */
  101. if(sprintf(newData.n, "%.4d%.2d%.2d", year, month, day) != 8) {
  102. fprintf(stderr, "ERROR: Wrong key format\n");
  103. exit(0);
  104. }
  105.  
  106. /* Добавляем в вектор */
  107. if(vectorAdd(&vectorWithData, newData)) {
  108. perror("ERROR");
  109. exit(0);
  110. }
  111. }
  112.  
  113. int totalElements = vectorWithData.sz;
  114.  
  115. /* Выделяем два массива айдишников, чтобы не лопатить вектор */
  116. oldIds = (int *) malloc(totalElements * sizeof(int));
  117. newIds = (int *) malloc(totalElements * sizeof(int));
  118.  
  119. /* Чекаем память */
  120. if(oldIds == NULL || newIds == NULL) {
  121. perror("ERROR");
  122. exit(0);
  123. }
  124.  
  125. /* Заполняем oldIds дефолтными значениями */
  126. for(int i = 0; i < totalElements; i++) {
  127. oldIds[i] = i;
  128. }
  129.  
  130. /* Сортируем */
  131. for(int i = 7; i >= 0; i--) {
  132. memset(count, 0, 10 * sizeof(int));
  133.  
  134. for(int j = 0; j < totalElements; j++) {
  135. int curId = oldIds[j];
  136. int curDigit = vectorWithData.values[curId].n[i] - '0';
  137. count[curDigit]++;
  138. }
  139.  
  140. for(int j = 1; j < 10; j++) {
  141. count[j] += count[j - 1];
  142. }
  143.  
  144. for(int j = totalElements - 1; j >= 0; j--) {
  145. int curId = oldIds[j];
  146. int curDigit = vectorWithData.values[curId].n[i] - '0';
  147. newIds[--count[curDigit]] = curId;
  148. }
  149.  
  150. memcpy(oldIds, newIds, totalElements * sizeof(int));
  151. }
  152.  
  153. /* Выводим */
  154. for(int i = 0; i < totalElements; i++) {
  155. int curId = oldIds[i];
  156.  
  157. printf("%s\t%s\n", vectorWithData.values[curId].s, vectorWithData.values[curId].value);
  158. }
  159.  
  160. /* Очищаем память */
  161. free(oldIds);
  162. free(newIds);
  163. vectorErase(&vectorWithData);
  164.  
  165. return 0;
  166. }
Add Comment
Please, Sign In to add comment