From a34f4616618a0c409921a336d25f4376846b5c40 Mon Sep 17 00:00:00 2001 From: Lennart Date: Fri, 9 May 2025 11:34:08 +0200 Subject: [PATCH] Test2 --- kommaadd.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ wordrepl.c | 41 +++++++++--------------- 2 files changed, 106 insertions(+), 27 deletions(-) create mode 100644 kommaadd.c diff --git a/kommaadd.c b/kommaadd.c new file mode 100644 index 0000000..5c3ae35 --- /dev/null +++ b/kommaadd.c @@ -0,0 +1,92 @@ + +#include +#include +#include + +#define MAX 100 + +typedef struct { + long vorkomma; + long nachkomma; + int nachkomma_stellen; +} Kommazahl; + +// Funktion zum Potenzieren (10^n) +long potenz(int basis, int exponent) { + long result = 1; + for (int i = 0; i < exponent; i++) { + result *= basis; + } + return result; +} + +// Funktion zum Aufteilen der Zahl in Vor- und Nachkommateil +Kommazahl parseKommazahl(char *eingabe) { + Kommazahl z = {0, 0, 0}; + char *komma = strchr(eingabe, ','); //Überprüfung ob Zahl ein Komma besitzt + + if (komma) { + *komma = '\0'; // 0 ersetzt Komma + z.vorkomma = atol(eingabe); // Wandelt Vorkomma in Ganzzahl um + char *nachkomma_str = komma + 1; + z.nachkomma = atol(nachkomma_str); // Wandelt nachkomma in Ganzzahl um + z.nachkomma_stellen = strlen(nachkomma_str); + } else { + z.vorkomma = atol(eingabe); + z.nachkomma = 0; + z.nachkomma_stellen = 0; + } + + return z; +} + +int main() { + Kommazahl eingaben[MAX]; + int anzahl = 0; + int max_nachkomma_stellen = 0; + + printf("Gib Deine Kommazahlen ein (Abschluss mit Leerzeile)\n"); + + char zeile[100]; + while (fgets(zeile, sizeof(zeile), stdin)) { + // Entferne Zeilenumbruch + size_t len = strlen(zeile); + if (len > 0 && zeile[len - 1] == '\n') zeile[len - 1] = '\0'; + if (strlen(zeile) == 0) break; + + // Entferne Leerzeichen + for (int i = 0; zeile[i]; i++) { + if (zeile[i] == ' ') { + memmove(&zeile[i], &zeile[i + 1], strlen(&zeile[i])); + i--; + } + } + + Kommazahl k = parseKommazahl(zeile); + eingaben[anzahl] = k; + if (k.nachkomma_stellen > max_nachkomma_stellen) + max_nachkomma_stellen = k.nachkomma_stellen; + anzahl++; + } + + Kommazahl summe = {0, 0, max_nachkomma_stellen}; + + for (int i = 0; i < anzahl; i++) { + Kommazahl z = eingaben[i]; + summe.vorkomma += z.vorkomma; + + int faktor = potenz(10, max_nachkomma_stellen - z.nachkomma_stellen); + summe.nachkomma += z.nachkomma * faktor; + } + + // Übertrag + long übertrag = summe.nachkomma / potenz(10, max_nachkomma_stellen); + summe.vorkomma += übertrag; + summe.nachkomma = summe.nachkomma % potenz(10, max_nachkomma_stellen); + + printf("= %ld,%0*ld\n", summe.vorkomma, max_nachkomma_stellen, summe.nachkomma); + + return 0; +} + + diff --git a/wordrepl.c b/wordrepl.c index 2365c3d..cafa0cf 100644 --- a/wordrepl.c +++ b/wordrepl.c @@ -13,56 +13,53 @@ #include #include -#define MAX_LENGTH 101 -#define MAX_WORD_LENGTH 11 -#define MAX_EXCHANGE_WORD_LENGTH 21 -#define MAX_POSITION 10 +#define MAX_LENGTH 1001 +#define MAX_WORD_LENGTH 101 +#define MAX_EXCHANGE_WORD_LENGTH 201 +#define MAX_POSITION 100 void get_input_string(char * input ) { printf("Geben Sie einen Text mit maximal %d Zeichen ein: ", MAX_LENGTH-1); - scanf("%100[^\n]", input); + scanf("%1000[^\n]", input); while (getchar() != '\n'); } void get_word_to_replace(char*get_word) { printf("Welches Wort soll ersetzt werden? (Maximale Wortlänge: %d): ", MAX_WORD_LENGTH-1); - scanf("%10[^\n]", get_word); + scanf("%100[^\n]", get_word); while (getchar() != '\n'); } void get_new_word(char*new_word) { printf("Durch welches Wort soll dieses ersetzt werden? (Maximale Wortlänge des neuen Wortes: %d): ", MAX_EXCHANGE_WORD_LENGTH-1); - scanf("%20[^\n]", new_word); + scanf("%200[^\n]", new_word); while (getchar() != '\n'); } int find_word_positions(const char *input_sentence, const char *target_word, int position_target_word[]) { int counter_word_found = 0; - char *sentence_ptr = (char*)input_sentence; - if(strstr(sentence_ptr, target_word) == NULL) { return 0; } - do{ - char *ptr_position_target_word = strstr(sentence_ptr, target_word); + do { + char *ptr_position_target_word = strstr(sentence_ptr, target_word); int position_before_target_word = ptr_position_target_word - input_sentence - 1; int position_after_target_word = ptr_position_target_word - input_sentence + strlen(target_word); - if (isalpha(input_sentence[position_before_target_word]) || isalpha(input_sentence[position_after_target_word])) { sentence_ptr = ptr_position_target_word + strlen(target_word); } + else { position_target_word[counter_word_found] = ptr_position_target_word - input_sentence; - sentence_ptr = ptr_position_target_word + strlen(target_word); - counter_word_found++; } - }while(strstr(sentence_ptr, target_word) != NULL); + + }while(strstr(sentence_ptr, target_word) != NULL); return counter_word_found; } @@ -74,15 +71,12 @@ void replace_word_in_sentence(char *input_sentence, const char *replacement_word int last_position = 0; for (int i = 0; i < counter_word_found; i++) { - strncat(updated_sentence, input_sentence + last_position, positions_target_word[i] - last_position); - strncat(updated_sentence, replacement_word, MAX_EXCHANGE_WORD_LENGTH - 1); - last_position = positions_target_word[i] + strlen(target_word); } - strncat(updated_sentence, input_sentence + last_position, MAX_LENGTH - strlen(updated_sentence) - 1); + strncat(updated_sentence, input_sentence + last_position, MAX_LENGTH - strlen(updated_sentence) - 1); strncpy(input_sentence, updated_sentence, MAX_LENGTH - 1); input_sentence[MAX_LENGTH] = '\0'; } @@ -100,9 +94,7 @@ int main(void) { do { get_word_to_replace(target_word); - get_new_word(replacement_word); - word_found = find_word_positions(input_sentence, target_word, positions_target_word); if (word_found == 0) { @@ -111,7 +103,7 @@ int main(void) { } else { replace_word_in_sentence(input_sentence, replacement_word, target_word, positions_target_word, word_found); - printf("\n....: %s\n", input_sentence); + printf("\n....Neuer String: %s\n", input_sentence); } printf("Noch eine Einsetzung (j/n)? "); @@ -128,8 +120,3 @@ int main(void) { // Created by Lennart Pecher on 25.04.25. // - - -// -// Created by Lennart Pecher on 25.04.25. -//