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

Vorbereitung 22.10.2018

parent 63680d7c
Branches
Tags
No related merge requests found
Showing
with 1746 additions and 0 deletions
char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}
year = ORIGINYEAR; /* = 1980 */
while (days > 365)
{
if (IsLeapYear (year))
{
if (days > 366)
{
days -= 366;
year += 1;
}
}
else
{
days -= 365;
year += 1;
}
}
File added
This diff is collapsed.
File added
% hp-uebung-20181022.pdf - Exercises on Low-Level Programming / Applied Computer Sciences
% Copyright (C) 2013, 2015, 2016, 2017, 2018 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: ROT13-Verschlüsselung, Programm analysieren, Kalender-Berechnung
\documentclass[a4paper]{article}
\usepackage{pgscript}
\begin{document}
\section*{Hardwarenahe Programmierung / Angewandte Informatik\\
Übungsaufgaben -- 22.\ Oktober 2018}
\exercise{ROT13-Verschlüsselung}
Schreiben Sie ein C-Programm, das einen Text entgegennimmt,
jeden Buchstaben von A bis Z zyklisch um 13 Stellen verschiebt
und den auf diese Weise "`verschlüsselten"' Text wieder ausgibt.
Umlaute, Ziffern, Satz- und Sonderzeichen sollen nicht verändert werden.
\begin{tabular}{ccc}
A & $\longrightarrow$ & N \\
B & $\longrightarrow$ & O \\
& \dots & \\
M & $\longrightarrow$ & Z \\
N & $\longrightarrow$ & A \\
& \dots & \\
Y & $\longrightarrow$ & L \\
Z & $\longrightarrow$ & M
\end{tabular}
\qquad
\begin{tabular}{ccc}
a & $\longrightarrow$ & n \\
b & $\longrightarrow$ & o \\
& \dots & \\
m & $\longrightarrow$ & z \\
n & $\longrightarrow$ & a \\
& \dots & \\
y & $\longrightarrow$ & l \\
z & $\longrightarrow$ & m
\end{tabular}
Beispiel: Aus "`Apfelkuchen"' wird "`Ncsryxhpura"'.
\exercise{Programm analysieren}
Wir betrachten das folgende C-Programm (Datei: \gitfile{hp}{20181022}{aufgabe-2.c}):
\begin{lstlisting}
char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}
\end{lstlisting}
\vspace{-\medskipamount}
\begin{itemize}
\item[(a)]
Was bewirkt dieses Programm?
\item[(b)]
Wofür stehen die Zahlen?
\item[(c)]
Ergänzen Sie das Programm derart, daß seine \lstinline{main()}-Funktion
\lstinline{int main (void)} lautet und eine \lstinline{return}-Anweisung hat,
wobei die in Aufgabenteil (a) festgestellte Eigenschaft erhalten bleiben soll.
\end{itemize}
\exercise{Kalender-Berechnung}
Am 3.\,1.\,2009 meldete \emph{heise online\/}:
\begin{quote}
Kunden des ersten mobilen Media-Players von Microsoft
erlebten zum Jahresende eine böse Überraschung:
Am 31.\ Dezember 2008 fielen weltweit alle Zune-Geräte der ersten Generation aus.
Ursache war ein interner Fehler bei der Handhabung von Schaltjahren.
\strut\hfill\url{http://heise.de/-193332},
\end{quote}
Der Artikel verweist auf ein Quelltextfragment (Datei: \gitfile{hp}{20181022}{aufgabe-3.c}),
das für einen gegebenen Wert \lstinline{days}
das Jahr und den Tag innerhalb des Jahres
für den \lstinline{days}-ten Tag nach dem 1.\,1.\,1980 berechnen soll:
\begin{lstlisting}
year = ORIGINYEAR; /* = 1980 */
while (days > 365)
{
if (IsLeapYear (year))
{
if (days > 366)
{
days -= 366;
year += 1;
}
}
else
{
days -= 365;
year += 1;
}
}
\end{lstlisting}
\vspace*{-1cm}
\goodbreak
Dieses Quelltextfragment weist mehrere Code-Verdopplungen auf:
\begin{itemize}
\item
Die Anweisung \lstinline{year += 1} taucht an zwei Stellen auf.
\item
Es gibt zwei unabhängige Abfragen \lstinline{days > 365} und \lstinline{days > 366}:\\
eine in einer \lstinline{while}- und die andere in einer \lstinline{if}-Bedingung.
\item
Die Länge eines Jahres wird nicht durch eine Funktion berechnet oder in einer Variablen gespeichert;
stattdessen werden an mehreren Stellen die expliziten numerischen Konstanten 365 und 366 verwendet.
\end{itemize}
Diese Probleme führten am 31.\ Dezember 2008 zu einer Endlosschleife,
die sich -- z.\,B.\ durch eine Funktion \lstinline{DaysInYear()} -- leicht hätte vermeiden lassen.
Gut hingegen ist die Verwendung einer Konstanten \lstinline{ORIGINYEAR}
anstelle der Zahl 1980
sowie die Kapselung der Berechnung der Schaltjahr-Bedingung
in einer Funktion \lstinline{IsLeapYear()}.
\begin{itemize}
\item[(a)]
Erklären Sie das Zustandekommen der Endlosschleife.
\item[(b)]
Schreiben Sie das Quelltextfragment so um, daß es die beschriebenen Probleme
nicht mehr enthält.
\end{itemize}
\end{document}
../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
../common/pgscript.sty
\ No newline at end of file
../common/pgslides.sty
\ No newline at end of file
#include <stdio.h>
int main (void)
{
for (int a = 0; a < 1000000; a++)
{
if ((a * 1117) % 65535 == 137)
{
for (int t = 2; t < 1000000; t++)
{
if (a % t == 0)
{
printf ("Found something:\n");
printf ("a = %d\nt = %d\n", a, t);
a = 2000000;
break;
}
else
continue;
}
}
}
printf ("Bye.\n");
return 0;
}
#include <stdio.h>
int main (void)
{
for (int a = 0; a < 1000000; a++)
{
if ((a * 1117) % 65535 == 137)
{
for (int t = 2; t < 1000000; t++)
{
if (a % t == 0)
{
printf ("Found something:\n");
printf ("a = %d\nt = %d\n", a, t);
break;
}
}
break;
}
}
printf ("Bye.\n");
return 0;
}
#include <stdio.h>
int main (void)
{
for (int a = 0; 1; a++)
{
if ((a * 1117) % 65535 == 137)
{
for (int t = 2; 1; t++)
{
if (a % t == 0)
{
printf ("Found something:\n");
printf ("a = %d\nt = %d\n", a, t);
goto found;
}
}
}
}
found:
printf ("Bye.\n");
return 0;
}
#include <stdio.h>
int main (void)
{
for (int a = 0; 1; a++)
{
if ((a * 1117) % 65535 == 137)
{
int t = 2;
while (a % t != 0)
t++;
printf ("Found something:\n");
printf ("a = %d\nt = %d\n", a, t);
break;
}
}
printf ("Bye.\n");
return 0;
}
#include <stdio.h>
int main (void)
{
int a = 0;
while ((a * 1117) % 65535 != 137)
{
a++;
if ((a * 1117) % 65535 == 137)
{
int t = 2;
while (a % t != 0)
t++;
printf ("Found something:\n");
printf ("a = %d\nt = %d\n", a, t);
break;
}
}
printf ("Bye.\n");
return 0;
}
#include <stdio.h>
int main (void)
{
int a = 0;
while ((a * 1117) % 65535 != 137)
a++;
int t = 2;
while (a % t != 0)
t++;
printf ("Found something:\n");
printf ("a = %d\nt = %d\n", a, t);
printf ("Bye.\n");
return 0;
}
#include <stdio.h>
int main (void)
{
int N = 65535;
int p = 1117;
int q = 137;
int a = 0;
while ((a * p) % N != q)
a++;
int t = 2;
while (a % t != 0)
t++;
printf ("Found something:\n");
printf ("a = %d\nt = %d\n", a, t);
printf ("Bye.\n");
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment