Skip to content
Snippets Groups Projects
Commit 481c9891 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Beispielprogramme 5.2.2018

parent 6ac649ea
No related branches found
No related tags found
No related merge requests found
#include <stdio.h>
#include <unistd.h>
#include "textgraph.h"
void draw_smiley (void)
{
clear (' ');
put_point (-5, 10, 'X');
for (int i = 16; i < 24; i++)
put_point (i, 5, '*');
put_point (15, 6, '*');
put_point (14, 7, '*');
put_point (13, 8, '*');
put_point (13, 9, '*');
put_point (14, 10, '*');
put_point (15, 11, '*');
for (int i = 16; i < 24; i++)
put_point (i, 12, '*');
put_point (24, 11, '*');
put_point (25, 10, '*');
put_point (26, 9, '*');
put_point (26, 8, '*');
put_point (25, 7, '*');
put_point (24, 6, '*');
put_point (18, 8, 'O');
put_point (21, 8, 'O');
put_point (17, 10, '`');
for (int i = 18; i < 22; i++)
put_point (i, 10, '-');
put_point (22, 10, '\'');
put_point (13, 42, 'Y');
printf ("get_point (%d, %d): '%c'\n", 13, 9, get_point (13, 9));
printf ("get_point (%d, %d): '%c'\n", 14, 9, get_point (14, 9));
printf ("get_point (%d, %d): '%c'\n", 94, 9, get_point (94, 9));
}
int main (void)
{
draw_smiley ();
fill_animated (21, 7, '.', ' ');
sleep (5);
draw_smiley ();
fill_stack (21, 7, '.', ' ');
sleep (5);
draw_smiley ();
fill_fifo (21, 7, '.', ' ');
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include "textgraph.h"
char buffer[HEIGHT][WIDTH];
void clear (char c)
{
for (int y = 0; y < HEIGHT; y++)
for (int x = 0; x < WIDTH; x++)
buffer[y][x] = c;
}
static int check_coordinates (int x, int y, char *function)
{
if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT)
return 1;
else
{
fprintf (stderr, "coordinates (%d,%d) out of range in %s\n", x, y, function);
return 0;
}
}
void put_point (int x, int y, char c)
{
if (check_coordinates (x, y, "put_point"))
buffer[y][x] = c;
}
char get_point (int x, int y)
{
if (check_coordinates (x, y, "get_point"))
return buffer[y][x];
else
return 0;
}
void fill (int x, int y, char c, char o)
{
if (get_point (x, y) == o)
{
put_point (x, y, c);
fill (x - 1, y, c, o);
fill (x + 1, y, c, o);
fill (x, y - 1, c, o);
fill (x, y + 1, c, o);
}
}
void display (void)
{
for (int y = 0; y < HEIGHT; y++)
{
for (int x = 0; x < WIDTH; x++)
printf ("%c", buffer[y][x]);
printf ("\n");
}
}
void clear_display (void)
{
printf ("\e[H\e[J");
}
void fill_animated (int x, int y, char c, char o)
{
if (get_point (x, y) == o)
{
put_point (x, y, c);
clear_display ();
display ();
usleep (200000);
fill_animated (x - 1, y, c, o);
fill_animated (x + 1, y, c, o);
fill_animated (x, y - 1, c, o);
fill_animated (x, y + 1, c, o);
}
}
pair stack[STACK_SIZE];
int stack_pointer = 0;
void stack_push (int x, int y)
{
stack[stack_pointer].x = x;
stack[stack_pointer].y = y;
stack_pointer++;
}
void stack_pop (int *x, int *y)
{
stack_pointer--;
*x = stack[stack_pointer].x;
*y = stack[stack_pointer].y;
}
void fill_stack (int x, int y, char c, char o)
{
stack_push (x, y);
while (stack_pointer > 0)
{
stack_pop (&x, &y);
if (get_point (x, y) == o)
{
put_point (x, y, c);
clear_display ();
display ();
usleep (200000);
stack_push (x, y + 1);
stack_push (x, y - 1);
stack_push (x + 1, y);
stack_push (x - 1, y);
}
}
}
pair fifo[FIFO_SIZE];
int fifo_read = 0;
int fifo_write = 0;
void fifo_push (int x, int y)
{
fifo[fifo_write].x = x;
fifo[fifo_write].y = y;
fifo_write = (fifo_write + 1) % FIFO_SIZE;
}
void fifo_pop (int *x, int *y)
{
*x = fifo[fifo_read].x;
*y = fifo[fifo_read].y;
fifo_read = (fifo_read + 1) % FIFO_SIZE;
}
void fill_fifo (int x, int y, char c, char o)
{
fifo_push (x, y);
while (fifo_write != fifo_read)
{
fifo_pop (&x, &y);
if (get_point (x, y) == o)
{
put_point (x, y, c);
clear_display ();
display ();
usleep (200000);
fifo_push (x, y + 1);
fifo_push (x, y - 1);
fifo_push (x + 1, y);
fifo_push (x - 1, y);
}
}
}
#ifndef TEXTGRAPH_H
#define TEXTGRAPH_H
#define WIDTH 40
#define HEIGHT 20
#define STACK_SIZE 100
#define FIFO_SIZE 100
typedef struct
{
int x, y;
} pair;
extern void clear (char c);
extern void put_point (int x, int y, char c);
extern char get_point (int x, int y);
extern void fill (int x, int y, char c, char o);
extern void display (void);
extern void clear_display (void);
extern void fill_animated (int x, int y, char c, char o);
extern void stack_push (int x, int y);
extern void stack_pop (int *x, int *y);
extern void fill_stack (int x, int y, char c, char o);
extern void fifo_push (int x, int y);
extern void fifo_pop (int *x, int *y);
extern void fill_fifo (int x, int y, char c, char o);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment