Compare commits

...

4 Commits

Author SHA1 Message Date
8b4a7b9150 Test2 2025-05-09 11:37:13 +02:00
5024214330 Merge remote-tracking branch 'origin/main'
# Conflicts:
#	farben.c
2025-05-09 11:34:44 +02:00
a34f461661 Test2 2025-05-09 11:34:08 +02:00
5bed228605 Test2 2025-04-30 18:04:32 +02:00
4 changed files with 198 additions and 39 deletions

View File

@ -5,21 +5,15 @@
int main() {
const char *farben[] = { "Gruen", "Rot", "Violett" };
const char *mischungen[SIZE][SIZE] = {
{"Gruen", "Gelb", "Blau"},
{"Gelb", "Rot", "Purpur"},
{"Blau", "Purpur", "Violett"}
{"Gelb", "Rot", "Purpur"},
{"Blau", "Purpur", "Violett"}
};
char eingabe1[20], eingabe2[20];
int index1 = -1, index2 = -1;
while (1) {
printf("Erste Grundfarbe (Gruen, Rot, Violett): ");
scanf("%s", eingabe1);
for (int i = 0; i < SIZE; i++) {
@ -33,9 +27,7 @@ int main() {
else printf("... unbekannte Grundfarbe '%s'. Bitte erneut eingeben.\n", eingabe1);
}
while (1) {
printf("Zweite Grundfarbe (Gruen, Rot, Violett): ");
scanf("%s", eingabe2);
for (int i = 0; i < SIZE; i++) {
@ -49,10 +41,8 @@ int main() {
else printf("... unbekannte Grundfarbe '%s'. Bitte erneut eingeben.\n", eingabe2);
}
const char *ergebnis = mischungen[index1][index2];
printf("Die Mischung aus %s und %s ergibt: %s\n", farben[index1], farben[index2], ergebnis);
return 0;
}

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;
}

90
kommahinzufügen.c Normal file
View File

@ -0,0 +1,90 @@
#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.
//