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

Musterlösung zu Aufgabe 3b der Klausur vom 29.1.2016

parent c0352cc9
No related branches found
No related tags found
No related merge requests found
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char symbol;
int (*calculate) (int a, int b);
}
operation;
operation *new_operation (void)
{
operation *op = malloc (sizeof (operation));
op->symbol = '?';
op->calculate = NULL;
return op;
}
/* Virtuelle Methoden */
int calculate_plus (int a, int b)
{
return a + b;
}
int calculate_minus (int a, int b)
{
return a - b;
}
int calculate_times (int a, int b)
{
return a * b;
}
/* Konstruktoren */
operation *new_plus ()
{
operation *op = malloc (sizeof (operation));
op->symbol = '+';
op->calculate = calculate_plus;
return op;
}
operation *new_minus ()
{
operation *op = malloc (sizeof (operation));
op->symbol = '-';
op->calculate = calculate_minus;
return op;
}
operation *new_times ()
{
operation *op = malloc (sizeof (operation));
op->symbol = '*';
op->calculate = calculate_times;
return op;
}
int main (void)
{
operation *op[4];
op[0] = new_plus ();
op[1] = new_minus ();
op[2] = new_times ();
op[3] = NULL;
for (int i = 0; op[i]; i++)
printf ("2 %c 3 = %d\n", op[i]->symbol, op[i]->calculate (2, 3));
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment