pvjc20/du6/program.c

82 lines
1.5 KiB
C
Raw Normal View History

2020-04-17 08:56:27 +00:00
#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++;
}
}
2020-04-17 09:10:14 +00:00
if(size==0){
2020-04-17 08:56:27 +00:00
puts("Ziadne prihlasky");
}
qsort(students, loaded, sizeof(char*), compare_func);
puts("Prijati studenti:");
for(int i=0; i<size; i++)
puts(students[i]);
2020-04-17 09:10:14 +00:00
puts("Neprijati studenti:");
for(int i=size; i<loaded; i++)
puts(students[i]);
2020-04-17 08:56:27 +00:00
for(int i=0; i<SIZE; i++){
free(students[i]);
}
return 0;
}