Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- #include <ctype.h>
- #define KEY_MAX_LEN 10
- #define VALUE_MAX_LEN 64
- /* Магические "+ 1" в объявлении чаровых массивов -- не магические, они под '\0' символ */
- typedef struct {
- char s[KEY_MAX_LEN + 1];
- char n[KEY_MAX_LEN + 1];
- char value[VALUE_MAX_LEN + 1];
- } data;
- typedef struct {
- data * values;
- int sz, cap;
- } vector;
- /* Создание вектора */
- int vectorCreate(vector * v) {
- if(v == NULL) return 1;
- v->sz = 0;
- v->cap = 8;
- v->values = (data *) malloc(v->cap * sizeof(data));
- if(v->values == NULL) return 1;
- return 0;
- }
- /* Добавление в вектор */
- int vectorAdd(vector * v, data val) {
- if(v == NULL) return 1;
- if(v->sz == v->cap) {
- v->cap = 1 + (v->cap * 3) / 2;
- v->values = (data *) realloc(v->values, v->cap * sizeof(data));
- if(v->values == NULL) return 1;
- }
- v->values[(v->sz)++] = val;
- return 0;
- }
- /* Очищаем вектор */
- void vectorErase(vector * v) {
- free(v->values);
- }
- int main() {
- /* Статус окочания считывания */
- bool notEnded = true;
- /* Структура для считывания данных */
- data newData;
- /* Вектор для хранения данных */
- vector vectorWithData;
- /* Инициализируем вектор */
- if(vectorCreate(&vectorWithData)) {
- perror("ERROR");
- exit(0);
- }
- /* Массивы для сортировки */
- int count[10];
- int * oldIds = NULL;
- int * newIds = NULL;
- /* Переменные для парсинга даты */
- int day, month, year;
- while(notEnded) {
- /* Считаем две строчки */
- if(scanf("%10s%*1c%64[^\n]", newData.s, newData.value) != 2) {
- notEnded = false;
- continue;
- }
- /* Выпарсиваем три числа */
- if(sscanf(newData.s, "%d.%d.%d", &day, &month, &year) != 3) {
- fprintf(stderr, "ERROR: Wrong key format\n");
- exit(0);
- }
- /* Проверяем числа */
- if(day < 0 || day >= 100 || month < 0 || month >= 100 || year < 0 || year >= 10000) {
- fprintf(stderr, "ERROR: Wrong key format\n");
- exit(0);
- }
- /* Записываем их в строчку */
- if(sprintf(newData.n, "%.4d%.2d%.2d", year, month, day) != 8) {
- fprintf(stderr, "ERROR: Wrong key format\n");
- exit(0);
- }
- /* Добавляем в вектор */
- if(vectorAdd(&vectorWithData, newData)) {
- perror("ERROR");
- exit(0);
- }
- }
- int totalElements = vectorWithData.sz;
- /* Выделяем два массива айдишников, чтобы не лопатить вектор */
- oldIds = (int *) malloc(totalElements * sizeof(int));
- newIds = (int *) malloc(totalElements * sizeof(int));
- /* Чекаем память */
- if(oldIds == NULL || newIds == NULL) {
- perror("ERROR");
- exit(0);
- }
- /* Заполняем oldIds дефолтными значениями */
- for(int i = 0; i < totalElements; i++) {
- oldIds[i] = i;
- }
- /* Сортируем */
- for(int i = 7; i >= 0; i--) {
- memset(count, 0, 10 * sizeof(int));
- for(int j = 0; j < totalElements; j++) {
- int curId = oldIds[j];
- int curDigit = vectorWithData.values[curId].n[i] - '0';
- count[curDigit]++;
- }
- for(int j = 1; j < 10; j++) {
- count[j] += count[j - 1];
- }
- for(int j = totalElements - 1; j >= 0; j--) {
- int curId = oldIds[j];
- int curDigit = vectorWithData.values[curId].n[i] - '0';
- newIds[--count[curDigit]] = curId;
- }
- memcpy(oldIds, newIds, totalElements * sizeof(int));
- }
- /* Выводим */
- for(int i = 0; i < totalElements; i++) {
- int curId = oldIds[i];
- printf("%s\t%s\n", vectorWithData.values[curId].s, vectorWithData.values[curId].value);
- }
- /* Очищаем память */
- free(oldIds);
- free(newIds);
- vectorErase(&vectorWithData);
- return 0;
- }
Add Comment
Please, Sign In to add comment