Compare commits
32 Commits
ct453rn-pa
...
master
Author | SHA1 | Date | |
---|---|---|---|
6672cd141a | |||
508b8baac6 | |||
631a979268 | |||
81742cafdf | |||
0affccd25f | |||
651bd07061 | |||
0c88c05287 | |||
a1f34bab30 | |||
cd8d289d30 | |||
3eb29b9adc | |||
7d39793ba4 | |||
97083e4ab7 | |||
25eb67de10 | |||
b3a30d7d3c | |||
8f62ce9248 | |||
da10364e62 | |||
d1b2c5c570 | |||
21fb2afe59 | |||
5f88ad33a9 | |||
9cee0d1287 | |||
72c07053cd | |||
20615955e6 | |||
37e8c6d95e | |||
5fe30441cf | |||
e6a33cf6f4 | |||
3231af40ae | |||
1369c121ed | |||
affd4a5ae5 | |||
4694d6cc27 | |||
11b326d5d6 | |||
bd4c1dd70a | |||
4aff3b0297 |
0
a1/program.c
Normal file
0
a1/program.c
Normal file
0
a3/binary_search_tree.c
Normal file
0
a3/binary_search_tree.c
Normal file
90
cv10/program.c
Normal file
90
cv10/program.c
Normal file
@ -0,0 +1,90 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_NAME_LENGTH 100
|
||||
|
||||
int compare(const void *a, const void *b) {
|
||||
const char *name1 = *(const char **)a;
|
||||
const char *name2 = *(const char **)b;
|
||||
return strcmp(name1, name2);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int maxStudents;
|
||||
char name[MAX_NAME_LENGTH];
|
||||
char **names = NULL;
|
||||
int count = 0;
|
||||
int capacity = 0;
|
||||
|
||||
|
||||
if (scanf("%d\n", &maxStudents) != 1 || maxStudents <= 0) {
|
||||
puts("Nespravny vstup");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
while (fgets(name, MAX_NAME_LENGTH, stdin)) {
|
||||
if (name[0] == '\n') break;
|
||||
name[strcspn(name, "\n")] = '\0';
|
||||
|
||||
|
||||
if (count >= capacity) {
|
||||
capacity = capacity == 0 ? 4 : capacity * 2;
|
||||
names = realloc(names, capacity * sizeof(char *));
|
||||
if (names == NULL) {
|
||||
perror("Failed to reallocate memory");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int isDuplicate = 0;
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (strcmp(names[i], name) == 0) {
|
||||
isDuplicate = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!isDuplicate) {
|
||||
names[count] = strdup(name);
|
||||
if (names[count] == NULL) {
|
||||
perror("Failed to duplicate name");
|
||||
return 0;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
if (count == 0) {
|
||||
puts("Ziadne prihlasky");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
qsort(names, count, sizeof(char *), compare);
|
||||
|
||||
|
||||
puts("Prijati studenti:");
|
||||
int limit = count < maxStudents ? count : maxStudents;
|
||||
for (int i = 0; i < limit; i++) {
|
||||
puts(names[i]);
|
||||
}
|
||||
|
||||
|
||||
if (count > maxStudents) {
|
||||
puts("Neprijati studenti:");
|
||||
for (int i = maxStudents; i < count; i++) {
|
||||
puts(names[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
free(names[i]);
|
||||
}
|
||||
free(names);
|
||||
|
||||
return 0;
|
||||
}
|
48
cv3/program.c
Normal file
48
cv3/program.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_COEFFICIENTS 100
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
double evaluatePolynomial(double x, double coefficients[], int n) {
|
||||
double result = 0.0;
|
||||
for (int i = 0; i < n; ++i) {
|
||||
result = result * x + coefficients[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int main() {
|
||||
char buffer[BUFFER_SIZE];
|
||||
double x, coef;
|
||||
double coefficients[MAX_COEFFICIENTS];
|
||||
int count = 0;
|
||||
|
||||
|
||||
fgets(buffer, BUFFER_SIZE, stdin);
|
||||
if (sscanf(buffer, "%lf", &x) != 1) {
|
||||
printf("Nepodarilo sa nacitat zaklad x\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
while (fgets(buffer, BUFFER_SIZE, stdin) && buffer[0] != '\n') {
|
||||
if (sscanf(buffer, "%lf", &coef) == 1) {
|
||||
coefficients[count++] = coef;
|
||||
if (count >= MAX_COEFFICIENTS) {
|
||||
printf("Maximum number of coefficients exceeded.\n");
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
printf("Nepodarilo sa nacitat polynom na %d mieste.\n", count + 1);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
double result = evaluatePolynomial(x, coefficients, count);
|
||||
printf("Vysledok je: %.2f\n", result);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
82
cv4/list_ops.c
Normal file
82
cv4/list_ops.c
Normal file
@ -0,0 +1,82 @@
|
||||
#include "list_ops.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
list_t *new_list(size_t length, list_element_t elements[]) {
|
||||
list_t *list = malloc(sizeof(list_t) + length * sizeof(list_element_t));
|
||||
list->length = length;
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
list->elements[i] = elements[i];
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
list_t *append_list(list_t *list1, list_t *list2) {
|
||||
size_t new_length = list1->length + list2->length;
|
||||
list_t *new_list = malloc(sizeof(list_t) + new_length * sizeof(list_element_t));
|
||||
new_list->length = new_length;
|
||||
for (size_t i = 0; i < list1->length; i++) {
|
||||
new_list->elements[i] = list1->elements[i];
|
||||
}
|
||||
for (size_t i = 0; i < list2->length; i++) {
|
||||
new_list->elements[list1->length + i] = list2->elements[i];
|
||||
}
|
||||
return new_list;
|
||||
}
|
||||
|
||||
list_t *filter_list(list_t *list, bool (*filter)(list_element_t)) {
|
||||
list_t *temp_list = malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
|
||||
temp_list->length = 0;
|
||||
for (size_t i = 0; i < list->length; i++) {
|
||||
if (filter(list->elements[i])) {
|
||||
temp_list->elements[temp_list->length++] = list->elements[i];
|
||||
}
|
||||
}
|
||||
list_t *filtered_list = new_list(temp_list->length, temp_list->elements);
|
||||
free(temp_list);
|
||||
return filtered_list;
|
||||
}
|
||||
|
||||
size_t length_list(list_t *list) {
|
||||
return list->length;
|
||||
}
|
||||
|
||||
list_t *map_list(list_t *list, list_element_t (*map)(list_element_t)) {
|
||||
list_t *mapped_list = malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
|
||||
mapped_list->length = list->length;
|
||||
for (size_t i = 0; i < list->length; i++) {
|
||||
mapped_list->elements[i] = map(list->elements[i]);
|
||||
}
|
||||
return mapped_list;
|
||||
}
|
||||
|
||||
list_element_t foldl_list(list_t *list, list_element_t initial, list_element_t (*foldl)(list_element_t, list_element_t)) {
|
||||
list_element_t accumulator = initial;
|
||||
for (size_t i = 0; i < list->length; i++) {
|
||||
accumulator = foldl(list->elements[i], accumulator);
|
||||
}
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
list_element_t foldr_list(list_t *list, list_element_t initial, list_element_t (*foldr)(list_element_t, list_element_t)) {
|
||||
list_element_t accumulator = initial;
|
||||
for (int i = list->length - 1; i >= 0; i--) {
|
||||
accumulator = foldr(list->elements[i], accumulator);
|
||||
}
|
||||
return accumulator;
|
||||
}
|
||||
|
||||
list_t *reverse_list(list_t *list) {
|
||||
list_t *reversed_list = malloc(sizeof(list_t) + list->length * sizeof(list_element_t));
|
||||
reversed_list->length = list->length;
|
||||
for (size_t i = 0; i < list->length; i++) {
|
||||
reversed_list->elements[i] = list->elements[list->length - 1 - i];
|
||||
}
|
||||
return reversed_list;
|
||||
}
|
||||
|
||||
void delete_list(list_t *list) {
|
||||
free(list);
|
||||
}
|
75
cv5/program.c
Normal file
75
cv5/program.c
Normal file
@ -0,0 +1,75 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_STUDENTOV 100
|
||||
#define MAX_MENO 100
|
||||
|
||||
typedef struct {
|
||||
char meno[MAX_MENO];
|
||||
int hlasy;
|
||||
} Student;
|
||||
|
||||
Student studenti[MAX_STUDENTOV];
|
||||
int pocetStudentov = 0;
|
||||
|
||||
int pridajHlas(char* meno, int pocetHlasov) {
|
||||
for (int i = 0; i < pocetStudentov; i++) {
|
||||
if (strcmp(studenti[i].meno, meno) == 0) {
|
||||
studenti[i].hlasy += pocetHlasov;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (pocetStudentov < MAX_STUDENTOV) {
|
||||
strcpy(studenti[pocetStudentov].meno, meno);
|
||||
studenti[pocetStudentov].hlasy = pocetHlasov;
|
||||
pocetStudentov++;
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int porovnaj(const void* a, const void* b) {
|
||||
Student *studentA = (Student *)a;
|
||||
Student *studentB = (Student *)b;
|
||||
|
||||
if (studentA->hlasy == studentB->hlasy) {
|
||||
return strcmp(studentA->meno, studentB->meno);
|
||||
}
|
||||
return studentB->hlasy - studentA->hlasy;
|
||||
}
|
||||
|
||||
int main() {
|
||||
char riadok[150];
|
||||
char meno[MAX_MENO];
|
||||
int pocetHlasov;
|
||||
int validneZaznamy = 0;
|
||||
|
||||
while (fgets(riadok, sizeof(riadok), stdin)) {
|
||||
if (sscanf(riadok, "%d %[^\n]s", &pocetHlasov, meno) == 2) {
|
||||
if (pridajHlas(meno, pocetHlasov)) {
|
||||
printf("Nepodarilo sa pridať hlas.\n");
|
||||
return 1;
|
||||
}
|
||||
validneZaznamy = 1;
|
||||
} else {
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!validneZaznamy) {
|
||||
|
||||
printf("Nepodarilo nacitat nic\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
qsort(studenti, pocetStudentov, sizeof(Student), porovnaj);
|
||||
|
||||
printf("Vysledky:\n");
|
||||
for (int i = 0; i < pocetStudentov; i++) {
|
||||
printf("%d %s\n", studenti[i].hlasy, studenti[i].meno);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
77
cv9/snake.c
Normal file
77
cv9/snake.c
Normal file
@ -0,0 +1,77 @@
|
||||
#include "snake.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
struct snake* add_snake(struct snake* head, int x, int y) {
|
||||
struct snake* new_head = (struct snake*)malloc(sizeof(struct snake));
|
||||
new_head->x = x;
|
||||
new_head->y = y;
|
||||
new_head->next = head;
|
||||
return new_head;
|
||||
}
|
||||
struct snake* remove_snake(struct snake* head) {
|
||||
if (head == NULL || head->next == NULL) {
|
||||
free(head);
|
||||
return NULL;
|
||||
}
|
||||
struct snake* current = head;
|
||||
while (current->next->next != NULL) {
|
||||
current = current->next;
|
||||
}
|
||||
free(current->next);
|
||||
current->next = NULL;
|
||||
return head;
|
||||
}
|
||||
int is_snake(struct snake* snake, int x, int y) {
|
||||
struct snake* current = snake;
|
||||
while (current != NULL) {
|
||||
if (current->x == x && current->y == y) {
|
||||
return 1;
|
||||
}
|
||||
current = current->next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void free_snake(struct snake* snake) {
|
||||
struct snake* current = snake;
|
||||
while (current != NULL) {
|
||||
struct snake* next = current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
int step_state(struct state* state) {
|
||||
int new_x = state->snake->x + state->sx;
|
||||
int new_y = state->snake->y + state->sy;
|
||||
|
||||
|
||||
if (new_x < 0 || new_y < 0 || new_x >= state->width || new_y >= state->height) {
|
||||
return END_WALL;
|
||||
}
|
||||
|
||||
|
||||
if (is_snake(state->snake, new_x, new_y)) {
|
||||
return END_SNAKE;
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < FOOD_COUNT; i++) {
|
||||
if (state->foodx[i] == new_x && state->foody[i] == new_y) {
|
||||
state->foodx[i] = state->foody[i] = -1;
|
||||
state->snake = add_snake(state->snake, new_x, new_y);
|
||||
|
||||
int all_eaten = 1;
|
||||
for (int j = 0; j < FOOD_COUNT; j++) {
|
||||
if (state->foodx[j] != -1) {
|
||||
all_eaten = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return all_eaten ? END_FOOD : END_CONTINUE;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
state->snake = add_snake(state->snake, new_x, new_y);
|
||||
state->snake = remove_snake(state->snake);
|
||||
return END_CONTINUE;
|
||||
}
|
Loading…
Reference in New Issue
Block a user