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

Beispiele 11.7.2024

parent 77f7719b
No related branches found
No related tags found
No related merge requests found
Showing
with 644 additions and 19 deletions
No preview for this file type
......@@ -60,7 +60,7 @@
\item[\textbf{4}] \textbf{Kryptographie}
\item[\textbf{5}] \textbf{Datenkompression}
\item[\color{orange}\textbf{6}] \textbf{\color{orange}Einführung in C++, Datenorganisation}
\item[\color{orange}\textbf{7}] \textbf{\color{orange}Hardwarenahe Algorithmen}
\item[\color{medgreen}\textbf{7}] \textbf{\color{medgreen}Hardwarenahe Algorithmen}
\end{itemize}
\end{frame}
......@@ -423,22 +423,4 @@
\end{itemize}
\end{frame}
\section{Hardwarenahe Algorithmen}
\begin{frame}
\showsection
\textbf{Aufgabe:}
Schreiben Sie die Sinusfunktion selbst.
\smallskip
\begin{itemize}
\item
Wir setzen nur die Grundrechenarten voraus.
\item
möglichst effizient
\end{itemize}
\end{frame}
\end{document}
#include <iostream>
using namespace std;
int main ()
{
int a = 42;
int b = 0;
if (b == 0)
{
cout << "Irgendwas ist schiefgelaufen: Ganzzahldivision durch Null" << endl;
}
else
{
int c = a / b;
cout << "c = " << c << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
try
{
int a = 42;
int b = 0;
if (b == 0)
throw "Ganzzahldivision durch Null";
int c = a / b;
cout << "c = " << c << endl;
}
catch (const char *msg)
{
cout << "Irgendwas ist schiefgelaufen: " << msg << endl;
}
return 0;
}
#include <iostream>
using namespace std;
void check_answer (int answer)
{
if (answer != 42)
{
try
{
if (answer == 10)
throw answer;
else if (answer == 137)
throw "alpha";
}
catch (int e)
{
cout << "Yeah!" << endl;
throw;
}
throw "bullshit";
}
}
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer == 23)
throw answer;
else
check_answer (answer);
}
catch (const char *e)
{
cout << "caught string exception: " << e << endl;
}
cout << "And what was the question?" << endl;
return 0;
}
#include <iostream>
using namespace std;
class philosophy_exception: public exception
{
int answer;
public:
philosophy_exception (int a) { answer = a; }
};
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer != 42)
throw philosophy_exception (answer);
cout << "And what was the question?" << endl;
}
catch (exception &e)
{
cout << "caught exception" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
class philosophy_exception: public exception
{
int answer;
public:
philosophy_exception (int a) { answer = a; }
int get_answer () { return answer; }
};
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer != 42)
throw philosophy_exception (answer);
cout << "And what was the question?" << endl;
}
catch (philosophy_exception &e)
{
cout << "caught philosophy exception: " << e.get_answer () << endl;
}
catch (exception &e)
{
cout << "caught exception" << endl;
}
return 0;
}
#include <iostream>
using namespace std;
class philosophy_exception: public exception
{
int answer;
public:
philosophy_exception (int a) { answer = a; }
int get_answer () { return answer; }
};
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer != 42)
throw philosophy_exception (answer);
cout << "And what was the question?" << endl;
}
catch (exception &e)
{
cout << "caught exception" << endl;
}
catch (philosophy_exception &e)
{
cout << "caught philosophy exception: " << e.get_answer () << endl;
}
return 0;
}
#include <iostream>
#include <sstream>
using namespace std;
class philosophy_exception: public exception
{
int answer;
public:
philosophy_exception (int a) { answer = a; }
virtual const char *what ();
};
const char *philosophy_exception::what ()
{
ostringstream buffer;
buffer << "philosophy exception #" << answer;
return buffer.str ().c_str ();
}
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer == 42)
cout << "And what was the question?" << endl;
else
throw philosophy_exception (answer);
}
catch (exception &e)
{
cout << "caught exception: " << e.what () << endl;
}
return 0;
}
#include <iostream>
#include <sstream>
using namespace std;
class philosophy_exception: public exception
{
int answer;
public:
philosophy_exception (int a) { answer = a; }
virtual const char *what () const noexcept;
};
const char *philosophy_exception::what () const noexcept
{
ostringstream buffer;
buffer << "philosophy exception #" << answer;
return buffer.str ().c_str ();
}
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer == 42)
cout << "And what was the question?" << endl;
else
throw philosophy_exception (answer);
}
catch (exception &e)
{
cout << "caught exception: " << e.what () << endl;
}
return 0;
}
#include <iostream>
#include <sstream>
using namespace std;
class philosophy_exception: public exception
{
int answer;
public:
philosophy_exception (int a) { answer = a; }
virtual const char *what () const noexcept;
};
const char *philosophy_exception::what () const noexcept
{
return "Marvin";
// ostringstream buffer;
// buffer << "philosophy exception #" << answer;
// return buffer.str ().c_str ();
}
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer == 42)
cout << "And what was the question?" << endl;
else
throw philosophy_exception (answer);
}
catch (exception &e)
{
cout << "caught exception: " << e.what () << endl;
}
return 0;
}
#include <iostream>
#include <sstream>
using namespace std;
class philosophy_exception: public exception
{
int answer;
static const char *buffer;
public:
philosophy_exception (int a) { answer = a; }
virtual const char *what () const noexcept;
};
const char *philosophy_exception::what () const noexcept
{
return buffer;
// ostringstream buffer;
// buffer << "philosophy exception #" << answer;
// return buffer.str ().c_str ();
}
const char *philosophy_exception::buffer = "Marvin";
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer == 42)
cout << "And what was the question?" << endl;
else
throw philosophy_exception (answer);
}
catch (exception &e)
{
cout << "caught exception: " << e.what () << endl;
}
return 0;
}
#include <iostream>
#include <sstream>
#include <string.h>
using namespace std;
class philosophy_exception: public exception
{
int answer;
static char buffer[42];
public:
philosophy_exception (int a) { answer = a; }
virtual const char *what () const noexcept;
};
const char *philosophy_exception::what () const noexcept
{
ostringstream tmp_buffer;
tmp_buffer << "philosophy exception #" << answer;
strcpy (buffer, tmp_buffer.str ().c_str ());
return buffer;
}
char philosophy_exception::buffer[42] = "Marvin";
int main ()
{
int answer;
cout << "What's your answer? ";
cin >> answer;
try
{
if (answer == 42)
cout << "And what was the question?" << endl;
else
throw philosophy_exception (answer);
}
catch (exception &e)
{
cout << "caught exception: " << e.what () << endl;
}
return 0;
}
#include <iostream>
int main ()
{
int prime[5] = { 2, 3, 5, 7, 11 };
for (int *p = prime; p != prime + 5; p++)
std::cout << *p << std::endl;
return 0;
}
.file "iterators-01.cpp"
.text
.globl main
.type main, @function
main:
.LFB1782:
.cfi_startproc
pushq %r13
.cfi_def_cfa_offset 16
.cfi_offset 13, -16
pushq %r12
.cfi_def_cfa_offset 24
.cfi_offset 12, -24
pushq %rbp
.cfi_def_cfa_offset 32
.cfi_offset 6, -32
pushq %rbx
.cfi_def_cfa_offset 40
.cfi_offset 3, -40
subq $40, %rsp
.cfi_def_cfa_offset 80
movl $2, (%rsp)
movl $3, 4(%rsp)
movl $5, 8(%rsp)
movl $7, 12(%rsp)
movl $11, 16(%rsp)
movq %rsp, %rbp
leaq _ZSt4cout(%rip), %r12
jmp .L5
.L9:
call _ZSt16__throw_bad_castv@PLT
.L3:
movq %r13, %rdi
call _ZNKSt5ctypeIcE13_M_widen_initEv@PLT
movq 0(%r13), %rax
movl $10, %esi
movq %r13, %rdi
call *48(%rax)
movl %eax, %esi
.L4:
movsbl %sil, %esi
movq %rbx, %rdi
call _ZNSo3putEc@PLT
movq %rax, %rdi
call _ZNSo5flushEv@PLT
addq $4, %rbp
leaq 20(%rsp), %rax
cmpq %rax, %rbp
je .L8
.L5:
movl 0(%rbp), %esi
movq %r12, %rdi
call _ZNSolsEi@PLT
movq %rax, %rbx
movq (%rax), %rax
movq -24(%rax), %rax
movq 240(%rbx,%rax), %r13
testq %r13, %r13
je .L9
cmpb $0, 56(%r13)
je .L3
movzbl 67(%r13), %esi
jmp .L4
.L8:
movl $0, %eax
addq $40, %rsp
.cfi_def_cfa_offset 40
popq %rbx
.cfi_def_cfa_offset 32
popq %rbp
.cfi_def_cfa_offset 24
popq %r12
.cfi_def_cfa_offset 16
popq %r13
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE1782:
.size main, .-main
.type _GLOBAL__sub_I_main, @function
_GLOBAL__sub_I_main:
.LFB2312:
.cfi_startproc
pushq %rbx
.cfi_def_cfa_offset 16
.cfi_offset 3, -16
leaq _ZStL8__ioinit(%rip), %rbx
movq %rbx, %rdi
call _ZNSt8ios_base4InitC1Ev@PLT
leaq __dso_handle(%rip), %rdx
movq %rbx, %rsi
movq _ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %rdi
call __cxa_atexit@PLT
popq %rbx
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE2312:
.size _GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main
.section .init_array,"aw"
.align 8
.quad _GLOBAL__sub_I_main
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.hidden __dso_handle
.ident "GCC: (Debian 12.2.0-14) 12.2.0"
.section .note.GNU-stack,"",@progbits
#include <iostream>
int main ()
{
int prime[5] = { 2, 3, 5, 7, 11 };
for (int *p = prime; p <= prime + 4; p++)
std::cout << *p << std::endl;
return 0;
}
#include <iostream>
#include <array>
int main ()
{
std::array <int, 5> prime = { { 2, 3, 5, 7, 11 } };
for (std::array <int, 5>::iterator p = prime.begin (); p != prime.end (); p++)
std::cout << *p << std::endl;
return 0;
}
.file "iterators-03.cpp"
.text
.globl main
.type main, @function
main:
.LFB1824:
.cfi_startproc
pushq %r13
.cfi_def_cfa_offset 16
.cfi_offset 13, -16
pushq %r12
.cfi_def_cfa_offset 24
.cfi_offset 12, -24
pushq %rbp
.cfi_def_cfa_offset 32
.cfi_offset 6, -32
pushq %rbx
.cfi_def_cfa_offset 40
.cfi_offset 3, -40
subq $40, %rsp
.cfi_def_cfa_offset 80
movl $2, (%rsp)
movl $3, 4(%rsp)
movl $5, 8(%rsp)
movl $7, 12(%rsp)
movl $11, 16(%rsp)
movq %rsp, %rbp
leaq _ZSt4cout(%rip), %r12
jmp .L5
.L9:
call _ZSt16__throw_bad_castv@PLT
.L3:
movq %r13, %rdi
call _ZNKSt5ctypeIcE13_M_widen_initEv@PLT
movq 0(%r13), %rax
movl $10, %esi
movq %r13, %rdi
call *48(%rax)
movl %eax, %esi
.L4:
movsbl %sil, %esi
movq %rbx, %rdi
call _ZNSo3putEc@PLT
movq %rax, %rdi
call _ZNSo5flushEv@PLT
addq $4, %rbp
leaq 20(%rsp), %rax
cmpq %rax, %rbp
je .L8
.L5:
movl 0(%rbp), %esi
movq %r12, %rdi
call _ZNSolsEi@PLT
movq %rax, %rbx
movq (%rax), %rax
movq -24(%rax), %rax
movq 240(%rbx,%rax), %r13
testq %r13, %r13
je .L9
cmpb $0, 56(%r13)
je .L3
movzbl 67(%r13), %esi
jmp .L4
.L8:
movl $0, %eax
addq $40, %rsp
.cfi_def_cfa_offset 40
popq %rbx
.cfi_def_cfa_offset 32
popq %rbp
.cfi_def_cfa_offset 24
popq %r12
.cfi_def_cfa_offset 16
popq %r13
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE1824:
.size main, .-main
.type _GLOBAL__sub_I_main, @function
_GLOBAL__sub_I_main:
.LFB2358:
.cfi_startproc
pushq %rbx
.cfi_def_cfa_offset 16
.cfi_offset 3, -16
leaq _ZStL8__ioinit(%rip), %rbx
movq %rbx, %rdi
call _ZNSt8ios_base4InitC1Ev@PLT
leaq __dso_handle(%rip), %rdx
movq %rbx, %rsi
movq _ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %rdi
call __cxa_atexit@PLT
popq %rbx
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE2358:
.size _GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main
.section .init_array,"aw"
.align 8
.quad _GLOBAL__sub_I_main
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.hidden __dso_handle
.ident "GCC: (Debian 12.2.0-14) 12.2.0"
.section .note.GNU-stack,"",@progbits
#include <iostream>
#include <vector>
int main ()
{
std::vector <int> prime = { { 2, 3, 5, 7, 11 } };
for (std::vector <int>::iterator p = prime.begin (); p != prime.end (); p++)
std::cout << *p << std::endl;
return 0;
}
#include <iostream>
#include <vector>
int main ()
{
std::vector <int> prime = { { 2, 3, 5, 7, 11 } };
for (std::vector <int>::iterator p = prime.begin (); p != prime.end (); p++)
std::cout << *p << std::endl;
prime.push_back (13);
prime.push_back (17);
for (std::vector <int>::iterator p = prime.begin (); p != prime.end (); p++)
std::cout << *p << std::endl;
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment