This commit is contained in:
Bora Zuenbuelkoek 2025-06-11 09:42:59 +02:00
parent dc6284a432
commit 7b6de3d556
19 changed files with 101059 additions and 0 deletions

193
Modul4 neu/automat.c Normal file
View File

@ -0,0 +1,193 @@
/**********************************************************************\
* Kurzbeschreibung: automat.c
* Stellt Funktionen zur Realisierung eines Automaten zur Verf<EFBFBD>gung,
* die <EFBFBD>ber die in der automat.h vorgegebene C-Schnittstelle
* mit einer grafischen Schnittstelle kommunizieren.
*
* Datum: Autor: Grund der Aenderung:
*
*
\**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "automat.h"
typedef enum {A=0,B,C,D,E,F,G,H,I} state_t;
typedef struct {
int input;
int mask;
state_t nxtstate;
} fsm_state_t;
typedef struct {
const fsm_state_t * transitions;
int nrOfTransitions;
state_t defaultNxtState;
} fsm_full_state_t;
#define SIZE_OF(a) (sizeof((a))/ sizeof(*(a)))
#define NR_OF_STATES (I-A + 1)
static state_t s_curstate = A;
static const fsm_state_t s_transitions_A[] = {
{0x002, ~0x004, B},
{0x003, ~0x004, C}
};
static const fsm_state_t s_transitions_B[] = {
{0x002, ~0x004, C},
{0x003, ~0x004, D}
};
static const fsm_state_t s_transitions_C[] = {
{0x002, ~0x004, D},
{0x003, ~0x000, F},
{0x007, ~0x000, H}
};
static const fsm_state_t s_transitions_D[] = {
{0x002, ~0x000, F},
{0x003, ~0x004, E},
{0x006, ~0x000, H}
};
static const fsm_state_t s_transitions_E[] = {
{0x006, ~0x000, H},
{0x002, ~0x004, F},
{0x003, ~0x004, G}
};
static const fsm_state_t s_transitions_F[] = {
{0x006, ~0x000, H}
};
static const fsm_state_t s_transitions_G[] = {
{0x006, ~0x000, I}
};
static const fsm_state_t s_transitions_H[] = {
{0x000, ~0x000, A}
};
static const fsm_state_t s_transitions_I[] = {
{0x000, ~0x000, A}
};
static const fsm_full_state_t s_state_table[NR_OF_STATES] = {
{ s_transitions_A, SIZE_OF(s_transitions_A), A },
{ s_transitions_B, SIZE_OF(s_transitions_B), B },
{ s_transitions_C, SIZE_OF(s_transitions_C), C },
{ s_transitions_D, SIZE_OF(s_transitions_D), D },
{ s_transitions_E, SIZE_OF(s_transitions_E), E },
{ s_transitions_F, SIZE_OF(s_transitions_F), F },
{ s_transitions_G, SIZE_OF(s_transitions_G), G },
{ s_transitions_H, SIZE_OF(s_transitions_H), H },
{ s_transitions_I, SIZE_OF(s_transitions_I), I }
};
static const fsm_action_t s_out_table[NR_OF_STATES] = {
{ false, false, false, 0, "Warten" }, // A
{ false, false, false, 1, "1 Euro" }, // B
{ false, false, false, 2, "2 Euro" }, // C
{ false, false, false, 3, "3 Euro" }, // D
{ true, false, false, 3, "3 Euro + M" }, // E
{ true, false, false, 4, "4 Euro" }, // F
{ true, true, false, 4, "4 Euro + M" }, // G
{ true, false, true, 4, "Kaffee" }, // H
{ false, false, false, 0, "Kaffee + M" } // I
};
void automat_reset(void) {
s_curstate = A;
}
void automat_transition(BOOL becher, BOOL muenze, BOOL muenz_wert) {
int input = ((becher & 0x1) << 2) | ((muenze & 0x1) << 1) | (muenz_wert & 0x1);
const fsm_full_state_t *current = &s_state_table[s_curstate];
for (int i = 0; i < current->nrOfTransitions; ++i) {
if ((input & ~current->transitions[i].mask) == (current->transitions[i].input & ~current->transitions[i].mask)) {
s_curstate = current->transitions[i].nxtstate;
return;
}
}
s_curstate = current->defaultNxtState;
}
fsm_action_t automat_output(void) {
return s_out_table[s_curstate];
}

26
Modul4 neu/automat.h Normal file
View File

@ -0,0 +1,26 @@
/**********************************************************************\
* Kurzbeschreibung: automat.h
* Stellt Funktionen zur Realisierung eines Automaten zur Verfügung,
* die über die in der automat.h vorgegebene C-Schnittstelle
* mit einer grafischen Schnittstelle kommunizieren.
*
* Datum: Autor: Grund der Aenderung:
*
*
\**********************************************************************/
#ifndef AUTOMAT_H
#define AUTOMAT_H
#define true 1
#define false 0
typedef int BOOL;
typedef struct {
BOOL display;
BOOL muenz_rueck;
BOOL kaffee_los;
int guthaben;
const char * display_string;
} fsm_action_t;
extern void automat_reset(void);
extern void automat_transition(BOOL becher, BOOL muenze, BOOL muenz_wert);
extern fsm_action_t automat_output(void);
#endif /* AUTOMAT_H */

View File

@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="kaffee_automat" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/kaffee_automat" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Linker>
<Add option="-Llib -lkaffee_automat" />
<Add option="-Lbin/Debug -lQt5Core -lQt5Gui -lQt5Widgets -pthread" />
<Add option="-lstdc++-6 -lgcc_s_dw2-1" />
</Linker>
<Unit filename="automat.c">
<Option compilerVar="CC" />
</Unit>
<Unit filename="automat.h" />
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>

21
Modul4/Makefile Normal file
View File

@ -0,0 +1,21 @@
# Aufruf linux: make -f makefilename
# Aufruf win: mingw32-make -f makefilename
# -B: Build all; Alle Ziele ohne Bedingungen erstellen
#
automat.exe: automat.o checker.o view.o io.o main.o
gcc -o automat.exe automat.o checker.o view.o io.o main.o
automat.o: automat.c automat.h
gcc -c -Wall automat.c
checker.o: checker.c checker.h automat.h s_transaction_list.h
gcc -c -Wall checker.c
view.o: view.c view.h automat.h io.h
gcc -c -Wall view.c
io.o: io.c io.h
gcc -c -Wall io.c
main.o: main.c view.h automat.h checker.h
gcc -c -Wall main.c

103
Modul4/automat.c Normal file
View File

@ -0,0 +1,103 @@
#include <stdio.h>
#include <stdbool.h>
#include "automat.h"
typedef enum {A=0,B,C,D,E,F,H,I, NR_OF_STATES} state_t;
static state_t s_curstate = A;
static int s_guthaben = 0;
static bool s_kaffee_los = false;
static bool s_muenz_rueck = false;
static bool s_display = false;
static const char* s_display_string = "Warten";
void automat_reset(void)
{
s_curstate = A;
s_guthaben = 0;
s_kaffee_los = false;
s_muenz_rueck = false;
s_display = false;
s_display_string = "Warten";
}
void automat_transition(BOOL becher, BOOL muenze, BOOL muenz_wert)
{
s_kaffee_los = false;
s_muenz_rueck = false;
s_display = false;
switch (s_curstate) {
case A: case B: case C: case D:
// Münzeinwurf
if (muenze) {
int wert = muenz_wert ? 2 : 1;
if (s_guthaben + wert > 4) {
s_muenz_rueck = true;
s_display_string = "Zu viel! Münze zurück";
} else {
s_guthaben += wert;
}
}
// *** Hier ist die Änderung ***
if (s_guthaben < 4) {
if (s_guthaben == 0) s_curstate = A, s_display_string = "Warten";
else if (s_guthaben == 1) s_curstate = B, s_display_string = "1 Euro";
else if (s_guthaben == 2) s_curstate = C, s_display_string = "2 Euro";
else if (s_guthaben == 3) s_curstate = D, s_display_string = "3 Euro";
} else if (s_guthaben == 4) {
if (becher) {
s_curstate = F;
s_kaffee_los = true;
s_display = true;
s_display_string = "Kaffee wird ausgegeben";
s_guthaben = 0; // Guthaben wird nach Ausgabe zurückgesetzt!
} else {
s_curstate = E;
s_display = true;
s_display_string = "Bitte Becher unterstellen!";
}
}
break;
case E: // 4 Euro, warte auf Becher
s_display = true;
s_display_string = "Bitte Becher unterstellen!";
if (becher) {
s_curstate = F;
s_kaffee_los = true;
s_display_string = "Kaffee wird ausgegeben";
s_guthaben = 0; // Guthaben wird nach Ausgabe zurückgesetzt!
}
break;
case F: // Kaffee läuft, warte auf Becherentnahme
s_display = true;
s_display_string = "Becher entnehmen";
if (!becher) {
s_curstate = A;
s_display = false;
s_display_string = "Warten";
}
break;
default:
s_curstate = A;
s_guthaben = 0;
s_display = false;
s_display_string = "Warten";
break;
}
}
fsm_action_t automat_output(void)
{
return (fsm_action_t){
.display = s_display,
.muenz_rueck = s_muenz_rueck,
.kaffee_los = s_kaffee_los,
.guthaben = s_guthaben,
.display_string = s_display_string
};
}
//gcc automat.c view.c main.c io.c checker.c -o console_automat

44
Modul4/automat.h Normal file
View File

@ -0,0 +1,44 @@
/**********************************************************************\
* Kurzbeschreibung: automat.h
* Stellt Funktionen zur Realisierung eines Automaten zur Verfügung,
* die über die in der automat.h vorgegebene C-Schnittstelle
* mit einer grafischen Schnittstelle kommunizieren.
*
* Datum: Autor: Grund der Aenderung:
*
*
\**********************************************************************/
#ifndef AUTOMAT_H
#define AUTOMAT_H
/*--- #defines -------------------------------------------------------*/
#define true 1
#define false 0
/*--- Datentypen (typedef) -------------------------------------------*/
typedef int BOOL;
typedef struct {
BOOL display;
BOOL muenz_rueck;
BOOL kaffee_los;
int guthaben;
const char * display_string;
} fsm_action_t;
/*--- Prototypen globaler Funktionen ---------------------------------*/
/*--------------------------------------------------------------------*
* Setzt den Automat in IDLE state *
*--------------------------------------------------------------------*/
extern void automat_reset(void);
/*--------------------------------------------------------------------*
* Fuehrt Zustandsuebergaenge durch *
*--------------------------------------------------------------------*/
extern void automat_transition(BOOL becher, BOOL muenze, BOOL muenz_wert);
/*--------------------------------------------------------------------*
* Gibt Informationen ueber aktuellen Zustand zurueck *
*--------------------------------------------------------------------*/
extern fsm_action_t automat_output(void);
#endif /* AUTOMAT_H */

123
Modul4/checker.c Normal file
View File

@ -0,0 +1,123 @@
#include "checker.h"
#include <stdio.h>
typedef struct {
BOOL reset;
BOOL muenz_wert;
BOOL muenze;
BOOL becher;
} fsm_input_t;
typedef struct {
fsm_input_t input;
fsm_action_t output;
} fsm_transaction_t;
#include "s_transaction_list.h"
static const int s_transaction_list_size =
sizeof(s_transaction_list) / sizeof(*s_transaction_list);
#define ERROR_STRING_SIZE 1000
static char s_error_string[ERROR_STRING_SIZE] = { 0 };
static void checker_propagate_input(fsm_input_t input);
static BOOL checker_compare(fsm_action_t o1, fsm_action_t o2);
static void checker_write_error_string(
fsm_action_t current, fsm_input_t input,
fsm_action_t actual, fsm_action_t expected);
static void checker_clear_error_string(void);
/*****************************************************************************/
/* implementation of extern functions ****************************************/
/*****************************************************************************/
BOOL checker_check()
{
fsm_action_t last_out;
fsm_action_t new_out;
int i;
automat_reset();
for (i = 0; i < s_transaction_list_size; i++) {
last_out = automat_output();
checker_propagate_input(s_transaction_list[i].input);
new_out = automat_output();
if (!checker_compare(new_out, s_transaction_list[i].output)) {
checker_write_error_string(last_out, s_transaction_list[i].input,
new_out, s_transaction_list[i].output);
return false;
}
}
checker_clear_error_string();
return true;
}
const char * checker_error_string() { return s_error_string; }
/*****************************************************************************/
/* implementation of static functions ****************************************/
/*****************************************************************************/
void checker_propagate_input(fsm_input_t input)
{
if (input.reset) automat_reset();
else automat_transition(input.becher, input.muenze, input.muenz_wert);
}
BOOL checker_compare(fsm_action_t o1, fsm_action_t o2)
{
return o1.display == o2.display &&
o1.muenz_rueck == o2.muenz_rueck &&
o1.kaffee_los == o2.kaffee_los &&
o1.guthaben == o2.guthaben;
}
void checker_write_error_string(
fsm_action_t current, fsm_input_t input,
fsm_action_t actual, fsm_action_t expected)
{
if (input.reset) {
/* actually wanna use snprintf, but it's defined in c99 */
sprintf(s_error_string,
"Ausgangsvektor enthält nicht die erwarteten Werte:\n"
"Letzter Zustand: disp:%d rueck:%d los:%d guthaben:%d \"%s\"\n"
"Eingang: Reset\n"
"Ausgang: disp:%d rueck:%d los:%d guthaben:%d \"%s\"\n"
"Erwartet: disp:%d rueck:%d los:%d guthaben:%d",
current.display, current.muenz_rueck, current.kaffee_los,
current.guthaben, current.display_string,
actual.display, actual.muenz_rueck, actual.kaffee_los,
actual.guthaben, actual.display_string,
expected.display, expected.muenz_rueck, expected.kaffee_los,
expected.guthaben);
} else {
/* actually wanna use snprintf, but it's defined in c99 */
sprintf(s_error_string,
"Ausgangsvektor enthält nicht die erwarteten Werte:\n"
"Letzter Zustand: disp:%d rueck:%d los:%d guthaben:%d \"%s\"\n"
"Eingang: becher:%d muenze:%d muenz_wert:%d\n"
"Ausgang: disp:%d rueck:%d los:%d guthaben:%d \"%s\"\n"
"Erwartet: disp:%d rueck:%d los:%d guthaben:%d",
current.display, current.muenz_rueck, current.kaffee_los,
current.guthaben, current.display_string,
input.becher, input.muenze, input.muenz_wert,
actual.display, actual.muenz_rueck, actual.kaffee_los,
actual.guthaben, actual.display_string,
expected.display, expected.muenz_rueck, expected.kaffee_los,
expected.guthaben);
}
}
void checker_clear_error_string()
{
s_error_string[0] = '\0';
}

9
Modul4/checker.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef __CHECKER_H__
#define __CHECKER_H__
#include "automat.h"
extern BOOL checker_check(void);
extern const char * checker_error_string(void);
#endif /* __CHECKER_H__ */

80
Modul4/io.c Normal file
View File

@ -0,0 +1,80 @@
#include "io.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
/******************************************************************************/
/* Module intern macros *******************************************************/
/******************************************************************************/
/* unfortunatly can't write this code as function, because of stack management
* => use makro instead */
#define DO_PRINTING(output_str) \
do { \
if (output_str != NULL) { \
va_list args; \
va_start(args, output_str); \
vprintf(output_str, args); \
va_end(args); \
} \
} while (0)
/******************************************************************************/
/* Implementations ************************************************************/
/******************************************************************************/
char * io_read_string(char * read_buffer, size_t buffer_size,
const char * output_str, ...)
{
char * ret, * ptr;
DO_PRINTING(output_str);
ret = fgets(read_buffer, buffer_size, stdin);
if (ret == NULL) return NULL;
/* search for \n */
ptr = strchr(read_buffer, '\n');
/* if found write \0 instead of \n */
if (ptr) *ptr = '\0';
/* else buffer is not read completly => clear buffer */
else while (getchar() != '\n');
return read_buffer;
}
int io_read_signed_number(const char * output_str, ...)
{
int number;
while (1) {
int ret;
DO_PRINTING(output_str);
ret = scanf("%d", &number);
/* clear buffer */
while (getchar() != '\n');
if (ret == 1) break;
else printf("Eingabefehler!!!\n");
}
return number;
}
unsigned int io_read_unsigned_number(const char * output_str, ...)
{
unsigned int number;
while (1) {
int ret;
DO_PRINTING(output_str);
ret = scanf("%u", &number);
/* clear buffer */
while (getchar() != '\n');
if (ret == 1) break;
else printf("Eingabefehler!!!\n");
}
return number;
}

18
Modul4/io.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef __IO_H__
#define __IO_H__
#include <stddef.h> /* for size_t */
/* prints the given string (if not NULL) and reads a string from stdin and
* removes \n
* the functions returns the value of fgets */
extern char * io_read_string(char * read_buffer, size_t buffer_size,
const char * output_str, ...);
/* prints the given string (if not NULL) and reads signed number from stdin */
extern int io_read_signed_number(const char * output_str, ...);
/* prints the given string (if not NULL) and reads unsigned number from stdin */
extern unsigned int io_read_unsigned_number(const char * output_str, ...);
#endif /* __IO_H__ */

70
Modul4/main.c Normal file
View File

@ -0,0 +1,70 @@
#include <stdio.h>
#include "view.h"
#include "automat.h"
#include "checker.h"
void insert_1_euro(void)
{
view_set_coin(1);
automat_transition(view_has_cup(), true, false);
}
void insert_2_euro(void)
{
view_set_coin(2);
automat_transition(view_has_cup(), true, true);
}
void toggle_cup(void)
{
view_set_coin(0);
view_toggle_cup();
automat_transition(view_has_cup(), false, false);
}
void check(void)
{
printf("\n");
if (checker_check()) {
printf("Der Zustandsautomat ist correct\n");
} else {
printf("%s\n", checker_error_string());
}
printf("\nWeiter mit Return ...");
while (getchar() != '\n');
automat_reset();
}
int main(void)
{
int finished = 0;
automat_reset();
view_reset();
while (!finished) {
view_menu_t selection;
view_show(automat_output());
selection = view_menu();
switch (selection) {
case VIEW_1_EURO: insert_1_euro(); break;
case VIEW_2_EURO: insert_2_euro(); break;
case VIEW_CUP: toggle_cup(); break;
case VIEW_RESET: automat_reset(); view_reset(); break;
case VIEW_CHECK: check(); break;
case VIEW_EXIT: finished = 1; break;
case VIEW_INVALID: break;
}
}
return 0;
}

100002
Modul4/s_transaction_list.h Normal file

File diff suppressed because it is too large Load Diff

115
Modul4/view.c Normal file
View File

@ -0,0 +1,115 @@
#include "view.h"
#include "io.h"
#include <stdio.h>
#define S_MACHINE_HEAD \
" /-----------------------------------\\\n" \
" | %33s |\n" \
" \\-----------------------------------/\n" \
" \n" \
" /~~~~~~~~~~~/| \n" \
" / /#########/ / |\n" \
" / /_________/ / |\n" \
" =============== /||\n" \
" | %03d EUR |/ ||\n" \
" |_____________|/ ||\n" \
" | \\___..___/ ||\n" \
" | || ||\n"
#define S_MACHINE_NO_COFFEE \
" | ||\n" \
" | ||\n" \
" | ||\n"
#define S_MACHINE_COFFEE \
" | ) ( ||\n" \
" | ( ) ) ||\n" \
" | ) ( ( ||\n" \
#define S_MACHINE_NO_CUP \
" _________ | ||\n" \
" .-'---------| |_______________||\n" \
" ( C|/\\/\\/\\/\\/| | / |\n" \
" '-./\\/\\/\\/\\/| _|_ _ _ _ _ _ _/ |\n" \
" '_________' | | / \n" \
" '-------' |_______________|/ \n" \
" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"
#define S_MACHINE_CUP \
" | _________ ||\n" \
" .-'---------|___||\n" \
" ( C|/\\/\\/\\/\\/| / |\n" \
" _'-./\\/\\/\\/\\/|_/ |\n" \
" | '_________' | / \n" \
" |____'-------'__|/ \n" \
" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"
#define S_MACHINE_NO_COIN_RETURN "\n"
#define S_MACHINE_INVALID_COIN_RETURN \
" -> keine Muenze verfuegbar !!!\n"
#define S_MACHINE_COIN_RETURN \
" -> %0d Euro zurueck\n"
/* machine status variables */
static int s_has_cup = 0;
static int s_coin_value = 0;
/* function implementations */
void view_show(fsm_action_t out_vector)
{
int i;
for (i = 0; i < 100; i++) printf("\n");
printf(S_MACHINE_HEAD, out_vector.display_string, out_vector.guthaben);
if (out_vector.kaffee_los) printf(S_MACHINE_COFFEE);
else printf(S_MACHINE_NO_COFFEE);
if (s_has_cup) printf(S_MACHINE_CUP);
else printf(S_MACHINE_NO_CUP);
if (out_vector.muenz_rueck) {
if (s_coin_value) printf(S_MACHINE_COIN_RETURN, s_coin_value);
else printf(S_MACHINE_INVALID_COIN_RETURN);
} else {
printf(S_MACHINE_NO_COIN_RETURN);
}
}
view_menu_t view_menu()
{
int read = io_read_signed_number(
"\n"
" 0) Einwurf 1 Euro\n"
" 1) Einwurf 2 Euro\n"
" 2) Becher verstellen\n"
" 3) Reset\n"
" 4) Automatischer Testlauf\n"
" 5) Programm beenden\n"
"\n"
" Auswahl: ");
switch (read) {
case 0: return VIEW_1_EURO;
case 1: return VIEW_2_EURO;
case 2: return VIEW_CUP;
case 3: return VIEW_RESET;
case 4: return VIEW_CHECK;
case 5: return VIEW_EXIT;
default: return VIEW_INVALID;
}
}
void view_toggle_cup() { s_has_cup = s_has_cup ? 0 : 1; }
int view_has_cup() { return s_has_cup; }
void view_set_coin(int value) { s_coin_value = value; }
void view_reset()
{
s_has_cup = 0;
s_coin_value = 0;
}

16
Modul4/view.h Normal file
View File

@ -0,0 +1,16 @@
#pragma once
#include "automat.h"
typedef enum { VIEW_1_EURO, VIEW_2_EURO, VIEW_CUP, VIEW_RESET, VIEW_CHECK, VIEW_EXIT, VIEW_INVALID } view_menu_t;
extern void view_show(fsm_action_t out_vector);
extern view_menu_t view_menu(void);
extern void view_toggle_cup(void);
extern int view_has_cup(void);
extern void view_set_coin(int value);
extern void view_reset(void);

38
dualrech.c Normal file
View File

@ -0,0 +1,38 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ist_dual(const char *s) {
while (*s) if (*s != '0' && *s != '1') return 0; else s++;
return 1;
}
void print_bin(int n) {
for (int i = 31; i >= 0; i--) putchar((n >> i) & 1 ? '1' : '0');
}
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Richtiger Aufruf: %s <operand> <operator> <operand>\n", argv[0]);
return 1;
}
char *a = argv[1], *op = argv[2], *b = argv[3];
int ok = 1;
if (!ist_dual(a)) { printf("....... %s ist keine erlaubte Dualzahl\n", a); ok = 0; }
if (!ist_dual(b)) { printf("....... %s ist keine erlaubte Dualzahl\n", b); ok = 0; }
if (strlen(op) != 1 || strchr("+-*/&^", op[0]) == NULL) {
printf("....... %s ist kein erlaubter Operator\n", op); ok = 0;
}
if (!ok) return 1;
int x = strtol(a, NULL, 2), y = strtol(b, NULL, 2), r = 0;
if (*op == '/' && y == 0) { printf("Fehler: Division durch 0\n"); return 1; }
switch (*op) {
case '+': r = x + y; break;
case '-': r = x - y; break;
case '*': r = x * y; break;
case '/': r = x / y; break;
case '&': r = x & y; break;
case '^': r = x ^ y; break;
}
printf("%s %s %s =\n....... ", a, op, b);
print_bin(r);
printf(" (0x%X)\n", r);
return 0;
}

46
josephus.c Normal file
View File

@ -0,0 +1,46 @@
/**********************************************************************\
* Kurzbeschreibung:
* Realisiert das Josephus Spiel zum Test einer Queue
*
* Datum: Autor:
* 30.9.2015 Prof. Dr. Helmut Herold
*
\**********************************************************************/
/*--- #includes ------------------------------------------------------*/
#define _CRT_SECURE_NO_WARNINGS //VC++: keine scanf() Warnungen
#include <stdio.h>
#include "queue.h"
/*--- #defines -------------------------------------------------------*/
/*--- Datentypen (typedef) -------------------------------------------*/
/*--- Prototypen lokaler Funktionen ----------------------------------*/
/*--- main -----------------------------------------------------------*/
int main(void)
{
int n, z, i=1, nr;
do {
printf("Wie viele Personen: ");
nr = scanf("%d", &n); getchar();
} while (nr != 1 || n < 0);
do {
printf("Wie vielte ist auszusondern: ");
nr = scanf("%d", &z); getchar();
} while (nr != 1 || z < 0);
for (i = 1; i <= n; i++) // Queue füllen
put(i);
while (!isEmpty()) {
for (i = 1; i < z; i++) { // z-1 Zahlen aus Queue lesen
if (put(get()) == FALSE) { // und wieder am Ende der Queue einfügen
fprintf(stderr, "Fehler beim Einfügen\n");
return 1;
}
}
printf("%d, ", get()); // z. Zahl aus Queue lesen und ausgeben
// Diese Zahl nicht mehr einfügen
}
printf("\n");
return 0;
}
//gcc -c queue.c -o queue.o
//ar rcs libqueue.a queue.o
//gcc josephus.c -L. -lqueue -o josephus

21
numausg.c Normal file
View File

@ -0,0 +1,21 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Bitte Dateinamen als erstes Argument angeben.\n");
return 1;
}
FILE *datei = fopen(argv[1], "r");
if (datei == NULL) {
perror("Fehler beim Öffnen der Datei");
return 1;
}
char zeile[1024];
int zeilennummer = 1;
while (fgets(zeile, sizeof(zeile), datei) != NULL) {
printf("%4d: %s", zeilennummer, zeile);
zeilennummer++;
}
fclose(datei);
return 0;
}

54
queue.c Normal file
View File

@ -0,0 +1,54 @@
/**********************************************************************\
* Kurzbeschreibung:
* queue.c - realisiert eine Queue (Warteschlange)
*
* Datum: Autor:
* 11.06.2025 OpenAI (auf Basis von Prof. Herold)
\**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
/*--- Struktur eines Listenelements ----------------------------------*/
typedef struct Node {
int value;
struct Node *next;
} Node;
/*--- Modulglobale Variablen: Zeiger auf Kopf und Ende der Liste -----*/
static Node *head = NULL;
static Node *tail = NULL;
/*--- put(): Einfügen am Ende der Liste ------------------------------*/
Bool put(int zahl) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (newNode == NULL) {
fprintf(stderr, "Speicher konnte nicht allokiert werden.\n");
return FALSE;
}
newNode->value = zahl;
newNode->next = NULL;
if (tail != NULL) {
tail->next = newNode;
} else {
head = newNode; // Liste war leer
}
tail = newNode;
return TRUE;
}
/*--- get(): Entfernt das erste Element ------------------------------*/
int get(void) {
if (head == NULL) {
fprintf(stderr, "Fehler: Queue ist leer.\n");
return QLEER;
}
Node *tmp = head;
int value = tmp->value;
head = head->next;
if (head == NULL) {
tail = NULL; // Liste ist jetzt leer
}
free(tmp);
return value;
}
/*--- isEmpty(): Prüft, ob Liste leer ist ----------------------------*/
Bool isEmpty(void) {
return head == NULL ? TRUE : FALSE;
}

42
queue.h Normal file
View File

@ -0,0 +1,42 @@
/**********************************************************************\
* Kurzbeschreibung:
* queue.h - realisiert eine Queue (Warteschlange) als verkettete Liste
*
* Datum: Autor:
* 30.9.2015 Prof. Dr. Helmut Herold / angepasst von OpenAI
\**********************************************************************/
#ifndef QUEUE_H
#define QUEUE_H
/*--- #includes ------------------------------------------------------*/
#include <limits.h>
/*--- #defines -------------------------------------------------------*/
#define TRUE 1
#define FALSE 0
#define QLEER INT_MIN // Rückgabewert bei leerer Queue
/*--- Datentypen -----------------------------------------------------*/
typedef int Bool;
/*--- Funktionsprototypen --------------------------------------------*/
/*--------------------------------------------------------------------*\
* put()
* Fügt eine Zahl ans Ende der Warteschlange.
* Rückgabewert:
* TRUE, wenn erfolgreich
* FALSE, wenn kein Speicher verfügbar
\*--------------------------------------------------------------------*/
Bool put(int zahl);
/*--------------------------------------------------------------------*\
* get()
* Entfernt die erste Zahl aus der Warteschlange und gibt sie zurück.
* Rückgabewert:
* Wert aus der Warteschlange oder QLEER bei leerer Queue
\*--------------------------------------------------------------------*/
int get(void);
/*--------------------------------------------------------------------*\
* isEmpty()
* Prüft, ob die Warteschlange leer ist.
* Rückgabewert:
* TRUE, wenn leer
* FALSE, wenn nicht leer
\*--------------------------------------------------------------------*/
Bool isEmpty(void);
#endif /* QUEUE_H */