Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #define INVALID_INPUT -1
  6. #define MAX_LENGTH 100
  7. struct student
  8. {
  9.     char meno[MAX_LENGTH];
  10.     long hlasy;
  11. };
  12. int compare(const void * a, const void * b);
  13. int compareString(const void *a, const void *b);
  14.  
  15. int main(){
  16.  
  17.     char input[MAX_LENGTH];
  18.     struct student db[MAX_LENGTH];
  19.     char* endptr;
  20.     long pocet;
  21.     bool zhoda;
  22.    
  23.     memset(db, 0, MAX_LENGTH*sizeof(struct student));
  24.     memset(input, 0, MAX_LENGTH);
  25.    
  26.     int size = 0;
  27.     int i = 0;
  28.    
  29.     while(fgets(input, MAX_LENGTH, stdin) != NULL){
  30.             zhoda = false;
  31.             if (input[0] == '\0'){
  32.                 printf("Koniec\n");
  33.                 return 0;
  34.             }
  35.    
  36.             pocet = strtol(input, &endptr, 10);
  37.             endptr[strlen(endptr)-1]= '\0';
  38.            // printf(">%s\n",endptr);
  39.             if(pocet == 0){
  40.                 return INVALID_INPUT;
  41.             }
  42.            
  43.             if(strcmp(endptr, " ") == 0){
  44.                     printf("Chybný vstup!\n");
  45.                     return INVALID_INPUT;
  46.             }
  47.             for(unsigned int i = 0; i < strlen(endptr); i++){
  48.                 //printf(">checking %c\n", endptr[i]);
  49.                 if(!((endptr[i] >= 'a' && endptr[i] <= 'z') || (endptr[i] >= 'A' && endptr[i] <= 'Z') || endptr[i] == ' ')){
  50.                     printf("Chybný vstup!\n");
  51.                     return INVALID_INPUT;
  52.                 }
  53.             }
  54.        
  55.             for (int i = 0; i < size; i++){
  56.                 //printf(">comparing %s with %s\n", endptr, db[i].meno);
  57.                 if(strcmp(endptr, db[i].meno) == 0){
  58.                   //  printf(">adding %ld to %s\n", pocet, db[i].meno);
  59.                     zhoda = true;
  60.                     db[i].hlasy += pocet;        
  61.                 }
  62.             }
  63.  
  64.         if(zhoda){
  65.             i--;
  66.         }else{
  67.            // printf(">>saving %s %ld to pos %d\n", endptr, pocet, i);
  68.             strcpy(db[i].meno, endptr);
  69.             db[i].hlasy = pocet;
  70.             size++;
  71.         }
  72.         //printf("%d %s\n",pocet, endptr);
  73.     i++;
  74.     }
  75.     qsort(db, size, sizeof(struct student), compare);
  76.  
  77.     for(int i = 0; i < size; i++){
  78.             if(db[i].hlasy == db[i+1].hlasy){
  79.                 int start = i;
  80.                 int end = 0;
  81.               //  printf(">start: %d", start);
  82.                 for(int j = i; j < size; j++){
  83.                     if(db[i].hlasy == db[j].hlasy){
  84.                         end++;
  85.                    //     printf(">end: %d\n", end);
  86.                         qsort(db+start, end, sizeof(struct student), compareString);
  87.                     }
  88.                 }
  89.             }
  90.     }
  91.     printf("Vysledky:\n");
  92.     for (int i = 0; i < size; i++)
  93.     {
  94.         printf("%ld%s\n", db[i].hlasy,db[i].meno);
  95.     }
  96.     return 0;
  97. }
  98.  
  99. int compare(const void *a, const void *b){
  100.     struct student* s1 = (struct student*)a;
  101.     struct student* s2 = (struct student*)b;
  102.    
  103.     return ( s2->hlasy - s1->hlasy );
  104. }
  105.                      
  106. int compareString(const void *a, const void *b)
  107. {
  108.     return strcmp(a, b);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement