funguje
This commit is contained in:
parent
f44d5266fb
commit
741cb2bc0d
@ -2,81 +2,72 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#define MAX_STUDENTS 100
|
|
||||||
#define MAX_NAME_LENGTH 100
|
#define MAX_NAME_LENGTH 100
|
||||||
|
#define MAX_BUFFER_LENGTH 256
|
||||||
|
|
||||||
// Structure to hold student information (name and votes)
|
// Štruktúra pre uchovanie informácií o študentovi
|
||||||
typedef struct {
|
typedef struct {
|
||||||
char name[100];
|
char name[MAX_NAME_LENGTH];
|
||||||
int votes;
|
int votes;
|
||||||
} Student;
|
} Student;
|
||||||
|
|
||||||
// Function to compare two students based on their votes and names
|
// Porovnávacia funkcia pre zoradenie študentov podľa počtu hlasov a mena
|
||||||
int compare_students(const void *a, const void *b) {
|
int compare_students(const void *a, const void *b) {
|
||||||
const Student *student1 = (const Student *)a;
|
const Student *student_a = (const Student *)a;
|
||||||
const Student *student2 = (const Student *)b;
|
const Student *student_b = (const Student *)b;
|
||||||
|
|
||||||
// Sort by number of votes in descending order
|
// Najprv porovnajte počet hlasov
|
||||||
if (student1->votes != student2->votes) {
|
if (student_a->votes != student_b->votes) {
|
||||||
return student2->votes - student1->votes;
|
return student_b->votes - student_a->votes; // Zoradiť zostupne
|
||||||
} else {
|
|
||||||
// If votes are equal, sort lexicographically by name
|
|
||||||
return strcmp(student1->name, student2->name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ak majú rovnaký počet hlasov, porovnajte podľa mena
|
||||||
|
return strcmp(student_a->name, student_b->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char line[256];
|
// Inicializácia poľa študentov
|
||||||
Student students[100];
|
Student students[MAX_BUFFER_LENGTH];
|
||||||
int total_students = 0;
|
int num_students = 0;
|
||||||
|
|
||||||
// Read votes from standard input
|
// Načítanie hlasov zo štandardného vstupu
|
||||||
while (fgets(line, sizeof(line), stdin)) {
|
char buffer[MAX_BUFFER_LENGTH];
|
||||||
|
while (fgets(buffer, sizeof(buffer), stdin) != NULL) {
|
||||||
|
// Načítajte počet hlasov a meno študenta
|
||||||
int votes;
|
int votes;
|
||||||
char name[100];
|
char name[MAX_NAME_LENGTH];
|
||||||
|
if (sscanf(buffer, "%d %99[^\n]", &votes, name) != 2) {
|
||||||
// Parse votes and name from input line
|
fprintf(stderr, "Chyba: Neplatný formát vstupu.\n");
|
||||||
if (sscanf(line, "%d %99[^\n]", &votes, name) != 2) {
|
|
||||||
fprintf(stderr, "Error: Invalid input format.\n");
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
// Check if the votes are positive
|
|
||||||
if (votes <= 0) {
|
// Uložte študenta do poľa
|
||||||
fprintf(stderr, "Error: Invalid number of votes.\n");
|
strncpy(students[num_students].name, name, MAX_NAME_LENGTH);
|
||||||
|
students[num_students].votes = votes;
|
||||||
|
num_students++;
|
||||||
|
|
||||||
|
// Kontrola prekročenia maximálnej veľkosti poľa študentov
|
||||||
|
if (num_students >= MAX_BUFFER_LENGTH) {
|
||||||
|
fprintf(stderr, "Chyba: Príliš veľa študentov.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
// Find if the student is already in the list
|
|
||||||
int found = 0;
|
|
||||||
for (int i = 0; i < total_students; i++) {
|
|
||||||
if (strcmp(students[i].name, name) == 0) {
|
|
||||||
students[i].votes += votes;
|
|
||||||
found = 1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If student not found, add to the list
|
|
||||||
if (!found) {
|
|
||||||
if (total_students >= MAX_STUDENTS) {
|
|
||||||
fprintf(stderr, "Error: Too many students.\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
strcpy(students[total_students].name, name);
|
|
||||||
students[total_students].votes = votes;
|
|
||||||
total_students++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort the list of students based on votes and names
|
// Ak neboli načítané žiadne dáta, vypíšte chybovú správu
|
||||||
qsort(students, total_students, sizeof(Student), compare_students);
|
if (num_students == 0) {
|
||||||
|
fprintf(stderr, "Chyba: Žiadne záznamy načítané.\n");
|
||||||
// Print the outcome
|
return 1;
|
||||||
printf("Results:\n");
|
}
|
||||||
for (int i = 0; i < total_students; i++) {
|
|
||||||
|
// Zoradenie študentov podľa počtu hlasov a mena
|
||||||
|
qsort(students, num_students, sizeof(Student), compare_students);
|
||||||
|
|
||||||
|
// Výpis výsledkov
|
||||||
|
printf("Vysledky:\n");
|
||||||
|
for (int i = 0; i < num_students; i++) {
|
||||||
printf("%d %s\n", students[i].votes, students[i].name);
|
printf("%d %s\n", students[i].votes, students[i].name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user