97 lines
2.1 KiB
C
97 lines
2.1 KiB
C
#include<string.h>
|
|
#include<stdio.h>
|
|
#include<math.h>
|
|
#include<stdlib.h>
|
|
|
|
#define SIZE 100
|
|
|
|
struct student {
|
|
char name[SIZE];
|
|
int votes;
|
|
};
|
|
|
|
|
|
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) {
|
|
return i; //ak najde studenta
|
|
}
|
|
}
|
|
//printf("nenaslo studenta\n"); // ak nenajde studenta
|
|
return -1;
|
|
}
|
|
|
|
|
|
int compare(const void* p1, const void* p2){
|
|
const struct student* s1 = (const struct student*)p1;
|
|
const struct student* s2 = (const struct student*)p2;
|
|
return strcmp(s2->name, s1->name);
|
|
}
|
|
|
|
|
|
int main(){
|
|
|
|
struct student databaza[SIZE];
|
|
memset(databaza, 0, SIZE*sizeof(struct student)); //inicializujem pamat, vynulujem
|
|
|
|
int size = 0;
|
|
|
|
//nacitanie jednej polozky do databazy
|
|
char line[SIZE];
|
|
memset(line,0,SIZE);
|
|
char* r = fgets(line,SIZE,stdin);
|
|
if (r == NULL){
|
|
return -1;
|
|
}
|
|
|
|
//nacitanie celeho cisla, premiena kym nepride na znak ktory nieje cislo
|
|
char* end = NULL;
|
|
int value = strtol(line,&end,10);
|
|
if (value == 0){
|
|
return 1;
|
|
}
|
|
// pomocné pole
|
|
char name[SIZE];
|
|
// vynulujeme
|
|
memset(name,0,SIZE);
|
|
// Vypocitame zaciatok mena
|
|
// Jedno miesto za medzerou
|
|
char* start_name = end + 1; //urcenie zaciatku mena
|
|
// Velkost mena je pocet znakov do konca retazca
|
|
// minus koniec riadka
|
|
int name_size = strlen(start_name) - 1; //urcenie konca mena
|
|
if (name_size > 0){
|
|
// nakopirujeme
|
|
memcpy(name,start_name,name_size);
|
|
// Na konci je v poli name ulozeny retazec s menom
|
|
// bez konca riadka a s nulou na konci
|
|
}
|
|
else {
|
|
printf( "nepodarilo sa nacitat meno \n");
|
|
return 1;
|
|
}
|
|
|
|
//prejde vsetky polozky v databaze
|
|
|
|
int id = find_student(databaza,size,name);
|
|
if (id< 0){
|
|
if (size < SIZE){
|
|
memcpy(databaza[size].name,name,name_size);
|
|
databaza[size].votes = value;
|
|
size++;
|
|
}
|
|
else{
|
|
printf("databaza je plna\n");
|
|
return 1;
|
|
}
|
|
}
|
|
else {
|
|
databaza[id].votes = value;
|
|
}
|
|
|
|
printf("Vysledky:\n");
|
|
for (int i = 0; i < size; i++){
|
|
printf ("%d %s\n", databaza[i].votes, databaza[i].name);
|
|
}
|
|
return 0;
|
|
} |