pvjc20/du6/program.c
Zoltán Szabó df4b43a5e4 c2
2020-04-17 11:10:14 +02:00

82 lines
1.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
int get_index(char* students[],int size, const char* name){
for(int i=0; i<size; i++){
if(!strcmp(students[i], name)){
return i;
}
}
return -1;
}
int compare_func(const void * a, const void * b){
return (strcmp(*(char**)a, *(char**)b));
}
int main(int argc, char const *argv[])
{
char line[SIZE];
memset(line,0,SIZE);
char* r = fgets(line,SIZE,stdin);
if(r==NULL){
puts("Nespravny vstup");
return 0;
}
char* pEnd= NULL;
int size = strtol(line, &pEnd, 10);
if(size<=0){ //TODO check for trailing chars //there is newline
puts("Nespravny vstup");
return 0;
}
char* students[SIZE];
for(int i=0; i<SIZE; i++)
{
students[i] = calloc(SIZE, sizeof(char));
}
//memset(students,0,SIZE*sizeof(char*));
int loaded = 0;
while(1){
memset(line,0,SIZE);
char* r = fgets(line,SIZE,stdin);
if (r == NULL){
break;
}
if(line[0]=='\n'){
break;
}
char tmp[SIZE];
memset(tmp, 0, SIZE);
memcpy(tmp, line, strlen(line)-1);
int idx = get_index(students, loaded, tmp);
if(idx==-1){
memcpy(students[loaded], tmp, strlen(tmp));
loaded++;
}
}
if(size==0){
puts("Ziadne prihlasky");
}
qsort(students, loaded, sizeof(char*), compare_func);
puts("Prijati studenti:");
for(int i=0; i<size; i++)
puts(students[i]);
puts("Neprijati studenti:");
for(int i=size; i<loaded; i++)
puts(students[i]);
for(int i=0; i<SIZE; i++){
free(students[i]);
}
return 0;
}