Compare commits

...

32 Commits

Author SHA1 Message Date
6672cd141a Update 'a3/binary_search_tree.c' 2024-04-18 12:50:53 +00:00
508b8baac6 Update 'a3/binary_search_tree.c' 2024-04-18 12:47:26 +00:00
631a979268 Update 'a3/binary_search_tree.c' 2024-04-18 12:44:33 +00:00
81742cafdf Add 'a3/binary_search_tree.c' 2024-04-18 12:41:47 +00:00
0affccd25f Update 'cv10/program.c' 2024-04-18 12:40:23 +00:00
651bd07061 Update 'cv10/program.c' 2024-04-18 12:35:06 +00:00
0c88c05287 Add 'cv10/program.c' 2024-04-18 12:33:58 +00:00
a1f34bab30 Update 'cv9/snake.c' 2024-04-08 18:28:51 +00:00
cd8d289d30 Add 'cv9/snake.c' 2024-04-08 18:27:30 +00:00
3eb29b9adc Add 'a1/program.c' 2024-03-21 17:41:18 +00:00
7d39793ba4 Update 'cv5/program.c' 2024-03-21 17:24:45 +00:00
97083e4ab7 Update 'cv5/program.c' 2024-03-21 17:21:50 +00:00
25eb67de10 Update 'cv5/program.c' 2024-03-21 17:18:40 +00:00
b3a30d7d3c Update 'cv5/program.c' 2024-03-21 17:16:16 +00:00
8f62ce9248 Add 'cv5/program.c' 2024-03-21 17:09:29 +00:00
da10364e62 Update 'cv4/list_ops.c' 2024-03-10 14:17:51 +00:00
d1b2c5c570 Update 'cv4/list_ops.c' 2024-03-10 14:12:43 +00:00
21fb2afe59 Update 'cv4/program.c' 2024-03-10 14:11:18 +00:00
5f88ad33a9 Add 'cv4/program.c' 2024-03-10 14:11:01 +00:00
9cee0d1287 Update 'cv3/program.c' 2024-03-08 08:40:36 +00:00
72c07053cd Update 'cv3/program.c' 2024-03-08 08:38:01 +00:00
20615955e6 Update 'cv3/program.c' 2024-03-08 08:32:21 +00:00
37e8c6d95e Update 'cv3/program.c' 2024-03-08 08:30:16 +00:00
5fe30441cf Update 'cv3/program.c' 2024-03-08 08:29:31 +00:00
e6a33cf6f4 Update 'cv3/program.c' 2024-03-08 08:27:27 +00:00
3231af40ae Update 'cv3/program.c' 2024-03-08 08:25:05 +00:00
1369c121ed Update 'cv3/program.c' 2024-03-08 08:23:26 +00:00
affd4a5ae5 Update 'cv3/program.c' 2024-03-08 08:21:46 +00:00
4694d6cc27 Update 'cv3/program.c' 2024-03-08 08:17:54 +00:00
11b326d5d6 Add 'cv3/program.c' 2024-03-08 08:16:31 +00:00
bd4c1dd70a Delete 'cv3' 2024-03-08 08:13:44 +00:00
4aff3b0297 program.c 2024-03-08 08:13:19 +00:00
7 changed files with 372 additions and 0 deletions

0
a1/program.c Normal file
View File

0
a3/binary_search_tree.c Normal file
View File

90
cv10/program.c Normal file
View 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
View 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
View 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
View 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
View 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;
}