diff --git a/20190113/aufgabe-1.c b/20190113/aufgabe-1.c new file mode 100644 index 0000000000000000000000000000000000000000..a1054f2b601850a402dccb6f4878437d1bb6909c --- /dev/null +++ b/20190113/aufgabe-1.c @@ -0,0 +1,22 @@ +#include <stdio.h> + +void foreach (int *a, void (*fun) (int x)) +{ + for (int *p = a; *p >= 0; p++) + fun (*p); +} + +void even_or_odd (int x) +{ + if (x % 2) + printf ("%d ist ungerade.\n", x); + else + printf ("%d ist gerade.\n", x); +} + +int main (void) +{ + int numbers[] = { 12, 17, 32, 1, 3, 16, 19, 18, -1 }; + foreach (numbers, even_or_odd); + return 0; +} diff --git a/20190107/aufgabe-3a.c b/20190113/aufgabe-2a.c similarity index 100% rename from 20190107/aufgabe-3a.c rename to 20190113/aufgabe-2a.c diff --git a/20190107/aufgabe-3b.c b/20190113/aufgabe-2b.c similarity index 100% rename from 20190107/aufgabe-3b.c rename to 20190113/aufgabe-2b.c diff --git a/20190107/gtk-1.c b/20190113/gtk-1.c similarity index 100% rename from 20190107/gtk-1.c rename to 20190113/gtk-1.c diff --git a/20190107/gtk-2.c b/20190113/gtk-2.c similarity index 100% rename from 20190107/gtk-2.c rename to 20190113/gtk-2.c diff --git a/20190107/gtk-3.c b/20190113/gtk-3.c similarity index 100% rename from 20190107/gtk-3.c rename to 20190113/gtk-3.c diff --git a/20190107/gtk-4.c b/20190113/gtk-4.c similarity index 100% rename from 20190107/gtk-4.c rename to 20190113/gtk-4.c diff --git a/20190107/gtk-5.c b/20190113/gtk-5.c similarity index 100% rename from 20190107/gtk-5.c rename to 20190113/gtk-5.c diff --git a/20190107/gtk-6.c b/20190113/gtk-6.c similarity index 100% rename from 20190107/gtk-6.c rename to 20190113/gtk-6.c diff --git a/20190107/gtk-7.c b/20190113/gtk-7.c similarity index 100% rename from 20190107/gtk-7.c rename to 20190113/gtk-7.c diff --git a/20190107/hello-gtk.png b/20190113/hello-gtk.png similarity index 100% rename from 20190107/hello-gtk.png rename to 20190113/hello-gtk.png diff --git a/20190113/hp-20190113.pdf b/20190113/hp-20190113.pdf new file mode 100644 index 0000000000000000000000000000000000000000..61094c4e509d8476b726fd88e37282b6f554c4c8 Binary files /dev/null and b/20190113/hp-20190113.pdf differ diff --git a/20190113/hp-20190113.tex b/20190113/hp-20190113.tex new file mode 100644 index 0000000000000000000000000000000000000000..c176e5f0939b5be11bae1255b258c6d2d72bc341 --- /dev/null +++ b/20190113/hp-20190113.tex @@ -0,0 +1,891 @@ +% hp-20190113.pdf - Lecture Slides on Low-Level Programming +% Copyright (C) 2012, 2013, 2015, 2016, 2017, 2018, 2019 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/>. + +% README: Objektorientierte Programmierung + +\documentclass[10pt,t]{beamer} + +\usepackage{pgslides} +\usepackage{pdftricks} +\usepackage{tikz} +\usepackage{rotating} + +\begin{psinputs} + \usepackage[utf8]{inputenc} + \usepackage[german]{babel} + \usepackage[T1]{fontenc} + \usepackage{helvet} + \renewcommand*\familydefault{\sfdefault} + \usepackage{pstricks,pst-grad} +\end{psinputs} + +\title{Hardwarenahe Programmierung} +\author{Prof.\ Dr.\ rer.\ nat.\ Peter Gerwinski} +\date{13.\ Januar 2019} + +\begin{document} + +\maketitleframe + +\nosectionnonumber{\inserttitle} + +\begin{frame} + + \shownosectionnonumber + + \begin{itemize} + \item[\textbf{1}] \textbf{Einführung} + \hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/hp.git}}} + \item[\textbf{2}] \textbf{Einführung in C} + \item[\textbf{3}] \textbf{Bibliotheken} + \item[\textbf{4}] \textbf{Hardwarenahe Programmierung} + \item[\textbf{5}] \textbf{Algorithmen} + \begin{itemize} + \item[5.1] Differentialgleichungen + \item[5.2] Rekursion + \color{medgreen} + \item[5.3] Aufwandsabschätzungen + \end{itemize} + \item[\textbf{6}] \textbf{Objektorientierte Programmierung} + \begin{itemize} + \color{medgreen} + \item[6.0] Dynamische Speicherverwaltung + \color{orange} + \item[6.1] Konzepte und Ziele + \color{red} + \item[6.2] Beispiel: Zahlen und Buchstaben + \item[6.3] Beispiel: Graphische Benutzeroberfläche (GUI) + \vspace*{-\smallskipamount} + \item[\dots] + \end{itemize} + \item[\textbf{7}] \textbf{Datenstrukturen} + \end{itemize} + \vspace*{-1cm} + +\end{frame} + +\setcounter{section}{4} +\section{Algorithmen} +\setcounter{subsection}{2} +\subsection{Aufwandsabschätzungen \protect\color{gray}-- Komplexitätsanalyse} + +\begin{frame} + + \newcommand{\w}{\hspace*{0.75pt}} + + \showsubsection + + \begin{picture}(0,0) + \put(7.6,-0.3){% + \begin{minipage}[t]{5.3cm} +% \vspace*{-1.0cm}\includegraphics{landau-symbols.pdf} + \vspace*{-1.0cm}{\includegraphics{landau-symbols.pdf}} \small + \begin{description}\itemsep0pt\leftskip-0.5cm + \item[$n$:] Eingabedaten + \item[$g(n)$:] Rechenzeit + \end{description} + \end{minipage}} + \end{picture} + + \vspace*{-1.5\bigskipamount} + + \begin{itemize} + \item + Türme von Hanoi: $\mathcal{O}(2^n)$ +% \begin{onlyenv}<1> +% \par\medskip +% Für jede zusätzliche Scheibe\\verdoppelt sich die Rechenzeit! +% % 32.712 * 2^32 / 3600 / 24 / 365.25 = 4452.08032888280477602859 +% \begin{itemize} +% \arrowitem +% $\frac{32,712\,\text{s}\,\cdot\,2^{32}}{3600\,\cdot\,24\,\cdot\,365,25} \approx 4452$ +% Jahre\\[\smallskipamount] +% für 64 Scheiben +% \end{itemize} +% \end{onlyenv} +% \end{itemize} +% +% \pause +% +% Beispiel: Sortieralgorithmen +% +% \begin{itemize} + \item + Minimum suchen: \alt<1->{$\mathcal{O}(n)$}{$\mathcal{O}(\textbf{\color{red}?})$} +% \pause + \item + \dots\ mit Schummeln: $\mathcal{O}(1)$ +% \pause +% \pause + \begin{picture}(0,0) + \put(-4.742,-4.3){\begin{minipage}[t]{12cm} + Faustregel:\\Schachtelung der Schleifen zählen\\ + $x$ Schleifen \textarrow\ $\mathcal{O}(n\w^x)$ + \vspace{0.5\smallskipamount} +% \begin{onlyenv}<1-> +% \begin{tabbing} +% Verschlüsselung brechen (Primfaktorzerlegung):~\=\kill +% \textbf{\color{red}RSA}: Schlüsselerzeugung (Berechnung von $d\/$): +% \> \color{red}$\mathcal{O}\bigl((\log n)^2\bigr)$,\\[0.5\smallskipamount] +% Ver- und Entschlüsselung (Exponentiation): +% \> \color{red}$\mathcal{O}\kern0.5pt(n\log n)$,\\[0.5\smallskipamount] +% Verschlüsselung brechen (Primfaktorzerlegung): +% \> \color{red}$\mathcal{O}\bigl(2^{\sqrt{\log n\,\cdot\,\log\log n}}\bigr)$ +% \end{tabbing} +% \end{onlyenv} + \end{minipage}} + \end{picture} +% \pause + \item + Minimum an den Anfang tauschen,\\ + nächstes Minimum suchen:\\ + \textarrow\ Selectionsort: $\mathcal{O}(n\w^2)$ +% \pause + \item + Während Minimumsuche prüfen\\und abbrechen, falls schon sortiert\\ + \textarrow\ Bubblesort: $\mathcal{O}(n)$ bis $\mathcal{O}(n\w^2)$ +% \pause + \item + Rekursiv sortieren\\ + \textarrow\ Quicksort: $\mathcal{O}(n\log n)$ bis $\mathcal{O}(n\w^2)$\hfill + \end{itemize} + +\end{frame} + +\begin{frame} + + \showsubsection + + \textbf{Quicksort} + + \begin{center} + \includegraphics[width=\textwidth]{../20190107/photo-20190107-173418.jpg} + \end{center} + +\end{frame} + +\begin{frame} + + \newcommand{\w}{\hspace*{0.75pt}} + + \showsubsection + + \begin{picture}(0,0) + \put(7.6,-0.3){% + \begin{minipage}[t]{5.3cm} +% \vspace*{-1.0cm}\includegraphics{landau-symbols.pdf} + \vspace*{-1.0cm}\alt<1->{\includegraphics{landau-symbols-2.pdf}}% + {\includegraphics{landau-symbols.pdf}} + \small + \begin{description}\itemsep0pt\leftskip-0.5cm + \item[$n$:] Eingabedaten + \item[$g(n)$:] Rechenzeit + \end{description} + \end{minipage}} + \end{picture} + + \vspace*{-1.5\bigskipamount} + + \begin{itemize} + \item + Türme von Hanoi: $\mathcal{O}(2^n)$ +% \begin{onlyenv}<1> +% \par\medskip +% Für jede zusätzliche Scheibe\\verdoppelt sich die Rechenzeit! +% % 32.712 * 2^32 / 3600 / 24 / 365.25 = 4452.08032888280477602859 +% \begin{itemize} +% \arrowitem +% $\frac{32,712\,\text{s}\,\cdot\,2^{32}}{3600\,\cdot\,24\,\cdot\,365,25} \approx 4452$ +% Jahre\\[\smallskipamount] +% für 64 Scheiben +% \end{itemize} +% \end{onlyenv} +% \end{itemize} +% +% \pause +% +% Beispiel: Sortieralgorithmen +% +% \begin{itemize} + \item + Minimum suchen: \alt<1->{$\mathcal{O}(n)$}{$\mathcal{O}(\textbf{\color{red}?})$} +% \pause + \item + \dots\ mit Schummeln: $\mathcal{O}(1)$ +% \pause +% \pause + \begin{picture}(0,0) + \put(-4.742,-4.3){\begin{minipage}[t]{12cm} + Faustregel:\\Schachtelung der Schleifen zählen\\ + $x$ Schleifen \textarrow\ $\mathcal{O}(n\w^x)$ + \vspace{0.5\smallskipamount} + \begin{onlyenv}<1-> + \begin{tabbing} + Verschlüsselung brechen (Primfaktorzerlegung):~\=\kill + \textbf{\color{red}RSA}: Schlüsselerzeugung (Berechnung von $d\/$): + \> \color{red}$\mathcal{O}\bigl((\log n)^2\bigr)$,\\[0.5\smallskipamount] + Ver- und Entschlüsselung (Exponentiation): + \> \color{red}$\mathcal{O}\kern0.5pt(n\log n)$,\\[0.5\smallskipamount] + Verschlüsselung brechen (Primfaktorzerlegung): + \> \color{red}$\mathcal{O}\bigl(2^{\sqrt{\log n\,\cdot\,\log\log n}}\bigr)$ + \end{tabbing} + \end{onlyenv} + \end{minipage}} + \end{picture} +% \pause + \item + Minimum an den Anfang tauschen,\\ + nächstes Minimum suchen:\\ + \textarrow\ Selectionsort: $\mathcal{O}(n\w^2)$ +% \pause + \item + Während Minimumsuche prüfen\\und abbrechen, falls schon sortiert\\ + \textarrow\ Bubblesort: $\mathcal{O}(n)$ bis $\mathcal{O}(n\w^2)$ +% \pause + \item + Rekursiv sortieren\\ + \textarrow\ Quicksort: $\mathcal{O}(n\log n)$ bis $\mathcal{O}(n\w^2)$\hfill + \end{itemize} + +\end{frame} + +\section{Objektorientierte Programmierung} +\addtocounter{subsection}{-1} +\subsection{Dynamische Speicherverwaltung} + +\begin{frame}[fragile] + + \showsection + \showsubsection + + \begin{itemize} + \item + Array: feste Anzahl von Elementen desselben Typs (z.\,B.\ 3 ganze Zahlen) + \item + Dynamisches Array: variable Anzahl von Elementen desselben Typs + \end{itemize} + + \bigskip + + \begin{lstlisting} + char *name[] = { "Anna", "Berthold", "Caesar" }; + + ... + + name[3] = "Dieter"; + \end{lstlisting} + + \begin{picture}(0,0) + \color{red} + \put(0,0){\line(3,1){3.5}} + \put(0,1){\line(3,-1){3.5}} + \end{picture} + +\end{frame} + +\begin{frame}[fragile] + + \showsection + \showsubsection + + \bigskip + + \begin{lstlisting} + #include <stdlib.h> + + ... + + char **name = malloc (3 * sizeof (char *)); + /* Speicherplatz für 3 Zeiger anfordern */ + + ... + + free (name) + /* Speicherplatz freigeben */ + + \end{lstlisting} + +\end{frame} + +\subsection{Konzepte und Ziele} + +\begin{frame} + + \showsection + \showsubsection + + \begin{itemize} + \item + Array: feste Anzahl von Elementen desselben Typs (z.\,B.\ 3 ganze Zahlen) + \item + Dynamisches Array: variable Anzahl von Elementen desselben Typs + \item + Problem: Elemente unterschiedlichen Typs + \item + Lösung: den Typ des Elements zusätzlich speichern + \end{itemize} + \begin{itemize} + \item + Funktionen, die mit dem Objekt arbeiten: \newterm{Methoden} + \begin{onlyenv}<1> + \item + Was die Funktion bewirkt,\\ + hängt vom Typ des Objekts ab + \item + Realisierung über endlose \lstinline{if}-Ketten + \end{onlyenv} + \begin{onlyenv}<2> + \item + Was die Funktion bewirkt + \begin{picture}(0,0) + \color{red} + \put(-4.00,-0.10){\tikz{\draw[thick](0,0.25)--(3.75,-0.05);% + \draw[thick](-0.1,-0.05)--(3.75,0.3);}} + \end{picture}% + Welche Funktion aufgerufen wird,\\ + hängt vom Typ des Objekts ab: \newterm{virtuelle Methode} + \item + Realisierung über endlose \lstinline{if}-Ketten% + \begin{picture}(0,0) + \color{red} + \put(-2.75,-0.10){\tikz{\draw[thick](0,0.25)--(2.5,-0.05);% + \draw[thick](-0.1,-0.05)--(2.5,0.3);}} + \put(1.5,-1.1){\begin{rotate}{7}\large\bf\textarrow\ kommt gleich\end{rotate}} + \end{picture} + Zeiger, die im Objekt gespeichert sind\\ + (Genaugenommen: Tabelle von Zeigern) + \end{onlyenv} + \end{itemize} + +\end{frame} + +\begin{frame} + + \showsection + \showsubsection + + \begin{itemize} + \item + Problem: Elemente unterschiedlichen Typs + \item + Lösung: den Typ des Elements zusätzlich speichern + \item + \newterm{Methoden\/} und \newterm{virtuelle Methoden} + \end{itemize} + + \begin{itemize} + \item + Zeiger auf verschiedene Strukturen\\ + mit einem gemeinsamen Anteil von Datenfeldern\\ + \textarrow\ "`verwandte"' \newterm{Objekte}, \newterm{Klassen} von Objekten + \item + Struktur, die \emph{nur\/} den gemeinsamen Anteil enthält\\ + \textarrow\ "`Vorfahr"', \newterm{Basisklasse}, \newterm{Vererbung} + \item +% Explizite Typumwandlung eines Zeigers auf die Basisklasse\\ +% in einen Zeiger auf die \newterm{abgeleitete Klasse}\\ +% \textarrow\ Man kann ein Array unterschiedlicher Objekte\\ +% \strut\phantom{\textarrow} in einer Schleife abarbeiten.\\ + Zeiger auf die Basisklasse dürfen auf Objekte\\ + der \newterm{abgeleiteten Klasse} zeigen\\ + \textarrow\ \newterm{Polymorphie} + \end{itemize} + +\end{frame} + +\subsection{Beispiel: Zahlen und Buchstaben} + +\begin{frame}[fragile] + + \showsection + \showsubsection + + \begin{center} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + int type; + } t_base;¿ + \end{lstlisting} + \end{minipage}\\[0.5cm] + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + int type; + int content; + } t_integer;¿ + \end{lstlisting} + \end{minipage} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + int type; + char *content; + } t_string;¿ + \end{lstlisting} + \end{minipage} + \end{center} + +\end{frame} + +\begin{frame}[fragile] + \begin{center} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + int type; + } t_base;¿ + \end{lstlisting} + \end{minipage}\\[0.5cm] + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + int type; + int content; + } t_integer;¿ + \end{lstlisting} + \end{minipage} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + int type; + char *content; + } t_string;¿ + \end{lstlisting} + \end{minipage}\\[0.7cm] + \begin{onlyenv}<1> + \begin{minipage}{8cm} + \begin{lstlisting}[gobble=10] + ¡t_integer i = { 1, 42 }; + t_string s = { 2, "Hello, world!" }; + + t_base *object[] = { (t_base *) &i, (t_base *) &s };¿ + \end{lstlisting} + \end{minipage}% + \begin{picture}(0,0) + \color{red} + \put(-5.4,-0.8){\mbox{$\underbrace{\rule{1.45cm}{0pt}}_{\shortstack{\strut explizite\\Typumwandlung}}$}} + \end{picture} + \end{onlyenv} +% \begin{onlyenv}<2> +% \begin{minipage}{5cm} +% \begin{lstlisting}[gobble=10] +% ¡typedef union +% { +% t_base base; +% t_integer integer; +% t_string string; +% } t_object;¿ +% \end{lstlisting} +% \end{minipage} +% \end{onlyenv} + \end{center} + +\end{frame} + +\subsection{Beispiel: Graphische Benutzeroberfläche (GUI)} + +\begin{frame}[fragile] + + \showsubsection + + \scriptsize + \begin{lstlisting} + #include <gtk/gtk.h> + + int main (int argc, char **argv) + { + gtk_init (&argc, &argv); + GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title (GTK_WINDOW (window), "Hello"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + GtkWidget *vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); + gtk_container_add (GTK_CONTAINER (window), vbox); + gtk_container_set_border_width (GTK_CONTAINER (vbox), 10); + GtkWidget *label = gtk_label_new ("Hello, world!"); + gtk_container_add (GTK_CONTAINER (vbox), label); + GtkWidget *button = gtk_button_new_with_label ("Quit"); + g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL); + gtk_container_add (GTK_CONTAINER (vbox), button); + gtk_widget_show (button); + gtk_widget_show (label); + gtk_widget_show (vbox); + gtk_widget_show (window); + gtk_main (); + return 0; + } + \end{lstlisting} + + \vspace*{-6cm}\strut\hfill + \includegraphics[scale=0.85]{hello-gtk.png}\\[2cm] + \begin{flushright} + \normalsize\bf Praktikumsversuch:\\ + Objektorientiertes Zeichenprogramm + \end{flushright} + +\end{frame} + +\subsection{Unions} + +\begin{frame}[fragile] + + \showsubsection + + Variable teilen sich denselben Speicherplatz. + + \medskip + + \begin{minipage}[t]{3.7cm} + \begin{lstlisting}[gobble=6] + ¡typedef union + { + int8_t i; + uint8_t u; + } num8_t;¿ + \end{lstlisting} + \end{minipage}% + \begin{minipage}[t]{4.5cm} + \begin{lstlisting}[gobble=6] + ¡int main (void) + { + num8_t test; + test.i = -1; + printf ("%d\n", test.u); + return 0; + }¿ + \end{lstlisting} + \end{minipage} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + Variable teilen sich denselben Speicherplatz. + + \medskip + + \begin{minipage}[t]{3.7cm} + \begin{lstlisting}[gobble=6] + ¡typedef union + { + char s[8]; + uint64_t x; + } num_char_t;¿ + \end{lstlisting} + \end{minipage}% + \begin{minipage}[t]{4.5cm} + \begin{lstlisting}[gobble=6] + ¡int main (void) + { + num_char_t test = { "Hello!" }; + printf ("%lx\n", test.x); + return 0; + }¿ + \end{lstlisting} + \end{minipage} + +\end{frame} + +\begin{frame}[fragile] + + \showsubsection + + Variable teilen sich denselben Speicherplatz. + + \medskip + + \begin{minipage}[t]{3.7cm} + \begin{lstlisting}[gobble=6] + ¡typedef union + { + t_base base; + t_integer integer; + t_string string; + } t_object;¿ + \end{lstlisting} + \end{minipage}% + \begin{minipage}[t]{3.0cm} + \begin{lstlisting}[gobble=6] + + ¡typedef struct + { + int type; + } t_base;¿ + \end{lstlisting} + \end{minipage}% + \begin{minipage}[t]{3.0cm} + \begin{lstlisting}[gobble=6] + + ¡typedef struct + { + int type; + int content; + } t_integer;¿ + \end{lstlisting} + \end{minipage}% + \begin{minipage}[t]{3.0cm} + \begin{lstlisting}[gobble=6] + + ¡typedef struct + { + int type; + char *content; + } t_string;¿ + \end{lstlisting} + \end{minipage} + + \bigskip + + \begin{center} + \begin{minipage}{8.5cm} + \begin{lstlisting}[gobble=8] + ¡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);¿ + \end{lstlisting} + \end{minipage} + \end{center} + +\end{frame} + +\subsection{Virtuelle Methoden} + +\begin{frame}[fragile] + \showsubsection + + \begin{lstlisting} + 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); + } + \end{lstlisting} + + \begin{picture}(0,0) + \color{red} + \put(9,1.7){\shortstack[l]{if-Kette:\\\strut wird unübersichtlich}} + \put(1,-2){\mbox{\textarrow}} + \put(0,-3){\mbox{Zeiger auf Funktionen}} + \end{picture} + + \begin{lstlisting}[xleftmargin=4cm] + 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); + } + \end{lstlisting} + +\end{frame} + +\begin{frame}[fragile] + \showsubsection + + Zeiger auf Funktionen + + \medskip + + \begin{lstlisting} + void (* print) (t_object *this); + \end{lstlisting} + \begin{picture}(0,1.2)(0,-0.9) + \color{red} + \put(0.95,0.3){\mbox{$\underbrace{\rule{1cm}{0pt}}$}} + \put(0.2,-0.7){\shortstack{das, worauf print zeigt,\\ist eine Funktion}} + \end{picture} + + \begin{itemize} + \item + Objekt enthält Zeiger auf Funktion + \begin{onlyenv}<1> + \medskip + \begin{lstlisting}[gobble=10] + typedef struct + { + void (* print) (union t_object *this); + int content; + } t_integer; + \end{lstlisting} + \end{onlyenv} + \begin{onlyenv}<2-> + \vspace*{-3.5cm} % Why doesn't a picture environment work here??? :-( + \begin{lstlisting}[gobble=10,xleftmargin=5.5cm] + typedef struct + { + void (* print) (union t_object *this); + int content; + } t_integer; + \end{lstlisting} + \vspace*{0.85cm} + \bigskip + \smallskip + \end{onlyenv} + \pause + \item + Konstruktor initialisiert diesen Zeiger + \begin{onlyenv}<2> + \medskip + \begin{lstlisting}[gobble=10] + t_object *new_integer (int i) + { + t_object *p = malloc (sizeof (t_integer)); + p->integer.print = print_integer; + p->integer.content = i; + return p; + } + \end{lstlisting} + \vspace*{-2cm} + \end{onlyenv} + \pause + \item + Aufruf: "`automatisch"' die richtige Funktion + \begin{onlyenv}<3> + \medskip + \begin{lstlisting}[gobble=10] + for (int i = 0; object[i]; i++) + object[i]->base.print (object[i]); + \end{lstlisting} + \end{onlyenv} + \pause + \medskip + \item + in größeren Projekten:\\ + Objekt enthält Zeiger auf Tabelle von Funktionen + \end{itemize} +\end{frame} + +\subsection{Ausblick: C++} + +\begin{frame}[fragile] + \showsubsection + + \begin{center} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + void (* print) (union t_object *this); + } t_base;¿ + \end{lstlisting} + \end{minipage}\\[0.5cm] + \begin{minipage}{5.5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + void (* print) (...); + int content; + } t_integer;¿ + \end{lstlisting} + \end{minipage} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡typedef struct + { + void (* print) (union t_object *this); + char *content; + } t_string;¿ + \end{lstlisting} + \end{minipage} + \end{center} + +\end{frame} + +\begin{frame}[fragile] + \showsubsection + + \begin{center} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡struct TBase + { + virtual void print (void); + };¿ + \end{lstlisting} + \end{minipage}\\[0.5cm] + \begin{minipage}{5.5cm} + \begin{lstlisting}[gobble=8] + ¡struct TInteger: public TBase + { + virtual void print (void); + int content; + };¿ + \end{lstlisting} + \end{minipage} + \begin{minipage}{5cm} + \begin{lstlisting}[gobble=8] + ¡struct TString: public TBase + { + virtual void print (void); + char *content; + };¿ + \end{lstlisting} + \end{minipage} + \end{center} + +\end{frame} + +\nosectionnonumber{\inserttitle} + +\begin{frame} + + \shownosectionnonumber + + \begin{itemize} + \item[\textbf{1}] \textbf{Einführung} + \hfill\makebox(0,0)[br]{\raisebox{2.25ex}{\url{https://gitlab.cvh-server.de/pgerwinski/hp.git}}} + \item[\textbf{2}] \textbf{Einführung in C} + \item[\textbf{3}] \textbf{Bibliotheken} + \item[\textbf{4}] \textbf{Hardwarenahe Programmierung} + \item[\textbf{5}] \textbf{Algorithmen} + \item[\textbf{6}] \textbf{Objektorientierte Programmierung} + \begin{itemize} + \item[6.0] Dynamische Speicherverwaltung + \color{medgreen} + \item[6.1] Konzepte und Ziele + \item[6.2] Beispiel: Zahlen und Buchstaben + \item[6.3] Beispiel: Graphische Benutzeroberfläche (GUI) + \item[6.4] Unions + \item[6.5] Virtuelle Methoden + \item[6.6] Ausblick: C++ + \end{itemize} + \item[\textbf{7}] \textbf{Datenstrukturen} + \end{itemize} + \vspace*{-1cm} + +\end{frame} + +\end{document} diff --git a/20190113/hp-uebung-20190113.pdf b/20190113/hp-uebung-20190113.pdf new file mode 100644 index 0000000000000000000000000000000000000000..3bdbdd519681e19b5e3134efa85b0deb6f702f9e Binary files /dev/null and b/20190113/hp-uebung-20190113.pdf differ diff --git a/20190113/hp-uebung-20190113.tex b/20190113/hp-uebung-20190113.tex new file mode 100644 index 0000000000000000000000000000000000000000..aa4f3f9dfccd271c7259103c8eb96a2ab26fb411 --- /dev/null +++ b/20190113/hp-uebung-20190113.tex @@ -0,0 +1,220 @@ +% hp-uebung-20190113.pdf - Exercises on Low-Level Programming / Applied Computer Sciences +% Copyright (C) 2013, 2015, 2016, 2017, 2018, 2019 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/>. + +% README: Iteratorfunktionen, Objektorientierte Tier-Datenbank + +\documentclass[a4paper]{article} + +\usepackage{pgscript} + +\begin{document} + +% \thispagestyle{empty} + + \section*{Hardwarenahe Programmierung\\ + Übungsaufgaben -- 13.\ Januar 2019} + + Diese Übung enthält Punkteangaben wie in einer Klausur. + Um zu "`bestehen"', müssen Sie innerhalb von 90 Minuten + unter Verwendung ausschließlich zugelassener Hilfsmittel + 16 Punkte (von insgesamt \totalpoints) erreichen. + + \exercise{Iterationsfunktionen} + + Wir betrachten das folgende Programm (\gitfile{hp}{20190113}{aufgabe-1.c}): + + \begin{minipage}[t]{0.4\textwidth} + \begin{lstlisting}[gobble=6] + #include <stdio.h> + + void foreach (int *a, void (*fun) (int x)) + { + for (int *p = a; *p >= 0; p++) + fun (*p); + } + + void even_or_odd (int x) + { + if (x % 2) + printf ("%d ist ungerade.\n", x); + else + printf ("%d ist gerade.\n", x); + } + \end{lstlisting} + \end{minipage}\hfill + \begin{minipage}[t]{0.52\textwidth} + \begin{lstlisting}[gobble=6] + int main (void) + { + int numbers[] = { 12, 17, 32, 1, 3, 16, 19, 18, -1 }; + foreach (numbers, even_or_odd); + return 0; + } + \end{lstlisting} + \begin{enumerate}[\quad(a)] + \item + Was bedeutet \lstinline{void (*fun) (int x)}, + und welchen Sinn hat seine Verwendung in der Funktion \lstinline{foreach()}? + \points{2} + \item + Schreiben Sie das Hauptprogramm \lstinline{main()} so um, + daß es unter Verwendung der Funktion \lstinline{foreach()} + die Summe aller positiven Zahlen in dem Array berechnet. + Sie dürfen dabei weitere Funktionen sowie globale Variable einführen. + \points{4} + \end{enumerate} + \end{minipage} + + \exercise{Objektorientierte Tier-Datenbank} + + Das auf der nächsten Seite in Blau dargestellte Programm (Datei: \gitfile{hp}{20190113}{aufgabe-2a.c})\\ + soll Daten von Tieren verwalten. + + Beim Compilieren erscheinen die folgende Fehlermeldungen: + \begin{lstlisting}[style=terminal] + $ ¡gcc -std=c99 -Wall -O aufgabe-2a.c -o aufgabe-2a¿ + aufgabe-2a.c: In function 'main': + aufgabe-2a.c:31: error: 'animal' has no member named 'wings' + aufgabe-2a.c:37: error: 'animal' has no member named 'legs' + \end{lstlisting} + + Der Programmierer nimmt die auf der nächsten Seite in Rot dargestellten Ersetzungen vor\\ + (Datei: \gitfile{hp}{20190113}{aufgabe-2b.c}). + Daraufhin gelingt das Compilieren, und die Ausgabe des Programms lautet: + \begin{lstlisting}[style=terminal] + $ ¡gcc -std=c99 -Wall -O aufgabe-2b.c -o aufgabe-2b¿ + $ ¡./aufgabe-2b¿ + A duck has 2 legs. + Error in animal: cow + \end{lstlisting} + + \begin{itemize} + \item[(a)] + Erklären Sie die o.\,a.\ Compiler-Fehlermeldungen. + \points{2} + \item[(b)] + Wieso verschwinden die Fehlermeldungen nach den o.\,a.\ Ersetzungen? + \points{3} + \item[(c)] + Erklären Sie die Ausgabe des Programms. + \points{5} + \item[(d)] + Beschreiben Sie -- in Worten und/oder als C-Quelltext -- einen Weg, + das Programm so zu berichtigen, daß es die Eingabedaten + (``A duck has 2 wings. A cow has 4 legs.'') korrekt speichert und ausgibt.\\ + \points{4} + \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} + + \begin{minipage}[t]{0.34\textwidth} + \begin{lstlisting}[gobble=6,xleftmargin=0pt] + #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 wings; + } with_wings; + + typedef struct with_legs + { + int legs; + } with_legs; + \end{lstlisting} + \end{minipage}\hfill + \begin{minipage}[t]{0.65\textwidth} + \begin{lstlisting}[gobble=6,xleftmargin=0pt] + int main (void) + { + animal *a[2]; + + animal duck; + a[0] = &duck; + a[0]->type = WITH_WINGS; + a[0]->name = "duck"; + a[0]->wings = 2; + + animal cow; + a[1] = &cow; + a[1]->type = WITH_LEGS; + a[1]->name = "cow"; + 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; + } + \end{lstlisting} + \begin{picture}(0,0) + \color{red} + \put(3.7,6.207){\vector(-1,0){0.7}} + \put(3.8,6.207){\makebox(0,0)[l]{\lstinline[basicstyle=\color{red}]{((with_legs *) a[1])->legs = 4;}}} + \put(4.0,8.735){\vector(-1,0){0.7}} + \put(4.1,8.735){\makebox(0,0)[l]{\lstinline[basicstyle=\color{red}]{((with_wings *) a[0])->wings = 2;}}} + \end{picture} + \end{minipage} + + \bigskip + \begin{flushright} + \textit{Viel Erfolg!} + \end{flushright} + + \makeatletter + \immediate\write\@mainaux{\string\gdef\string\totalpoints{\arabic{points}}} + \makeatother + +\end{document} diff --git a/20190113/landau-symbols-2.pdf b/20190113/landau-symbols-2.pdf new file mode 120000 index 0000000000000000000000000000000000000000..6b458b6efd8e274824a6dfcaabc4b9c27d196dc4 --- /dev/null +++ b/20190113/landau-symbols-2.pdf @@ -0,0 +1 @@ +../common/landau-symbols-2.pdf \ No newline at end of file diff --git a/20190113/landau-symbols.pdf b/20190113/landau-symbols.pdf new file mode 120000 index 0000000000000000000000000000000000000000..ca145425bf07439c680632aa0663f84be601a565 --- /dev/null +++ b/20190113/landau-symbols.pdf @@ -0,0 +1 @@ +../common/landau-symbols.pdf \ No newline at end of file diff --git a/20190113/logo-hochschule-bochum-cvh-text-v2.pdf b/20190113/logo-hochschule-bochum-cvh-text-v2.pdf new file mode 120000 index 0000000000000000000000000000000000000000..4aa99b8f81061aca6dcaf43eed2d9efef40555f8 --- /dev/null +++ b/20190113/logo-hochschule-bochum-cvh-text-v2.pdf @@ -0,0 +1 @@ +../common/logo-hochschule-bochum-cvh-text-v2.pdf \ No newline at end of file diff --git a/20190113/logo-hochschule-bochum.pdf b/20190113/logo-hochschule-bochum.pdf new file mode 120000 index 0000000000000000000000000000000000000000..b6b9491e370e499c9276918182cdb82cb311bcd1 --- /dev/null +++ b/20190113/logo-hochschule-bochum.pdf @@ -0,0 +1 @@ +../common/logo-hochschule-bochum.pdf \ No newline at end of file diff --git a/20190107/objects-1.c b/20190113/objects-1.c similarity index 100% rename from 20190107/objects-1.c rename to 20190113/objects-1.c diff --git a/20190107/objects-2.c b/20190113/objects-2.c similarity index 100% rename from 20190107/objects-2.c rename to 20190113/objects-2.c diff --git a/20190107/objects-3.c b/20190113/objects-3.c similarity index 100% rename from 20190107/objects-3.c rename to 20190113/objects-3.c diff --git a/20190107/objects-4.c b/20190113/objects-4.c similarity index 100% rename from 20190107/objects-4.c rename to 20190113/objects-4.c diff --git a/20190107/objects-5.c b/20190113/objects-5.c similarity index 100% rename from 20190107/objects-5.c rename to 20190113/objects-5.c diff --git a/20190107/objects-6.c b/20190113/objects-6.c similarity index 100% rename from 20190107/objects-6.c rename to 20190113/objects-6.c diff --git a/20190113/pgscript.sty b/20190113/pgscript.sty new file mode 120000 index 0000000000000000000000000000000000000000..95c888478c99ea7fda0fd11ccf669ae91be7178b --- /dev/null +++ b/20190113/pgscript.sty @@ -0,0 +1 @@ +../common/pgscript.sty \ No newline at end of file diff --git a/20190113/pgslides.sty b/20190113/pgslides.sty new file mode 120000 index 0000000000000000000000000000000000000000..5be1416f4216f076aa268901f52a15d775e43f64 --- /dev/null +++ b/20190113/pgslides.sty @@ -0,0 +1 @@ +../common/pgslides.sty \ No newline at end of file diff --git a/README.md b/README.md index 0a9b5a44c3bccc7ff90b63d7b5c2e3dd4a32dcc7..914837b5053ee54a6a495d9d0410690cf53b2593 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Vortragsfolien und Beispiele: * [10.12.2018: volatile-Variable, Byte-Reihenfolge - Endianness, Binärdarstellung negativer Zahlen, Speicherausrichtung - Alignment](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20181210/hp-20181210.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/master/20181210/) * [17.12.2018: Rekursion, Aufwandsabschätzungen](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20181217/hp-20181217.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/master/20181217/) * [07.01.2019: Aufwandsabschätzungen, dynamische Speicherverwaltung](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20190107/hp-20190107.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/master/20190107/) + * [13.01.2019: Objektorientierte Programmierung](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20190113/hp-20190113.pdf) [**(Beispiele)**](https://gitlab.cvh-server.de/pgerwinski/hp/tree/master/20190113/) * [alle in 1 Datei](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/hp-slides-2018ws.pdf) Übungsaufgaben: @@ -45,6 +46,7 @@ Vortragsfolien und Beispiele: * [10.12.2018: Trickprogrammierung, Thermometer-Baustein an I²C-Bus](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20181210/hp-uebung-20181210.pdf) * [17.12.2018: Fakultät, Lauflicht, Länge von Strings (Neuauflage)](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20181217/hp-uebung-20181217.pdf) * [07.01.2019: Speicherformate von Zahlen, Zeigerarithmetik](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20190107/hp-uebung-20190107.pdf) + * [13.01.2019: Iteratorfunktionen, Objektorientierte Tier-Datenbank](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20190113/hp-uebung-20190113.pdf) Musterlösungen: --------------- diff --git a/hp-slides-2018ws.pdf b/hp-slides-2018ws.pdf index 7acf4f495ae2753e84b71f955be3c8a8ea2bdd18..ec91c823554d28fab6079c58e99b6d266370be71 100644 Binary files a/hp-slides-2018ws.pdf and b/hp-slides-2018ws.pdf differ diff --git a/hp-slides-2018ws.tex b/hp-slides-2018ws.tex index 3857cfffb7d651540dd07210f95adead692fef55..35ca4399e07c9f94e07b168983b7cc87e6a4084a 100644 --- a/hp-slides-2018ws.tex +++ b/hp-slides-2018ws.tex @@ -34,4 +34,6 @@ \includepdf[pages=-]{20181217/hp-20181217.pdf} \pdfbookmark[1]{07.01.2019: Aufwandsabschätzungen, dynamische Speicherverwaltung}{20190107} \includepdf[pages=-]{20190107/hp-20190107.pdf} + \pdfbookmark[1]{13.01.2019: Objektorientierte Programmierung}{20190113} + \includepdf[pages=-]{20190113/hp-20190113.pdf} \end{document}