Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <stdbool.h>
- struct Stud
- {
- char name[20];
- int count;
- };
- int cmpfunc(const void * a, const void * b)
- {
- const struct Stud **ia = (const struct Stud **)a;//** weil pointer auf pointer;(....**)Typecast von void zeiger
- const struct Stud **ib = (const struct Stud **)b;
- const struct Stud *sa = *ia;//pointer auf struct wird wert von struct zugewiesen
- const struct Stud *sb = *ib;
- return (*sb).count - (*sa).count;
- }
- int main()
- {
- FILE *datei;
- struct Stud *(namept[250]);
- const int maxNameCount = 250;
- struct Stud emptyStud = { .name = "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ", .count = 0 };
- struct Stud * ptrEmpty = &emptyStud;
- for (int i = 0; i < maxNameCount; i++)
- {
- namept[i] = ptrEmpty;
- }
- char InputBuffer[20];
- datei = fopen("vornamen.txt", "r");
- int k = 0;
- while (fscanf(datei, "%s", InputBuffer) != EOF)
- {
- bool found = false; //falls keine übereinstimmung gefundenb, bool datentyp
- for (int i = 0; i<maxNameCount; i++)
- {
- int cmp = strcmp((*namept[i]).name, InputBuffer);
- if (cmp == 0)
- {
- (*namept[i]).count++;
- found = true; //zeigt dass bereits name vorhanden
- }
- }
- if (!found)
- {
- struct Stud *ptr;
- ptr = malloc(sizeof(struct Stud));
- (*ptr).count = 1;
- strcpy((*ptr).name, InputBuffer);
- namept[k] = ptr;
- k++;
- }
- }
- fclose(datei);
- qsort(namept, k, sizeof(struct Stud *), cmpfunc);
- char str[20] = "";
- printf("Geben Sie Ihren Namen ein:");
- scanf("%s", &str);
- bool gefunden = false;
- for (int n = 0; n < maxNameCount; n++)
- {
- if (strcmp((*namept[n]).name, str) == 0)
- {
- printf("---------------------------------------------\n");
- printf("%s steht an %i. Stelle und ist %ix vorhanden.", str, (n + 1), (*namept[n]).count);
- gefunden = true;
- break;
- }
- }
- if (!gefunden)
- {
- printf("%s ist nicht vorhanden.", str);
- }
- for (int i=0; i<k; i++)
- {
- free(namept[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment