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

Vorbereitung 16.1.2025

parent d14f85ca
Branches
No related tags found
No related merge requests found
Showing
with 2568 additions and 4 deletions
No preview for this file type
......@@ -20,7 +20,7 @@
% Attribution-ShareAlike 3.0 Unported License along with this
% document. If not, see <http://creativecommons.org/licenses/>.
% README: Objektorientierte Programmierung: Einführung
% README: Objektorientierte Programmierung: Einführung, Unions
\documentclass[10pt,t]{beamer}
......@@ -529,8 +529,8 @@
\put(-2.75,-0.05){\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
% nächste Woche
% kommt gleich
nächste Woche
\end{rotate}}
\end{picture}
Zeiger, die im Objekt gespeichert sind\\
......@@ -796,6 +796,8 @@
\end{frame}
\iffalse
\subsection{Virtuelle Methoden}
\begin{frame}[fragile]
......@@ -1020,4 +1022,6 @@
\end{frame}
\fi
\end{document}
% hp-uebung-20241219.pdf - Exercises on Low-Level Programming / Applied Computer Sciences
% hp-uebung-20250109.pdf - Exercises on Low-Level Programming / Applied Computer Sciences
% Copyright (C) 2013, 2015-2024, 2025 Peter Gerwinski
%
% This document is free software: you can redistribute it and/or
......
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *next;
} node;
void output_list (node *first)
{
for (node *p = first; p; p = p->next)
printf ("%d ", p->content);
printf ("\n");
}
void insert_into_list (node *what, node *where)
{
what->next = where->next;
where->next = what;
}
int main (void)
{
node *element3 = malloc (sizeof (node));
node *element7 = malloc (sizeof (node));
node *element137 = malloc (sizeof (node));
element3->content = 3;
element7->content = 7;
element137->content = 137;
node *first = element3;
element3->next = element7;
element7->next = element137;
element137->next = NULL;
output_list (first);
node *element5 = malloc (sizeof (node));
element5->content = 5;
insert_into_list (element5, element3);
output_list (first);
return 0;
}
#include <gtk/gtk.h>
#define WIDTH 320
#define HEIGHT 240
double t = 0.0;
double dt = 0.2;
int r = 5;
double x = 10;
double y = 200;
double vx = 20;
double vy = -60;
double g = 9.81;
gboolean draw (GtkWidget *widget, cairo_t *c, gpointer data)
{
GdkRGBA blue = { 0.0, 0.5, 1.0, 1.0 };
gdk_cairo_set_source_rgba (c, &blue);
cairo_arc (c, x, y, r, 0, 2 * G_PI);
cairo_fill (c);
return FALSE;
}
gboolean timer (GtkWidget *widget)
{
t += dt;
x += vx * dt;
y += vy * dt;
vx = vx;
vy = 0.5 * g * (t * t);
if (y + r >= HEIGHT)
vy = -vy * 0.9;
if (x + r >= WIDTH)
vx = -vx * 0.9;
if (x - r <= 0)
vx = -vx * 0.9;
gtk_widget_queue_draw_area (widget, 0, 0, WIDTH, HEIGHT);
g_timeout_add (50, (GSourceFunc) timer, widget);
return FALSE;
}
int main (int argc, char **argv)
{
gtk_init (&argc, &argv);
GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_show (window);
gtk_window_set_title (GTK_WINDOW (window), "Hello");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
GtkWidget *drawing_area = gtk_drawing_area_new ();
gtk_widget_show (drawing_area);
gtk_container_add (GTK_CONTAINER (window), drawing_area);
gtk_widget_set_size_request (drawing_area, WIDTH, HEIGHT);
gtk_main ();
return 0;
}
#include <stdio.h>
#define STACK_SIZE 10
int stack[STACK_SIZE];
int stack_pointer = 0;
void push (int x)
{
stack[stack_pointer++] = x;
}
void show (void)
{
printf ("stack content:");
for (int i = 0; i < stack_pointer; i++)
printf (" %d", stack[i]);
if (stack_pointer)
printf ("\n");
else
printf (" (empty)\n");
}
void insert (int x, int pos)
{
for (int i = pos; i < stack_pointer; i++)
stack[i + 1] = stack[i];
stack[pos] = x;
stack_pointer++;
}
void insert_sorted (int x)
{
int i = 0;
while (i < stack_pointer && x < stack[i])
i++;
insert (x, i);
}
int main (void)
{
push (3);
push (7);
push (137);
show ();
insert (5, 1);
show ();
insert_sorted (42);
show ();
insert_sorted (2);
show ();
return 0;
}
File moved
File added
This diff is collapsed.
File added
hp-uebung-20250116.tex
\ No newline at end of file
File added
This diff is collapsed.
../common/landau-symbols-2.pdf
\ No newline at end of file
../common/landau-symbols-3.pdf
\ No newline at end of file
../common/landau-symbols.pdf
\ No newline at end of file
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *next;
} node;
void output_list (node *first)
{
for (node *p = first; p; p = p->next)
printf ("%d ", p->content);
printf ("\n");
}
void insert_into_list (node *what, node *where)
{
what->next = where->next;
where->next = what;
}
void delete_from_list (node *what, node **first)
{
if (what == *first)
*first = what->next;
else
{
node *p = *first;
while (p && p->next != what)
p = p->next;
if (p)
p->next = what->next;
}
free (what);
}
int main (void)
{
node *element3 = malloc (sizeof (node));
node *element7 = malloc (sizeof (node));
node *element137 = malloc (sizeof (node));
element3->content = 3;
element7->content = 7;
element137->content = 137;
node *first = element3;
element3->next = element7;
element7->next = element137;
element137->next = NULL;
output_list (first);
node *element5 = malloc (sizeof (node));
element5->content = 5;
insert_into_list (element5, element3);
output_list (first);
delete_from_list (element5, &first);
output_list (first);
delete_from_list (element3, &first);
output_list (first);
delete_from_list (element137, &first);
output_list (first);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content;
struct node *next, *prev;
} node;
void check_list (node *first)
{
for (node *p = first; p; p = p->next)
{
if (p->next && p->next->prev != p)
fprintf (stderr, "List inconsistency!\n");
if (p->prev && p->prev->next != p)
fprintf (stderr, "List inconsistency!\n");
}
}
void output_list (node *first)
{
for (node *p = first; p; p = p->next)
printf ("%d ", p->content);
printf ("\n");
}
void insert_into_list (node *what, node *where)
{
what->next = where->next;
if (where->next)
where->next->prev = what;
what->prev = where;
where->next = what;
}
void delete_from_list (node *what, node **first)
{
if (what == *first)
{
*first = what->next;
if (*first)
(*first)->prev = NULL;
}
else
{
node *p = *first;
while (p && p->next != what)
p = p->next;
if (p)
p->next = what->next;
if (what->next)
what->next->prev = p;
}
free (what);
}
int main (void)
{
node *element3 = malloc (sizeof (node));
node *element7 = malloc (sizeof (node));
node *element137 = malloc (sizeof (node));
element3->content = 3;
element7->content = 7;
element137->content = 137;
node *first = element3;
element3->prev = NULL;
element3->next = element7;
element7->prev = element3;
element7->next = element137;
element137->prev = element7;
element137->next = NULL;
output_list (first);
check_list (first);
node *element5 = malloc (sizeof (node));
element5->content = 5;
insert_into_list (element5, element3);
output_list (first);
check_list (first);
delete_from_list (element5, &first);
output_list (first);
check_list (first);
delete_from_list (element3, &first);
output_list (first);
check_list (first);
delete_from_list (element137, &first);
output_list (first);
check_list (first);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int content_left, content_right;
struct node *left, *middle, *right;
} node;
void insert_into_tree (node **root, int value)
{
if (*root)
{
if ((*root)->content_right >= 0)
if (value < (*root)->content_left)
insert_into_tree (&(*root)->left, value);
else if (value < (*root)->content_right)
insert_into_tree (&(*root)->middle, value);
else
insert_into_tree (&(*root)->right, value);
else
if (value < (*root)->content_left)
{
(*root)->content_right = (*root)->content_left;
(*root)->content_left = value;
}
else
(*root)->content_right = value;
}
else
{
*root = malloc (sizeof (node));
(*root)->left = NULL;
(*root)->content_left = value;
(*root)->middle = NULL;
(*root)->content_right = -1;
(*root)->right = NULL;
}
}
void output_tree (node *root)
{
if (root)
{
output_tree (root->left);
printf ("%d\n", root->content_left);
output_tree (root->middle);
if (root->content_right >= 0)
printf ("%d\n", root->content_right);
output_tree (root->right);
}
}
int main (void)
{
node *root = NULL;
insert_into_tree (&root, 7);
insert_into_tree (&root, 137);
insert_into_tree (&root, 3);
insert_into_tree (&root, 5);
insert_into_tree (&root, 6);
insert_into_tree (&root, 42);
insert_into_tree (&root, 1);
insert_into_tree (&root, 2);
insert_into_tree (&root, 12);
output_tree (root);
return 0;
}
#include <stdio.h>
#include <stdint.h>
uint8_t buffer = 0;
void bit_array_set (int i, int value)
{
uint8_t mask = 1 << i;
if (value)
buffer |= mask;
else
buffer &= ~mask;
}
void bit_array_flip (int i)
{
uint8_t mask = 1 << i;
buffer ^= mask;
}
int bit_array_get (int i)
{
uint8_t mask = 1 << i;
if (buffer & mask)
return 1;
else
return 0;
}
void output (void)
{
for (int i = 0; i < 8; i++)
{
if (i % 4 == 0)
printf (" ");
printf ("%d", bit_array_get (i));
}
printf ("\n");
}
int main (void)
{
output ();
bit_array_set (2, 1);
output ();
bit_array_flip (7);
output ();
bit_array_set (2, 0);
output ();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment