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

Vorbereitung 9.1.2017

parent 715e3cfe
No related branches found
No related tags found
No related merge requests found
Showing
with 2471 additions and 0 deletions
File added
This diff is collapsed.
#include <stdio.h>
#include <stdint.h>
typedef union
{
int16_t i;
uint16_t u;
} num;
int main (void)
{
num n;
n.u = 65104;
printf ("%d\n", n.i);
return 0;
}
#include <stdio.h>
#define ANIMAL 0
#define WITH_WINGS 1
#define WITH_LEGS 2
typedef struct animal
{
int type;
char *name;
} animal;
typedef struct with_wings
{
int type;
char *name;
int wings;
} with_wings;
typedef struct with_legs
{
int type;
char *name;
int legs;
} with_legs;
int main (void)
{
animal *a[2];
with_wings duck;
a[0] = (animal *) &duck;
a[0]->type = WITH_WINGS;
a[0]->name = "duck";
((with_wings *) a[0])->wings = 2;
with_legs cow;
a[1] = (animal *) &cow;
a[1]->type = WITH_LEGS;
a[1]->name = "cow";
((with_legs *) a[1])->legs = 4;
for (int i = 0; i < 2; i++)
if (a[i]->type == WITH_LEGS)
printf ("A %s has %d legs.\n", a[i]->name,
((with_legs *) a[i])-> legs);
else if (a[i]->type == WITH_WINGS)
printf ("A %s has %d wings.\n", a[i]->name,
((with_wings *) a[i])-> wings);
else
printf ("Error in animal: %s\n", a[i]->name);
return 0;
}
#include <stdio.h>
#define ANIMAL 0
#define WITH_WINGS 1
#define WITH_LEGS 2
typedef struct animal
{
int type;
char *name;
} animal;
typedef struct with_wings
{
int type;
char *name;
int wings;
} with_wings;
typedef struct with_legs
{
int type;
char *name;
int legs;
} with_legs;
int main (void)
{
animal *a[2];
with_wings duck;
a[0] = (animal *) &duck;
duck.type = WITH_WINGS;
duck.name = "duck";
duck.wings = 2;
with_legs cow;
a[1] = (animal *) &cow;
cow.type = WITH_LEGS;
cow.name = "cow";
cow.legs = 4;
for (int i = 0; i < 2; i++)
if (a[i]->type == WITH_LEGS)
printf ("A %s has %d legs.\n", a[i]->name,
((with_legs *) a[i])-> legs);
else if (a[i]->type == WITH_WINGS)
printf ("A %s has %d wings.\n", a[i]->name,
((with_wings *) a[i])-> wings);
else
printf ("Error in animal: %s\n", a[i]->name);
return 0;
}
#include <stdio.h>
#define ANIMAL 0
#define WITH_WINGS 1
#define WITH_LEGS 2
typedef struct animal
{
int type;
char *name;
} animal;
typedef struct with_wings
{
int type;
char *name;
int wings;
} with_wings;
typedef struct with_legs
{
int type;
char *name;
int legs;
} with_legs;
int main (void)
{
animal *a[2];
animal duck;
a[0] = &duck;
a[0]->type = WITH_WINGS;
a[0]->name = "duck";
((with_wings *) a[0])->wings = 2;
animal cow;
a[1] = &cow;
a[1]->type = WITH_LEGS;
a[1]->name = "cow";
((with_legs *) a[1])->legs = 4;
for (int i = 0; i < 2; i++)
if (a[i]->type == WITH_LEGS)
printf ("A %s has %d legs.\n", a[i]->name,
((with_legs *) a[i])-> legs);
else if (a[i]->type == WITH_WINGS)
printf ("A %s has %d wings.\n", a[i]->name,
((with_wings *) a[i])-> wings);
else
printf ("Error in animal: %s\n", a[i]->name);
return 0;
}
#include <stdio.h>
#include <stdint.h>
typedef union
{
uint32_t number;
char *name;
uint8_t bytes[4];
} data;
int main (void)
{
data x;
x.number = 303108111;
for (int i = 0; i < 4; i++)
printf ("%d ", x.bytes[i]);
printf ("\n");
printf ("%s\n", x.name);
return 0;
}
#include <stdio.h>
#define POINT 0
#define CIRCLE 1
#define TEXT 2
typedef union
{
int radius;
char *text;
} extra_data;
typedef struct graphics_object
{
int type;
void (*draw) (struct graphics_object *this);
int x, y;
extra_data data;
} graphics_object;
void draw_point (struct graphics_object *this)
{
printf ("point at (%d,%d)\n", this->x, this->y);
}
void draw_circle (struct graphics_object *this)
{
printf ("circle at (%d,%d) with radius %d\n",
this->x, this->y, this->data.radius);
}
void draw_text (struct graphics_object *this)
{
printf ("text at (%d,%d): \"%s\"\n",
this->x, this->y, this->data.text);
}
int main (void)
{
graphics_object a_point = { POINT, draw_point, 35, 17 };
graphics_object a_circle = { CIRCLE, draw_circle, 20, 30 };
a_circle.data.radius = 12;
graphics_object some_text = { TEXT, draw_text, 42, 23 };
some_text.data.text = "Hello, world!";
graphics_object *g[3] = { &a_point, &a_circle, &some_text };
for (int i = 0; i < 3; i++)
g[i]->draw (g[i]);
return 0;
}
#include <stdio.h>
#define POINT 0
#define CIRCLE 1
#define TEXT 2
typedef union
{
int radius;
char *text;
} extra_data;
typedef struct graphics_object
{
int type;
void (*draw) (struct graphics_object *this);
int x, y;
extra_data data;
} graphics_object;
void draw_point (struct graphics_object *this)
{
printf ("point at (%d,%d)\n", this->x, this->y);
}
void draw_circle (struct graphics_object *this)
{
printf ("circle at (%d,%d) with radius %d\n",
this->x, this->y, this->data.radius);
}
void draw_text (struct graphics_object *this)
{
printf ("text at (%d,%d): \"%s\"\n",
this->x, this->y, this->data.text);
}
int main (void)
{
graphics_object a_point = { POINT, draw_point, 35, 17 };
graphics_object a_circle = { CIRCLE, draw_text, 20, 30 };
a_circle.data.radius = 12;
graphics_object some_text = { TEXT, draw_text, 42, 23 };
some_text.data.text = "Hello, world!";
graphics_object *g[3] = { &a_point, &a_circle, &some_text };
for (int i = 0; i < 3; i++)
g[i]->draw (g[i]);
return 0;
}
../common/hello-gtk.png
\ No newline at end of file
File added
This diff is collapsed.
File added
% hp-uebung-20170109.pdf - Exercises on Low-Level Programming / Applied Computer Sciences
% Copyright (C) 2013, 2015, 2016, 2017 Peter Gerwinski
%
% This document is free software: you can redistribute it and/or
% modify it either under the terms of the Creative Commons
% Attribution-ShareAlike 3.0 License, or under the terms of the
% GNU General Public License as published by the Free Software
% Foundation, either version 3 of the License, or (at your option)
% any later version.
%
% This document is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this document. If not, see <http://www.gnu.org/licenses/>.
%
% You should have received a copy of the Creative Commons
% Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <http://creativecommons.org/licenses/>.
\documentclass[a4paper]{article}
\usepackage{pgscript}
\usepackage{enumerate}
\usepackage{ifthen}
\usepackage{gensymb}
\newcounter{exercise}
\newcommand{\exercise}[1]{\addtocounter{exercise}{1}\subsection*{Aufgabe \arabic{exercise}: #1}}
\newcounter{points}
\newcommand{\points}[1]{\ifthenelse{#1=1}{(1 Punkt)}{(#1 Punkte)}\addtocounter{points}{#1}}
\newcommand{\ItwoC}{I\raisebox{0.5ex}{\footnotesize 2}C}
\newcommand{\ITWOC}{I\raisebox{0.5ex}{\normalsize 2}C}
\newcommand{\gitfile}[2]{\href{https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/#1/#2}{\file{#2}}}
\begin{document}
% \thispagestyle{empty}
\section*{Hardwarenahe Programmierung / Angewandte Informatik\\
Übungsaufgaben -- 9.\ Januar 2017}
Diese Übung enthält Punkteangaben wie in einer Klausur.
Um zu "`bestehen"', müssen Sie innerhalb von 80 Minuten
unter Verwendung ausschließlich zugelassener Hilfsmittel
13 Punkte (von insgesamt \totalpoints) erreichen.
\exercise{Objektorientierte Programmierung mit dem C-Datentyp \emph{union}}
(Klausuraufgabe aus dem Wintersemester 2014/15\\[\smallskipamount]
In Wintersemester 2014/15 wurde der Datentyp \lstinline{union}
in der Vorlesung \emph{nicht\/} behandelt.)
\bigskip
Aus der Vorlesung ist der Verbund-Datentyp \lstinline{struct} bekannt.
% Ein \lstinline{struct} faßt verschiedene Daten zusammen,
% so daß diese gemeinsam angesprochen werden können.
Die Programmiersprache C kennt noch einen weiteren
Verbund-Datentyp \lstinline{union}:
\begin{lstlisting}
typedef union
{
int number;
char *name;
uint8_t bytes[4];
} data;
\end{lstlisting}
Von der Syntax her entspricht die \lstinline{union} genau dem
\lstinline{struct}. Insbesondere lassen sich die Datenfelder über
einen Punkt ansprechen, bei Zeigern auch mit \lstinline{->}. Der
Unterschied zum \lstinline{struct} besteht darin, daß sich in
einer \lstinline{union} die Datenfelder \emph{dieselben
Speicherzellen teilen}. Alle Datenfelder einer \lstinline{union}
haben \emph{dieselbe Speicheradresse}. (In einem \lstinline{struct}
beginnt das nächste Datenfeld immer dort, wo das vorherige aufhört.)
Bei Zuweisung eines Wertes an \emph{ein\/} Datenfeld einer
\lstinline{union} ändern sich daher die Werte \emph{aller\/}
Datenfelder. Dies kann man nutzen, um Speicher zu sparen (wenn man
immer nur eines der Datenfelder auf einmal nutzen möchte) oder um
"`verwandte"' Datentypen zu konstruieren (im Sinne der
objektorientierten Programmierung) oder um die Byte-Muster von
Variablen zu untersuchen.
\begin{enumerate}[\quad(a)]
\item
Was bewirkt das folgende Programm (\gitfile{20170109}{aufgabe-1a.c}), wenn
es auf einem LittleEndian-Rechner ausgeführt wird, und warum?
\points{4}
\begin{lstlisting}[gobble=8]
#include <stdio.h>
#include <stdint.h>
typedef union
{
uint32_t number;
char *name;
uint8_t bytes[4];
} data;
int main (void)
{
data x;
x.number = 303108111;
for (int i = 0; i < 4; i++)
printf ("%d ", x.bytes[i]);
printf ("\n");
printf ("%s\n", x.name);
return 0;
}
\end{lstlisting}
\item
Was würde dasselbe Programm auf einem BigEndian-Rechner ausgeben?
\points{1}
\end{enumerate}
\clearpage
Wir betrachten nun das folgende Programm (\gitfile{20170109}{aufgabe-1c.c}):
\begin{minipage}[t]{0.5\textwidth}
\begin{lstlisting}[gobble=6]
#include <stdio.h>
#define POINT 0
#define CIRCLE 1
#define TEXT 2
typedef union
{
int radius;
char *text;
} extra_data;
typedef struct graphics_object
{
int type;
void (*draw) (struct graphics_object *this);
int x, y;
extra_data data;
} graphics_object;
\end{lstlisting}
\end{minipage}\hfill
\begin{minipage}[t]{0.5\textwidth}
\vspace*{1cm}
\begin{lstlisting}[gobble=6]
void draw_point (struct graphics_object *this)
{
printf ("point at (%d,%d)\n", this->x, this->y);
}
void draw_circle (struct graphics_object *this)
{
printf ("circle at (%d,%d) with radius %d\n",
this->x, this->y, this->data.radius);
}
void draw_text (struct graphics_object *this)
{
printf ("text at (%d,%d): \"%s\"\n",
this->x, this->y, this->data.text);
}
\end{lstlisting}%
\vspace*{1.0cm}
\begin{lstlisting}[gobble=6,xleftmargin=-4cm]
int main (void)
{
graphics_object a_point = { POINT, draw_point, 35, 17 };
graphics_object a_circle = { CIRCLE, draw_circle, 20, 30 };
a_circle.data.radius = 12;
graphics_object some_text = { TEXT, draw_text, 42, 23 };
some_text.data.text = "Hello, world!";
graphics_object *g[3] = { &a_point, &a_circle, &some_text };
for (int i = 0; i < 3; i++)
g[i]->draw (g[i]);
return 0;
}
\end{lstlisting}
\end{minipage}
\bigskip
Dieses Programm verwaltet verschiedenartige Grafikobjekte in
einem Array \lstinline{graphics_object *g[3]}.\\
Anstatt tatsächlich zu zeichnen, gibt es der Einfachheit halber
als Text aus, was ggf.\ zu sehen wäre.
Der Datentyp \lstinline{graphics_object} ist ein
\lstinline{struct}, in dem eins der Datenfelder
eine \lstinline{union} (mit weiteren Datenfeldern) ist.
\begin{enumerate}[\quad(a)]
\setcounter{enumi}{2}
\item
Beschreiben Sie (in Worten, evtl.\ mit Skizze) die
in diesem Programm realisierte Objekt-Hierarchie.
Worum handelt es sich insbesondere bei
\lstinline{void (*draw) (struct graphics_object *this)}?
\points{3}
\item
Was passiert, wenn man im Hauptprogramm \lstinline{main()}
die an \lstinline{a_circle} übergebene Funktion
\lstinline{draw_circle} durch \lstinline{draw_text} ersetzt
(\gitfile{20170109}{aufgabe-1d.c}) und warum? \points{2}
\item
Erweitern Sie das Programm so, daß es einen weiteren
Grafikobjekttyp \lstinline{SQUARE} kennt, bei dem man
eine Kantenlänge \lstinline{a} angibt und das beim
"`Zeichnen"' den Text "`square at (x,y) with edge length a"'
(mit den korrekten Zahlen anstelle von \lstinline{x},
\lstinline{y} und \lstinline{a}) ausgibt. \points{5}
(Hinweis für die Klausur: Abgabe auf Datenträger ist erlaubt und erwünscht,
aber nicht zwingend.)
\end{enumerate}
\clearpage
\exercise{Objektorientierte Tier-Datenbank}
Diese Übung ist eine Ergänzung zu Aufgabe 4
der Übungen vom 19.\,12.\,2016 (\gitfile{20161219}{hp-uebung-20161219.pdf}).
Wir betrachten das korrigierte Programm
(wahlweise Ihre eigene Lösung von Teilaufgabe (d)
oder eine der Musterlösungen
\gitfile{20161219}{loesung-4-1.c} oder \gitfile{20161219}{loesung-4-2.c}).
\begin{itemize}
\item[(e)]
Schreiben Sie das Programm so um,
daß es keine expliziten Typumwandlungen mehr benötigt.\par
Hinweis: Verwenden Sie \lstinline{union}.
\points{4}
\item[(f)]
Schreiben Sie das Programm weiter um,
so daß es die Objektinstanzen \lstinline{duck} und \lstinline{cow}
dynamisch erzeugt.\par
Hinweis: Verwenden Sie \lstinline{malloc()} und schreiben Sie Konstruktoren.
\points{4}
\item[(g)]
Schreiben Sie das Programm weiter um,
so daß die Ausgabe nicht mehr direkt im Hauptprogramm erfolgt,
sondern stattdessen eine virtuelle Methode \lstinline{print()}
aufgerufen wird.\par
Hinweis: Verwenden Sie in den Objekten Zeiger auf Funktionen,
und initialisieren Sie diese in den Konstruktoren.
\points{4}
\end{itemize}
\bigskip
\begin{flushright}
\textit{Viel Erfolg!}
\end{flushright}
\makeatletter
\immediate\write\@mainaux{\string\gdef\string\totalpoints{\arabic{points}}}
\makeatother
\end{document}
../common/logo-hochschule-bochum-cvh-text.pdf
\ No newline at end of file
../common/logo-hochschule-bochum.pdf
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
typedef struct
{
int type;
void (* print) (t_object *this);
} t_base;
typedef struct
{
int type;
void (* print) (t_object *this);
int content;
} t_integer;
typedef struct
{
int type;
void (* print) (t_object *this);
char *content;
} t_string;
typedef union
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_integer (t_object *this)
{
printf ("Integer: %d\n", this->integer.content);
}
void print_string (t_object *this)
{
printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->integer.type = T_INTEGER;
p->integer.print = print_integer;
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (t_string));
p->string.type = T_STRING;
p->string.print = print_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.print (object[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
union t_object;
typedef struct
{
int type;
void (* print) (union t_object *this);
} t_base;
typedef struct
{
int type;
void (* print) (union t_object *this);
int content;
} t_integer;
typedef struct
{
int type;
void (* print) (union t_object *this);
char *content;
} t_string;
typedef union t_object
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_integer (t_object *this)
{
printf ("Integer: %d\n", this->integer.content);
}
void print_string (t_object *this)
{
printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->integer.type = T_INTEGER;
p->integer.print = print_integer;
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (t_string));
p->string.type = T_STRING;
p->string.print = print_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.print (object[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
union t_object;
typedef struct
{
void (* print) (union t_object *this);
} t_base;
typedef struct
{
void (* print) (union t_object *this);
int content;
} t_integer;
typedef struct
{
void (* print) (union t_object *this);
char *content;
} t_string;
typedef union t_object
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_integer (t_object *this)
{
printf ("Integer: %d\n", this->integer.content);
}
void print_string (t_object *this)
{
printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->integer.print = print_integer;
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (t_string));
p->string.print = print_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.print (object[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
typedef union
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_object (t_object *this)
{
if (this->base.type == T_INTEGER)
printf ("Integer: %d\n", this->integer.content);
else if (this->base.type == T_STRING)
printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->integer.type = T_INTEGER;
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (t_string));
p->string.type = T_STRING;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
print_object (object[i]);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment