Compare commits
4 Commits
122dcd5a02
...
8b4a7b9150
Author | SHA1 | Date | |
---|---|---|---|
8b4a7b9150 | |||
5024214330 | |||
a34f461661 | |||
5bed228605 |
10
farben.c
10
farben.c
@ -5,11 +5,7 @@
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
const char *farben[] = { "Gruen", "Rot", "Violett" };
|
|
||||||
|
|
||||||
|
|
||||||
const char *mischungen[SIZE][SIZE] = {
|
const char *mischungen[SIZE][SIZE] = {
|
||||||
{"Gruen", "Gelb", "Blau"},
|
|
||||||
{"Gelb", "Rot", "Purpur"},
|
{"Gelb", "Rot", "Purpur"},
|
||||||
{"Blau", "Purpur", "Violett"}
|
{"Blau", "Purpur", "Violett"}
|
||||||
};
|
};
|
||||||
@ -17,9 +13,7 @@ int main() {
|
|||||||
char eingabe1[20], eingabe2[20];
|
char eingabe1[20], eingabe2[20];
|
||||||
int index1 = -1, index2 = -1;
|
int index1 = -1, index2 = -1;
|
||||||
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
printf("Erste Grundfarbe (Gruen, Rot, Violett): ");
|
|
||||||
scanf("%s", eingabe1);
|
scanf("%s", eingabe1);
|
||||||
|
|
||||||
for (int i = 0; i < SIZE; i++) {
|
for (int i = 0; i < SIZE; i++) {
|
||||||
@ -33,9 +27,7 @@ int main() {
|
|||||||
else printf("... unbekannte Grundfarbe '%s'. Bitte erneut eingeben.\n", eingabe1);
|
else printf("... unbekannte Grundfarbe '%s'. Bitte erneut eingeben.\n", eingabe1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
printf("Zweite Grundfarbe (Gruen, Rot, Violett): ");
|
|
||||||
scanf("%s", eingabe2);
|
scanf("%s", eingabe2);
|
||||||
|
|
||||||
for (int i = 0; i < SIZE; i++) {
|
for (int i = 0; i < SIZE; i++) {
|
||||||
@ -49,10 +41,8 @@ int main() {
|
|||||||
else printf("... unbekannte Grundfarbe '%s'. Bitte erneut eingeben.\n", eingabe2);
|
else printf("... unbekannte Grundfarbe '%s'. Bitte erneut eingeben.\n", eingabe2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const char *ergebnis = mischungen[index1][index2];
|
const char *ergebnis = mischungen[index1][index2];
|
||||||
printf("Die Mischung aus %s und %s ergibt: %s\n", farben[index1], farben[index2], ergebnis);
|
printf("Die Mischung aus %s und %s ergibt: %s\n", farben[index1], farben[index2], ergebnis);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
92
kommaadd.c
Normal file
92
kommaadd.c
Normal 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
90
kommahinzufügen.c
Normal 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;
|
||||||
|
}
|
||||||
|
|
39
wordrepl.c
39
wordrepl.c
@ -13,55 +13,52 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
#define MAX_LENGTH 101
|
#define MAX_LENGTH 1001
|
||||||
#define MAX_WORD_LENGTH 11
|
#define MAX_WORD_LENGTH 101
|
||||||
#define MAX_EXCHANGE_WORD_LENGTH 21
|
#define MAX_EXCHANGE_WORD_LENGTH 201
|
||||||
#define MAX_POSITION 10
|
#define MAX_POSITION 100
|
||||||
|
|
||||||
void get_input_string(char * input ) {
|
void get_input_string(char * input ) {
|
||||||
printf("Geben Sie einen Text mit maximal %d Zeichen ein: ", MAX_LENGTH-1);
|
printf("Geben Sie einen Text mit maximal %d Zeichen ein: ", MAX_LENGTH-1);
|
||||||
scanf("%100[^\n]", input);
|
scanf("%1000[^\n]", input);
|
||||||
while (getchar() != '\n');
|
while (getchar() != '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_word_to_replace(char*get_word) {
|
void get_word_to_replace(char*get_word) {
|
||||||
printf("Welches Wort soll ersetzt werden? (Maximale Wortlänge: %d): ", MAX_WORD_LENGTH-1);
|
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');
|
while (getchar() != '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_new_word(char*new_word) {
|
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);
|
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');
|
while (getchar() != '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
int find_word_positions(const char *input_sentence, const char *target_word, int position_target_word[]) {
|
int find_word_positions(const char *input_sentence, const char *target_word, int position_target_word[]) {
|
||||||
|
|
||||||
int counter_word_found = 0;
|
int counter_word_found = 0;
|
||||||
|
|
||||||
char *sentence_ptr = (char*)input_sentence;
|
char *sentence_ptr = (char*)input_sentence;
|
||||||
|
|
||||||
if(strstr(sentence_ptr, target_word) == NULL) {
|
if(strstr(sentence_ptr, target_word) == NULL) {
|
||||||
return 0;
|
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_before_target_word = ptr_position_target_word - input_sentence - 1;
|
||||||
|
|
||||||
int position_after_target_word = ptr_position_target_word - input_sentence + strlen(target_word);
|
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])) {
|
if (isalpha(input_sentence[position_before_target_word]) || isalpha(input_sentence[position_after_target_word])) {
|
||||||
sentence_ptr = ptr_position_target_word + strlen(target_word);
|
sentence_ptr = ptr_position_target_word + strlen(target_word);
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
position_target_word[counter_word_found] = ptr_position_target_word - input_sentence;
|
position_target_word[counter_word_found] = ptr_position_target_word - input_sentence;
|
||||||
|
|
||||||
sentence_ptr = ptr_position_target_word + strlen(target_word);
|
sentence_ptr = ptr_position_target_word + strlen(target_word);
|
||||||
|
|
||||||
counter_word_found++;
|
counter_word_found++;
|
||||||
}
|
}
|
||||||
|
|
||||||
}while(strstr(sentence_ptr, target_word) != NULL);
|
}while(strstr(sentence_ptr, target_word) != NULL);
|
||||||
|
|
||||||
return counter_word_found;
|
return counter_word_found;
|
||||||
@ -74,15 +71,12 @@ void replace_word_in_sentence(char *input_sentence, const char *replacement_word
|
|||||||
int last_position = 0;
|
int last_position = 0;
|
||||||
|
|
||||||
for (int i = 0; i < counter_word_found; i++) {
|
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, input_sentence + last_position, positions_target_word[i] - last_position);
|
||||||
|
|
||||||
strncat(updated_sentence, replacement_word, MAX_EXCHANGE_WORD_LENGTH - 1);
|
strncat(updated_sentence, replacement_word, MAX_EXCHANGE_WORD_LENGTH - 1);
|
||||||
|
|
||||||
last_position = positions_target_word[i] + strlen(target_word);
|
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);
|
strncpy(input_sentence, updated_sentence, MAX_LENGTH - 1);
|
||||||
input_sentence[MAX_LENGTH] = '\0';
|
input_sentence[MAX_LENGTH] = '\0';
|
||||||
}
|
}
|
||||||
@ -100,9 +94,7 @@ int main(void) {
|
|||||||
|
|
||||||
do {
|
do {
|
||||||
get_word_to_replace(target_word);
|
get_word_to_replace(target_word);
|
||||||
|
|
||||||
get_new_word(replacement_word);
|
get_new_word(replacement_word);
|
||||||
|
|
||||||
word_found = find_word_positions(input_sentence, target_word, positions_target_word);
|
word_found = find_word_positions(input_sentence, target_word, positions_target_word);
|
||||||
|
|
||||||
if (word_found == 0) {
|
if (word_found == 0) {
|
||||||
@ -111,7 +103,7 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
replace_word_in_sentence(input_sentence, replacement_word, target_word, positions_target_word, word_found);
|
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)? ");
|
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.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
// Created by Lennart Pecher on 25.04.25.
|
|
||||||
//
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user