Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <string.h>
- #define MAX 50
- // Перевод строки в нижний регистр
- char* str_tolower(char* str)
- {
- int count = 0;
- char* result = (char*)malloc(MAX * sizeof(char));
- while (str[count] != 0)
- {
- result[count] = (char) tolower(str[count]);
- count++;
- }
- result[count] = 0;
- return result;
- }
- // Поиск индекса в массиве строк
- int find_index(char** arr, char* key)
- {
- int i;
- for (i = 0; i < MAX; i++)
- {
- if (arr[i] == NULL || strcmp(arr[i], str_tolower(key)) == 0)
- return i;
- }
- return 0;
- }
- // Сортировка 2 массивов
- void sort(char** keys, int* values)
- {
- int i, j, n = 0;
- while (keys[n] != NULL)
- n++;
- for (i = 0; i < n - 1; i++) {
- // сравниваем два соседних элемента.
- for (j = 0; j < n - i - 1; j++) {
- if (strcmp(keys[j], keys[j+1]) > 0) {
- // если они идут в неправильном порядке, то
- // меняем их местами.
- char* tmp_key = keys[j];
- int tmp_val = values[j];
- keys[j] = keys[j + 1];
- values[j] = values[j + 1];
- keys[j + 1] = tmp_key;
- values[j + 1] = tmp_val;
- }
- }
- }
- }
- int main()
- {
- char** keys = (char**) malloc(MAX * sizeof(char*));
- int* values = (int*) malloc(MAX * sizeof(int));
- // Заполняем массив нулями
- int i;
- for (i = 0; i < MAX; i++)
- {
- keys[i] = NULL;
- values[i] = 0;
- }
- char* word = (char*)malloc(MAX * sizeof(char));
- int count = 0, value = 0;
- while (1)
- {
- scanf("%s", word);
- if (strcmp(word, "Total") == 0)
- break;
- scanf("%d", &value);
- int ind = find_index(keys, word);
- keys[ind] = str_tolower(word);
- values[ind] += value;
- count++;
- }
- sort(keys, values);
- count = 0;
- while (keys[count] != NULL)
- {
- printf("%s %d\n", keys[count], values[count]);
- count++;
- }
- free(word);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement