From be0e538ff408889a93be82bf391d215765defc7a Mon Sep 17 00:00:00 2001 From: alexchilab Date: Wed, 21 May 2025 11:53:14 +0200 Subject: [PATCH] Program: 28.5.2.c --- 26.1.c | 96 ++++++++++++++++++++++++++++++++ 27.2.c | 2 +- 28.4.4.c | 114 ++++++++++++++++++++++++++++++++++++++ 28.6.2.c | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 28.6.c | 132 ++++++++++++++++++++++++++++++++++++++++++++ 29.1.c | 73 +++++++++++++++++++++++++ 30.1.3.c | 61 ++++++++++++++------- 7 files changed, 621 insertions(+), 20 deletions(-) create mode 100644 26.1.c create mode 100644 28.4.4.c create mode 100644 28.6.2.c create mode 100644 28.6.c create mode 100644 29.1.c diff --git a/26.1.c b/26.1.c new file mode 100644 index 0000000..6848e33 --- /dev/null +++ b/26.1.c @@ -0,0 +1,96 @@ +#include +#include + +long int ZahlBinaer(int num) +{ + int sum = 0; + for (int mult = 1, mod = 2; num > 0; mult *= 10, mod *= 2) + { + if (num % mod) + { + sum += mult; + num -= mod / 2; + } + } + return sum; +} +int ZahlOktal(int num) +{ + int sum = 0; + for (int mult = 1, mod = 8, div = 1; num > 0; mult *= 10, mod *= 8, div *= 8) + { + if (num % mod) + { + sum += (num % mod / div) * mult; + num -= num % mod; + } + } + return sum; +} +int main(int argc, char *argv[]) +{ + int doBin = 0; + int doHex = 0; + int doOct = 0; + int num = 0; + + for (int i = 1; i < argc; i++) + { + if (argv[i][0] != '-') + { + if (num == 0) + { + num = atoi(argv[i]); + printf("Dezimal: %d\n", num); + if (argc == 2) + { + doBin = 1, doHex = 1, doOct = 1; + } + } + else + { + printf("Nur eine Zahl ist erlaubt!\n\n"); + printf("Nur folgende Parameter sind erlaubt:\n--b Binaersystem\n--o Oktalsystem\n--h Hexadezimalsystem"); + return 0; + } + } + else + { + int j = 2; + while (argv[i][j] != '\0') + { + if (argv[i][j] == 'b') + { + doBin = 1; + } + else if (argv[i][j] == 'o') + { + doOct = 1; + } + else if (argv[i][j] == 'h') + { + doHex = 1; + } + else + { + printf("Unerlaubte Option: --%c\n", argv[i][j]); + return 0; + } + j++; + } + } + } + if (doBin) + { + printf("Binär: %ld\n", ZahlBinaer(num)); + } + if (doHex) + { + printf("Hexadezimal: %X\n", num); + } + if (doOct) + { + printf("Oktal: %d\n", ZahlOktal(num)); + } + return 1; +} \ No newline at end of file diff --git a/27.2.c b/27.2.c index 626a808..ebd2f6b 100644 --- a/27.2.c +++ b/27.2.c @@ -1,7 +1,7 @@ #include #include //malloc und free -void ausgabe_liste(const char **listen) //Array von (listen) als Parameter +void ausgabe_liste(const char **listen) //Zeiger auf Array von (listen) als Parameter { for (int i = 0; i < 10; i++) //Schleife geht von 0-9 - Gibt jedes Element des Arrays mit Position aus { diff --git a/28.4.4.c b/28.4.4.c new file mode 100644 index 0000000..8042b56 --- /dev/null +++ b/28.4.4.c @@ -0,0 +1,114 @@ +#include +#include + +#define ANZ_SCHUELER 30 + +typedef struct +{ + char name[10]; + char vorname[10]; + unsigned note; +} Person; + +int eingabe(Person *p, int i) +{ + printf("---Eingabe des %d. Schuelers----\n", i); + printf("Name: "); + fgets(p->name, sizeof(p->name), stdin); + if (p->name[strlen(p->name) - 1] == '\n') + { + p->name[strlen(p->name) - 1] = '\0'; + } + + if (strlen(p->name) == 0) + { + return 0; + } + + printf("Vorname: "); + fgets(p->vorname, sizeof(p->vorname), stdin); + if (p->vorname[strlen(p->vorname) - 1] == '\n') + { + p->vorname[strlen(p->vorname) - 1] = '\0'; + } + + if (strlen(p->vorname) == 0) + { + return 0; + } + + printf("Note: "); + if (scanf("%u", &p->note) != 1 || p->note < 0 || p->note > 6) + { + return 0; + } + while (getchar() != '\n'); + + return 1; +} + +float durchschnittsnote(Person *n, int anzahl) +{ + float sum = 0; + float durchschnitt; + for (int i = 0; i < anzahl; ++i) + { + sum += n[i].note; + } + durchschnitt = sum / anzahl; + return durchschnitt; + +} + +void notenspiegel(Person *n, int anzahl) +{ + int notenHaeufigkeit[6] = {0}; + + for (int i = 0; i < anzahl; ++i) + { + notenHaeufigkeit[n[i].note - 1]++; + } + + printf("\nNotenspiegel:\n"); + for (int i = 0; i < 6; ++i) + { + printf("Note %d: ", i + 1); + for (int j = 0; j < notenHaeufigkeit[i]; ++j) + { + printf("*"); + } + printf("\n"); + } +} + +void printListe(Person *liste, int anzahl) +{ + printf("Name \t\t ,Vorname \t\t ,Note "); + printf("\n------------------------------------------------------------------------\n"); + for (int i = 0; i < anzahl; ++i) + { + if (strlen(liste[i].name) > 0) + { + printf("%s \t\t ,%s \t\t ,%u\n", liste[i].name, liste[i].vorname, liste[i].note); + } + } +} + +int main() +{ + Person schueler[ANZ_SCHUELER]; + int i; + + for (i = 0; i < ANZ_SCHUELER; ++i) + { + if (!eingabe(&schueler[i], i + 1)) + { + break; + } + } + + printListe(schueler, i); + printf("Durchschnittsnote = %2.f ", durchschnittsnote(schueler, i)); + notenspiegel(schueler,i); + return 0; +} diff --git a/28.6.2.c b/28.6.2.c new file mode 100644 index 0000000..58cb999 --- /dev/null +++ b/28.6.2.c @@ -0,0 +1,163 @@ +#include +#include +#include + +typedef struct Eintrag +{ + char name[25]; + char tel[15]; + struct Eintrag *naechster_Name; + struct Eintrag *naechste_Tel; + struct Eintrag *vorheriger_Name; + struct Eintrag *vorheriger_Tel; +} Eintrag; + + +void einfuegen(Eintrag **Anfang_Name, Eintrag **Anfang_Tel, char name[], char tel[]) +{ + Eintrag *neuerEintrag = (Eintrag *)malloc(sizeof(Eintrag)); + strcpy(neuerEintrag->name, name); + strcpy(neuerEintrag->tel, tel); + neuerEintrag->naechster_Name = NULL; + neuerEintrag->naechste_Tel = NULL; + + Eintrag *aktuellerEintrag_Name = *Anfang_Name; + Eintrag *vorherigerEintrag_Name = NULL; + + while (aktuellerEintrag_Name != NULL && strcmp(aktuellerEintrag_Name->name, neuerEintrag->name) < 0) + { + vorherigerEintrag_Name = aktuellerEintrag_Name; + aktuellerEintrag_Name = aktuellerEintrag_Name->naechster_Name; + } + if (vorherigerEintrag_Name == NULL) + { + neuerEintrag->naechster_Name = *Anfang_Name; + *Anfang_Name = neuerEintrag; + } + else + { + neuerEintrag->naechster_Name = aktuellerEintrag_Name; + vorherigerEintrag_Name->naechster_Name = neuerEintrag; + } + if (neuerEintrag->naechster_Name != NULL) + { + neuerEintrag->naechster_Name->vorheriger_Name = neuerEintrag; + } + Eintrag *aktuellerEintrag_tel = *Anfang_Tel; + Eintrag *vorherigerEintrag_tel = NULL; + + while (aktuellerEintrag_tel != NULL && strcmp(aktuellerEintrag_tel->tel, neuerEintrag->tel) < 0) + { + vorherigerEintrag_tel = aktuellerEintrag_tel; + aktuellerEintrag_tel = aktuellerEintrag_tel->naechste_Tel; + } + if (vorherigerEintrag_tel == NULL) + { + neuerEintrag->naechste_Tel = *Anfang_Tel; + *Anfang_Tel = neuerEintrag; + } + else + { + neuerEintrag->naechste_Tel = aktuellerEintrag_tel; + vorherigerEintrag_tel->naechste_Tel = neuerEintrag; + } + + if (neuerEintrag->naechste_Tel != NULL) + { + neuerEintrag->naechste_Tel->vorheriger_Tel = neuerEintrag; + } +} + +void ausgebenNachName(Eintrag *Anfang_Name) +{ + printf("....Liste nach Namen sortiert..........\n"); + while (Anfang_Name != NULL) + { + printf("%s \t (Tel. %s)\n", Anfang_Name->name, Anfang_Name->tel); + Anfang_Name = Anfang_Name->naechster_Name; + } +} + +void ausgebenNachTel(Eintrag *Anfang_Tel) +{ + printf("....Liste nach Telefonnummern sortiert..........\n"); + + while (Anfang_Tel != NULL) + { + printf("(Tel. %s) \t %s\n", Anfang_Tel->tel, Anfang_Tel->name); + Anfang_Tel = Anfang_Tel->naechste_Tel; + } + + printf("\n"); +} + +void speicherfreigeben(Eintrag *Anfang_Name) +{ + Eintrag *aktuellerEintrag = Anfang_Name; + Eintrag *naechsterEintrag; + + while (aktuellerEintrag != NULL) + { + naechsterEintrag = aktuellerEintrag->naechster_Name; + free(aktuellerEintrag); + aktuellerEintrag = naechsterEintrag; + } +} + +int main() +{ + struct Eintrag *head_name = NULL; + struct Eintrag *head_telefon = NULL; + + char name[50]; + char telefonnummer[15]; + char eingabe; + + do + { + printf("Namens- und Telefonliste\n"); + printf("========================\n"); + printf("N Einfügen neuer Namen mit Telefonnummer\n"); + printf("A Ausgeben der Liste\n"); + printf("E Ende Deine Wahl: "); + if (scanf(" %c", &eingabe) != 1) + { + return 0; + } + getchar(); + + if (eingabe == 'N' || eingabe == 'n') + { + while (1) + { + printf("Name: "); + fgets(name, sizeof(name), stdin); + if (name[0] == '\n') + { + break; + } + if (name[strlen(name) - 1] == '\n') + { + name[strlen(name) - 1] = '\0'; + } + printf("Tel: "); + fgets(telefonnummer, sizeof(telefonnummer), stdin); + if (telefonnummer[strlen(telefonnummer) - 1] == '\n') + { + telefonnummer[strlen(telefonnummer) - 1] = '\0'; + } + einfuegen(&head_name, &head_telefon, name, telefonnummer); + } + } + + if (eingabe == 'A' || eingabe == 'a') + { + ausgebenNachName(head_name); + ausgebenNachTel(head_telefon); + } + printf("\n"); + + } while (eingabe != 'E' && eingabe != 'e'); + + return 0; +} diff --git a/28.6.c b/28.6.c new file mode 100644 index 0000000..d222491 --- /dev/null +++ b/28.6.c @@ -0,0 +1,132 @@ +#include +#include +#include + +#define LEN 100 + +typedef struct listElement { + char name[LEN]; + struct listElement *next; +} listElement; + +typedef struct { + listElement *head; +} liste; + +listElement *eingabeName() { + listElement *name = malloc(sizeof(listElement)); + if (name == NULL) { + printf("Speicher konnte nicht zugewiesen werden.\n"); + exit(EXIT_FAILURE); + } + + printf("Name: "); + fgets(name->name, LEN, stdin); + name->next = NULL; + + if (strcmp(name->name, "\n") == 0) { + free(name); + return NULL; + } + + if (name->name[strlen(name->name) - 1] == '\n') { + name->name[strlen(name->name) - 1] = '\0'; + } + + return name; +} + +void einfuegenName(listElement *newElement, liste *liste1, liste *liste2) { + int index; + printf("\tWelche Liste (1 oder 2): "); + if (scanf("%d", &index) != 1 || index < 1 || index > 2) { + printf("Falsche Eingabe.\n"); + exit(EXIT_FAILURE); + } + getchar(); + + listElement **targetList = (index == 1) ? &liste1->head : &liste2->head; + + listElement *temp = *targetList; + if (temp == NULL) { + *targetList = newElement; + } else { + while (temp->next != NULL) { + temp = temp->next; + } + temp->next = newElement; + } +} + +void sortiereListe(liste *lst) { + if (!lst->head) return; + listElement *current, *index; + char temp[LEN]; + + for (current = lst->head; current->next != NULL; current = current->next) { + for (index = current->next; index != NULL; index = index->next) { + if (strcmp(current->name, index->name) > 0) { + strcpy(temp, current->name); + strcpy(current->name, index->name); + strcpy(index->name, temp); + } + } + } +} + +void mergeLists(liste *liste1, liste *liste2, liste *mergedList) { + listElement *temp; + + for (temp = liste1->head; temp != NULL; temp = temp->next) { + listElement *newElement = malloc(sizeof(listElement)); + strcpy(newElement->name, temp->name); + newElement->next = mergedList->head; + mergedList->head = newElement; + } + + for (temp = liste2->head; temp != NULL; temp = temp->next) { + listElement *newElement = malloc(sizeof(listElement)); + strcpy(newElement->name, temp->name); + newElement->next = mergedList->head; + mergedList->head = newElement; + } +} + +void printliste(liste *lst) { + listElement *temp = lst->head; + while (temp != NULL) { + printf("%s\n", temp->name); + temp = temp->next; + } +} + +int main() { + liste liste1 = {NULL}; + liste liste2 = {NULL}; + liste gemischteListe = {NULL}; + + listElement *name; + while ((name = eingabeName()) != NULL) { + einfuegenName(name, &liste1, &liste2); + } + + sortiereListe(&liste1); + sortiereListe(&liste2); + + mergeLists(&liste1, &liste2, &gemischteListe); + sortiereListe(&gemischteListe); + + printf("\n......1. Liste......\n"); + printliste(&liste1); + printf("....Listenende....\n"); + + printf("\n......2. Liste......\n"); + printliste(&liste2); + printf("....Listenende....\n"); + + printf("\n....Gemischte Liste....\n"); + printliste(&gemischteListe); + printf("....Listenende....\n"); + + return 0; +}} \ No newline at end of file diff --git a/29.1.c b/29.1.c new file mode 100644 index 0000000..98db553 --- /dev/null +++ b/29.1.c @@ -0,0 +1,73 @@ +#include +#include + +// Aufzählung der Säuren +typedef enum +{ + Salzsaure, + Schwefelsaure, + Salpetersaure, + Kohlensaure +} Saeure; + +// Aufzählung der Laugen +typedef enum +{ + Natronlauge, + Kalilauge, + Kalkwasser +} Lauge; + +// Namen der Säuren für die Ausgabe +char *Saeuren[] = {"Salzsäure", "Schwefelsäure", "Salpetersäure", "Kohlensäure"}; + +// Namen der Laugen für die Ausgabe +char *Laugen[] = {"Natronlauge", "Kalilauge", "Kalkwasser"}; + +// Ergebnisse der Reaktionen (Laugen x Säuren = 3 x 4 = 12 Einträge) +char *Ausgabe[] = { + " Natriumchlorid", " Natriumsulfat", " Natriumnitrat", " Natriumcarbonat", + " Kaliumchlorid", " Kaliumsulfat", " Kaliumnitrat", " Kaliumcarbonat", + " Calciumchlorid", " Calciumsulfat", " Calciumnitrat", " Calciumcarbonat" +}; + +int main() +{ + Saeure b; + Lauge a; + + // Tabellenkopf: Säuren als Spaltenüberschriften + printf("\t \t"); + printf("|| "); + + for (b = Salzsaure; b < Kohlensaure; b++) + { + printf(" %s", Saeuren[b]); // Säurename ausgeben + printf("\t"); + printf("|"); + } + printf(" %s |", Saeuren[3]); // Letzte Säure extra ausgeben (weil Schleife nur bis <) + printf("\n"); + + // Trennlinie unter dem Kopf + printf("---------------------------------------------------------------------------------|"); + + // Für jede Lauge (Zeilen) + for (a = Natronlauge; a <= Kalkwasser; ++a) + { + printf("\n"); + printf("%s\t||", Laugen[a]); // Laugenname links am Rand + + // Für jede Säure (Spalten) + for (b = Salzsaure; b <= Kohlensaure; ++b) + { + // Index berechnen: (Lauge * 4) + Säure + // 4 Säuren pro Lauge = 4 Spalten pro Zeile + printf(Ausgabe[b + a * 4]); // Reaktionsprodukt ausgeben + printf("| "); + } + printf("\n"); + } + + return 0; +} \ No newline at end of file diff --git a/30.1.3.c b/30.1.3.c index fcaaca5..fa649d7 100644 --- a/30.1.3.c +++ b/30.1.3.c @@ -3,73 +3,96 @@ #define MAXLEN 50 + void getstring(char *ptr) { - fgets(ptr, MAXLEN, stdin); - ptr[strcspn(ptr, "\n")] = '\0'; - fflush(stdin); + fgets(ptr, MAXLEN, stdin); // Liest bis zu MAXLEN Zeichen aus der Standardeingabe + ptr[strcspn(ptr, "\n")] = '\0'; // Ersetzt das '\n' Zeichen durch '\0' (String-Ende) + fflush(stdin); // Leert den Eingabepuffer } +// Funktion zur Eingabe der Adressdaten +// Gibt die Anzahl der gewünschten Wiederholungen zurück oder 0 bei fehlerhafter Eingabe int eingabe(char *vorname, char *nachname, char *strasse, char *wohnort, char *hausnr, int *postleitzahl, char *telefon, char *fax) { printf("Vorname\t\t:"); getstring(vorname); + printf("Nachname\t:"); getstring(nachname); + printf("Strasse\t\t:"); getstring(strasse); + printf("Hausnummer\t:"); getstring(hausnr); + printf("Postleitzahl\t:"); - if(scanf("%d", postleitzahl)!=1) + // Überprüft die Eingabe auf einen gültigen Integer + if(scanf("%d", postleitzahl) != 1) { - return 0; + return 0; // Fehler bei der Eingabe } - fflush(stdin); + + fflush(stdin); // Eingabepuffer leeren + printf("Wohnort\t\t:"); getstring(wohnort); + printf("Telefon\t\t:"); getstring(telefon); + printf("Fax\t\t:"); getstring(fax); + printf("Wie oft soll die Adresse in die Datei geschrieben werden?\t:"); int anzahl; - if(scanf("%d", &anzahl)!=1) + // Überprüft die Eingabe auf einen gültigen Integer + if(scanf("%d", &anzahl) != 1) { - return 0; + return 0; // Fehler bei der Eingabe } - return anzahl; + + return anzahl; // Gibt die Anzahl der Wiederholungen zurück } int main() { + // Öffnet die Datei "datei.txt" im Anhangmodus ("a") FILE *ziel; ziel = fopen("datei.txt", "a"); + // Überprüft, ob die Datei erfolgreich geöffnet wurde if (ziel == NULL) { printf("Fehler beim Öffnen der Datei\n"); - return 1; + return 1; // Fehlercode 1: Datei konnte nicht geöffnet werden } + // Definition der Variablen für die Adressdaten char vorname[MAXLEN], nachname[MAXLEN], strasse[MAXLEN], wohnort[MAXLEN], hausnr[MAXLEN], telefon[MAXLEN], fax[MAXLEN]; int postleitzahl, anzahl; - if((anzahl = eingabe(vorname, nachname, strasse, wohnort, hausnr, &postleitzahl, telefon, fax))==0) + // Ruft die Eingabefunktion auf und prüft, ob die Eingabe erfolgreich war + if((anzahl = eingabe(vorname, nachname, strasse, wohnort, hausnr, &postleitzahl, telefon, fax)) == 0) { - return 0; + return 0; // Fehlerhafte Eingabe, Programm wird beendet } + // Schreibt einen Trenner in die Datei fprintf(ziel, "-----------------------------------------------------\n"); + + // Schleife zum mehrfachen Schreiben der Daten in die Datei for (int i = 0; i < anzahl; i++) { - fprintf(ziel, "%s %s\n", vorname, nachname); - fprintf(ziel, "%s %s\n", strasse, hausnr); - fprintf(ziel, "%d %s\n\n", postleitzahl, wohnort); - fprintf(ziel, "Tel. %s\n", telefon); - fprintf(ziel, "Fax %s\n", fax); + fprintf(ziel, "%s %s\n", vorname, nachname); // Vorname und Nachname + fprintf(ziel, "%s %s\n", strasse, hausnr); // Straße und Hausnummer + fprintf(ziel, "%d %s\n\n", postleitzahl, wohnort); // Postleitzahl und Wohnort + fprintf(ziel, "Tel. %s\n", telefon); // Telefonnummer + fprintf(ziel, "Fax %s\n", fax); // Faxnummer fprintf(ziel, "-----------------------------------------------------\n"); } - fclose(ziel); - return 0; + + fclose(ziel); // Schließt die Datei + return 0; // Programmende } \ No newline at end of file