Compare commits
18
Commits
7f99fb7eee
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e6857d28b1 | ||
|
|
923049e6a1 | ||
|
|
7f836d6689 | ||
|
|
77c0ef9e04 | ||
|
|
fc0ecd6b27 | ||
|
|
d48bacbae7 | ||
|
|
6da144e049 | ||
|
|
7b6de3d556 | ||
|
|
dc6284a432 | ||
|
|
6eb4e0e658 | ||
|
|
ae3b8a086e | ||
|
|
3408eeeb49 | ||
|
|
1d16bb3625 | ||
|
|
c0160343e0 | ||
|
|
685bf9837c | ||
|
|
2c14751863 | ||
|
|
9242c7e4a0 | ||
|
|
445e541ddc |
No files matched your search
@@ -0,0 +1,46 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// Struktur für eine römische Ziffer und ihren Wert
|
||||
typedef struct {
|
||||
int wert; // Der Wert der Ziffer (z.B. 1)
|
||||
const char *roemisch; // Das römische Zeichen (z.B. "I")
|
||||
} RoemZahl;
|
||||
|
||||
int main() {
|
||||
// Tabelle der römischen Ziffern, aufsteigend sortiert (klein nach groß)
|
||||
RoemZahl ziffern[] = {
|
||||
{1, "I"},
|
||||
{4, "IV"},
|
||||
{5, "V"},
|
||||
{9, "IX"},
|
||||
{10, "X"},
|
||||
{40, "XL"},
|
||||
{50, "L"},
|
||||
{90, "XC"},
|
||||
{100, "C"},
|
||||
{400, "CD"},
|
||||
{500, "D"},
|
||||
{900, "CM"},
|
||||
{1000, "M"}
|
||||
};
|
||||
const int n = sizeof(ziffern) / sizeof(ziffern[0]);
|
||||
int zahl;
|
||||
|
||||
// Zahl vom Benutzer einlesen
|
||||
printf("Zu wandelnde Zahl: ");
|
||||
scanf("%d", &zahl);
|
||||
|
||||
printf("... %d = ", zahl);
|
||||
|
||||
// Über die Ziffern von groß nach klein iterieren (von hinten nach vorne)
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
// So oft wie möglich die aktuelle Ziffer abziehen und ausgeben
|
||||
for (int j = 0; j < zahl / ziffern[i].wert; j++) {
|
||||
printf("%s", ziffern[i].roemisch);
|
||||
}
|
||||
zahl %= ziffern[i].wert;
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// Struktur für eine römische Ziffer und ihren Wert
|
||||
typedef struct {
|
||||
int wert;
|
||||
const char *roemisch;
|
||||
} RoemZahl;
|
||||
|
||||
int main() {
|
||||
// Array aufsteigend sortiert (klein nach groß)
|
||||
RoemZahl ziffern[] = {
|
||||
{1, "I"},
|
||||
{4, "IV"},
|
||||
{5, "V"},
|
||||
{9, "IX"},
|
||||
{10, "X"},
|
||||
{40, "XL"},
|
||||
{49, "IL"}, // 50 - 1
|
||||
{50, "L"},
|
||||
{90, "XC"},
|
||||
{99, "IC"}, // 100 - 1
|
||||
{100, "C"},
|
||||
{400, "CD"},
|
||||
{499, "ID"}, // 500 - 1
|
||||
{500, "D"},
|
||||
{900, "CM"},
|
||||
{990, "XM"}, // 1000 - 10
|
||||
{999, "IM"}, // 1000 - 1
|
||||
{1000, "M"}
|
||||
};
|
||||
const int n = sizeof(ziffern) / sizeof(ziffern[0]);
|
||||
int zahl;
|
||||
|
||||
printf("Zu wandelnde Zahl: ");
|
||||
scanf("%d", &zahl);
|
||||
|
||||
printf("... %d = ", zahl);
|
||||
|
||||
// Von groß nach klein durch das aufsteigende Array
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
for (int j = 0; j < zahl / ziffern[i].wert; j++) {
|
||||
printf("%s", ziffern[i].roemisch);
|
||||
}
|
||||
zahl %= ziffern[i].wert;
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct Schueler {
|
||||
char name[30];
|
||||
char vorname[30];
|
||||
int note;
|
||||
} Schueler_t;
|
||||
|
||||
int main() {
|
||||
Schueler_t *klasse = NULL; // Dynamisches Array für Schüler
|
||||
int anzahl = 0; // Aktuelle Anzahl der Schüler
|
||||
int kapazitaet = 0; // Aktuelle Kapazität des Arrays
|
||||
int summe = 0;
|
||||
int haeufigkeit[6] = {0}; // Notenspiegel
|
||||
|
||||
while (1) {
|
||||
// Speicher vergrößern, falls nötig
|
||||
if (anzahl >= kapazitaet) {
|
||||
kapazitaet = kapazitaet == 0 ? 10 : kapazitaet * 2;
|
||||
Schueler_t *tmp = realloc(klasse, kapazitaet * sizeof(Schueler_t));
|
||||
if (!tmp) {
|
||||
printf("Speicherfehler!\n");
|
||||
free(klasse);
|
||||
return 1;
|
||||
}
|
||||
klasse = tmp;
|
||||
}
|
||||
|
||||
printf("\n----Eingabe des %d. Schuelers----\n", anzahl + 1);
|
||||
|
||||
printf("Name (oder ENTER zum Beenden): ");
|
||||
if (fgets(klasse[anzahl].name, sizeof(klasse[anzahl].name), stdin) == NULL)
|
||||
break;
|
||||
klasse[anzahl].name[strcspn(klasse[anzahl].name, "\n")] = 0;
|
||||
if (klasse[anzahl].name[0] == '\0')
|
||||
break;
|
||||
|
||||
printf("Vorname: ");
|
||||
if (fgets(klasse[anzahl].vorname, sizeof(klasse[anzahl].vorname), stdin) == NULL)
|
||||
break;
|
||||
klasse[anzahl].vorname[strcspn(klasse[anzahl].vorname, "\n")] = 0;
|
||||
|
||||
do {
|
||||
printf("Note (1-6): ");
|
||||
if (scanf("%d", &klasse[anzahl].note) != 1) {
|
||||
while (getchar() != '\n');
|
||||
printf("Ungültige Eingabe. Abbruch.\n");
|
||||
free(klasse);
|
||||
return 1;
|
||||
}
|
||||
} while (klasse[anzahl].note < 1 || klasse[anzahl].note > 6);
|
||||
|
||||
// Eingabepuffer leeren (wegen scanf)
|
||||
while (getchar() != '\n');
|
||||
|
||||
summe += klasse[anzahl].note;
|
||||
haeufigkeit[klasse[anzahl].note - 1]++;
|
||||
anzahl++;
|
||||
}
|
||||
|
||||
if (anzahl == 0) {
|
||||
printf("Keine Schüler eingegeben.\n");
|
||||
free(klasse);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Übersicht ausgeben
|
||||
printf("\n%-4s %-15s %-15s %-5s\n", "Nr.", "Name", "Vorname", "Note");
|
||||
for (int i = 0; i < anzahl; i++) {
|
||||
printf("%-4d %-15s %-15s %-5d\n", i + 1, klasse[i].name, klasse[i].vorname, klasse[i].note);
|
||||
}
|
||||
// Durchschnittsnote
|
||||
double durchschnitt = (double)summe / anzahl;
|
||||
printf("\n....Durchschnittsnote: %.2f\n", durchschnitt);
|
||||
// Notenspiegel
|
||||
printf("....Notenspiegel\n");
|
||||
for (int i = 0; i < 6; i++) {
|
||||
printf("Note %d: ", i + 1);
|
||||
for (int j = 0; j < haeufigkeit[i]; j++) {
|
||||
printf("*");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
free(klasse);
|
||||
return 0;
|
||||
}
|
||||
+34
-13
@@ -1,35 +1,56 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// Definition einer Struktur zur Speicherung von Zeitangaben
|
||||
struct zeit {
|
||||
int tag;
|
||||
int std;
|
||||
int min;
|
||||
int sek;
|
||||
int tag; // Tage
|
||||
int std; // Stunden
|
||||
int min; // Minuten
|
||||
int sek; // Sekunden
|
||||
};
|
||||
|
||||
// Funktion: Wandelt eine Zeitstruktur in die Gesamtanzahl an Sekunden um
|
||||
unsigned long zeit_in_sek(struct zeit z) {
|
||||
// 1 Tag = 86400 Sekunden, 1 Stunde = 3600 Sekunden, 1 Minute = 60 Sekunden
|
||||
return z.tag * 86400 + z.std * 3600 + z.min * 60 + z.sek;
|
||||
}
|
||||
|
||||
// Funktion: Wandelt eine Anzahl von Sekunden in eine Zeitstruktur um
|
||||
struct zeit sek_in_zeit(unsigned long sek) {
|
||||
struct zeit z;
|
||||
z.tag = sek / 86400;
|
||||
sek %= 86400;
|
||||
z.std = sek / 3600;
|
||||
sek %= 3600;
|
||||
z.min = sek / 60;
|
||||
z.sek = sek % 60;
|
||||
z.tag = sek / 86400; // Ganze Tage berechnen
|
||||
sek %= 86400; // Restsekunden nach Tagen
|
||||
z.std = sek / 3600; // Ganze Stunden berechnen
|
||||
sek %= 3600; // Restsekunden nach Stunden
|
||||
z.min = sek / 60; // Ganze Minuten berechnen
|
||||
z.sek = sek % 60; // Restsekunden nach Minuten
|
||||
return z;
|
||||
}
|
||||
|
||||
int main() {
|
||||
struct zeit z1, z2, summe;
|
||||
unsigned long s1, s2, sgesamt;
|
||||
struct zeit z1, z2, summe; // Zeitstrukturen für die beiden Eingaben und die Summe
|
||||
unsigned long s1, s2, sgesamt; // Variablen für die Sekundenwerte
|
||||
|
||||
// Eingabe der ersten Zeit im Format tt.hh.mm.ss
|
||||
printf("Gib 1. Zeit ein (tt.hh.mm.ss): ");
|
||||
scanf("%d.%d.%d.%d", &z1.tag, &z1.std, &z1.min, &z1.sek);
|
||||
|
||||
// Eingabe der zweiten Zeit im gleichen Format
|
||||
printf("Gib 2. Zeit ein (tt.hh.mm.ss): ");
|
||||
scanf("%d.%d.%d.%d", &z2.tag, &z2.std, &z2.min, &z2.sek);
|
||||
|
||||
// Umwandlung der beiden Zeiten in Sekunden
|
||||
s1 = zeit_in_sek(z1);
|
||||
s2 = zeit_in_sek(z2);
|
||||
|
||||
// Addition der Sekundenwerte
|
||||
sgesamt = s1 + s2;
|
||||
|
||||
// Umwandlung der Gesamtsumme der Sekunden zurück in eine Zeitstruktur
|
||||
summe = sek_in_zeit(sgesamt);
|
||||
|
||||
// Ausgabe des Ergebnisses in Tagen, Stunden, Minuten, Sekunden und Gesamtsekunden
|
||||
printf("= %d Tage, %d:%d:%d; %lu Gesamtsekunden\n",
|
||||
summe.tag, summe.std, summe.min, summe.sek, sgesamt);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define MAXWORD 100 // Maximale Wortlänge
|
||||
|
||||
// Definition eines Baumknotens für ein Wort
|
||||
typedef struct TreeNode {
|
||||
char *word; // Das gespeicherte Wort
|
||||
int count; // Wie oft das Wort vorkam
|
||||
struct TreeNode *left; // Linker Teilbaum (Wörter, die alphabetisch davor kommen)
|
||||
struct TreeNode *right; // Rechter Teilbaum (Wörter, die alphabetisch danach kommen)
|
||||
} TreeNode_t;
|
||||
|
||||
// Funktion zum Einfügen eines Wortes in den Baum
|
||||
// Falls das Wort schon existiert, wird der Zähler erhöht
|
||||
TreeNode_t* insert(TreeNode_t *root, const char *word) {
|
||||
if (root == NULL) {
|
||||
// Neuer Knoten, falls das Wort noch nicht im Baum ist
|
||||
TreeNode_t *newNode = malloc(sizeof(TreeNode_t));
|
||||
if (!newNode) {
|
||||
perror("Speicherfehler");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
newNode->word = strdup(word); // Wort kopieren und speichern
|
||||
newNode->count = 1; // Erstes Vorkommen
|
||||
newNode->left = newNode->right = NULL; // Noch keine Kinder
|
||||
return newNode;
|
||||
}
|
||||
|
||||
int cmp = strcmp(word, root->word); // Alphabetischer Vergleich
|
||||
if (cmp == 0) {
|
||||
// Wort schon vorhanden: Zähler erhöhen
|
||||
root->count++;
|
||||
} else if (cmp < 0) {
|
||||
// Wort ist alphabetisch VOR dem aktuellen Knoten: links einfügen
|
||||
root->left = insert(root->left, word);
|
||||
} else {
|
||||
// Wort ist alphabetisch NACH dem aktuellen Knoten: rechts einfügen
|
||||
root->right = insert(root->right, word);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
// Inorder-Ausgabe: Gibt die Wörter alphabetisch sortiert mit Häufigkeit aus
|
||||
void printTree(TreeNode_t *root) {
|
||||
if (root == NULL)
|
||||
return;
|
||||
printTree(root->left); // Erst linker Teilbaum
|
||||
printf("%-12s : %d\n", root->word, root->count); // Dann aktueller Knoten
|
||||
printTree(root->right); // Dann rechter Teilbaum
|
||||
}
|
||||
|
||||
// Gibt den belegten Speicher des Baumes wieder frei
|
||||
void freeTree(TreeNode_t *root) {
|
||||
if (root == NULL)
|
||||
return;
|
||||
freeTree(root->left); // Erst linker Teilbaum
|
||||
freeTree(root->right); // Dann rechter Teilbaum
|
||||
free(root->word); // Erst das Wort freigeben
|
||||
free(root); // Dann den Knoten selbst
|
||||
}
|
||||
|
||||
// Prüft, ob ein Zeichen ein Buchstabe ist (a-z, A-Z)
|
||||
int isLetter(char c) {
|
||||
return isalpha((unsigned char)c);
|
||||
}
|
||||
|
||||
// Liest Wörter aus stdin, zählt sie und gibt die Statistik aus
|
||||
void readTextAndPrintStats() {
|
||||
char word[MAXWORD]; // Zwischenspeicher für das aktuelle Wort
|
||||
int index = 0; // Position im Wort
|
||||
int c; // Aktuelles Zeichen
|
||||
TreeNode_t *tree = NULL; // Wurzel des Baumes
|
||||
|
||||
// Zeichenweise Einlesen bis Dateiende (EOF)
|
||||
while ((c = getchar()) != EOF) {
|
||||
if (isLetter(c)) {
|
||||
// Nur Buchstaben werden akzeptiert, alles in Kleinbuchstaben
|
||||
if (index < MAXWORD - 1)
|
||||
word[index++] = tolower(c);
|
||||
} else if (index > 0) {
|
||||
// Wortende erreicht (kein Buchstabe mehr)
|
||||
word[index] = '\0'; // String abschließen
|
||||
tree = insert(tree, word); // Wort in Baum einfügen
|
||||
index = 0; // Für das nächste Wort zurücksetzen
|
||||
}
|
||||
}
|
||||
// Falls am Ende noch ein Wort übrig ist (z.B. Datei endet ohne Leerzeichen)
|
||||
if (index > 0) {
|
||||
word[index] = '\0';
|
||||
tree = insert(tree, word);
|
||||
}
|
||||
|
||||
printTree(tree); // Statistik ausgeben (alphabetisch sortiert)
|
||||
freeTree(tree); // Speicher aufräumen
|
||||
}
|
||||
|
||||
// Hauptfunktion: Startet das Programm
|
||||
int main() {
|
||||
readTextAndPrintStats();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Kompilieren: gcc -o 28.6.3_wortstat 28.6.3_wortstat.c
|
||||
|
||||
// Ausführen: 28.6.3_wortstat.exe < test.txt
|
||||
@@ -0,0 +1,193 @@
|
||||
/**********************************************************************\
|
||||
* Kurzbeschreibung: automat.c
|
||||
* 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:
|
||||
*
|
||||
*
|
||||
\**********************************************************************/
|
||||
#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];
|
||||
|
||||
}
|
||||
@@ -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 */
|
||||
@@ -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>
|
||||
@@ -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
|
||||
@@ -0,0 +1,240 @@
|
||||
/**********************************************************************\
|
||||
* Kurzbeschreibung: automat.c
|
||||
* 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:
|
||||
*
|
||||
*
|
||||
\**********************************************************************/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "automat.h"
|
||||
|
||||
/*--- #defines -------------------------------------------------------*/
|
||||
/* Macro zur Ermittlung der Array Groesse */
|
||||
#define SIZE_OF(a) (sizeof((a))/ sizeof(*(a)))
|
||||
|
||||
#define NR_OF_STATES (I-A + 1)
|
||||
|
||||
/*--- Datentypen (typedef) -------------------------------------------*/
|
||||
/* Definition der Zustaende */
|
||||
typedef enum {A=0,B,C,D,E,F,G,H,I} state_t;
|
||||
|
||||
/* Definition der fsm-spezifischen Variablen */
|
||||
/* Struktur zur Beschreibung eines Uebergangs mit "Don't Care"-Moeglichkeit */
|
||||
typedef struct {
|
||||
int input;
|
||||
int mask;
|
||||
state_t nxtstate;
|
||||
} fsm_state_t;
|
||||
|
||||
/* Struktur zur Beschreibung aller Uebergaenge eines Zustands */
|
||||
typedef struct {
|
||||
const fsm_state_t * transitions;
|
||||
int nrOfTransitions;
|
||||
state_t defaultNxtState;
|
||||
} fsm_full_state_t;
|
||||
|
||||
/*--- Modulglobale static Variablen ----------------------------------*/
|
||||
/* Definition der Zustandsvariablen */
|
||||
static state_t s_curstate = A; /* Initialisierung */
|
||||
|
||||
/* Definition aller Zustandsuebergaenge fuer jeden Zustand */
|
||||
static const fsm_state_t s_transitions_A[] =
|
||||
{ /* input mask nxtstate */
|
||||
// Wenn 1?-Münze eingeworfen (muenze=1, muenz_wert=0), egal ob Becher steht oder nicht (weil mask ~0x004, also becher-Bit egal)
|
||||
{ 0x002, ~0x004, B }, // A -> B: 1? eingeworfen
|
||||
// Wenn 2?-Münze eingeworfen (muenze=1, muenz_wert=1), egal ob Becher steht oder nicht
|
||||
{ 0x003, ~0x004, C } // A -> C: 2? eingeworfen
|
||||
};
|
||||
|
||||
static const fsm_state_t s_transitions_B[] =
|
||||
{ /* input mask nxtstate */
|
||||
// Nochmal 1? eingeworfen (muenze=1, muenz_wert=0), becher egal
|
||||
{ 0x002, ~0x004, C }, // B -> C: 1? dazu, jetzt 2?
|
||||
// 2? eingeworfen (muenze=1, muenz_wert=1), becher egal
|
||||
{ 0x003, ~0x004, D } // B -> D: 2? dazu, jetzt 3?
|
||||
};
|
||||
|
||||
static const fsm_state_t s_transitions_C[] =
|
||||
{ /* input mask nxtstate */
|
||||
// Nochmal 1? eingeworfen (muenze=1, muenz_wert=0), becher egal
|
||||
{ 0x002, ~0x004, D }, // C -> D: 1? dazu, jetzt 3?
|
||||
// Becher steht (becher=1), egal was mit Münze, weil mask ~0x000 (alle Bits relevant)
|
||||
{ 0x003, ~0x000, F }, // C -> F: Becher untergestellt (becher=1, muenze=1, muenz_wert=1)
|
||||
// Becher steht (becher=1), muenze=1, muenz_wert=1 (also 2?), mask ~0x000
|
||||
{ 0x007, ~0x000, H } // C -> H: Becher steht, 2? eingeworfen
|
||||
};
|
||||
|
||||
static const fsm_state_t s_transitions_D[] =
|
||||
{ /* input mask nxtstate */
|
||||
// Becher steht (becher=1), egal was mit Münze, mask ~0x000
|
||||
{ 0x002, ~0x000, F }, // D -> F: Becher untergestellt
|
||||
// 2? eingeworfen (muenze=1, muenz_wert=1), becher egal
|
||||
{ 0x003, ~0x004, E }, // D -> E: 2? dazu, jetzt 5? (Überzahlung)
|
||||
// Becher steht (becher=1), muenze=1, muenz_wert=0 (1?), mask ~0x000
|
||||
{ 0x006, ~0x000, H } // D -> H: Becher steht, 1? eingeworfen
|
||||
};
|
||||
|
||||
static const fsm_state_t s_transitions_E[] =
|
||||
{
|
||||
// Becher steht (becher=1), egal was mit Münze
|
||||
{ 0x002, ~0x000, F }, // E -> F: Becher untergestellt
|
||||
// 2? eingeworfen, becher egal
|
||||
{ 0x000, ~0x005, D }, // E bleibt E: weitere Überzahlung
|
||||
// Becher steht (becher=1), muenze=1, muenz_wert=0 (1?)
|
||||
{ 0x006, ~0x000, H } // E -> H: Becher steht, 1? eingeworfen
|
||||
};
|
||||
|
||||
static const fsm_state_t s_transitions_F[] =
|
||||
{
|
||||
// Becher steht, 1? eingeworfen (becher=1, muenze=1, muenz_wert=0), mask ~0x001 (muenz_wert egal)
|
||||
{ 0x002, ~0x001, G }, // F -> G: Becher steht, Münze eingeworfen
|
||||
// Becher steht, 2? eingeworfen (becher=1, muenze=0, muenz_wert=1)
|
||||
{ 0x004, ~0x001, H }, // F -> H: Becher steht, 2? eingeworfen
|
||||
// Becher steht, 1? eingeworfen (becher=1, muenze=1, muenz_wert=0)
|
||||
{ 0x006, ~0x001, I } // F -> I: Becher steht, 1? eingeworfen
|
||||
};
|
||||
|
||||
static const fsm_state_t s_transitions_G[] =
|
||||
{
|
||||
// Becher steht, 1? eingeworfen
|
||||
{ 0x000, ~0x001, F }, // G bleibt G: Becher steht, Münze eingeworfen
|
||||
// Becher steht, 2? eingeworfen
|
||||
{ 0x004, ~0x001, H }, // G -> H: Becher steht, 2? eingeworfen
|
||||
// Becher steht, 1? eingeworfen
|
||||
{ 0x006, ~0x001, I } // G -> I: Becher steht, 1? eingeworfen
|
||||
};
|
||||
|
||||
static const fsm_state_t s_transitions_H[] =
|
||||
{
|
||||
// Kein Becher, keine Münze, kein Wert (alles 0)
|
||||
{ 0x000, ~0x001, A }, // H -> A: Reset, alles leer
|
||||
// Becher steht, keine Münze, kein Wert
|
||||
{ 0x002, ~0x000, B }, // H -> B: Becher steht, 1? eingeworfen
|
||||
// Becher steht, 2? eingeworfen
|
||||
{ 0x003, ~0x000, C }, // H -> C: Becher steht, 2? eingeworfen
|
||||
// Becher steht, 1? eingeworfen
|
||||
{ 0x006, ~0x001, I } // H -> I: Becher steht, 1? eingeworfen
|
||||
};
|
||||
|
||||
static const fsm_state_t s_transitions_I[] =
|
||||
{ // Kein Becher, keine Münze, kein Wert (alles 0)
|
||||
{ 0x000, ~0x001, A }, // I -> A: Reset, alles leer
|
||||
// Becher steht, keine Münze, kein Wert
|
||||
{ 0x002, ~0x000, B }, // I -> B: Becher steht, 1? eingeworfen
|
||||
// Becher steht, 2? eingeworfen
|
||||
{ 0x003, ~0x000, C }, // I -> C: Becher steht, 2? eingeworfen
|
||||
// Becher steht, 1? eingeworfen
|
||||
{ 0x004, ~0x001, H} // I bleibt I: Becher steht, 1? eingeworfen
|
||||
};
|
||||
|
||||
/* Definition der Uebergangstabelle */
|
||||
/* Die Reihenfolge der Zustaende in der enum Definition muss der
|
||||
* Reihenfolge der Zustaende in der Uebergangstabelle entsprechen
|
||||
* [Zeile] [Spalte] */
|
||||
static const fsm_full_state_t s_state_table[NR_OF_STATES] =
|
||||
{
|
||||
/* transitions nrOfTransitions defaultNxtState */
|
||||
{ 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 },
|
||||
|
||||
/* TODO */
|
||||
|
||||
{ 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 }
|
||||
};
|
||||
|
||||
/* Definition der Ausgaenge */
|
||||
static const fsm_action_t s_out_table[NR_OF_STATES] =
|
||||
{
|
||||
/* display muenz_rueck kaffee_los guthaben, display_string */
|
||||
{ false, false, false, 0, "Warten" }, /* state A */
|
||||
{ false, false, false, 1, "1 Euro" }, /* state B */
|
||||
{ false, false, false, 2, "2 Euro" }, /* state C */
|
||||
{ false, false, false, 3, "3 Euro" }, /* state D */
|
||||
|
||||
/* TODO */
|
||||
|
||||
{ false, true, false, 3, "3 Euro + M" }, /* state E */
|
||||
{ true, false, false, 4, "4 Euro" }, /* state F */
|
||||
{ true, true, false, 4, "4 Euro + M" }, /* state G */
|
||||
{ true, false, true, 0, "Kaffe kommt" }, /* state H */
|
||||
{ true, true, true, 0, "Kaffe + M" } /* state I */
|
||||
|
||||
};
|
||||
|
||||
/*--- Funktionsdefinition --------------------------------------------*/
|
||||
void automat_reset(void)
|
||||
{
|
||||
printf("---- automat_reset ----\n");
|
||||
|
||||
/* TODO go into IDLE state */
|
||||
|
||||
s_curstate = A;
|
||||
|
||||
}
|
||||
|
||||
/*--- Funktionsdefinition --------------------------------------------*/
|
||||
void automat_transition(BOOL becher, BOOL muenze, BOOL muenz_wert)
|
||||
{
|
||||
// Ausgabe der aktuellen Eingabewerte zur Fehlersuche
|
||||
printf("---- automat_transition becher(%0d) muenze(%0d) muenz_wert(%0d) ----\n",
|
||||
becher, muenze, muenz_wert);
|
||||
|
||||
// Kombiniere die Eingabewerte zu einem einzigen Integer mittels Bitoperationen.
|
||||
// Die Eingaben werden wie folgt kombiniert:
|
||||
// - 'becher' wird um 2 Bits nach links verschoben (belegt die dritte Bitposition)
|
||||
// - 'muenze' wird um 1 Bit nach links verschoben (belegt die zweite Bitposition)
|
||||
// - 'muenz_wert' bleibt in der ersten Bitposition
|
||||
// Dies ergibt einen 3-Bit-Integer, der den aktuellen Eingabestatus darstellt.
|
||||
int in = (becher << 2) | (muenze << 1) | (muenz_wert << 0);
|
||||
|
||||
// Hole die Übergangsinformationen des aktuellen Zustands aus der Zustandstabelle.
|
||||
// 's_curstate' enthält den aktuellen Zustand, und 's_state_table' liefert die
|
||||
// Übergangsdetails für jeden Zustand.
|
||||
const fsm_full_state_t * full_transition = &s_state_table[s_curstate];
|
||||
|
||||
// Hole das Array der Übergänge für den aktuellen Zustand.
|
||||
// Jeder Übergang spezifiziert eine Eingabebedingung, eine Maske und einen Folgezustand.
|
||||
const fsm_state_t * transition = full_transition->transitions;
|
||||
int i;
|
||||
|
||||
// Iteriere über alle definierten Übergänge für den aktuellen Zustand.
|
||||
for(i = 0; i < full_transition->nrOfTransitions; i++)
|
||||
{
|
||||
// Überprüfe, ob die aktuelle Eingabe die Übergangsbedingung erfüllt.
|
||||
// Die Bedingung ist erfüllt, wenn die Eingabe, maskiert durch die Übergangsmaske,
|
||||
// dem spezifizierten Eingabewert des Übergangs entspricht.
|
||||
if(transition[i].input == (in & transition[i].mask))
|
||||
{
|
||||
// Wenn ein passender Übergang gefunden wird, aktualisiere den aktuellen Zustand
|
||||
// auf den im Übergang spezifizierten Folgezustand.
|
||||
s_curstate = transition[i].nxtstate;
|
||||
// Beende die Schleife, da der Übergang erfolgreich angewendet wurde.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Wenn nach Überprüfung aller Übergänge kein passender Übergang gefunden wurde,
|
||||
// setze den aktuellen Zustand auf den Standard-Folgezustand für den aktuellen Zustand.
|
||||
// Dies behandelt Fälle, in denen die Eingabe keinem spezifischen Übergang entspricht.
|
||||
if(i == full_transition->nrOfTransitions)
|
||||
{
|
||||
s_curstate = full_transition->defaultNxtState;
|
||||
}
|
||||
}
|
||||
/*--- Funktionsdefinition --------------------------------------------*/
|
||||
fsm_action_t automat_output(void)
|
||||
{
|
||||
return s_out_table[s_curstate];
|
||||
}
|
||||
|
||||
//gcc automat.c view.c main.c io.c checker.c -o console_automat
|
||||
@@ -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 */
|
||||
@@ -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';
|
||||
}
|
||||
@@ -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
@@ -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
@@ -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__ */
|
||||
@@ -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
File diff suppressed because it is too large.
Load diff
+115
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
+41
-14
@@ -3,26 +3,53 @@
|
||||
|
||||
int main() {
|
||||
double re1, im1, re2, im2;
|
||||
printf("1. Zahl eingeben\nRealteil: "); scanf("%lf", &re1);
|
||||
printf("Imaginärteil: "); scanf("%lf", &im1);
|
||||
printf("2. Zahl eingeben\nRealteil: "); scanf("%lf", &re2);
|
||||
printf("Imaginärteil: "); scanf("%lf", &im2);
|
||||
|
||||
// Eingabe der ersten komplexen Zahl
|
||||
printf("1. Zahl eingeben\nRealteil: ");
|
||||
scanf("%lf", &re1);
|
||||
printf("Imaginärteil: ");
|
||||
scanf("%lf", &im1);
|
||||
|
||||
// Eingabe der zweiten komplexen Zahl
|
||||
printf("2. Zahl eingeben\nRealteil: ");
|
||||
scanf("%lf", &re2);
|
||||
printf("Imaginärteil: ");
|
||||
scanf("%lf", &im2);
|
||||
|
||||
// Erzeugen der komplexen Zahlen als Zeiger auf Complex-Strukturen
|
||||
Complex *x = createComplex(re1, im1);
|
||||
Complex *y = createComplex(re2, im2);
|
||||
|
||||
// Berechnung der Summe, Differenz, Produkt und Quotient
|
||||
Complex *sum = addComplex(x, y);
|
||||
Complex *diff = subtractComplex(x, y);
|
||||
Complex *prod = multiplyComplex(x, y);
|
||||
Complex *quot = divideComplex(x, y);
|
||||
printf("x = "); printComplex(x);
|
||||
printf("y = "); printComplex(y);
|
||||
printf("Summe: x + y = "); printComplex(sum);
|
||||
printf("Differenz: x - y = "); printComplex(diff);
|
||||
printf("Produkt: x * y = "); printComplex(prod);
|
||||
printf("Quotient: x / y = "); printComplex(quot);
|
||||
freeComplex(x); freeComplex(y);
|
||||
freeComplex(sum); freeComplex(diff);
|
||||
freeComplex(prod); freeComplex(quot);
|
||||
|
||||
// Ausgabe der Ergebnisse
|
||||
printf("x = ");
|
||||
printComplex(x);
|
||||
printf("y = ");
|
||||
printComplex(y);
|
||||
printf("Summe: x + y = ");
|
||||
printComplex(sum);
|
||||
printf("Differenz: x - y = ");
|
||||
printComplex(diff);
|
||||
printf("Produkt: x * y = ");
|
||||
printComplex(prod);
|
||||
printf("Quotient: x / y = ");
|
||||
printComplex(quot);
|
||||
|
||||
// Speicher wieder freigeben
|
||||
freeComplex(x);
|
||||
freeComplex(y);
|
||||
freeComplex(sum);
|
||||
freeComplex(diff);
|
||||
freeComplex(prod);
|
||||
freeComplex(quot);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//gcc TestComplex.c -L. -lcomplex -o TestComplex
|
||||
// Kompilieren mit:
|
||||
// gcc TestComplex.c -L. -lcomplex -o TestComplex
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include "matrixOp.h"
|
||||
|
||||
int main() {
|
||||
// Erstellt zwei 2x3-Matrizen mit aufsteigenden Werten
|
||||
Matrix *a = CreateMatrix(2, 3, 1.0); // 1.0, 2.0, 3.0, 4.0, 5.0, 6.0
|
||||
Matrix *b = CreateMatrix(2, 3, 7.0); // 7.0, 8.0, 9.0, 10.0, 11.0, 12.0
|
||||
|
||||
Matrix *c, *d, *e, *f;
|
||||
|
||||
printf("Matrix a:\n");
|
||||
PrintMatrix(a);
|
||||
|
||||
printf("Matrix b:\n");
|
||||
PrintMatrix(b);
|
||||
|
||||
// Addition: a + b
|
||||
c = AddMatrix(a, b);
|
||||
printf("a + b:\n");
|
||||
PrintMatrix(c);
|
||||
|
||||
// Subtraktion: a - b
|
||||
d = SubMatrix(a, b);
|
||||
printf("a - b:\n");
|
||||
PrintMatrix(d);
|
||||
|
||||
// Transponieren von a
|
||||
e = TransposeMatrix(a);
|
||||
printf("Transpose of a:\n");
|
||||
PrintMatrix(e);
|
||||
|
||||
// Matrixmultiplikation: a * a^T
|
||||
f = MulMatrix(a, e);
|
||||
printf("a * a^T:\n");
|
||||
PrintMatrix(f);
|
||||
|
||||
// Determinante der Ergebnismatrix (nur für 2x2 möglich)
|
||||
printf("det(a * a^T): %.2f\n", DetMatrix(f));
|
||||
|
||||
// Speicher wieder freigeben
|
||||
FreeMatrix(a);
|
||||
FreeMatrix(b);
|
||||
FreeMatrix(c);
|
||||
FreeMatrix(d);
|
||||
FreeMatrix(e);
|
||||
FreeMatrix(f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//gcc TestMatricOp.c -L. -lmatrixOp -o TestMatricOp
|
||||
@@ -3,38 +3,51 @@
|
||||
#include <math.h>
|
||||
#include "complex.h"
|
||||
|
||||
// Erzeugt eine neue komplexe Zahl und gibt einen Zeiger darauf zurück
|
||||
Complex* createComplex(double real, double imag) {
|
||||
Complex* z = malloc(sizeof(Complex));
|
||||
Complex* z = malloc(sizeof(Complex)); // Speicher reservieren
|
||||
if (z != NULL) {
|
||||
z->real = real;
|
||||
z->imag = imag;
|
||||
}
|
||||
return z;
|
||||
}
|
||||
|
||||
// Gibt den Speicher einer komplexen Zahl frei
|
||||
void freeComplex(Complex* z) {
|
||||
free(z);
|
||||
}
|
||||
|
||||
// Addiert zwei komplexe Zahlen und gibt das Ergebnis als neue Zahl zurück
|
||||
Complex* addComplex(const Complex* a, const Complex* b) {
|
||||
return createComplex(a->real + b->real, a->imag + b->imag);
|
||||
}
|
||||
|
||||
// Subtrahiert zwei komplexe Zahlen und gibt das Ergebnis als neue Zahl zurück
|
||||
Complex* subtractComplex(const Complex* a, const Complex* b) {
|
||||
return createComplex(a->real - b->real, a->imag - b->imag);
|
||||
}
|
||||
|
||||
// Multipliziert zwei komplexe Zahlen und gibt das Ergebnis als neue Zahl zurück
|
||||
Complex* multiplyComplex(const Complex* a, const Complex* b) {
|
||||
double real = a->real * b->real - a->imag * b->imag;
|
||||
double imag = a->real * b->imag + a->imag * b->real;
|
||||
return createComplex(real, imag);
|
||||
}
|
||||
|
||||
// Dividiert zwei komplexe Zahlen und gibt das Ergebnis als neue Zahl zurück
|
||||
Complex* divideComplex(const Complex* a, const Complex* b) {
|
||||
double denom = b->real * b->real + b->imag * b->imag;
|
||||
if (denom == 0.0) return NULL;
|
||||
double denom = b->real * b->real + b->imag * b->imag; // Nenner berechnen
|
||||
if (denom == 0.0) return NULL; // Division durch 0 abfangen
|
||||
double real = (a->real * b->real + a->imag * b->imag) / denom;
|
||||
double imag = (a->imag * b->real - a->real * b->imag) / denom;
|
||||
return createComplex(real, imag);
|
||||
}
|
||||
|
||||
// Gibt eine komplexe Zahl in der Form (a + bi) aus
|
||||
void printComplex(const Complex* z) {
|
||||
if (z)
|
||||
printf("(%.3f + %.3fi)\n", z->real, z->imag);
|
||||
else
|
||||
printf("NULL (division durch 0?)\n");
|
||||
}
|
||||
printf("NULL -> Division durch 0 nicht möglich\n");
|
||||
}
|
||||
@@ -1,14 +1,19 @@
|
||||
#ifndef COMPLEX_H
|
||||
#define COMPLEX_H
|
||||
|
||||
// Definition der Complex-Struktur für komplexe Zahlen
|
||||
typedef struct {
|
||||
double real;
|
||||
double imag;
|
||||
double real; // Realteil
|
||||
double imag; // Imaginärteil
|
||||
} Complex;
|
||||
Complex* createComplex(double real, double imag);
|
||||
void freeComplex(Complex* z);
|
||||
Complex* addComplex(const Complex* a, const Complex* b);
|
||||
Complex* subtractComplex(const Complex* a, const Complex* b);
|
||||
Complex* multiplyComplex(const Complex* a, const Complex* b);
|
||||
Complex* divideComplex(const Complex* a, const Complex* b);
|
||||
void printComplex(const Complex* z);
|
||||
#endif
|
||||
|
||||
// Funktionsprototypen für Operationen mit komplexen Zahlen
|
||||
Complex* createComplex(double real, double imag); // Erzeugt eine neue komplexe Zahl
|
||||
void freeComplex(Complex* z); // Gibt den Speicher einer komplexen Zahl frei
|
||||
Complex* addComplex(const Complex* a, const Complex* b); // Addition
|
||||
Complex* subtractComplex(const Complex* a, const Complex* b); // Subtraktion
|
||||
Complex* multiplyComplex(const Complex* a, const Complex* b); // Multiplikation
|
||||
Complex* divideComplex(const Complex* a, const Complex* b); // Division
|
||||
void printComplex(const Complex* z); // Ausgabe
|
||||
|
||||
#endif
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Prüft, ob der übergebene String nur aus '0' und '1' besteht (also eine Dualzahl ist). Gibt 1 zurück, wenn ja, sonst 0.*/
|
||||
int ist_dual(const char *s) {
|
||||
while (*s)
|
||||
if (*s != '0' && *s != '1')
|
||||
return 0; // Nicht erlaubt, also keine Dualzahl
|
||||
else
|
||||
s++; // Nächstes Zeichen prüfen
|
||||
return 1; // Passt, Dualzahl
|
||||
}
|
||||
|
||||
/*Gibt die 32-Bit-Binärdarstellung einer Zahl aus.
|
||||
Beginnt beim höchsten Bit (31) und prüft jedes Bit.
|
||||
Gibt für jedes Bit ‘1’ oder ‘0’ aus (auch führende Nullen).*/
|
||||
void print_bin(int n) {
|
||||
for (int i = 31; i >= 0; i--)
|
||||
putchar((n >> i) & 1 ? '1' : '0');
|
||||
}
|
||||
|
||||
/*Erwartet 3 Argumente:
|
||||
1. Operand (als Dualzahl, z.B. "1011")
|
||||
2. Operator (als einzelnes Zeichen: + - * / & ^)
|
||||
3. Operand (als Dualzahl)
|
||||
Führt die Operation aus und gibt das Ergebnis als Binärzahl und Hexadezimalzahl aus.*/
|
||||
int main(int argc, char *argv[]) {
|
||||
// Prüfen, ob genau 3 Argumente übergeben wurden (Programmname + 3 = 4)
|
||||
if (argc != 4) {
|
||||
printf("Richtiger Aufruf: %s <operand> <operator> <operand>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Argumente zuweisen
|
||||
/*a: erster Operand (als String)
|
||||
op: Operator (als String)
|
||||
b: zweiter Operand (als String)
|
||||
ok: Flag, ob alles gültig ist*/
|
||||
char *a = argv[1], *op = argv[2], *b = argv[3];
|
||||
int ok = 1;
|
||||
|
||||
// Prüfen, ob beide Operanden gültige Dualzahlen sind
|
||||
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;
|
||||
}
|
||||
|
||||
// Prüfen, ob der Operator ein erlaubtes Zeichen ist
|
||||
if (strlen(op) != 1 || strchr("+-*/&^", op[0]) == NULL) {
|
||||
printf("....... %s ist kein erlaubter Operator\n", op);
|
||||
ok = 0;
|
||||
}
|
||||
|
||||
// Falls etwas ungültig war, Programm beenden
|
||||
if (!ok) return 1;
|
||||
|
||||
// Dualzahlen in int umwandeln (Basis 2)
|
||||
int x = strtol(a, NULL, 2), y = strtol(b, NULL, 2), r = 0;
|
||||
|
||||
// Division durch 0 abfangen
|
||||
if (*op == '/' && y == 0) {
|
||||
printf("Fehler: Division durch 0\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Die gewünschte Operation ausführen
|
||||
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; // Bitweises UND
|
||||
case '^': r = x ^ y; break; // Bitweises XOR
|
||||
}
|
||||
|
||||
// Ergebnis ausgeben: Eingabe, Operator, Ergebnis in Binär- und Hexadezimaldarstellung
|
||||
printf("%s %s %s =\n....... ", a, op, b);
|
||||
print_bin(r);
|
||||
printf(" (0x%X)\n", r);
|
||||
|
||||
return 0;
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
/**********************************************************************\
|
||||
* Kurzbeschreibung:
|
||||
* Realisiert das Josephus Spiel zum Test einer Queue
|
||||
*
|
||||
* Datum: Autor:
|
||||
* 30.9.2015 Prof. Dr. Helmut Herold
|
||||
*
|
||||
\**********************************************************************/
|
||||
/*--- #includes ------------------------------------------------------*/
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include <stdio.h>
|
||||
#include "queue.h"
|
||||
/*--- #defines -------------------------------------------------------*/
|
||||
/*--- Datentypen (typedef) -------------------------------------------*/
|
||||
/*--- Prototypen lokaler Funktionen ----------------------------------*/
|
||||
/*--- main -----------------------------------------------------------*/
|
||||
int main(void)
|
||||
{
|
||||
int n, z, i=1, nr; // Variablen für Personenanzahl, Schrittweite, Zähler, Rückgabewert scanf
|
||||
do {
|
||||
printf("Wie viele Personen: "); // Benutzer nach Anzahl der Personen fragen
|
||||
nr = scanf("%d", &n); getchar(); // Eingabe lesen, Rückgabewert speichern, Zeilenumbruch entfernen
|
||||
} while (nr != 1 || n < 0); // Wiederholen, falls ungültige Eingabe
|
||||
|
||||
do {
|
||||
printf("Wie vielte ist auszusondern: "); // Benutzer nach Schrittweite fragen
|
||||
nr = scanf("%d", &z); getchar(); // Eingabe lesen, Rückgabewert speichern, Zeilenumbruch entfernen
|
||||
} while (nr != 1 || z < 0); // Wiederholen, falls ungültige Eingabe
|
||||
|
||||
for (i = 1; i <= n; i++) // Queue füllen
|
||||
put(i); // Personen mit Nummer 1 bis n in die Queue einfügen
|
||||
|
||||
while (!isEmpty()) { // Solange noch Personen in der Queue sind
|
||||
for (i = 1; i < z; i++) { // z-1 Personen überspringen
|
||||
if (put(get()) == FALSE) { // Erste Person aus der Queue nehmen und wieder hinten einfügen
|
||||
fprintf(stderr, "Fehler beim Einfügen\n"); // Fehlerausgabe, falls put fehlschlägt
|
||||
return 1; // Programm mit Fehlercode beenden
|
||||
}
|
||||
}
|
||||
printf("%d, ", get()); // Die z-te Person aus der Queue nehmen und ausgeben (ausscheiden)
|
||||
// Diese Person wird nicht mehr eingefügt
|
||||
}
|
||||
printf("\n"); // Zeilenumbruch nach Ausgabe aller ausgeschiedenen Personen
|
||||
return 0;
|
||||
}
|
||||
|
||||
//gcc -c queue.c -o queue.o
|
||||
//ar rcs libqueue.a queue.o
|
||||
//gcc josephus.c -L. -lqueue -o josephus
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include "matrixOp.h"
|
||||
|
||||
// Erstellt eine Matrix mit allen Einträgen 0
|
||||
Matrix* CreateMatrixZero(int rows, int cols) {
|
||||
Matrix *m = malloc(sizeof(Matrix)); // Speicher für Matrix-Struktur
|
||||
m->rows = rows;
|
||||
m->cols = cols;
|
||||
m->data = malloc(rows * sizeof(double*)); // Speicher für Zeilenzeiger
|
||||
for (int i = 0; i < rows; i++) {
|
||||
m->data[i] = calloc(cols, sizeof(double)); // Speicher für jede Zeile, initialisiert mit 0
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
// Erstellt eine Matrix, deren Einträge mit startValue beginnen und dann hochzählen
|
||||
Matrix* CreateMatrix(int rows, int cols, double startValue) {
|
||||
Matrix *m = CreateMatrixZero(rows, cols);
|
||||
double val = startValue;
|
||||
for (int i = 0; i < rows; i++) {
|
||||
for (int j = 0; j < cols; j++) {
|
||||
m->data[i][j] = val++;
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
// Gibt den Speicher einer Matrix wieder frei
|
||||
void FreeMatrix(Matrix *m) {
|
||||
for (int i = 0; i < m->rows; i++) {
|
||||
free(m->data[i]); // Jede Zeile freigeben
|
||||
}
|
||||
free(m->data); // Zeilenzeiger-Array freigeben
|
||||
free(m); // Matrix-Struktur freigeben
|
||||
}
|
||||
|
||||
// Gibt die Matrix schön formatiert auf der Konsole aus
|
||||
void PrintMatrix(Matrix *m) {
|
||||
for (int i = 0; i < m->rows; i++) {
|
||||
for (int j = 0; j < m->cols; j++) {
|
||||
printf("%6.2f ", m->data[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Addiert zwei Matrizen gleicher Größe
|
||||
Matrix* AddMatrix(Matrix *a, Matrix *b) {
|
||||
if (a->rows != b->rows || a->cols != b->cols) return NULL; // Nur gleiche Größe erlaubt
|
||||
Matrix *m = CreateMatrixZero(a->rows, a->cols);
|
||||
for (int i = 0; i < m->rows; i++) {
|
||||
for (int j = 0; j < m->cols; j++) {
|
||||
m->data[i][j] = a->data[i][j] + b->data[i][j];
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
// Subtrahiert zwei Matrizen gleicher Größe
|
||||
Matrix* SubMatrix(Matrix *a, Matrix *b) {
|
||||
if (a->rows != b->rows || a->cols != b->cols) return NULL;
|
||||
Matrix *m = CreateMatrixZero(a->rows, a->cols);
|
||||
for (int i = 0; i < m->rows; i++) {
|
||||
for (int j = 0; j < m->cols; j++) {
|
||||
m->data[i][j] = a->data[i][j] - b->data[i][j];
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
// Transponiert eine Matrix (Zeilen und Spalten vertauschen)
|
||||
Matrix* TransposeMatrix(Matrix *a) {
|
||||
Matrix *m = CreateMatrixZero(a->cols, a->rows); // Zeilen und Spalten vertauscht
|
||||
for (int i = 0; i < a->rows; i++) {
|
||||
for (int j = 0; j < a->cols; j++) {
|
||||
m->data[j][i] = a->data[i][j];
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
// Multipliziert zwei Matrizen (a: m x n, b: n x p)
|
||||
Matrix* MulMatrix(Matrix *a, Matrix *b) {
|
||||
if (a->cols != b->rows) return NULL; // Nur möglich, wenn Spalten von a == Zeilen von b
|
||||
Matrix *m = CreateMatrixZero(a->rows, b->cols);
|
||||
for (int i = 0; i < a->rows; i++) {
|
||||
for (int j = 0; j < b->cols; j++) {
|
||||
for (int k = 0; k < a->cols; k++) {
|
||||
m->data[i][j] += a->data[i][k] * b->data[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
// Berechnet die Determinante einer 2x2-Matrix
|
||||
double DetMatrix(Matrix *m) {
|
||||
if (m->rows != m->cols) return 0; // Nur quadratische Matrizen
|
||||
if (m->rows == 2) {
|
||||
// Formel: ad - bc
|
||||
return m->data[0][0] * m->data[1][1] - m->data[0][1] * m->data[1][0];
|
||||
}
|
||||
// Für größere Matrizen nicht implementiert
|
||||
return 0;
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
#ifndef MATRIX_OP_H
|
||||
#define MATRIX_OP_H
|
||||
typedef struct {
|
||||
int rows;
|
||||
int cols;
|
||||
double **data;
|
||||
} Matrix;
|
||||
Matrix* CreateMatrixZero(int rows, int cols);
|
||||
Matrix* CreateMatrixRand(int rows, int cols);
|
||||
Matrix* CreateMatrix(int rows, int cols, double startValue);
|
||||
void FreeMatrix(Matrix *m);
|
||||
void PrintMatrix(Matrix *m);
|
||||
Matrix* AddMatrix(Matrix *a, Matrix *b);
|
||||
Matrix* SubMatrix(Matrix *a, Matrix *b);
|
||||
Matrix* TransposeMatrix(Matrix *a);
|
||||
Matrix* MulMatrix(Matrix *a, Matrix *b);
|
||||
double DetMatrix(Matrix *m);
|
||||
#endif
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char *argv[]) { //Kommandozeilenargumente und Array der Argumente als strings
|
||||
// Prüfen, ob ein Dateiname als Argument übergeben wurde
|
||||
if (argc < 2) {
|
||||
printf("Bitte Dateinamen als erstes Argument angeben.\n");
|
||||
return 1; // Fehlercode zurückgeben und Programm beenden
|
||||
}
|
||||
|
||||
// Datei im Lesemodus ("r") öffnen
|
||||
FILE *datei = fopen(argv[1], "r");
|
||||
if (datei == NULL) {
|
||||
// Fehler beim Öffnen der Datei, z.B. Datei existiert nicht
|
||||
perror("Fehler beim Öffnen der Datei");
|
||||
return 1; // Fehlercode zurückgeben und Programm beenden
|
||||
}
|
||||
|
||||
char zeile[1024]; // Puffer für eine Zeile (max. 1023 Zeichen + '\0')
|
||||
int zeilennummer = 1; // Startwert für die Zeilennummerierung
|
||||
|
||||
// Solange noch Zeilen gelesen werden können
|
||||
while (fgets(zeile, sizeof(zeile), datei) != NULL) {
|
||||
// Zeilennummer und Inhalt der Zeile ausgeben
|
||||
// %4d: Zeilennummer rechtsbündig mit mindestens 4 Stellen
|
||||
printf("%4d: %s", zeilennummer, zeile);
|
||||
zeilennummer++; // Zeilennummer erhöhen
|
||||
}
|
||||
|
||||
// Datei schließen
|
||||
fclose(datei);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/**********************************************************************\
|
||||
* Kurzbeschreibung:
|
||||
* queue.c - realisiert eine Queue (Warteschlange)
|
||||
*
|
||||
* Datum: Autor:
|
||||
*
|
||||
\**********************************************************************/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "queue.h"
|
||||
|
||||
/*--- Struktur eines Listenelements ----------------------------------*/
|
||||
|
||||
/*Ein Listenelement (Node) besteht aus:
|
||||
- value: dem gespeicherten Wert (hier ein int)
|
||||
- next: Zeiger auf das nächste Element in der Liste
|
||||
*/
|
||||
typedef struct Node {
|
||||
int value;
|
||||
struct Node *next;
|
||||
} Node_t;
|
||||
|
||||
/*--- Modulglobale Variablen: Zeiger auf Kopf und Ende der Liste -----*/
|
||||
|
||||
/*head: Zeigt auf das erste Element der Queue (Kopf)
|
||||
tail: Zeigt auf das letzte Element der Queue (Ende)
|
||||
Sind beide NULL, ist die Queue leer.*/
|
||||
static Node_t *head = NULL;
|
||||
static Node_t *tail = NULL;
|
||||
|
||||
/*--- put(): Einfügen am Ende der Liste ------------------------------*/
|
||||
/*Fügt eine neue Zahl am Ende der Queue ein.
|
||||
Rückgabewert: TRUE bei Erfolg, FALSE bei Speicherfehler.*/
|
||||
Bool put(int zahl) {
|
||||
// Speicher für neues Element reservieren
|
||||
Node_t *newNode = (Node_t *)malloc(sizeof(Node_t));
|
||||
if (newNode == NULL) {
|
||||
fprintf(stderr, "Speicher konnte nicht freigegeben werden.\n");
|
||||
return FALSE; // Fehler: kein Speicher verfügbar
|
||||
}
|
||||
newNode->value = zahl; // Wert setzen
|
||||
newNode->next = NULL; // Am Ende zeigt next auf NULL
|
||||
|
||||
if (tail != NULL) {
|
||||
// Es gibt schon Elemente: neues Element ans Ende anhängen
|
||||
tail->next = newNode;
|
||||
} else {
|
||||
// Die Liste war leer: head zeigt jetzt auf das neue Element
|
||||
head = newNode;
|
||||
}
|
||||
// tail zeigt immer auf das letzte Element
|
||||
tail = newNode;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/*--- get(): Entfernt das erste Element ------------------------------*/
|
||||
|
||||
/*Entfernt das erste Element (Kopf) der Queue und gibt dessen Wert zurück.
|
||||
Ist die Queue leer, wird QLEER zurückgegeben und eine Fehlermeldung ausgegeben.*/
|
||||
int get(void) {
|
||||
if (head == NULL) {
|
||||
// Die Queue ist leer
|
||||
fprintf(stderr, "Fehler: Queue ist leer.\n");
|
||||
return QLEER;
|
||||
}
|
||||
Node_t *tmp = head; // Temporärer Zeiger auf das erste Element
|
||||
int value = tmp->value; // Wert des ersten Elements speichern
|
||||
head = head->next; // Kopf auf das nächste Element setzen
|
||||
if (head == NULL) {
|
||||
// Nach dem Entfernen ist die Queue leer, also tail auch auf NULL setzen
|
||||
tail = NULL;
|
||||
}
|
||||
free(tmp); // Speicher des entfernten Elements freigeben
|
||||
return value; // Wert zurückgeben
|
||||
}
|
||||
|
||||
/*--- isEmpty(): Prüft, ob Liste leer ist ----------------------------*/
|
||||
/*
|
||||
* Gibt TRUE zurück, wenn die Queue leer ist, sonst FALSE.
|
||||
*/
|
||||
Bool isEmpty(void) {
|
||||
return head == NULL ? TRUE : FALSE;
|
||||
}
|
||||
@@ -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 */
|
||||
Reference in new issue
Block a user