refresh
This commit is contained in:
parent
d9c10c0967
commit
8e5e2a2dae
2
Makefile
2
Makefile
@ -1,6 +1,6 @@
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -Wall -Wextra -std=c99
|
CFLAGS = -Wall -Wextra -std=c99
|
||||||
dIRS = a1 du1 du2 du3 du4
|
dIRS = a1 du1 du2 du3 du4 du5
|
||||||
all:
|
all:
|
||||||
@for %%d in ($(dIRS)) do \
|
@for %%d in ($(dIRS)) do \
|
||||||
$(CC) $(CFLAGS) %%d\program.c -o %%d\program.exe
|
$(CC) $(CFLAGS) %%d\program.c -o %%d\program.exe
|
||||||
|
@ -13,8 +13,8 @@ void funkcia (int c, int *line_count) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int c; //текущий считанный символ
|
int c;
|
||||||
int c2 = 0; //счётчик строк
|
int c2 = 0;
|
||||||
while ((c = getchar()) !=EOF) {
|
while ((c = getchar()) !=EOF) {
|
||||||
funkcia(c, &c2);
|
funkcia(c, &c2);
|
||||||
}
|
}
|
||||||
|
BIN
a1/program.exe
BIN
a1/program.exe
Binary file not shown.
@ -11,15 +11,15 @@ int main() {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
results[count] = num;
|
results[count] = num;
|
||||||
count++; //нумерация чисел
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count == 0) { //если не было введено никакое число
|
if (count == 0) {
|
||||||
printf("Chyba: Malo platnych hodnot.\n");
|
printf("Chyba: Malo platnych hodnot.\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < count; i++) { //выписывание всех чисел
|
for (int i = 0; i < count; i++) {
|
||||||
printf("Sutaziaci c. %d vypil %d poharov.\n",i+1,results[i]);
|
printf("Sutaziaci c. %d vypil %d poharov.\n",i+1,results[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,17 +3,17 @@
|
|||||||
|
|
||||||
int read_number (double *num) {
|
int read_number (double *num) {
|
||||||
char buffer[50];
|
char buffer[50];
|
||||||
if (!fgets(buffer, sizeof(buffer), stdin)) { //если fgets не смог считать строку
|
if (!fgets(buffer, sizeof(buffer), stdin)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sscanf(buffer, "%lf", num) != 1) { //переделываем в double. Если всё успешно
|
if (sscanf(buffer, "%lf", num) != 1) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
double hornerovaschema (double x, double cofecients[], int count) { //вычесляет значение многочлена в точке x
|
double hornerovaschema (double x, double cofecients[], int count) {
|
||||||
double result = 0.0;
|
double result = 0.0;
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
result = result * x + cofecients[i];
|
result = result * x + cofecients[i];
|
||||||
@ -22,19 +22,19 @@ double hornerovaschema (double x, double cofecients[], int count) { //вычес
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
double x; //хранит точку, в которой вычисляется многочлен.
|
double x;
|
||||||
double cof[maxcoe];
|
double cof[maxcoe];
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
if (!read_number(&x)) { //если не может считать
|
if (!read_number(&x)) {
|
||||||
printf("Nepodarilo sa nacitat zaklad x\n");
|
printf("Nepodarilo sa nacitat zaklad x\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
while (count < maxcoe) {
|
while (count < maxcoe) {
|
||||||
double coef; //переменна для хранения считанного коэффициента
|
double coef;
|
||||||
char buffer[50];//строковый буфер для fgetc
|
char buffer[50];
|
||||||
|
|
||||||
if (!fgets(buffer, sizeof(buffer), stdin) || buffer[0] == '\n') {
|
if (!fgets(buffer, sizeof(buffer), stdin) || buffer[0] == '\n') {
|
||||||
break;
|
break;
|
||||||
|
BIN
du2/program.exe
BIN
du2/program.exe
Binary file not shown.
43
du5/snake.c
Normal file
43
du5/snake.c
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "snake.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
struct snake* add_snake (struct snake* snake, int x, int y) {
|
||||||
|
struct snake* head = NULL;
|
||||||
|
|
||||||
|
head = malloc(sizeof(struct snake));
|
||||||
|
|
||||||
|
if (!head) {
|
||||||
|
return snake;
|
||||||
|
}
|
||||||
|
head->x = x;
|
||||||
|
head->y = y;
|
||||||
|
|
||||||
|
head->next = NULL;
|
||||||
|
|
||||||
|
if (snake !=NULL) {
|
||||||
|
head->next = snake;
|
||||||
|
}
|
||||||
|
return head;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct snake* remove_snake(struct snake* snake) {
|
||||||
|
if(snake == NULL || snake-> next == NULL) {
|
||||||
|
free(snake);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct snake* previous = NULL;
|
||||||
|
struct snake* current = NULL;
|
||||||
|
while (current->next !=NULL) {
|
||||||
|
previous = current;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (previous !=NULL) {
|
||||||
|
free(previous->next);
|
||||||
|
previous->next = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
111
du5/snake.h
Normal file
111
du5/snake.h
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
#ifndef snake_h_INCLUDED
|
||||||
|
#define snake_h_INCLUDED
|
||||||
|
|
||||||
|
// Number of food items on the plane
|
||||||
|
#define FOOD_COUNT 5
|
||||||
|
|
||||||
|
/**
|
||||||
|
* One part of the snake;
|
||||||
|
*
|
||||||
|
* The snake is a linked list;
|
||||||
|
*/
|
||||||
|
struct snake {
|
||||||
|
// x position of the snake part
|
||||||
|
int x;
|
||||||
|
// y position of the snake part
|
||||||
|
int y;
|
||||||
|
// Pointer to the next snake part.
|
||||||
|
// The last part of the snake has NULL pointer to the next part.
|
||||||
|
struct snake* next;
|
||||||
|
};
|
||||||
|
|
||||||
|
// End game reason constants, return value of step_state
|
||||||
|
enum endgame {
|
||||||
|
// Continue the game
|
||||||
|
END_CONTINUE = 0,
|
||||||
|
// Snake hit a wall
|
||||||
|
END_WALL,
|
||||||
|
// Snake hit itself
|
||||||
|
END_SNAKE,
|
||||||
|
// No food left
|
||||||
|
END_FOOD,
|
||||||
|
// Other reason to end
|
||||||
|
END_USER
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* State of the game.
|
||||||
|
*
|
||||||
|
* The state consists of the snake, its speed and food on the plane.
|
||||||
|
*
|
||||||
|
* The snake is a linked list of snake parts.
|
||||||
|
*
|
||||||
|
* Speed vector is a vector added to the last head position to create a new head.
|
||||||
|
*
|
||||||
|
* Food are points on the plane. Food with negative coordinates means food is already eaten.
|
||||||
|
*/
|
||||||
|
struct state {
|
||||||
|
// Snake as a linked list
|
||||||
|
struct snake* snake;
|
||||||
|
// X of the food positions
|
||||||
|
int foodx[FOOD_COUNT];
|
||||||
|
// Y of the food positions
|
||||||
|
int foody[FOOD_COUNT];
|
||||||
|
int sx;
|
||||||
|
int sy;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a new snake part with given position. The new snake part becomes the new head.
|
||||||
|
*
|
||||||
|
* @param head of the snake.
|
||||||
|
* @param x coordinate of the new head;
|
||||||
|
* @param y coordinate of the new head.
|
||||||
|
* @return new head of the snake.
|
||||||
|
*/
|
||||||
|
struct snake* add_snake(struct snake* snake, int x, int y);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the last snake part.
|
||||||
|
* The last snake part should always have NULL next pointer.
|
||||||
|
*
|
||||||
|
* @param head of the snake.
|
||||||
|
* @return new head of the snake.
|
||||||
|
*/
|
||||||
|
struct snake* remove_snake(struct snake* snake);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds out if given coordinates are part of the snake.
|
||||||
|
* @param snake
|
||||||
|
* @param x coordinate to search in snake
|
||||||
|
* @param y coordinate to search in snake
|
||||||
|
* @return True, if there is a snake part with coordinates x,y. False otherwise
|
||||||
|
*/
|
||||||
|
int is_snake(struct snake* snake, int x, int y);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove and free each snake part;
|
||||||
|
* @param head of the snake.
|
||||||
|
*/
|
||||||
|
void free_snake(struct snake* sn);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change game state.
|
||||||
|
*
|
||||||
|
* The function should calculate new position of the snake head
|
||||||
|
* from the current position and speed vector.
|
||||||
|
* Then it should modify snake parts or food coordinates according to the rules:
|
||||||
|
*
|
||||||
|
* - If the new position is on the snake, end the game, return END_SNAKE.
|
||||||
|
* - If the new position is on the food, mark food as eaten
|
||||||
|
* (set its coordinates to -1) and add new snake part on the position of the food. If there is no food left, return END_FOOD. Else return END_CONTINUE.
|
||||||
|
* - If the new position is on the plane, add new snake part on the new position and remove the last part of the snake, return END_CONTINUE.
|
||||||
|
*
|
||||||
|
* @param current state of the game
|
||||||
|
* @return reason to end the game according to enum endgame.
|
||||||
|
*/
|
||||||
|
int step_state(struct state* state);
|
||||||
|
|
||||||
|
#endif // snake_h_INCLUDED
|
BIN
du5/snake.o
Normal file
BIN
du5/snake.o
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user