From 698e37b107dd4b9944a78e6b4bf25e787c53ebbc Mon Sep 17 00:00:00 2001
From: Peter Gerwinski <peter.gerwinski@hs-bochum.de>
Date: Mon, 5 Oct 2020 12:59:09 +0200
Subject: [PATCH] =?UTF-8?q?Musterl=C3=B6sungen=20zu=20Aufgabe=204=20der=20?=
 =?UTF-8?q?Klausur=20vom=206.2.2017=20sowie=20Aufgabe=201=20und=203=20vom?=
 =?UTF-8?q?=209.1.2020?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 20200109/loesung-1-1.c      | 18 ++++++++++
 20200109/loesung-1-2.c      | 18 ++++++++++
 20200109/loesung-3-1.c      | 23 +++++++++++++
 20200109/loesung-3-2.c      | 23 +++++++++++++
 20200109/loesung-3-3.c      | 23 +++++++++++++
 20200109/loesung-3-4.c      | 25 ++++++++++++++
 exams/20170206/loesung-4.c  | 65 +++++++++++++++++++++++++++++++++++++
 exams/20170206/loesung-4f.c | 65 +++++++++++++++++++++++++++++++++++++
 8 files changed, 260 insertions(+)
 create mode 100644 20200109/loesung-1-1.c
 create mode 100644 20200109/loesung-1-2.c
 create mode 100644 20200109/loesung-3-1.c
 create mode 100644 20200109/loesung-3-2.c
 create mode 100644 20200109/loesung-3-3.c
 create mode 100644 20200109/loesung-3-4.c
 create mode 100644 exams/20170206/loesung-4.c
 create mode 100644 exams/20170206/loesung-4f.c

diff --git a/20200109/loesung-1-1.c b/20200109/loesung-1-1.c
new file mode 100644
index 0000000..175923f
--- /dev/null
+++ b/20200109/loesung-1-1.c
@@ -0,0 +1,18 @@
+#include <stdio.h>
+#include <stdint.h>
+
+typedef struct
+{
+  uint32_t a;
+  uint64_t b;
+  uint8_t c;
+} three_numbers;
+
+int main (void)
+{
+  three_numbers xyz = { 1819042120, 2410670883059281007, 0 };
+  printf ("%x %x %x\n", xyz.a, xyz.b, xyz.c);
+  printf ("%08x %016x %02x\n", xyz.a, xyz.b, xyz.c);
+  printf ("%s\n", &xyz);
+  return 0;
+}
diff --git a/20200109/loesung-1-2.c b/20200109/loesung-1-2.c
new file mode 100644
index 0000000..07ca068
--- /dev/null
+++ b/20200109/loesung-1-2.c
@@ -0,0 +1,18 @@
+#include <stdio.h>
+#include <stdint.h>
+
+typedef struct
+{
+  uint32_t a;
+  uint64_t b;
+  uint8_t c;
+} three_numbers;
+
+int main (void)
+{
+  three_numbers xyz = { 1819042120, 2410670883059281007, 0 };
+  printf ("%x %lx %x\n", xyz.a, xyz.b, xyz.c);
+  printf ("%08x %016lx %02x\n", xyz.a, xyz.b, xyz.c);
+  printf ("%s\n", &xyz);
+  return 0;
+}
diff --git a/20200109/loesung-3-1.c b/20200109/loesung-3-1.c
new file mode 100644
index 0000000..416c5df
--- /dev/null
+++ b/20200109/loesung-3-1.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <string.h>
+
+typedef struct
+{
+  char first_name[10];
+  char family_name[20];
+  char day, month;
+  int year;
+} person;
+
+int main (void)
+{
+  person sls;
+  strcpy (sls.first_name, "Sabine");
+  strcpy (sls.family_name, "Leutheusser-Schnarrenberger");
+  sls.day = 26;
+  sls.month = 7;
+  sls.year = 1951;
+  printf ("%s %s wurde am %d.%d.%d geboren.\n",
+          sls.first_name, sls.family_name, sls.day, sls.month, sls.year);
+  return 0;
+}
diff --git a/20200109/loesung-3-2.c b/20200109/loesung-3-2.c
new file mode 100644
index 0000000..825373e
--- /dev/null
+++ b/20200109/loesung-3-2.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <string.h>
+
+typedef struct
+{
+  char first_name[10];
+  char family_name[20];
+  char day, month;
+  int year;
+} person;
+
+int main (void)
+{
+  person sls;
+  sls.day = 26;
+  sls.month = 7;
+  sls.year = 1951;
+  strncpy (sls.first_name, "Sabine", 10);
+  strncpy (sls.family_name, "Leutheusser-Schnarrenberger", 20);
+  printf ("%s %s wurde am %d.%d.%d geboren.\n",
+          sls.first_name, sls.family_name, sls.day, sls.month, sls.year);
+  return 0;
+}
diff --git a/20200109/loesung-3-3.c b/20200109/loesung-3-3.c
new file mode 100644
index 0000000..7a4b1bf
--- /dev/null
+++ b/20200109/loesung-3-3.c
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <string.h>
+
+typedef struct
+{
+  char first_name[10];
+  char family_name[20];
+  char day, month;
+  int year;
+} person;
+
+int main (void)
+{
+  person sls;
+  sls.day = 26;
+  sls.month = 7;
+  sls.year = 1951;
+  strncpy (sls.first_name, "Sabine", 9);
+  strncpy (sls.family_name, "Leutheusser-Schnarrenberger", 19);
+  printf ("%s %s wurde am %d.%d.%d geboren.\n",
+          sls.first_name, sls.family_name, sls.day, sls.month, sls.year);
+  return 0;
+}
diff --git a/20200109/loesung-3-4.c b/20200109/loesung-3-4.c
new file mode 100644
index 0000000..3c635df
--- /dev/null
+++ b/20200109/loesung-3-4.c
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <string.h>
+
+typedef struct
+{
+  char first_name[10];
+  char family_name[20];
+  char day, month;
+  int year;
+} person;
+
+int main (void)
+{
+  person sls;
+  sls.day = 26;
+  sls.month = 7;
+  sls.year = 1951;
+  strncpy (sls.first_name, "Sabine", 9);
+  sls.first_name[9] = 0;
+  strncpy (sls.family_name, "Leutheusser-Schnarrenberger", 19);
+  sls.family_name[19] = 0;
+  printf ("%s %s wurde am %d.%d.%d geboren.\n",
+          sls.first_name, sls.family_name, sls.day, sls.month, sls.year);
+  return 0;
+}
diff --git a/exams/20170206/loesung-4.c b/exams/20170206/loesung-4.c
new file mode 100644
index 0000000..8d41f91
--- /dev/null
+++ b/exams/20170206/loesung-4.c
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <errno.h>
+#include <error.h>
+
+/* Die Aufgabe besteht darin, die Funktionen pbm_open(),
+ * pbm_line() und pbm_close() zu schreiben.
+ */
+
+int pbm_width = 0;
+int pbm_height = 0;
+FILE *pbm_file;  /* globale Variable für die PBM-Datei */
+
+void pbm_open (int width, int height, char *filename)
+{
+  pbm_file = fopen (filename, "w");
+  if (!pbm_file)
+    error (errno, errno, "cannot open file %s for writing", filename);
+  pbm_width = width;
+  pbm_height = height;
+  fprintf (pbm_file, "P4\n%d %d\n", pbm_width, pbm_height);
+}
+
+void pbm_line (char *line)
+{
+  int pbm_bytes = (pbm_width + 7) / 8;   /* benötigte Bytes pro Zeile (immer aufrunden) */
+  uint8_t buffer[pbm_bytes];
+  for (int i = 0; i < pbm_bytes; i++)   /* Puffer auf 0 initialisieren */
+    buffer[i] = 0;
+  for (int x = 0; line[x]; x++)
+    {
+      int i = x / 8;  /* In welches Byte des Puffers gehört dieses Pixel? */
+      int b = x % 8;  /* Welches Bit innerhalb des Bytes ist dieses Pixel? */
+      if (line[x] != ' ')   /* Kein Leerzeichen --> Bit auf 1 setzen */
+        buffer[i] |= 0x80 >> b;  /* MSB first. LSB first wäre 1 << b. */
+    }
+  for (int i = 0; i < pbm_bytes; i++)   /* Puffer in Datei ausgeben */
+    fprintf (pbm_file, "%c", buffer[i]);
+}
+
+void pbm_close (void)
+{
+  fclose (pbm_file);
+}
+
+int main (void)
+{
+  pbm_open (14, 14, "test.pbm");
+  pbm_line ("              ");
+  pbm_line ("    XXXXXX    ");
+  pbm_line ("   X      X   ");
+  pbm_line ("  X        X  ");
+  pbm_line (" X          X ");
+  pbm_line (" X  XX  XX  X ");
+  pbm_line (" X   X   X  X ");
+  pbm_line (" X          X ");
+  pbm_line (" X X      X X ");
+  pbm_line (" X  X    X  X ");
+  pbm_line ("  X  XXXX  X  ");
+  pbm_line ("   X      X   ");
+  pbm_line ("    XXXXXX    ");
+  pbm_line ("              ");
+  pbm_close ();
+  return 0;
+}
diff --git a/exams/20170206/loesung-4f.c b/exams/20170206/loesung-4f.c
new file mode 100644
index 0000000..652a631
--- /dev/null
+++ b/exams/20170206/loesung-4f.c
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <errno.h>
+#include <error.h>
+
+/* Die Aufgabe besteht darin, die Funktionen pbm_open(),
+ * pbm_line() und pbm_close() zu schreiben.
+ */
+
+int pbm_width = 0;
+int pbm_height = 0;
+FILE *pbm_file;  /* globale Variable für die PBM-Datei */
+
+void pbm_open (int width, int height, char *filename)
+{
+  pbm_file = fopen (filename, "w");
+  if (!pbm_file)
+    error (errno, errno, "cannot open file %s for writing", filename);
+  pbm_width = width;
+  pbm_height = height;
+  fprintf (pbm_file, "P4\n%d %d\n", pbm_width, pbm_height);
+}
+
+void pbm_line (char *line)
+{
+  int pbm_bytes = (pbm_width + 7) / 8;   /* benötigte Bytes pro Zeile (immer aufrunden) */
+  uint8_t buffer[pbm_bytes];
+  for (int i = 0; i < pbm_bytes; i++)   /* Puffer auf 0 initialisieren */
+    buffer[i] = 0;
+  for (int x = 0; line[x]; x++)
+    {
+      int i = x / 8;  /* In welches Byte des Puffers gehört dieses Pixel? */
+      int b = x % 8;  /* Welches Bit innerhalb des Bytes ist dieses Pixel? */
+      if (line[x] != ' ')   /* Kein Leerzeichen --> Bit auf 1 setzen */
+        buffer[i] |= 1 << b;
+    }
+  for (int i = 0; i < pbm_bytes; i++)   /* Puffer in Datei ausgeben */
+    fprintf (pbm_file, "%c", buffer[i]);
+}
+
+void pbm_close (void)
+{
+  fclose (pbm_file);
+}
+
+int main (void)
+{
+  pbm_open (14, 14, "test.pbm");
+  pbm_line ("              ");
+  pbm_line ("    XXXXXX    ");
+  pbm_line ("   X      X   ");
+  pbm_line ("  X        X  ");
+  pbm_line (" X          X ");
+  pbm_line (" X  XX  XX  X ");
+  pbm_line (" X   X   X  X ");
+  pbm_line (" X          X ");
+  pbm_line (" X X      X X ");
+  pbm_line (" X  X    X  X ");
+  pbm_line ("  X  XXXX  X  ");
+  pbm_line ("   X      X   ");
+  pbm_line ("    XXXXXX    ");
+  pbm_line ("              ");
+  pbm_close ();
+  return 0;
+}
-- 
GitLab