Übungsaufgaben: Lösung
Completion requirements
Übung
Aufgabe 1
- Erstelle eine Variable
x
mit einem Typen, der aus 5 Worten besteht. - Erstelle eine identische Variable
y
, mit 2 Worten für den Typen - Weise nach, dass beide Variablen identisch sind
const unsigned long long int x = 42;
const uint64_t y = 42;
assert(x == y);
assert(sizeof(x) == sizeof(y);
Aufgabe 2
- nutze nur ISO Datentypen
- lege einen vorzeichenbehafteten 16-Bit Integer
a
an - lege einen vorzeichenlosen 64-Bit Integer
b
an - lege eine Gleitkomma-Zahl
c
an - weise
c
das mathematisch korrekte Ergebnis ausa / b
zu.
int16_t a = 1;
uint64_t b = 5;
double_t c = (double)a / (double)b;
Aufgabe 3
Lege eine Funktion an, die folgende Kiterien erfüllt:
- einen kopierten Parameter
- einen unveränderlichen Parameter (Pointer)
- zwei Rückgabewerte
- mit C-typischer Fehlerbehandlung
int foo(int a, const int *b, int *c, int *res1, int *res2) {
*res1 = a;
*res2 = a * a;
*b = *b + 1;
return 0;
}
Aufgabe 4
Programmiere einen Taschenrechner, der wahlweise den Modulus (%
) zweier Zahlen a
und b
berechent, oder das Maximum ermittelt. Nutze Callbacks!
#include <stdio.h>
#include <stdlib.h>
typedef double (*CalculateFn_t)(double, double);
double mod(double a, double b)
{
return a % b;
}
double max(double a, double b)
{
return a > b ? a : b;
}
int main(int argc, char *argv[])
{
if (argc != 4) {
usage();
return 0;
}
double a = atof(argv[1]);
double b = atof(argv[3]);
char op = argv[2][0];
CalculateFn_t calculateFn;
switch(op)
{
case '%':
calculateFn = &mod;
break;
case '>':
calculateFn = &max;
break;
break;
default:
printf("ERROR: input invalid\n");
return EXIT_FAILURE;
}
double res = calculateFn(a, b);
printf("%f\n", res);
return 0;
}
Last modified: Friday, 1 June 2018, 12:46 PM