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

Musterlösung zu den Übungsaufgaben vom 9.1.2017

parent 9309ae85
No related branches found
No related tags found
No related merge requests found
File added
This diff is collapsed.
#include <stdio.h>
#define POINT 0
#define CIRCLE 1
#define TEXT 2
#define SQUARE 3
typedef union
{
int radius;
char *text;
int edge;
} 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);
}
void draw_square (struct graphics_object *this)
{
printf ("square at (%d,%d) with edge length %d\n",
this->x, this->y, this->data.edge);
}
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 a_square = { SQUARE, draw_square, 137, 42 };
a_square.data.edge = 13;
graphics_object *g[4] = { &a_point, &a_circle, &some_text, &a_square };
for (int i = 0; i < 4; i++)
g[i]->draw (g[i]);
return 0;
}
#include <stdio.h>
#define ANIMAL 0
#define WITH_WINGS 1
#define WITH_LEGS 2
typedef struct
{
int type;
char *name;
} base;
typedef struct
{
int type;
char *name;
int wings;
} with_wings;
typedef struct
{
int type;
char *name;
int legs;
} with_legs;
typedef union
{
base b;
with_wings w;
with_legs l;
} animal;
int main (void)
{
animal *a[2];
animal duck;
a[0] = &duck;
duck.b.type = WITH_WINGS;
duck.b.name = "duck";
duck.w.wings = 2;
animal cow;
a[1] = &cow;
cow.b.type = WITH_LEGS;
cow.b.name = "cow";
cow.l.legs = 4;
for (int i = 0; i < 2; i++)
if (a[i]->b.type == WITH_LEGS)
printf ("A %s has %d legs.\n", a[i]->b.name,
a[i]->l.legs);
else if (a[i]->b.type == WITH_WINGS)
printf ("A %s has %d wings.\n", a[i]->b.name,
a[i]->w.wings);
else
printf ("Error in animal: %s\n", a[i]->b.name);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define ANIMAL 0
#define WITH_WINGS 1
#define WITH_LEGS 2
typedef struct
{
int type;
char *name;
} base;
typedef struct
{
int type;
char *name;
int wings;
} with_wings;
typedef struct
{
int type;
char *name;
int legs;
} with_legs;
typedef union
{
base b;
with_wings w;
with_legs l;
} animal;
animal *new_with_wings (char *name, int wings)
{
animal *a = malloc (sizeof (with_wings));
a->b.type = WITH_WINGS;
a->b.name = name;
a->w.wings = wings;
return a;
}
animal *new_with_legs (char *name, int legs)
{
animal *a = malloc (sizeof (with_legs));
a->b.type = WITH_LEGS;
a->b.name = name;
a->l.legs = legs;
return a;
}
int main (void)
{
animal *a[2] = { new_with_wings ("duck", 2),
new_with_legs ("cow", 4) };
for (int i = 0; i < 2; i++)
if (a[i]->b.type == WITH_LEGS)
printf ("A %s has %d legs.\n", a[i]->b.name,
a[i]->l.legs);
else if (a[i]->b.type == WITH_WINGS)
printf ("A %s has %d wings.\n", a[i]->b.name,
a[i]->w.wings);
else
printf ("Error in animal: %s\n", a[i]->b.name);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define ANIMAL 0
#define WITH_WINGS 1
#define WITH_LEGS 2
union animal;
typedef struct
{
int type;
char *name;
void (*print) (union animal *this);
} base;
typedef struct
{
int type;
char *name;
void (*print) (union animal *this);
int wings;
} with_wings;
typedef struct
{
int type;
char *name;
void (*print) (union animal *this);
int legs;
} with_legs;
typedef union animal
{
base b;
with_wings w;
with_legs l;
} animal;
void print_with_wings (animal *this)
{
printf ("A %s has %d wings.\n", this->b.name, this->w.wings);
}
void print_with_legs (animal *this)
{
printf ("A %s has %d legs.\n", this->b.name, this->l.legs);
}
animal *new_with_wings (char *name, int wings)
{
animal *a = malloc (sizeof (with_wings));
a->b.type = WITH_WINGS;
a->b.name = name;
a->b.print = print_with_wings;
a->w.wings = wings;
return a;
}
animal *new_with_legs (char *name, int legs)
{
animal *a = malloc (sizeof (with_legs));
a->b.type = WITH_LEGS;
a->b.name = name;
a->b.print = print_with_legs;
a->l.legs = legs;
return a;
}
int main (void)
{
animal *a[2] = { new_with_wings ("duck", 2),
new_with_legs ("cow", 4) };
for (int i = 0; i < 2; i++)
a[i]->b.print (a[i]);
return 0;
}
...@@ -74,7 +74,8 @@ Musterlösungen zu den Übungsaufgaben: ...@@ -74,7 +74,8 @@ Musterlösungen zu den Übungsaufgaben:
* [28.11.2016: Länge von Strings, fehlerhaftes Programm, Mikro-Controller](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20161128/hp-musterloesung-20161128.pdf) * [28.11.2016: Länge von Strings, fehlerhaftes Programm, Mikro-Controller](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20161128/hp-musterloesung-20161128.pdf)
* [05.12.2016: Trickprogrammierung, Ausgabe von Hexadezimalzahlen, Thermometer-Baustein an I²C-Bus](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20161205/hp-musterloesung-20161205.pdf) * [05.12.2016: Trickprogrammierung, Ausgabe von Hexadezimalzahlen, Thermometer-Baustein an I²C-Bus](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20161205/hp-musterloesung-20161205.pdf)
* [12.12.2016: Daten im Speicher, Zeigerarithmetik, XBM-Grafik](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20161212/hp-musterloesung-20161212.pdf) * [12.12.2016: Daten im Speicher, Zeigerarithmetik, XBM-Grafik](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20161212/hp-musterloesung-20161212.pdf)
* [19.12.2016: Bürgerentscheid, Lokale Variable im Speicher, Blinkende LEDs, Objektorientierte Tier-Datenbank](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20161219/hp-musterloesung-20161219.pdf) * [19.12.2016: Bürgerentscheid, Lokale Variable im Speicher, Blinkende LEDs, objektorientierte Tier-Datenbank](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20161219/hp-musterloesung-20161219.pdf)
* [09.01.2017: Objektorientierte Programmierung mit dem C-Datentyp _union, objektorientierte Tier-Datenbank](https://gitlab.cvh-server.de/pgerwinski/hp/raw/master/20170109/hp-musterloesung-20170109.pdf)
Praktikumsunterlagen: Praktikumsunterlagen:
--------------------- ---------------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment