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

Musterlösungen zu Aufgabe 4 der Klausur vom 6.2.2017 sowie Aufgabe 1 und 3 vom 9.1.2020

parent 5e136be7
No related branches found
No related tags found
No related merge requests found
#include <stdio.h>
#include <stdint.h>
typedef struct
{
uint32_t a;
uint64_t b;
uint8_t c;
} three_numbers;
int main (void)
{
three_numbers xyz = { 1819042120, 2410670883059281007, 0 };
printf ("%x %x %x\n", xyz.a, xyz.b, xyz.c);
printf ("%08x %016x %02x\n", xyz.a, xyz.b, xyz.c);
printf ("%s\n", &xyz);
return 0;
}
#include <stdio.h>
#include <stdint.h>
typedef struct
{
uint32_t a;
uint64_t b;
uint8_t c;
} three_numbers;
int main (void)
{
three_numbers xyz = { 1819042120, 2410670883059281007, 0 };
printf ("%x %lx %x\n", xyz.a, xyz.b, xyz.c);
printf ("%08x %016lx %02x\n", xyz.a, xyz.b, xyz.c);
printf ("%s\n", &xyz);
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct
{
char first_name[10];
char family_name[20];
char day, month;
int year;
} person;
int main (void)
{
person sls;
strcpy (sls.first_name, "Sabine");
strcpy (sls.family_name, "Leutheusser-Schnarrenberger");
sls.day = 26;
sls.month = 7;
sls.year = 1951;
printf ("%s %s wurde am %d.%d.%d geboren.\n",
sls.first_name, sls.family_name, sls.day, sls.month, sls.year);
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct
{
char first_name[10];
char family_name[20];
char day, month;
int year;
} person;
int main (void)
{
person sls;
sls.day = 26;
sls.month = 7;
sls.year = 1951;
strncpy (sls.first_name, "Sabine", 10);
strncpy (sls.family_name, "Leutheusser-Schnarrenberger", 20);
printf ("%s %s wurde am %d.%d.%d geboren.\n",
sls.first_name, sls.family_name, sls.day, sls.month, sls.year);
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct
{
char first_name[10];
char family_name[20];
char day, month;
int year;
} person;
int main (void)
{
person sls;
sls.day = 26;
sls.month = 7;
sls.year = 1951;
strncpy (sls.first_name, "Sabine", 9);
strncpy (sls.family_name, "Leutheusser-Schnarrenberger", 19);
printf ("%s %s wurde am %d.%d.%d geboren.\n",
sls.first_name, sls.family_name, sls.day, sls.month, sls.year);
return 0;
}
#include <stdio.h>
#include <string.h>
typedef struct
{
char first_name[10];
char family_name[20];
char day, month;
int year;
} person;
int main (void)
{
person sls;
sls.day = 26;
sls.month = 7;
sls.year = 1951;
strncpy (sls.first_name, "Sabine", 9);
sls.first_name[9] = 0;
strncpy (sls.family_name, "Leutheusser-Schnarrenberger", 19);
sls.family_name[19] = 0;
printf ("%s %s wurde am %d.%d.%d geboren.\n",
sls.first_name, sls.family_name, sls.day, sls.month, sls.year);
return 0;
}
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <error.h>
/* Die Aufgabe besteht darin, die Funktionen pbm_open(),
* pbm_line() und pbm_close() zu schreiben.
*/
int pbm_width = 0;
int pbm_height = 0;
FILE *pbm_file; /* globale Variable für die PBM-Datei */
void pbm_open (int width, int height, char *filename)
{
pbm_file = fopen (filename, "w");
if (!pbm_file)
error (errno, errno, "cannot open file %s for writing", filename);
pbm_width = width;
pbm_height = height;
fprintf (pbm_file, "P4\n%d %d\n", pbm_width, pbm_height);
}
void pbm_line (char *line)
{
int pbm_bytes = (pbm_width + 7) / 8; /* benötigte Bytes pro Zeile (immer aufrunden) */
uint8_t buffer[pbm_bytes];
for (int i = 0; i < pbm_bytes; i++) /* Puffer auf 0 initialisieren */
buffer[i] = 0;
for (int x = 0; line[x]; x++)
{
int i = x / 8; /* In welches Byte des Puffers gehört dieses Pixel? */
int b = x % 8; /* Welches Bit innerhalb des Bytes ist dieses Pixel? */
if (line[x] != ' ') /* Kein Leerzeichen --> Bit auf 1 setzen */
buffer[i] |= 0x80 >> b; /* MSB first. LSB first wäre 1 << b. */
}
for (int i = 0; i < pbm_bytes; i++) /* Puffer in Datei ausgeben */
fprintf (pbm_file, "%c", buffer[i]);
}
void pbm_close (void)
{
fclose (pbm_file);
}
int main (void)
{
pbm_open (14, 14, "test.pbm");
pbm_line (" ");
pbm_line (" XXXXXX ");
pbm_line (" X X ");
pbm_line (" X X ");
pbm_line (" X X ");
pbm_line (" X XX XX X ");
pbm_line (" X X X X ");
pbm_line (" X X ");
pbm_line (" X X X X ");
pbm_line (" X X X X ");
pbm_line (" X XXXX X ");
pbm_line (" X X ");
pbm_line (" XXXXXX ");
pbm_line (" ");
pbm_close ();
return 0;
}
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <error.h>
/* Die Aufgabe besteht darin, die Funktionen pbm_open(),
* pbm_line() und pbm_close() zu schreiben.
*/
int pbm_width = 0;
int pbm_height = 0;
FILE *pbm_file; /* globale Variable für die PBM-Datei */
void pbm_open (int width, int height, char *filename)
{
pbm_file = fopen (filename, "w");
if (!pbm_file)
error (errno, errno, "cannot open file %s for writing", filename);
pbm_width = width;
pbm_height = height;
fprintf (pbm_file, "P4\n%d %d\n", pbm_width, pbm_height);
}
void pbm_line (char *line)
{
int pbm_bytes = (pbm_width + 7) / 8; /* benötigte Bytes pro Zeile (immer aufrunden) */
uint8_t buffer[pbm_bytes];
for (int i = 0; i < pbm_bytes; i++) /* Puffer auf 0 initialisieren */
buffer[i] = 0;
for (int x = 0; line[x]; x++)
{
int i = x / 8; /* In welches Byte des Puffers gehört dieses Pixel? */
int b = x % 8; /* Welches Bit innerhalb des Bytes ist dieses Pixel? */
if (line[x] != ' ') /* Kein Leerzeichen --> Bit auf 1 setzen */
buffer[i] |= 1 << b;
}
for (int i = 0; i < pbm_bytes; i++) /* Puffer in Datei ausgeben */
fprintf (pbm_file, "%c", buffer[i]);
}
void pbm_close (void)
{
fclose (pbm_file);
}
int main (void)
{
pbm_open (14, 14, "test.pbm");
pbm_line (" ");
pbm_line (" XXXXXX ");
pbm_line (" X X ");
pbm_line (" X X ");
pbm_line (" X X ");
pbm_line (" X XX XX X ");
pbm_line (" X X X X ");
pbm_line (" X X ");
pbm_line (" X X X X ");
pbm_line (" X X X X ");
pbm_line (" X XXXX X ");
pbm_line (" X X ");
pbm_line (" XXXXXX ");
pbm_line (" ");
pbm_close ();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment