Guest User

Untitled

a guest
Jan 6th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6.  
  7. struct Stud
  8. {
  9. char name[20];
  10. int count;
  11. };
  12.  
  13. int cmpfunc(const void * a, const void * b)
  14. {
  15. const struct Stud **ia = (const struct Stud **)a;//** weil pointer auf pointer;(....**)Typecast von void zeiger
  16. const struct Stud **ib = (const struct Stud **)b;
  17. const struct Stud *sa = *ia;//pointer auf struct wird wert von struct zugewiesen
  18. const struct Stud *sb = *ib;
  19. return (*sb).count - (*sa).count;
  20. }
  21.  
  22. int main()
  23. {
  24.  
  25. FILE *datei;
  26.  
  27. struct Stud *(namept[250]);
  28. const int maxNameCount = 250;
  29.  
  30. struct Stud emptyStud = { .name = "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ", .count = 0 };
  31. struct Stud * ptrEmpty = &emptyStud;
  32. for (int i = 0; i < maxNameCount; i++)
  33. {
  34. namept[i] = ptrEmpty;
  35. }
  36.  
  37.  
  38. char InputBuffer[20];
  39. datei = fopen("vornamen.txt", "r");
  40. int k = 0;
  41. while (fscanf(datei, "%s", InputBuffer) != EOF)
  42. {
  43. bool found = false; //falls keine übereinstimmung gefundenb, bool datentyp
  44. for (int i = 0; i<maxNameCount; i++)
  45. {
  46. int cmp = strcmp((*namept[i]).name, InputBuffer);
  47. if (cmp == 0)
  48. {
  49. (*namept[i]).count++;
  50. found = true; //zeigt dass bereits name vorhanden
  51. }
  52. }
  53. if (!found)
  54. {
  55. struct Stud *ptr;
  56. ptr = malloc(sizeof(struct Stud));
  57. (*ptr).count = 1;
  58. strcpy((*ptr).name, InputBuffer);
  59. namept[k] = ptr;
  60. k++;
  61. }
  62. }
  63. fclose(datei);
  64.  
  65. qsort(namept, k, sizeof(struct Stud *), cmpfunc);
  66.  
  67.  
  68.  
  69. char str[20] = "";
  70. printf("Geben Sie Ihren Namen ein:");
  71. scanf("%s", &str);
  72. bool gefunden = false;
  73. for (int n = 0; n < maxNameCount; n++)
  74. {
  75. if (strcmp((*namept[n]).name, str) == 0)
  76. {
  77. printf("---------------------------------------------\n");
  78. printf("%s steht an %i. Stelle und ist %ix vorhanden.", str, (n + 1), (*namept[n]).count);
  79. gefunden = true;
  80. break;
  81. }
  82. }
  83.  
  84. if (!gefunden)
  85. {
  86. printf("%s ist nicht vorhanden.", str);
  87. }
  88. for (int i=0; i<k; i++)
  89. {
  90. free(namept[i]);
  91. }
  92.  
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment