Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 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.                 printf("Chybný vstup!\n");
  41.                 return INVALID_INPUT;
  42.             }
  43.            
  44.             if(strcmp(endptr, " ") == 0){
  45.                     printf("Chybný vstup!\n");
  46.                     return INVALID_INPUT;
  47.             }
  48.             for(unsigned int i = 0; i < strlen(endptr); i++){
  49.                 //printf(">checking %c\n", endptr[i]);
  50.                 if(!((endptr[i] >= 'a' && endptr[i] <= 'z') || (endptr[i] >= 'A' && endptr[i] <= 'Z') || endptr[i] == ' ')){
  51.                     printf("Chybný vstup!\n");
  52.                     return INVALID_INPUT;
  53.                 }
  54.             }
  55.        
  56.             for (int i = 0; i < size; i++){
  57.                 //printf(">comparing %s with %s\n", endptr, db[i].meno);
  58.                 if(strcmp(endptr, db[i].meno) == 0){
  59.                   //  printf(">adding %ld to %s\n", pocet, db[i].meno);
  60.                     zhoda = true;
  61.                     db[i].hlasy += pocet;        
  62.                 }
  63.             }
  64.  
  65.         if(zhoda){
  66.             i--;
  67.         }else{
  68.            // printf(">>saving %s %ld to pos %d\n", endptr, pocet, i);
  69.             strcpy(db[i].meno, endptr);
  70.             db[i].hlasy = pocet;
  71.             size++;
  72.         }
  73.         //printf("%d %s\n",pocet, endptr);
  74.     i++;
  75.     }
  76.     qsort(db, size, sizeof(struct student), compare);
  77.  
  78.     for(int i = 0; i < size; i++){
  79.             if(db[i].hlasy == db[i+1].hlasy){
  80.                 int start = i;
  81.                 int end = 0;
  82.               //  printf(">start: %d", start);
  83.                 for(int j = i; j < size; j++){
  84.                     if(db[i].hlasy == db[j].hlasy){
  85.                         end++;
  86.                    //     printf(">end: %d\n", end);
  87.                         qsort(db+start, end, sizeof(struct student), compareString);
  88.                     }
  89.                 }
  90.             }
  91.     }
  92.     printf("Vysledky:\n");
  93.     for (int i = 0; i < size; i++)
  94.     {
  95.         printf("%ld%s\n", db[i].hlasy,db[i].meno);
  96.     }
  97.     return 0;
  98. }
  99.  
  100. int compare(const void *a, const void *b){
  101.     struct student* s1 = (struct student*)a;
  102.     struct student* s2 = (struct student*)b;
  103.    
  104.     return ( s2->hlasy - s1->hlasy );
  105. }
  106.                      
  107. int compareString(const void *a, const void *b)
  108. {
  109.     return strcmp(a, b);
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement