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

Beispielprogramme 14.1.2019

parent 962d2b7d
Branches
No related tags found
No related merge requests found
Showing with 562 additions and 0 deletions
#include <stdio.h>
char *name[] = { "Anton", "Bertha", "Cäsar" };
void store_new_name (char *s)
{
char *new_name[4];
for (int i = 0; i < 3; i++)
new_name[i] = name[i];
new_name[3] = s;
}
int main (void)
{
store_new_name ("Dieter");
return 0;
}
File moved
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
typedef struct
{
int type;
void (* print) (t_object *this);
} t_base;
typedef struct
{
int type;
void (* print) (t_object *this);
int content;
} t_integer;
typedef struct
{
int type;
void (* print) (t_object *this);
char *content;
} t_string;
typedef union
{
t_base base;
t_integer integer;
t_string string;
} t_object;
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_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->integer.type = T_INTEGER;
p->integer.print = print_integer;
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
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)
{
printf ("Integer: %d\n", this->integer.content);
}
void print_string (t_object *this)
{
printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->integer.type = T_INTEGER;
p->integer.print = print_integer;
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
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>
union t_object;
typedef struct
{
void (* print) (union t_object *this);
} t_base;
typedef struct
{
void (* print) (union t_object *this);
int content;
} t_integer;
typedef struct
{
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)
{
printf ("Integer: %d\n", this->integer.content);
}
void print_string (t_object *this)
{
printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->integer.print = print_integer;
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (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>
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->integer.vmt = &vmt_integer;
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (t_string));
p->integer.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>
struct TBase
{
virtual void print ();
};
struct TInteger: TBase
{
int content;
virtual void print ();
TInteger (int i);
};
struct TString: TBase
{
const char *content;
virtual void print ();
TString (const char *s);
};
void TBase::print ()
{
printf ("Base\n");
}
void TInteger::print ()
{
printf ("Integer: %d\n", content);
}
void TString::print ()
{
printf ("String: \"%s\"\n", content);
}
TInteger::TInteger (int i)
{
content = i;
}
TString::TString (const 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;
}
File moved
File moved
File moved
File moved
File moved
#include <stdio.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
void print_object (t_base *this)
{
if (this->type == T_INTEGER)
printf ("Integer: %d\n", ((t_integer *) this)->content);
else if (this->type == T_STRING)
printf ("String: \"%s\"\n", ((t_string *) this)->content);
}
int main (void)
{
t_integer i = { T_INTEGER, 42 };
t_string s = { T_STRING, "Hello, world!" };
t_base *object[] = { (t_base *) &i, (t_base *) &s, NULL };
for (int i = 0; object[i]; i++)
print_object (object[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
void print_object (t_base *this)
{
if (this->type == T_INTEGER)
printf ("Integer: %d\n", ((t_integer *) this)->content);
else if (this->type == T_STRING)
printf ("String: \"%s\"\n", ((t_string *) this)->content);
}
t_integer *new_integer (int i)
{
t_integer *p = malloc (sizeof (t_integer));
p->type = T_INTEGER;
p->content = i;
return p;
}
t_string *new_string (char *s)
{
t_string *p = malloc (sizeof (t_string));
p->type = T_STRING;
p->content = s;
return p;
}
int main (void)
{
t_base *object[] = { (t_base *) new_integer (42),
(t_base *) new_string ("Hello, world!"),
NULL };
for (int i = 0; object[i]; i++)
print_object (object[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define T_BASE 0
#define T_INTEGER 1
#define T_STRING 2
typedef struct
{
int type;
} t_base;
typedef struct
{
int type;
int content;
} t_integer;
typedef struct
{
int type;
char *content;
} t_string;
typedef union
{
t_base base;
t_integer integer;
t_string string;
} t_object;
void print_object (t_object *this)
{
if (this->base.type == T_INTEGER)
printf ("Integer: %d\n", this->integer.content);
else if (this->base.type == T_STRING)
printf ("String: \"%s\"\n", this->string.content);
}
t_object *new_integer (int i)
{
t_object *p = malloc (sizeof (t_integer));
p->integer.type = T_INTEGER;
p->integer.content = i;
return p;
}
t_object *new_string (char *s)
{
t_object *p = malloc (sizeof (t_string));
p->string.type = T_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++)
print_object (object[i]);
return 0;
}
File moved
File moved
#include <stdio.h>
#include <stdint.h>
typedef union
{
int8_t i;
uint8_t u;
} num8_t;
int main (void)
{
num8_t test;
test.i = -1;
printf ("%d\n", test.u);
return 0;
}
#include <stdio.h>
#include <stdint.h>
typedef union
{
char s[8];
uint64_t x;
} num_char_t;
int main (void)
{
num_char_t test = { "Hello!" };
printf ("%lx\n", test.x);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment