This commit is contained in:
Lennart Pecher 2025-05-09 11:34:08 +02:00
parent 5bed228605
commit a34f461661
2 changed files with 106 additions and 27 deletions

92
kommaadd.c Normal file
View File

@ -0,0 +1,92 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}

View File

@ -13,56 +13,53 @@
#include <string.h>
#include <ctype.h>
#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.
//