Compare commits

...

2 Commits

Author SHA1 Message Date
3b8d18d5d9 10.4 2024-04-10 23:11:18 +02:00
3bd27c0a06 22.3 2024-03-22 13:01:57 +01:00
2 changed files with 152 additions and 15 deletions

View File

@ -10,6 +10,11 @@ bool is_operator(char c) {
return c == '+' || c == '-' || c == '*' || c == '/';
}
// Funkcia na kontrolu, či je daný znak medzera
bool is_space(char c) {
return c == ' ';
}
// Funkcia na kontrolu, či je daný znak číslica
bool is_digit(char c) {
return isdigit(c);
@ -31,6 +36,48 @@ double calculate(double num1, char op, double num2) {
}
}
// Funkcia na kontrolu, či je zadaný riadok úlohy v správnom formáte
bool is_valid_task(char *line) {
int len = strlen(line);
if (len < 7) // Minimálna dĺžka: "a+b=c"
return false;
int i = 0;
// Kontrola či prvé číslo je platné
if (!is_digit(line[i]) && line[i] != '-')
return false;
while (is_digit(line[i]) || is_space(line[i]))
i++;
// Kontrola či operátor je platný
if (!is_operator(line[i]))
return false;
i++;
// Kontrola či druhé číslo je platné
if (!is_digit(line[i]) && line[i] != '-')
return false;
while (is_digit(line[i]) || is_space(line[i]))
i++;
// Kontrola či '=' je na správnom mieste
if (line[i] != '=')
return false;
i++;
// Kontrola či výsledok je platný
if (!is_digit(line[i]) && line[i] != '-')
return false;
while (is_digit(line[i]) || is_space(line[i]))
i++;
// Kontrola či na konci riadka už nie sú ďalšie znaky
if (line[i] != '\0')
return false;
return true;
}
int main() {
char line[100]; // Predpokladáme, že žiadny riadok nebude dlhší ako 100 znakov
@ -38,24 +85,23 @@ int main() {
if (strcmp(line, "\n") == 0) // Ak je riadok prázdny, končíme
break;
int len = strlen(line);
if (line[len - 1] == '\n') // Odstránenie konca riadka
line[len - 1] = '\0';
// Odstránenie konca riadka
line[strcspn(line, "\n")] = '\0';
char op;
double num1, num2, result;
int scanned = sscanf(line, "%lf%*[^0-9-]%c%*[^0-9-]%lf=%lf", &num1, &op, &num2, &result);
// Kontrola, či boli načítané správne hodnoty
if (scanned != 4 || !is_operator(op))
if (!is_valid_task(line)) {
printf("CHYBA\n");
else {
double calculated_result = calculate(num1, op, num2);
if (fabs(calculated_result - result) < 0.01) // Zaokrúhlenie na 2 desatinné miesta
printf("OK\n");
else
printf("ZLE\n");
continue;
}
double num1, num2, result;
char op;
sscanf(line, "%lf %c %lf = %lf", &num1, &op, &num2, &result);
double calculated_result = calculate(num1, op, num2);
if (fabs(calculated_result - result) < 0.01) // Zaokrúhlenie na 2 desatinné miesta
printf("OK\n");
else
printf("ZLE\n");
}
return 0;

91
du7/snake.c Normal file
View File

@ -0,0 +1,91 @@
#include "snake.h"
#include <stdlib.h>
struct snake* add_snake(struct snake* snake, int x, int y) {
struct snake* new_head = (struct snake*)malloc(sizeof(struct snake));
if (!new_head) {
// Memory allocation failed
return NULL;
}
new_head->x = x;
new_head->y = y;
new_head->next = snake;
return new_head;
}
struct snake* remove_snake(struct snake* snake) {
if (!snake) {
// Snake is empty
return NULL;
}
struct snake* new_head = snake->next;
free(snake);
return new_head;
}
int is_snake(struct snake* snake, int x, int y) {
while (snake) {
if (snake->x == x && snake->y == y) {
return 1; // Found snake part at given coordinates
}
snake = snake->next;
}
return 0; // Snake part not found
}
void free_snake(struct snake* sn) {
while (sn) {
struct snake* temp = sn;
sn = sn->next;
free(temp);
}
}
int step_state(struct state* state) {
// Calculate new position of the snake head
int new_x = state->snake->x + state->sx;
int new_y = state->snake->y + state->sy;
// Check if snake hits a wall
if (new_x < 0 || new_x >= state->width || new_y < 0 || new_y >= state->height) {
return END_WALL;
}
// Check if snake hits itself
if (is_snake(state->snake->next, new_x, new_y)) {
return END_SNAKE;
}
// Check if snake eats food
int i;
for (i = 0; i < FOOD_COUNT; i++) {
if (state->foodx[i] == new_x && state->foody[i] == new_y) {
// Food eaten, mark it as negative
state->foodx[i] = -1;
state->foody[i] = -1;
// Add new snake part at the food position
state->snake = add_snake(state->snake, new_x, new_y);
if (state->snake == NULL) {
// Memory allocation failed
return END_USER;
}
// Check if no food left
int food_left = 0;
for (int j = 0; j < FOOD_COUNT; j++) {
if (state->foodx[j] >= 0 && state->foody[j] >= 0) {
food_left = 1;
break;
}
}
if (!food_left) {
return END_FOOD; // No food left
}
return END_CONTINUE; // Food eaten, continue the game
}
}
// Add new snake part on the new position and remove the last part of the snake
state->snake = add_snake(state->snake, new_x, new_y);
state->snake = remove_snake(state->snake->next);
return END_CONTINUE;
}