Advertisement
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>
- #define INVALID_INPUT -1
- #define MAX_LENGTH 100
- struct student
- {
- char meno[MAX_LENGTH];
- long hlasy;
- };
- int compare(const void * a, const void * b);
- int compareString(const void *a, const void *b);
- int main(){
- char input[MAX_LENGTH];
- struct student db[MAX_LENGTH];
- char* endptr;
- long pocet;
- bool zhoda;
- memset(db, 0, MAX_LENGTH*sizeof(struct student));
- memset(input, 0, MAX_LENGTH);
- int size = 0;
- int i = 0;
- while(fgets(input, MAX_LENGTH, stdin) != NULL){
- zhoda = false;
- if (input[0] == '\0'){
- printf("Koniec\n");
- return 0;
- }
- pocet = strtol(input, &endptr, 10);
- endptr[strlen(endptr)-1]= '\0';
- // printf(">%s\n",endptr);
- if(pocet == 0){
- printf("Chybný vstup!\n");
- return INVALID_INPUT;
- }
- if(strcmp(endptr, " ") == 0){
- printf("Chybný vstup!\n");
- return INVALID_INPUT;
- }
- for(unsigned int i = 0; i < strlen(endptr); i++){
- //printf(">checking %c\n", endptr[i]);
- if(!((endptr[i] >= 'a' && endptr[i] <= 'z') || (endptr[i] >= 'A' && endptr[i] <= 'Z') || endptr[i] == ' ')){
- printf("Chybný vstup!\n");
- return INVALID_INPUT;
- }
- }
- for (int i = 0; i < size; i++){
- //printf(">comparing %s with %s\n", endptr, db[i].meno);
- if(strcmp(endptr, db[i].meno) == 0){
- // printf(">adding %ld to %s\n", pocet, db[i].meno);
- zhoda = true;
- db[i].hlasy += pocet;
- }
- }
- if(zhoda){
- i--;
- }else{
- // printf(">>saving %s %ld to pos %d\n", endptr, pocet, i);
- strcpy(db[i].meno, endptr);
- db[i].hlasy = pocet;
- size++;
- }
- //printf("%d %s\n",pocet, endptr);
- i++;
- }
- qsort(db, size, sizeof(struct student), compare);
- for(int i = 0; i < size; i++){
- if(db[i].hlasy == db[i+1].hlasy){
- int start = i;
- int end = 0;
- // printf(">start: %d", start);
- for(int j = i; j < size; j++){
- if(db[i].hlasy == db[j].hlasy){
- end++;
- // printf(">end: %d\n", end);
- qsort(db+start, end, sizeof(struct student), compareString);
- }
- }
- }
- }
- printf("Vysledky:\n");
- for (int i = 0; i < size; i++)
- {
- printf("%ld%s\n", db[i].hlasy,db[i].meno);
- }
- return 0;
- }
- int compare(const void *a, const void *b){
- struct student* s1 = (struct student*)a;
- struct student* s2 = (struct student*)b;
- return ( s2->hlasy - s1->hlasy );
- }
- int compareString(const void *a, const void *b)
- {
- return strcmp(a, b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement