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

Beispiele 4.2.2021, Praktikumsunterlagen aktualisiert

parent d8dfb207
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -90,6 +90,13 @@
\end{lstlisting}
(Den Backslash am Ende der ersten Zeile entfällt,
wenn Sie den ganzen Befehl in eine einzige Zeile schreiben.)
\item
\textbf{Abgabe:}
Ihre Quelltexte mit den Lösungen der Praktikumsaufgabe schicken Sie bitte
per E-Mail an \file{peter.gerwinski@hs-bochum.de}
mit dem \textbf{Betreff:} \lstinline[style=terminal]{eif7booD}
unter Angabe von Name, Matrikel-Nummer,
Studiengang (MI/MP/TI) und Studienmodell (KIA/KIS/GS).
\end{itemize}
\bigskip
......
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
union t_object;
typedef struct
{
int type;
void (* print) (union t_object *this);
} t_base;
typedef struct
{
int type;
void (* print) (union t_object *this);
int content;
} t_integer;
typedef struct
{
int type;
void (* print) (union t_object *this);
char *content;
} t_string;
typedef union t_object
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_integer (t_object *this) /* virtuelle Methode des Objekts t_integer */
{
printf ("Integer: %d\n", this->integer.content);
}
void print_string (t_object *this) /* virtuelle Methode des Objekts t_string */
{
printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i) /* Konstruktor des Objekts t_integer */
{
t_object *p = malloc (sizeof (t_integer)); /* Anforderung von Speicherplatz */
p->integer.type = T_INTEGER;
p->integer.print = print_integer; /* Zuweisung der virtuellen Methode */
p->integer.content = i; /* Initialisierung des Objekts */
return p;
}
t_object *new_string (char *s) /* Konstruktor des Objekts t_string */
{
t_object *p = malloc (sizeof (t_string));
p->string.type = T_STRING;
p->string.print = print_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.print (object[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
union t_object;
typedef struct
{
int type;
void (* print) (union t_object *this);
} t_base;
typedef struct
{
int type;
void (* print) (union t_object *this);
int content;
} t_integer;
typedef struct
{
int type;
void (* print) (union t_object *this);
char *content;
} t_string;
typedef union t_object
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_integer (t_object *this, char *comment) /* virtuelle Methode des Objekts t_integer */
{
printf ("Integer: %d\n", this->integer.content);
printf ("Kommentar: %s\n", comment);
}
void print_string (t_object *this) /* virtuelle Methode des Objekts t_string */
{
printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i) /* Konstruktor des Objekts t_integer */
{
t_object *p = malloc (sizeof (t_integer)); /* Anforderung von Speicherplatz */
p->integer.type = T_INTEGER;
p->integer.print = print_integer; /* Zuweisung der virtuellen Methode */
p->integer.content = i; /* Initialisierung des Objekts */
return p;
}
t_object *new_string (char *s) /* Konstruktor des Objekts t_string */
{
t_object *p = malloc (sizeof (t_string));
p->string.type = T_STRING;
p->string.print = print_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.print (object[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
union t_object;
typedef struct
{
int type;
void (* print) (union t_object *this);
} t_base;
typedef struct
{
int type;
void (* print) (union t_object *this, char *comment);
int content;
} t_integer;
typedef struct
{
int type;
void (* print) (union t_object *this);
char *content;
} t_string;
typedef union t_object
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_integer (t_object *this, char *comment) /* virtuelle Methode des Objekts t_integer */
{
printf ("Integer: %d\n", this->integer.content);
printf ("Kommentar: %s\n", comment);
}
void print_string (t_object *this) /* virtuelle Methode des Objekts t_string */
{
printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i) /* Konstruktor des Objekts t_integer */
{
t_object *p = malloc (sizeof (t_integer)); /* Anforderung von Speicherplatz */
p->integer.type = T_INTEGER;
p->integer.print = print_integer; /* Zuweisung der virtuellen Methode */
p->integer.content = i; /* Initialisierung des Objekts */
return p;
}
t_object *new_string (char *s) /* Konstruktor des Objekts t_string */
{
t_object *p = malloc (sizeof (t_string));
p->string.type = T_STRING;
p->string.print = print_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.print (object[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
union t_object;
typedef struct
{
int type;
void (* print) (union t_object *this);
} t_base;
typedef struct
{
int type;
void (* print) (union t_object *this, char *comment);
int content;
} t_integer;
typedef struct
{
int type;
void (* print) (union t_object *this);
char *content;
} t_string;
typedef union t_object
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_integer (t_object *this, char *comment) /* virtuelle Methode des Objekts t_integer */
{
printf ("Integer: %d\n", this->integer.content);
printf ("Kommentar: %s\n", comment);
}
void print_string (t_object *this) /* virtuelle Methode des Objekts t_string */
{
printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i) /* Konstruktor des Objekts t_integer */
{
t_object *p = malloc (sizeof (t_integer)); /* Anforderung von Speicherplatz */
p->integer.type = T_INTEGER;
p->integer.print = print_integer; /* Zuweisung der virtuellen Methode */
p->integer.content = i; /* Initialisierung des Objekts */
return p;
}
t_object *new_string (char *s) /* Konstruktor des Objekts t_string */
{
t_object *p = malloc (sizeof (t_string));
p->string.type = T_STRING;
p->string.print = print_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.print (object[i], "Dies ist ein Test.");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
union t_object;
typedef struct
{
int type;
void (* print) (union t_object *this, char *comment);
} t_base;
typedef struct
{
int type;
void (* print) (union t_object *this, char *comment);
int content;
} t_integer;
typedef struct
{
int type;
void (* print) (union t_object *this);
char *content;
} t_string;
typedef union t_object
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_integer (t_object *this, char *comment) /* virtuelle Methode des Objekts t_integer */
{
printf ("Integer: %d\n", this->integer.content);
printf ("Kommentar: %s\n", comment);
}
void print_string (t_object *this) /* virtuelle Methode des Objekts t_string */
{
printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i) /* Konstruktor des Objekts t_integer */
{
t_object *p = malloc (sizeof (t_integer)); /* Anforderung von Speicherplatz */
p->integer.type = T_INTEGER;
p->integer.print = print_integer; /* Zuweisung der virtuellen Methode */
p->integer.content = i; /* Initialisierung des Objekts */
return p;
}
t_object *new_string (char *s) /* Konstruktor des Objekts t_string */
{
t_object *p = malloc (sizeof (t_string));
p->string.type = T_STRING;
p->string.print = print_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.print (object[i], "Dies ist ein Test.");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
union t_object;
typedef struct
{
int type;
void (* print) (union t_object *this, char *comment);
} t_base;
typedef struct
{
int type;
void (* print) (union t_object *this, char *comment);
int content;
} t_integer;
typedef struct
{
int type;
void (* print) (union t_object *this, char *comment);
char *content;
} t_string;
typedef union t_object
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_integer (t_object *this, char *comment) /* virtuelle Methode des Objekts t_integer */
{
printf ("Integer: %d\n", this->integer.content);
printf ("Kommentar: %s\n", comment);
}
void print_string (t_object *this, char *comment) /* virtuelle Methode des Objekts t_string */
{
printf ("String: \"%s\"\n", this->string.content);
(void) comment;
}
t_object *new_integer (int i) /* Konstruktor des Objekts t_integer */
{
t_object *p = malloc (sizeof (t_integer)); /* Anforderung von Speicherplatz */
p->integer.type = T_INTEGER;
p->integer.print = print_integer; /* Zuweisung der virtuellen Methode */
p->integer.content = i; /* Initialisierung des Objekts */
return p;
}
t_object *new_string (char *s) /* Konstruktor des Objekts t_string */
{
t_object *p = malloc (sizeof (t_string));
p->string.type = T_STRING;
p->string.print = print_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.print (object[i], "Dies ist ein Test.");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
union t_object;
typedef struct
{
int type;
void (* print) (union t_object *this, char *comment);
} t_base;
typedef struct
{
int type;
void (* print) (union t_object *this, char *comment);
int content;
} t_integer;
typedef struct
{
int type;
void (* print) (union t_object *this, char *comment);
char *content;
} t_string;
typedef union t_object
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_integer (t_object *this, char *comment) /* virtuelle Methode des Objekts t_integer */
{
printf ("Integer: %d\n", this->integer.content);
printf ("Kommentar: %s\n", comment);
}
void print_string (t_object *this) /* virtuelle Methode des Objekts t_string */
{
printf ("String: \"%s\"\n", this->string.content);
}
void print_string (t_object *this, char *comment) /* virtuelle Methode des Objekts t_string */
{
printf ("String: \"%s\"\n", this->string.content);
printf ("Kommentar: %s\n", comment);
}
t_object *new_integer (int i) /* Konstruktor des Objekts t_integer */
{
t_object *p = malloc (sizeof (t_integer)); /* Anforderung von Speicherplatz */
p->integer.type = T_INTEGER;
p->integer.print = print_integer; /* Zuweisung der virtuellen Methode */
p->integer.content = i; /* Initialisierung des Objekts */
return p;
}
t_object *new_string (char *s) /* Konstruktor des Objekts t_string */
{
t_object *p = malloc (sizeof (t_string));
p->string.type = T_STRING;
p->string.print = print_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.print (object[i], "Dies ist ein Test.");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
union t_object;
typedef struct
{
int type;
void (* print) (union t_object *this, void *data);
} t_base;
typedef struct
{
int type;
void (* print) (union t_object *this, void *data);
int content;
} t_integer;
typedef struct
{
int type;
void (* print) (union t_object *this, void *data);
char *content;
} t_string;
typedef union t_object
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_integer (t_object *this, void *data) /* virtuelle Methode des Objekts t_integer */
{
printf ("Integer: %d\n", this->integer.content);
if (data)
{
char *comment = data;
printf ("Kommentar: %s\n", comment);
}
}
void print_string (t_object *this, void *data) /* virtuelle Methode des Objekts t_string */
{
printf ("String: \"%s\"\n", this->string.content);
(void) data;
}
t_object *new_integer (int i) /* Konstruktor des Objekts t_integer */
{
t_object *p = malloc (sizeof (t_integer)); /* Anforderung von Speicherplatz */
p->integer.type = T_INTEGER;
p->integer.print = print_integer; /* Zuweisung der virtuellen Methode */
p->integer.content = i; /* Initialisierung des Objekts */
return p;
}
t_object *new_string (char *s) /* Konstruktor des Objekts t_string */
{
t_object *p = malloc (sizeof (t_string));
p->string.type = T_STRING;
p->string.print = print_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.print (object[i], "Dies ist ein Test.");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
union t_object;
typedef struct
{
int type;
void (* print) (union t_object *this);
} t_base;
typedef struct
{
int type;
void (* print) (union t_object *this);
int content;
char *comment;
} t_integer;
typedef struct
{
int type;
void (* print) (union t_object *this);
char *content;
} t_string;
typedef union t_object
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_integer (t_object *this) /* virtuelle Methode des Objekts t_integer */
{
printf ("Integer: %d\n", this->integer.content);
if (this->integer.comment)
printf ("Kommentar: %s\n", this->integer.comment);
}
void print_string (t_object *this) /* virtuelle Methode des Objekts t_string */
{
printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i, char *comment) /* Konstruktor des Objekts t_integer */
{
t_object *p = malloc (sizeof (t_integer)); /* Anforderung von Speicherplatz */
p->integer.type = T_INTEGER;
p->integer.print = print_integer; /* Zuweisung der virtuellen Methode */
p->integer.content = i; /* Initialisierung des Objekts */
p->integer.comment = comment; /* Initialisierung des Objekts */
return p;
}
t_object *new_string (char *s) /* Konstruktor des Objekts t_string */
{
t_object *p = malloc (sizeof (t_string));
p->string.type = T_STRING;
p->string.print = print_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42, "Dies ist ein Test."),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.print (object[i]);
return 0;
}
......@@ -57,7 +57,7 @@ t_object *new_integer (int i)
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (t_string));
p->integer.vmt = &vmt_string;
p->string.vmt = &vmt_string;
p->string.content = s;
return p;
}
......
#include <stdio.h>
#include <stdlib.h>
union t_object;
struct t_vmt;
typedef struct
{
struct t_vmt *vmt;
} t_base;
typedef struct
{
struct t_vmt *vmt;
int content;
} t_integer;
typedef struct
{
struct t_vmt *vmt;
char *content;
} t_string;
typedef union t_object
{
t_base base;
t_integer integer;
t_string string;
} t_object;
typedef struct t_vmt
{
void (* print) (union t_object *this);
} t_vmt;
void print_integer (t_object *this)
{
printf ("Integer: %d\n", this->integer.content);
}
void print_string (t_object *this)
{
printf ("String: \"%s\"\n", this->string.content);
}
t_vmt vmt_integer = { print_integer };
t_vmt vmt_string = { print_string };
t_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->base.vmt = &vmt_integer;
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (t_string));
p->base.vmt = &vmt_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.vmt->print (object[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
union t_object;
struct t_vmt;
typedef struct
{
struct t_vmt *vmt;
} t_base;
typedef struct
{
struct t_vmt *vmt;
int content;
} t_integer;
typedef struct
{
struct t_vmt *vmt;
char *content;
} t_string;
typedef union t_object
{
t_base base;
t_integer integer;
t_string string;
} t_object;
typedef struct t_vmt
{
void (* print) (union t_object *this);
} t_vmt;
void print_integer (t_object *this)
{
printf ("Integer: %d\n", this->integer.content);
}
void print_integer_with_comment (t_object *this, char *comment)
{
printf ("%s: %d\n", comment, this->integer.content);
}
void print_string (t_object *this)
{
printf ("String: \"%s\"\n", this->string.content);
}
t_vmt vmt_integer = { print_integer };
t_vmt vmt_string = { print_string };
t_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->base.vmt = &vmt_integer;
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (t_string));
p->base.vmt = &vmt_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.vmt->print (object[i]);
print_integer_with_comment (object[1], "Die Antwort lautet");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
union t_object;
struct t_vmt;
typedef struct
{
struct t_vmt *vmt;
} t_base;
typedef struct
{
struct t_vmt *vmt;
int content;
} t_integer;
typedef struct
{
struct t_vmt *vmt;
char *content;
} t_string;
typedef union t_object
{
t_base base;
t_integer integer;
t_string string;
} t_object;
typedef struct t_vmt
{
void (* print) (union t_object *this);
} t_vmt;
void print_integer (t_object *this)
{
printf ("Integer: %d\n", this->integer.content);
}
void print_integer_with_comment (t_object *this, char *comment)
{
printf ("%s: %d\n", comment, this->integer.content);
}
void print_string (t_object *this)
{
printf ("String: \"%s\"\n", this->string.content);
}
t_vmt vmt_integer = { print_integer };
t_vmt vmt_string = { print_string };
t_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->base.vmt = &vmt_integer;
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (t_string));
p->base.vmt = &vmt_string;
p->string.content = s;
return p;
}
int main (void)
{
t_object *object[] = { new_integer (42),
new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->base.vmt->print (object[i]);
print_integer_with_comment (object[0], "Die Antwort lautet");
return 0;
}
#include <stdio.h>
struct TBase
{
virtual void print () = 0;
};
struct TInteger: TBase
{
int content;
virtual void print ();
TInteger (int i);
};
struct TString: TBase
{
char *content;
virtual void print ();
TString (char *s);
};
void TInteger::print ()
{
printf ("Integer: %d\n", content);
}
void TString::print ()
{
printf ("String: \"%s\"\n", content);
}
TInteger::TInteger (int i)
{
content = i;
}
TString::TString (char *s)
{
content = s;
}
int main (void)
{
TBase *object[] = { new TInteger (42),
new TString ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
object[i]->print ();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment