h
This commit is contained in:
parent
b6c18a05ce
commit
bc34a78e02
@ -9,37 +9,6 @@ struct student {
|
||||
char name[SIZE];
|
||||
int votes;
|
||||
};
|
||||
/*char* read_line(char* buffer, int size) {
|
||||
char* result = fgets(buffer, size, stdin);
|
||||
if (result) {
|
||||
// Удаление символа новой строки, если он есть
|
||||
char* newline = strchr(buffer, '\n');
|
||||
if (newline)
|
||||
*newline = '\0';
|
||||
}
|
||||
return result;
|
||||
}*/
|
||||
|
||||
|
||||
/*char* read_line(char* buffer, int size) {
|
||||
char* result = fgets(buffer, size, stdin);
|
||||
if (result) {
|
||||
|
||||
buffer[strcspn(buffer, "\n")] = '\0';
|
||||
}
|
||||
return result;
|
||||
}*/
|
||||
|
||||
/*int read_input(int* votes, char* name, int size) {
|
||||
char line[SIZE];
|
||||
if (fgets(line, size, stdin) != NULL) {
|
||||
|
||||
if (sscanf(line, "%d %s", votes, name) == 2) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}*/
|
||||
|
||||
int read_input(int* votes, char* name, int size) {
|
||||
char line[SIZE];
|
||||
@ -49,8 +18,7 @@ int read_input(int* votes, char* name, int size) {
|
||||
if (space != NULL) {
|
||||
*votes = atoi(line);
|
||||
strcpy(name, space + 1);
|
||||
|
||||
char* newline = strchr(name, '\n');
|
||||
char* newline = strchr(name, '\n');
|
||||
if (newline)
|
||||
*newline = '\0';
|
||||
return 1;
|
||||
@ -60,10 +28,12 @@ int read_input(int* votes, char* name, int size) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
int parse_int(const char* str) {
|
||||
return atoi(str);
|
||||
}
|
||||
|
||||
|
||||
int find_student(struct student* students, int size, const char* name) {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
if (strcmp(students[i].name, name) == 0) {
|
||||
@ -83,7 +53,7 @@ void add_student(struct student* students, int* size, const char* name, int vote
|
||||
students[*size].votes = votes;
|
||||
(*size)++;
|
||||
} else {
|
||||
printf("Database is crowded!\n");
|
||||
printf("Datebase is crowded!\n");
|
||||
}
|
||||
} else {
|
||||
students[index].votes += votes;
|
||||
@ -104,51 +74,6 @@ int compare_students(const void* p1, const void* p2) {
|
||||
}
|
||||
}
|
||||
|
||||
/*int main() {
|
||||
struct student database[SIZE];
|
||||
int size = 0;
|
||||
|
||||
char line[SIZE];
|
||||
|
||||
|
||||
while (read_line(line, SIZE)) {
|
||||
int votes = parse_int(line);
|
||||
read_line(line, SIZE);
|
||||
add_student(database, &size, line, votes);
|
||||
}
|
||||
|
||||
|
||||
qsort(database, size, sizeof(struct student), compare_students);
|
||||
|
||||
|
||||
printf("Vysledky hlasovania:\n");
|
||||
for (int i = 0; i < size; ++i) {
|
||||
printf("%s: %d Hlas(ov) \n", database[i].name, database[i].votes);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}*/
|
||||
/*int main() {
|
||||
struct student database[SIZE];
|
||||
int size = 0;
|
||||
|
||||
int votes;
|
||||
char name[SIZE];
|
||||
|
||||
|
||||
while (read_input(&votes, name, SIZE)) {
|
||||
add_student(database, &size, name, votes);
|
||||
qsort(database, size, sizeof(struct student), compare_students);
|
||||
|
||||
|
||||
printf("Vysledky hlasovania:\n");
|
||||
for (int i = 0; i < size; ++i) {
|
||||
printf("%d %s\n", database[i].votes, database[i].name);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}*/
|
||||
int main() {
|
||||
struct student database[SIZE];
|
||||
int size = 0;
|
||||
@ -165,10 +90,11 @@ int main() {
|
||||
qsort(database, size, sizeof(struct student), compare_students);
|
||||
|
||||
|
||||
printf("Vysledky hlasovania:\n");
|
||||
printf("Vysledky:\n");
|
||||
for (int i = 0; i < size; ++i) {
|
||||
printf("%d %s\n", database[i].votes, database[i].name);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user