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

Vorbereitung 15.1.2018

parent 0092d470
Branches
No related tags found
No related merge requests found
Showing with 866 additions and 0 deletions
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int comparisons = 0;
void display (char **name, int left, int right)
{
printf ("\e[H\e[J");
for (int i = 0; name[i]; i++)
{
printf ("%s", name[i]);
if (i == left || i == right)
printf (" <--");
printf ("\n");
}
printf ("%d\n", comparisons);
}
int compare (char **name, int left, int right)
{
int result = strcmp (name[left], name[right]);
comparisons++;
display (name, left, right);
usleep (200000);
return result;
}
void sort (char **name)
{
int must_continue = 1;
int sorted = 0;
while (name[sorted])
sorted++;
while (sorted > 0 && must_continue)
{
must_continue = 0;
for (int i = 1; i < sorted; i++)
if (compare (name, i - 1, i) > 0)
{
must_continue = 1;
char *temp = name[i - 1];
name[i - 1] = name[i];
name[i] = temp;
}
sorted--;
}
}
int main (void)
{
char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter",
"Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans",
"Thomas", "Ulrich", "Zacharias", NULL };
sort (name);
display (name, -1, -1);
return 0;
}
#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 type;
char *name;
int wings;
} with_wings;
typedef struct with_legs
{
int type;
char *name;
int legs;
} with_legs;
int main (void)
{
animal *a[2];
with_wings duck;
a[0] = (animal *) &duck;
a[0]->type = WITH_WINGS;
a[0]->name = "duck";
((with_wings *) a[0])->wings = 2;
with_legs cow;
a[1] = (animal *) &cow;
a[1]->type = WITH_LEGS;
a[1]->name = "cow";
((with_legs *) 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;
}
#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 type;
char *name;
int wings;
} with_wings;
typedef struct with_legs
{
int type;
char *name;
int legs;
} with_legs;
int main (void)
{
animal *a[2];
with_wings duck;
a[0] = (animal *) &duck;
duck.type = WITH_WINGS;
duck.name = "duck";
duck.wings = 2;
with_legs cow;
a[1] = (animal *) &cow;
cow.type = WITH_LEGS;
cow.name = "cow";
cow.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;
}
#include <stdio.h>
#include <stdint.h>
typedef union
{
uint32_t number;
char *name;
uint8_t bytes[4];
} data;
int main (void)
{
data x;
x.number = 303108111;
for (int i = 0; i < 4; i++)
printf ("%d ", x.bytes[i]);
printf ("\n");
printf ("%s\n", x.name);
return 0;
}
#include <stdio.h>
#define POINT 0
#define CIRCLE 1
#define TEXT 2
typedef union
{
int radius;
char *text;
} extra_data;
typedef struct graphics_object
{
int type;
void (*draw) (struct graphics_object *this);
int x, y;
extra_data data;
} graphics_object;
void draw_point (struct graphics_object *this)
{
printf ("point at (%d,%d)\n", this->x, this->y);
}
void draw_circle (struct graphics_object *this)
{
printf ("circle at (%d,%d) with radius %d\n",
this->x, this->y, this->data.radius);
}
void draw_text (struct graphics_object *this)
{
printf ("text at (%d,%d): \"%s\"\n",
this->x, this->y, this->data.text);
}
int main (void)
{
graphics_object a_point = { POINT, draw_point, 35, 17 };
graphics_object a_circle = { CIRCLE, draw_circle, 20, 30 };
a_circle.data.radius = 12;
graphics_object some_text = { TEXT, draw_text, 42, 23 };
some_text.data.text = "Hello, world!";
graphics_object *g[3] = { &a_point, &a_circle, &some_text };
for (int i = 0; i < 3; i++)
g[i]->draw (g[i]);
return 0;
}
#include <stdio.h>
#define POINT 0
#define CIRCLE 1
#define TEXT 2
typedef union
{
int radius;
char *text;
} extra_data;
typedef struct graphics_object
{
int type;
void (*draw) (struct graphics_object *this);
int x, y;
extra_data data;
} graphics_object;
void draw_point (struct graphics_object *this)
{
printf ("point at (%d,%d)\n", this->x, this->y);
}
void draw_circle (struct graphics_object *this)
{
printf ("circle at (%d,%d) with radius %d\n",
this->x, this->y, this->data.radius);
}
void draw_text (struct graphics_object *this)
{
printf ("text at (%d,%d): \"%s\"\n",
this->x, this->y, this->data.text);
}
int main (void)
{
graphics_object a_point = { POINT, draw_point, 35, 17 };
graphics_object a_circle = { CIRCLE, draw_text, 20, 30 };
a_circle.data.radius = 12;
graphics_object some_text = { TEXT, draw_text, 42, 23 };
some_text.data.text = "Hello, world!";
graphics_object *g[3] = { &a_point, &a_circle, &some_text };
for (int i = 0; i < 3; i++)
g[i]->draw (g[i]);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int comparisons = 0;
void display (char **name, int left, int right)
{
printf ("\e[H\e[J");
for (int i = 0; name[i]; i++)
{
printf ("%s", name[i]);
if (i == left || i == right)
printf (" <--");
printf ("\n");
}
printf ("%d\n", comparisons);
}
int compare (char **name, int left, int right)
{
int result = strcmp (name[left], name[right]);
comparisons++;
display (name, left, right);
usleep (200000);
return result;
}
void sort (char **name)
{
for (int i = 1; name[i]; i++)
if (compare (name, i - 1, i) > 0)
{
char *temp = name[i - 1];
name[i - 1] = name[i];
name[i] = temp;
}
}
int main (void)
{
char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter",
"Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans",
"Thomas", "Ulrich", "Zacharias", NULL };
sort (name);
display (name, -1, -1);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int comparisons = 0;
void display (char **name, int left, int right)
{
printf ("\e[H\e[J");
for (int i = 0; name[i]; i++)
{
printf ("%s", name[i]);
if (i == left || i == right)
printf (" <--");
printf ("\n");
}
printf ("%d\n", comparisons);
}
int compare (char **name, int left, int right)
{
int result = strcmp (name[left], name[right]);
comparisons++;
display (name, left, right);
usleep (200000);
return result;
}
void sort (char **name)
{
int sorted = 0;
while (name[sorted])
sorted++;
while (sorted > 0)
{
for (int i = 1; i < sorted; i++)
if (compare (name, i - 1, i) > 0)
{
char *temp = name[i - 1];
name[i - 1] = name[i];
name[i] = temp;
}
sorted--;
}
}
int main (void)
{
char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter",
"Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans",
"Thomas", "Ulrich", "Zacharias", NULL };
sort (name);
display (name, -1, -1);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int comparisons = 0;
void display (char **name, int left, int right)
{
printf ("\e[H\e[J");
for (int i = 0; name[i]; i++)
{
printf ("%s", name[i]);
if (i == left || i == right)
printf (" <--");
printf ("\n");
}
printf ("%d\n", comparisons);
}
int compare (char **name, int left, int right)
{
int result = strcmp (name[left], name[right]);
comparisons++;
display (name, left, right);
usleep (200000);
return result;
}
void sort (char **name)
{
int done = 0;
int sorted = 0;
while (name[sorted])
sorted++;
while (sorted > 0 && !done)
{
done = 1;
for (int i = 1; i < sorted; i++)
if (compare (name, i - 1, i) > 0)
{
done = 0;
char *temp = name[i - 1];
name[i - 1] = name[i];
name[i] = temp;
}
sorted--;
}
}
int main (void)
{
char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter",
"Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans",
"Thomas", "Ulrich", "Zacharias", NULL };
sort (name);
display (name, -1, -1);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int comparisons = 0;
void display (char **name, int left, int right)
{
printf ("\e[H\e[J");
for (int i = 0; name[i]; i++)
{
printf ("%s", name[i]);
if (i == left || i == right)
printf (" <--");
printf ("\n");
}
printf ("%d\n", comparisons);
}
int compare (char **name, int left, int right)
{
int result = strcmp (name[left], name[right]);
comparisons++;
display (name, left, right);
usleep (200000);
return result;
}
void sort (char **name)
{
int done = 0;
int sorted = 0;
while (name[sorted])
sorted++;
while (sorted > 0 && !done)
{
done = 1;
for (int i = 1; i < sorted; i++)
if (compare (name, i - 1, i) > 0)
{
done = 0;
char *temp = name[i - 1];
name[i - 1] = name[i];
name[i] = temp;
}
sorted--;
}
}
int main (void)
{
char *name[] = { "Otto", "Lisa", "Heinrich", "Siegfried", "Peter",
"Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans",
"Thomas", "Ulrich", "Zacharias", "Anna", NULL };
sort (name);
display (name, -1, -1);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int comparisons = 0;
void display (char **name, int left, int right)
{
printf ("\e[H\e[J");
for (int i = 0; name[i]; i++)
{
printf ("%s", name[i]);
if (i == left || i == right)
printf (" <--");
printf ("\n");
}
printf ("%d\n", comparisons);
}
int compare (char **name, int left, int right)
{
int result = strcmp (name[left], name[right]);
comparisons++;
display (name, left, right);
usleep (200000);
return result;
}
void sort (char **name)
{
int done = 0;
int sorted = 0;
while (name[sorted])
sorted++;
while (sorted > 0 && !done)
{
done = 1;
for (int i = 1; i < sorted; i++)
if (compare (name, i - 1, i) > 0)
{
done = 0;
char *temp = name[i - 1];
name[i - 1] = name[i];
name[i] = temp;
}
sorted--;
}
}
int main (void)
{
char *name[] = { "Anna", "Berta", "Box", "Dieter", "Fritz", "Hans", "Heinrich",
"Hugo", "Lisa", "Maria", "Otto", "Peter", "Siegfried", "Thomas",
"Ulrich", "Zacharias", NULL };
sort (name);
display (name, -1, -1);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int comparisons = 0;
void display (char **name, int left, int right)
{
printf ("\e[H\e[J");
for (int i = 0; name[i]; i++)
{
printf ("%s", name[i]);
if (i == left || i == right)
printf (" <--");
printf ("\n");
}
printf ("%d\n", comparisons);
}
int compare (char **name, int left, int right)
{
int result = strcmp (name[left], name[right]);
comparisons++;
display (name, left, right);
usleep (200000);
return result;
}
void sort (char **name)
{
int must_continue = 1;
int sorted = 0;
while (name[sorted])
sorted++;
while (sorted > 0 && must_continue)
{
must_continue = 0;
for (int i = 1; i < sorted; i++)
if (compare (name, i - 1, i) > 0)
{
must_continue = 1;
char *temp = name[i - 1];
name[i - 1] = name[i];
name[i] = temp;
}
sorted--;
}
}
int main (void)
{
char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter",
"Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans",
"Thomas", "Ulrich", "Zacharias", NULL };
sort (name);
display (name, -1, -1);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int comparisons = 0;
void display (char **name, int left, int right)
{
printf ("\e[H\e[J");
for (int i = 0; name[i]; i++)
{
printf ("%s", name[i]);
if (i == left || i == right)
printf (" <--");
printf ("\n");
}
printf ("%d\n", comparisons);
}
int compare (char **name, int left, int right)
{
int result = strcmp (name[left], name[right]);
comparisons++;
display (name, left, right);
usleep (200000);
return result;
}
void sort (char **name)
{
int sorted = 0;
while (name[sorted])
sorted++;
while (sorted > 0)
{
int new_sorted = 0;
for (int i = 1; i < sorted; i++)
if (compare (name, i - 1, i) > 0)
{
new_sorted = i;
char *temp = name[i - 1];
name[i - 1] = name[i];
name[i] = temp;
}
sorted = new_sorted;
}
}
int main (void)
{
char *name[] = { "Otto", "Lisa", "Anna", "Heinrich", "Siegfried", "Peter",
"Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans",
"Thomas", "Ulrich", "Zacharias", NULL };
sort (name);
display (name, -1, -1);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int comparisons = 0;
void display (char **name, int left, int right)
{
printf ("\e[H\e[J");
for (int i = 0; name[i]; i++)
{
printf ("%s", name[i]);
if (i == left || i == right)
printf (" <--");
printf ("\n");
}
printf ("%d\n", comparisons);
}
int compare (char **name, int left, int right)
{
int result = strcmp (name[left], name[right]);
comparisons++;
display (name, left, right);
usleep (200000);
return result;
}
void sort (char **name)
{
int sorted = 0;
while (name[sorted])
sorted++;
while (sorted > 0)
{
int new_sorted = 0;
for (int i = 1; i < sorted; i++)
if (compare (name, i - 1, i) > 0)
{
new_sorted = i;
char *temp = name[i - 1];
name[i - 1] = name[i];
name[i] = temp;
}
sorted = new_sorted;
}
}
int main (void)
{
char *name[] = { "Otto", "Lisa", "Heinrich", "Siegfried", "Peter",
"Dieter", "Hugo", "Berta", "Maria", "Fritz", "Box", "Hans",
"Thomas", "Ulrich", "Zacharias", "Anna", NULL };
sort (name);
display (name, -1, -1);
return 0;
}
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int comparisons = 0;
void display (char **name, int left, int right)
{
printf ("\e[H\e[J");
for (int i = 0; name[i]; i++)
{
printf ("%s", name[i]);
if (i == left || i == right)
printf (" <--");
printf ("\n");
}
printf ("%d\n", comparisons);
}
int compare (char **name, int left, int right)
{
int result = strcmp (name[left], name[right]);
comparisons++;
display (name, left, right);
usleep (200000);
return result;
}
void sort (char **name)
{
int sorted = 0;
while (name[sorted])
sorted++;
while (sorted > 0)
{
int new_sorted = 0;
for (int i = 1; i < sorted; i++)
if (compare (name, i - 1, i) > 0)
{
new_sorted = i;
char *temp = name[i - 1];
name[i - 1] = name[i];
name[i] = temp;
}
sorted = new_sorted;
}
}
int main (void)
{
char *name[] = { "Anna", "Berta", "Box", "Dieter", "Fritz", "Hans", "Heinrich",
"Hugo", "Lisa", "Maria", "Otto", "Peter", "Siegfried", "Thomas",
"Ulrich", "Zacharias", NULL };
sort (name);
display (name, -1, -1);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
char **name = malloc (3 * sizeof (char *));
name[0] = "Anton";
name[1] = "Berthold";
name[2] = "Caesar";
for (int i = 0; i < 3; i++)
printf ("%s\n", name[i]);
free (name);
return 0;
}
#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");
gtk_widget_show (window);
gtk_main ();
return 0;
}
#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);
gtk_widget_show (window);
gtk_main ();
return 0;
}
#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 *button = gtk_button_new_with_label ("Quit");
g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);
gtk_main ();
return 0;
}
#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);
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);
GtkWidget *drawing_area = gtk_drawing_area_new ();
gtk_container_add (GTK_CONTAINER (vbox), drawing_area);
gtk_widget_show (button);
gtk_widget_show (vbox);
gtk_widget_show (window);
gtk_main ();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment