diff --git a/20151126/fifo-1.c b/20151126/fifo-1.c new file mode 100644 index 0000000000000000000000000000000000000000..0efa5725e445adad0f936e373938e3129b0f507b --- /dev/null +++ b/20151126/fifo-1.c @@ -0,0 +1,67 @@ +#include <stdio.h> +#include <stdlib.h> + +#define FIFO_SIZE 42 + +int fifo[FIFO_SIZE]; +int fifo_pointer = 0; + +void fatal_error (char *msg) +{ + fprintf (stderr, "%s\n", msg); + exit (1); +} + +void push (int x) +{ + if (fifo_pointer < 0 || fifo_pointer >= FIFO_SIZE) + fatal_error ("fifo overflow"); + else + fifo[fifo_pointer++] = x; +} + +int pop (void) +{ + if (fifo_pointer < 1 || fifo_pointer > FIFO_SIZE) + { + fatal_error ("fifo underflow"); + return 0; + } + else + return fifo[--fifo_pointer]; +} + +void dump (void) +{ + if (fifo_pointer < 0) + fatal_error ("fifo underflow"); + else if (fifo_pointer >= FIFO_SIZE) + fatal_error ("fifo overflow"); + else if (fifo_pointer == 0) + printf ("fifo is empty\n"); + else + { + printf ("fifo:"); + for (int i = 0; i < fifo_pointer; i++) + printf (" %d", fifo[i]); + printf ("\n"); + } +} + +int main (void) +{ + dump (); + push (7); + dump (); + push (13); + dump (); + push (42); + dump (); + printf ("%d\n", pop ()); /* 42 */ + dump (); + printf ("%d\n", pop ()); /* 13 */ + dump (); + printf ("%d\n", pop ()); /* 7 */ + dump (); + return 0; +} diff --git a/20151126/fifo-2.c b/20151126/fifo-2.c new file mode 100644 index 0000000000000000000000000000000000000000..7dcc39bd9412e210964e1b08289dd131b65083a8 --- /dev/null +++ b/20151126/fifo-2.c @@ -0,0 +1,73 @@ +#include <stdio.h> +#include <stdlib.h> + +#define FIFO_SIZE 42 + +int fifo[FIFO_SIZE]; +int fifo_pointer = 0; + +void fatal_error (char *msg) +{ + fprintf (stderr, "%s\n", msg); + exit (1); +} + +void push (int x) +{ + if (fifo_pointer < 0 || fifo_pointer >= FIFO_SIZE) + fatal_error ("fifo overflow"); + else + fifo[fifo_pointer++] = x; +} + +int pop (void) +{ + if (fifo_pointer < 1 || fifo_pointer > FIFO_SIZE) + { + fatal_error ("fifo underflow"); + return 0; + } + else + { + int value = fifo[0]; + for (int i = 1; i < fifo_pointer; i++) + fifo[i - 1] = fifo[i]; + fifo_pointer--; + return value; + } +} + +void dump (void) +{ + if (fifo_pointer < 0) + fatal_error ("fifo underflow"); + else if (fifo_pointer >= FIFO_SIZE) + fatal_error ("fifo overflow"); + else if (fifo_pointer == 0) + printf ("fifo is empty\n"); + else + { + printf ("fifo:"); + for (int i = 0; i < fifo_pointer; i++) + printf (" %d", fifo[i]); + printf ("\n"); + } +} + +int main (void) +{ + dump (); + push (7); + dump (); + push (13); + dump (); + push (42); + dump (); + printf ("%d\n", pop ()); /* 42 */ + dump (); + printf ("%d\n", pop ()); /* 13 */ + dump (); + printf ("%d\n", pop ()); /* 7 */ + dump (); + return 0; +} diff --git a/20151126/fifo-3.c b/20151126/fifo-3.c new file mode 100644 index 0000000000000000000000000000000000000000..df62cbbbd471e60a42f60dd1cd293785cfe5c137 --- /dev/null +++ b/20151126/fifo-3.c @@ -0,0 +1,70 @@ +#include <stdio.h> +#include <stdlib.h> + +#define FIFO_SIZE 42 + +int fifo[FIFO_SIZE]; +int fifo_read_pointer = 0; +int fifo_write_pointer = 0; + +void fatal_error (char *msg) +{ + fprintf (stderr, "%s\n", msg); + exit (1); +} + +void push (int x) +{ + if (fifo_write_pointer < 0 || fifo_write_pointer >= FIFO_SIZE) + fatal_error ("fifo overflow"); + else + fifo[fifo_write_pointer++] = x; +} + +int pop (void) +{ + if (fifo_read_pointer < 0 || fifo_read_pointer >= fifo_write_pointer) + { + fatal_error ("fifo underflow"); + return 0; + } + else + return fifo[fifo_read_pointer++]; +} + +void dump (void) +{ +/* + if (fifo_pointer < 0) + fatal_error ("fifo underflow"); + else if (fifo_pointer >= FIFO_SIZE) + fatal_error ("fifo overflow"); + else if (fifo_pointer == 0) + printf ("fifo is empty\n"); + else + { + printf ("fifo:"); + for (int i = 0; i < fifo_pointer; i++) + printf (" %d", fifo[i]); + printf ("\n"); + } +*/ +} + +int main (void) +{ + dump (); + push (7); + dump (); + push (13); + dump (); + push (42); + dump (); + printf ("%d\n", pop ()); /* 42 */ + dump (); + printf ("%d\n", pop ()); /* 13 */ + dump (); + printf ("%d\n", pop ()); /* 7 */ + dump (); + return 0; +} diff --git a/20151126/fifo-4.c b/20151126/fifo-4.c new file mode 100644 index 0000000000000000000000000000000000000000..d4efc400ef4580a672a2b12037d98e70f594ac3d --- /dev/null +++ b/20151126/fifo-4.c @@ -0,0 +1,62 @@ +#include <stdio.h> +#include <stdlib.h> + +#define FIFO_SIZE 42 + +int fifo[FIFO_SIZE]; +int fifo_read_pointer = 0; +int fifo_write_pointer = 0; + +void fatal_error (char *msg) +{ + fprintf (stderr, "%s\n", msg); + exit (1); +} + +void push (int x) +{ + if (fifo_write_pointer < 0 || fifo_write_pointer >= FIFO_SIZE) + fatal_error ("push: fifo inconsistence"); + else + { + fifo[fifo_write_pointer++] = x; + if (fifo_write_pointer >= FIFO_SIZE) + fifo_write_pointer = 0; + if (fifo_write_pointer == fifo_read_pointer) + fatal_error ("fifo overflow"); + } +} + +int pop (void) +{ + if (fifo_read_pointer < 0 || fifo_read_pointer >= FIFO_SIZE) + { + fatal_error ("pop: fifo inconsistence"); + return 0; + } + else if (fifo_read_pointer == fifo_write_pointer) + { + fatal_error ("fifo underflow"); + return 0; + } + else + { + int value = fifo[fifo_read_pointer++]; + if (fifo_read_pointer >= FIFO_SIZE) + fifo_read_pointer = 0; + return value; + } +} + +int main (void) +{ + int value = 42; + for (int i = 0; i < 7; i++) + { + for (int j = 0; j < 10; j++) + push (value++); + for (int j = 0; j < 10; j++) + printf ("%d\n", pop ()); + } + return 0; +} diff --git a/20151126/orzol/Text-Grafik-Bibliothek-1.c b/20151126/orzol/Text-Grafik-Bibliothek-1.c new file mode 100644 index 0000000000000000000000000000000000000000..3cb47ec44a49eeed5b8ef847101e085b2c1544a3 --- /dev/null +++ b/20151126/orzol/Text-Grafik-Bibliothek-1.c @@ -0,0 +1,57 @@ +#include "Text-Grafik-Bibliothek-20x15.h" +#include <stdio.h> +#include "stack.h" + +char screen[WIDTH][HEIGHT]; + +//Setze alle Einträge in display auf c +void clear (char c) +{ + for (int i=0;i<WIDTH;i++) + { + for (int j=0;j<HEIGHT;j++) + { + screen[i][j]=c; + } + } +} + +//setze display [x][y] auf c +void putPoint (int x, int y, char c) +{ + screen[x][y]=c; +} + +//Gebe display[x][y] zurück +char getPoint (int x, int y) +{ + char c=screen[x][y]; + return c; +} + +//Fülle eine Fläche in display, die aus den Zeichen o besteht mit den Zeichen c aus +void fill (int x, int y, char c, char o) +{ + if (getPoint(x,y)==o) + { + putPoint(x,y,c); + push(x+1,y); + push(x-1,y); + push(x,y+1); + push(x,y-1); + } +} + +//Gib display aus +void display (void) +{ + for (int i=0;i<HEIGHT;i++) + { + for (int j=0;j<WIDTH;j++) + { + printf("%c",screen[j][i]); + } + printf("\n"); + } +} + diff --git a/20151126/orzol/Text-Grafik-Bibliothek-2.c b/20151126/orzol/Text-Grafik-Bibliothek-2.c new file mode 100644 index 0000000000000000000000000000000000000000..54df172957266735cc72b1b75f61379f136b3b0e --- /dev/null +++ b/20151126/orzol/Text-Grafik-Bibliothek-2.c @@ -0,0 +1,62 @@ +#include "Text-Grafik-Bibliothek-20x15.h" +#include <stdio.h> +#include "stack.h" + +char screen[WIDTH][HEIGHT]; + +//Setze alle Einträge in display auf c +void clear (char c) +{ + for (int i=0;i<WIDTH;i++) + { + for (int j=0;j<HEIGHT;j++) + { + screen[i][j]=c; + } + } +} + +//setze display [x][y] auf c +void putPoint (int x, int y, char c) +{ + screen[x][y]=c; +} + +//Gebe display[x][y] zurück +char getPoint (int x, int y) +{ + char c=screen[x][y]; + return c; +} + +//Fülle eine Fläche in display, die aus den Zeichen o besteht mit den Zeichen c aus +void fill (int x, int y, char c, char o) +{ + push (x, y); + while (!stack_empty ()) + { + pop (&x, &y); + if (getPoint (x, y) == o) + { + putPoint (x, y, c); + push (x + 1, y); + push (x - 1, y); + push (x, y + 1); + push (x, y - 1); + } + } +} + +//Gib display aus +void display (void) +{ + for (int i=0;i<HEIGHT;i++) + { + for (int j=0;j<WIDTH;j++) + { + printf("%c",screen[j][i]); + } + printf("\n"); + } +} + diff --git a/20151126/orzol/Text-Grafik-Bibliothek-20x15.c b/20151126/orzol/Text-Grafik-Bibliothek-20x15.c new file mode 100644 index 0000000000000000000000000000000000000000..e721fe2aa9592d0d68f37daad6b39af41b5d6aa4 --- /dev/null +++ b/20151126/orzol/Text-Grafik-Bibliothek-20x15.c @@ -0,0 +1,56 @@ +#include "Text-Grafik-Bibliothek-20x15.h" +#include <stdio.h> + +char screen[WIDTH][HEIGHT]; + +//Setze alle Einträge in display auf c +void clear (char c) +{ + for (int i=0;i<WIDTH;i++) + { + for (int j=0;j<HEIGHT;j++) + { + screen[i][j]=c; + } + } +} + +//setze display [x][y] auf c +void putPoint (int x, int y, char c) +{ + screen[x][y]=c; +} + +//Gebe display[x][y] zurück +char getPoint (int x, int y) +{ + char c=screen[x][y]; + return c; +} + +//Fülle eine Fläche in display, die aus den Zeichen o besteht mit den Zeichen c aus +void fill (int x, int y, char c, char o) +{ + if (getPoint(x,y)==o) + { + putPoint(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); + } +} + +//Gib display aus +void display (void) +{ + for (int i=0;i<HEIGHT;i++) + { + for (int j=0;j<WIDTH;j++) + { + printf("%c",screen[j][i]); + } + printf("\n"); + } +} + diff --git a/20151126/orzol/Text-Grafik-Bibliothek-20x15.h b/20151126/orzol/Text-Grafik-Bibliothek-20x15.h new file mode 100644 index 0000000000000000000000000000000000000000..39486cbc46e85372555e7d3d5faec3e30134ea83 --- /dev/null +++ b/20151126/orzol/Text-Grafik-Bibliothek-20x15.h @@ -0,0 +1,8 @@ +#define HEIGHT 20 +#define WIDTH 15 + +extern void clear(char c); +extern void fill(int x, int y, char c, char o); +extern void putPoint(int x, int y, char c); +extern char getPoint(int x, int y); +extern void display(void); diff --git a/20151126/orzol/Text-Grafik-Bibliothek-3.c b/20151126/orzol/Text-Grafik-Bibliothek-3.c new file mode 100644 index 0000000000000000000000000000000000000000..9353e6a787e95b74b50b4131fd239db72cfa67a9 --- /dev/null +++ b/20151126/orzol/Text-Grafik-Bibliothek-3.c @@ -0,0 +1,62 @@ +#include "Text-Grafik-Bibliothek-20x15.h" +#include <stdio.h> +#include "stack.h" + +char screen[WIDTH][HEIGHT]; + +//Setze alle Einträge in display auf c +void clear (char c) +{ + for (int i=0;i<WIDTH;i++) + { + for (int j=0;j<HEIGHT;j++) + { + screen[i][j]=c; + } + } +} + +//setze display [x][y] auf c +void putPoint (int x, int y, char c) +{ + screen[x][y]=c; +} + +//Gebe display[x][y] zurück +char getPoint (int x, int y) +{ + char c=screen[x][y]; + return c; +} + +//Fülle eine Fläche in display, die aus den Zeichen o besteht mit den Zeichen c aus +void fill (int x, int y, char c, char o) +{ + push (x, y); + while (!stack_empty ()) + { + pop (&x, &y); + if (getPoint (x, y) == o) + { + putPoint (x, y, c++); + push (x + 1, y); + push (x - 1, y); + push (x, y + 1); + push (x, y - 1); + } + } +} + +//Gib display aus +void display (void) +{ + for (int i=0;i<HEIGHT;i++) + { + for (int j=0;j<WIDTH;j++) + { + printf("%c",screen[j][i]); + } + printf("\n"); + } +} + diff --git a/20151126/orzol/Text-Grafik-Bibliothek-pg.c b/20151126/orzol/Text-Grafik-Bibliothek-pg.c new file mode 100644 index 0000000000000000000000000000000000000000..ec61ddcd8ae01b448a5dff940e5015e635753f15 --- /dev/null +++ b/20151126/orzol/Text-Grafik-Bibliothek-pg.c @@ -0,0 +1,56 @@ +#include "Text-Grafik-Bibliothek.h" +#include <stdio.h> + +char screen[WIDTH][HEIGHT]; + +//Setze alle Einträge in display auf c +void clear (char c) +{ + for (int i=0;i<WIDTH;i++) + { + for (int j=0;j<HEIGHT;j++) + { + screen[i][j]=c; + } + } +} + +//setze display [x][y] auf c +void putPoint (int x, int y, char c) +{ + screen[x][y]=c; +} + +//Gebe display[x][y] zurück +char getPoint (int x, int y) +{ + char c=screen[x][y]; + return c; +} + +//Fülle eine Fläche in display, die aus den Zeichen o besteht mit den Zeichen c aus +void fill (int x, int y, char c, char o) +{ + if (getPoint(x,y)==o) + { + putPoint(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); + } +} + +//Gib display aus +void display (void) +{ + for (int i=0;i<HEIGHT;i++) + { + for (int j=0;j<WIDTH;j++) + { + printf("%c",screen[j][i]); + } + printf("\n"); + } +} + diff --git a/20151126/orzol/Text-Grafik-Bibliothek.c b/20151126/orzol/Text-Grafik-Bibliothek.c new file mode 100644 index 0000000000000000000000000000000000000000..476ed457a5d90c2d717859c26d105a08d41fab98 --- /dev/null +++ b/20151126/orzol/Text-Grafik-Bibliothek.c @@ -0,0 +1,56 @@ +#include "Text-Grafik-Bibliothek.h" +#include <stdio.h> + +char screen[WIDTH][HEIGHT]; + +//Setze alle Einträge in display auf c +void clear (char c) +{ + for (int i=0;i<WIDTH;i++) + { + for (int j=0;j<HEIGHT;j++) + { + screen[i][j]=c; + } + } +} + +//setze display [x][y] auf c +void putPoint (int x, int y, char c) +{ + screen[x][y]=c; +} + +//Gebe display[x][y] zurück +char getPoint (int x, int y) +{ + char c=screen[x][y]; + return c; +} + +//Fülle eine Fläche in display, die aus den Zeichen o besteht mit den Zeichen c aus +void fill (int x, int y, char c, char o) +{ + if (getPoint(x,y)==o) + { + putPoint(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); + } +} + +//Gib display aus +void display (void) +{ + for (int i=0;i<HEIGHT;i++) + { + for (int j=0;j<WIDTH;j++) + { + printf("%c",screen[i][j]); + } + printf("\n"); + } +} + diff --git a/20151126/orzol/Text-Grafik-Bibliothek.h b/20151126/orzol/Text-Grafik-Bibliothek.h new file mode 100644 index 0000000000000000000000000000000000000000..287e36bff8184a4e0ed4b4776ec4b2f29dd961f8 --- /dev/null +++ b/20151126/orzol/Text-Grafik-Bibliothek.h @@ -0,0 +1,8 @@ +#define HEIGHT 5 +#define WIDTH 10 + +extern void clear(char c); +extern void fill(int x, int y, char c, char o); +extern void putPoint(int x, int y, char c); +extern char getPoint(int x, int y); +extern void display(void); diff --git a/20151126/orzol/blink.sh b/20151126/orzol/blink.sh new file mode 100644 index 0000000000000000000000000000000000000000..bd584461ce9c60ce6a74154c823c852c4f649119 --- /dev/null +++ b/20151126/orzol/blink.sh @@ -0,0 +1 @@ +while true; do clear; ./test; sleep 1; clear; ./test-pg; sleep 1; done diff --git a/20151126/orzol/stack.c b/20151126/orzol/stack.c new file mode 100644 index 0000000000000000000000000000000000000000..db90edee88af742b699f1eb302539465d8d802fc --- /dev/null +++ b/20151126/orzol/stack.c @@ -0,0 +1,62 @@ +#include "stack.h" + +typedef struct +{ + int x, y; +} +coordinate; + +coordinate stack[STACK_SIZE]; +int stack_pointer = 0; + +int stack_empty (void) +{ + return !stack_pointer; +} + +void fatal_error (char *msg) +{ + fprintf (stderr, "%s\n", msg); + exit (1); +} + +void push (int x, int y) +{ + if (stack_pointer < 0 || stack_pointer >= STACK_SIZE) + fatal_error ("stack overflow"); + else + { + stack[stack_pointer].x = x; + stack[stack_pointer].y = y; + stack_pointer++; + } +} + +void pop (int *x, int *y) +{ + if (stack_pointer < 1 || stack_pointer > STACK_SIZE) + fatal_error ("stack underflow"); + else + { + --stack_pointer; + *x = stack[stack_pointer].x; + *y = stack[stack_pointer].y; + } +} + +void dump (void) +{ + if (stack_pointer < 0) + fatal_error ("stack underflow"); + else if (stack_pointer >= STACK_SIZE) + fatal_error ("stack overflow"); + else if (stack_pointer == 0) + printf ("stack is empty\n"); + else + { + printf ("stack:"); + for (int i = 0; i < stack_pointer; i++) + printf (" (%d,%d)", stack[i].x, stack[i].y); + printf ("\n"); + } +} diff --git a/20151126/orzol/stack.h b/20151126/orzol/stack.h new file mode 100644 index 0000000000000000000000000000000000000000..b27f7ece304f4e6f0dc6326bb29d1024194c6882 --- /dev/null +++ b/20151126/orzol/stack.h @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <stdlib.h> + +#define STACK_SIZE (7 * 42) + +extern int stack_empty (void); +extern void push (int x, int y); +extern void pop (int *x, int *y); +extern void dump (void); diff --git a/20151126/orzol/test-1.c b/20151126/orzol/test-1.c new file mode 100644 index 0000000000000000000000000000000000000000..3ea0c45db64758ef7e741595a34f22072dc675d3 --- /dev/null +++ b/20151126/orzol/test-1.c @@ -0,0 +1,10 @@ +#include "Text-Grafik-Bibliothek.h" +#include <stdio.h> + +int main (void) +{ + clear ('.'); + putPoint (3, 1, '*'); + display (); + return 0; +} diff --git a/20151126/orzol/test-2.c b/20151126/orzol/test-2.c new file mode 100644 index 0000000000000000000000000000000000000000..efb9c854850ed9052c65c937ac3f400f8693d7e2 --- /dev/null +++ b/20151126/orzol/test-2.c @@ -0,0 +1,10 @@ +#include "Text-Grafik-Bibliothek.h" +#include <stdio.h> + +int main (void) +{ + clear ('.'); + putPoint (10, 1, '*'); + display (); + return 0; +} diff --git a/20151126/orzol/test-3.c b/20151126/orzol/test-3.c new file mode 100644 index 0000000000000000000000000000000000000000..fc3262a15bb7f9fb1a3a9cf6d6402c168466ae77 --- /dev/null +++ b/20151126/orzol/test-3.c @@ -0,0 +1,10 @@ +#include "Text-Grafik-Bibliothek.h" +#include <stdio.h> + +int main (void) +{ + clear ('.'); + putPoint (3, 6, '*'); + display (); + return 0; +} diff --git a/20151126/orzol/test-4.c b/20151126/orzol/test-4.c new file mode 100644 index 0000000000000000000000000000000000000000..c1fb84c51f6f9f29dbdd738201f3737ede8f61a9 --- /dev/null +++ b/20151126/orzol/test-4.c @@ -0,0 +1,23 @@ +#include "Text-Grafik-Bibliothek-20x15.h" +#include <stdio.h> + +int main (void) +{ + clear ('.'); + for (int i = 3; i < 13; i++) + putPoint (i, 3, '*'); + for (int j = 4; j < 11; j++) + putPoint (2, j, '*'); + putPoint (13, 4, '*'); + putPoint (12, 5, '*'); + putPoint (12, 6, '*'); + putPoint (13, 7, '*'); + putPoint (14, 8, '*'); + putPoint (14, 9, '*'); + putPoint (13, 10, '*'); + for (int i = 3; i < 13; i++) + putPoint (i, 11, '*'); + fill (5, 5, '%', '.'); + display (); + return 0; +} diff --git a/20151126/orzol/test-5.c b/20151126/orzol/test-5.c new file mode 100644 index 0000000000000000000000000000000000000000..400e0ce2fadaa0ea3a4c1a878a9eb44955fe6d74 --- /dev/null +++ b/20151126/orzol/test-5.c @@ -0,0 +1,23 @@ +#include "Text-Grafik-Bibliothek-20x15.h" +#include <stdio.h> + +int main (void) +{ + clear ('.'); + for (int i = 3; i < 13; i++) + putPoint (i, 3, '*'); + for (int j = 4; j < 11; j++) + putPoint (2, j, '*'); + putPoint (13, 4, '*'); + putPoint (12, 5, '*'); + putPoint (12, 6, '*'); + putPoint (13, 7, '*'); + putPoint (14, 8, '*'); + putPoint (14, 9, '*'); + putPoint (13, 10, '*'); + for (int i = 3; i < 13; i++) + putPoint (i, 11, 'A'); + fill (5, 5, '%', '.'); + display (); + return 0; +} diff --git a/20151126/orzol/test.c b/20151126/orzol/test.c new file mode 100644 index 0000000000000000000000000000000000000000..36d29c9b3247fd21fa50a19134f0ac12edd8277b --- /dev/null +++ b/20151126/orzol/test.c @@ -0,0 +1,22 @@ +#include "Text-Grafik-Bibliothek.h" +#include <stdio.h> + +int main(void) +{ +clear(' '); +display(); +printf("______________\n"); +putPoint(1,1,'A'); +display(); +printf("______________\n"); +fill(2,3,'*',' '); +display(); +printf("______________\n"); +putPoint(3,4,'B'); +display(); + + + + +return 0; +} diff --git a/20151126/stack-0.c b/20151126/stack-0.c new file mode 100644 index 0000000000000000000000000000000000000000..08b4cbd2790f0b090620b4fe8dbea77323268ba2 --- /dev/null +++ b/20151126/stack-0.c @@ -0,0 +1,12 @@ +#include <stdio.h> + +int main (void) +{ + push (7); + push (13); + push (42); + printf ("%d\n", pop ()); /* 42 */ + printf ("%d\n", pop ()); /* 13 */ + printf ("%d\n", pop ()); /* 7 */ + return 0; +} diff --git a/20151126/stack-1.c b/20151126/stack-1.c new file mode 100644 index 0000000000000000000000000000000000000000..a5d62784f47df7f9e2bbf2dcb51a76e65c93c20b --- /dev/null +++ b/20151126/stack-1.c @@ -0,0 +1,21 @@ +#include <stdio.h> + +void push (int x) +{ +} + +int pop (void) +{ + return 0; +} + +int main (void) +{ + push (7); + push (13); + push (42); + printf ("%d\n", pop ()); /* 42 */ + printf ("%d\n", pop ()); /* 13 */ + printf ("%d\n", pop ()); /* 7 */ + return 0; +} diff --git a/20151126/stack-2.c b/20151126/stack-2.c new file mode 100644 index 0000000000000000000000000000000000000000..37eaac67693807b1b019ee94211f10d2a1ad61bd --- /dev/null +++ b/20151126/stack-2.c @@ -0,0 +1,27 @@ +#include <stdio.h> + +#define STACK_SIZE 42 + +int stack[STACK_SIZE]; +int stack_pointer = 0; + +void push (int x) +{ + stack[stack_pointer++] = x; +} + +int pop (void) +{ + return 0; +} + +int main (void) +{ + push (7); + push (13); + push (42); + printf ("%d\n", pop ()); /* 42 */ + printf ("%d\n", pop ()); /* 13 */ + printf ("%d\n", pop ()); /* 7 */ + return 0; +} diff --git a/20151126/stack-3.c b/20151126/stack-3.c new file mode 100644 index 0000000000000000000000000000000000000000..ceacf173a0d9095ce94f51487dfe2162eadb635e --- /dev/null +++ b/20151126/stack-3.c @@ -0,0 +1,37 @@ +#include <stdio.h> +#include <stdlib.h> + +#define STACK_SIZE 42 + +int stack[STACK_SIZE]; +int stack_pointer = 0; + +void fatal_error (char *msg) +{ + fprintf (stderr, "%s\n", msg); + exit (1); +} + +void push (int x) +{ + if (stack_pointer < 0 || stack_pointer >= STACK_SIZE) + fatal_error ("stack overflow"); + else + stack[stack_pointer++] = x; +} + +int pop (void) +{ + return 0; +} + +int main (void) +{ + push (7); + push (13); + push (42); + printf ("%d\n", pop ()); /* 42 */ + printf ("%d\n", pop ()); /* 13 */ + printf ("%d\n", pop ()); /* 7 */ + return 0; +} diff --git a/20151126/stack-4.c b/20151126/stack-4.c new file mode 100644 index 0000000000000000000000000000000000000000..ceacf173a0d9095ce94f51487dfe2162eadb635e --- /dev/null +++ b/20151126/stack-4.c @@ -0,0 +1,37 @@ +#include <stdio.h> +#include <stdlib.h> + +#define STACK_SIZE 42 + +int stack[STACK_SIZE]; +int stack_pointer = 0; + +void fatal_error (char *msg) +{ + fprintf (stderr, "%s\n", msg); + exit (1); +} + +void push (int x) +{ + if (stack_pointer < 0 || stack_pointer >= STACK_SIZE) + fatal_error ("stack overflow"); + else + stack[stack_pointer++] = x; +} + +int pop (void) +{ + return 0; +} + +int main (void) +{ + push (7); + push (13); + push (42); + printf ("%d\n", pop ()); /* 42 */ + printf ("%d\n", pop ()); /* 13 */ + printf ("%d\n", pop ()); /* 7 */ + return 0; +} diff --git a/20151126/stack-5.c b/20151126/stack-5.c new file mode 100644 index 0000000000000000000000000000000000000000..d36031f200bb98154cdab689e97ae90a1e5f6138 --- /dev/null +++ b/20151126/stack-5.c @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <stdlib.h> + +#define STACK_SIZE 42 + +int stack[STACK_SIZE]; +int stack_pointer = 0; + +void fatal_error (char *msg) +{ + fprintf (stderr, "%s\n", msg); + exit (1); +} + +void push (int x) +{ + if (stack_pointer < 0 || stack_pointer >= STACK_SIZE) + fatal_error ("stack overflow"); + else + stack[stack_pointer++] = x; +} + +int pop (void) +{ + if (stack_pointer < 1 || stack_pointer > STACK_SIZE) + fatal_error ("stack underflow"); + else + return stack[--stack_pointer]; +} + +int main (void) +{ + push (7); + push (13); + push (42); + printf ("%d\n", pop ()); /* 42 */ + printf ("%d\n", pop ()); /* 13 */ + printf ("%d\n", pop ()); /* 7 */ + return 0; +} diff --git a/20151126/stack-6.c b/20151126/stack-6.c new file mode 100644 index 0000000000000000000000000000000000000000..5bed677f9fe452f00d5b77d73222af279704933e --- /dev/null +++ b/20151126/stack-6.c @@ -0,0 +1,43 @@ +#include <stdio.h> +#include <stdlib.h> + +#define STACK_SIZE 42 + +int stack[STACK_SIZE]; +int stack_pointer = 0; + +void fatal_error (char *msg) +{ + fprintf (stderr, "%s\n", msg); + exit (1); +} + +void push (int x) +{ + if (stack_pointer < 0 || stack_pointer >= STACK_SIZE) + fatal_error ("stack overflow"); + else + stack[stack_pointer++] = x; +} + +int pop (void) +{ + if (stack_pointer < 1 || stack_pointer > STACK_SIZE) + { + fatal_error ("stack underflow"); + return 0; + } + else + return stack[--stack_pointer]; +} + +int main (void) +{ + push (7); + push (13); + push (42); + printf ("%d\n", pop ()); /* 42 */ + printf ("%d\n", pop ()); /* 13 */ + printf ("%d\n", pop ()); /* 7 */ + return 0; +} diff --git a/20151126/stack-7.c b/20151126/stack-7.c new file mode 100644 index 0000000000000000000000000000000000000000..03e7ae564379393ef9c6d6cf3f55219e632695d7 --- /dev/null +++ b/20151126/stack-7.c @@ -0,0 +1,40 @@ +#include <stdio.h> +#include <stdlib.h> + +#define STACK_SIZE 42 + +int stack[STACK_SIZE]; +int stack_pointer = 0; + +__attribute__ ((noreturn)) void fatal_error (char *msg) +{ + fprintf (stderr, "%s\n", msg); + exit (1); +} + +void push (int x) +{ + if (stack_pointer < 0 || stack_pointer >= STACK_SIZE) + fatal_error ("stack overflow"); + else + stack[stack_pointer++] = x; +} + +int pop (void) +{ + if (stack_pointer < 1 || stack_pointer > STACK_SIZE) + fatal_error ("stack underflow"); + else + return stack[--stack_pointer]; +} + +int main (void) +{ + push (7); + push (13); + push (42); + printf ("%d\n", pop ()); /* 42 */ + printf ("%d\n", pop ()); /* 13 */ + printf ("%d\n", pop ()); /* 7 */ + return 0; +} diff --git a/20151126/stack-8.c b/20151126/stack-8.c new file mode 100644 index 0000000000000000000000000000000000000000..1954814317c2b595212511dd5d66e597f959f3b7 --- /dev/null +++ b/20151126/stack-8.c @@ -0,0 +1,67 @@ +#include <stdio.h> +#include <stdlib.h> + +#define STACK_SIZE 42 + +int stack[STACK_SIZE]; +int stack_pointer = 0; + +void fatal_error (char *msg) +{ + fprintf (stderr, "%s\n", msg); + exit (1); +} + +void push (int x) +{ + if (stack_pointer < 0 || stack_pointer >= STACK_SIZE) + fatal_error ("stack overflow"); + else + stack[stack_pointer++] = x; +} + +int pop (void) +{ + if (stack_pointer < 1 || stack_pointer > STACK_SIZE) + { + fatal_error ("stack underflow"); + return 0; + } + else + return stack[--stack_pointer]; +} + +void dump (void) +{ + if (stack_pointer < 0) + fatal_error ("stack underflow"); + else if (stack_pointer >= STACK_SIZE) + fatal_error ("stack overflow"); + else if (stack_pointer == 0) + printf ("stack is empty\n"); + else + { + printf ("stack:"); + for (int i = 0; i < stack_pointer; i++) + printf (" %d", stack[i]); + printf ("\n"); + } +} + +int main (void) +{ + dump (); + push (7); + dump (); + push (13); + dump (); + push (42); + dump (); + printf ("%d\n", pop ()); /* 42 */ + dump (); + printf ("%d\n", pop ()); /* 13 */ + dump (); + printf ("%d\n", pop ()); /* 7 */ + dump (); + return 0; +}