Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Resourse {
- char name[30];
- int count;
- };
- int comparator(const void *r1, const void *r2) {
- const struct Resourse *res1 = (const struct Resourse *)r1;
- const struct Resourse *res2 = (const struct Resourse *)r2;
- return strcmp(res1->name, res2->name);
- }
- int main() {
- size_t res_count = 0;
- size_t res_size = 1;
- size_t sz = sizeof(struct Resourse);
- struct Resourse *res = (struct Resourse *)malloc(res_size * sz);
- do {
- struct Resourse res_cond;
- scanf("%s", res_cond.name);
- if (strcmp(res_cond.name, "Total") == 0)
- break;
- scanf("%d", &res_cond.count);
- struct Resourse *search_result = bsearch(&res_cond, res, res_count, sz, comparator);
- if (search_result) {
- search_result->count += res_cond.count;
- continue;
- }
- if (res_count == res_size) {
- res_size <<= 1;
- res = (struct Resourse *)realloc(res, res_size * sz);
- }
- res[res_count] = res_cond;
- ++res_count;
- qsort(res, res_count, sz, comparator);
- } while (1);
- size_t i;
- for (i = 0; i < res_count; ++i)
- printf("%s %d\n", res[i].name, res[i].count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement