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

Vorbereitung 13.1.2019

parent 5a00428f
Branches
No related tags found
No related merge requests found
Showing
with 1137 additions and 0 deletions
#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;
}
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File moved
File added
% 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}
File added
% 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}
../common/landau-symbols-2.pdf
\ No newline at end of file
../common/landau-symbols.pdf
\ No newline at end of file
../common/logo-hochschule-bochum-cvh-text-v2.pdf
\ No newline at end of file
../common/logo-hochschule-bochum.pdf
\ No newline at end of file
File moved
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment