Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX 5000
  5. typedef struct{
  6. char nume[35];
  7. float nota;
  8. }student;
  9.  
  10. void quicksort(student st[], int s, int d) {
  11. int i = s;
  12. int j = d;
  13. student x = st[(s + d) / 2];
  14. do{
  15. while(strcmp(st[i].nume, x.nume) < 0) i++;
  16. while(strcmp(st[j].nume, x.nume) > 0) j--;
  17. if(i <= j) {
  18. student temp = st[i];
  19. st[i] = st[j];
  20. st[j] = temp;
  21. i++;
  22. j--;
  23. }
  24. }
  25. while(i <= j);
  26. if(s < j) quicksort(st, s, j);
  27. if(d > i) quicksort(st, i ,d);
  28. }
  29.  
  30. void printStructure(student st[], int n) {
  31. for(int i = 0; i < n; i++) {
  32. printf("%s\t%f", st[i].nume, st[i].nota);
  33. printf("\n");
  34. }
  35. }
  36. int main() {
  37.  
  38. student s[MAX];
  39. FILE *f = fopen("Studenti2.txt", "r");
  40. if(f == NULL) {
  41. printf("Eroare la deschidere. ");
  42. exit(1);
  43. }
  44. for(int i = 0; i < MAX; i++) {
  45. fgets(s[i].nume,35,f);
  46. s[i].nume[strlen(s[i].nume) - 1] = '\0';
  47. fscanf(f, "%f\n", &s[i].nota);
  48. }
  49. printStructure(s, 100);
  50. printf("\nSorted: \n");
  51. quicksort(s, 0, MAX - 1);
  52. printStructure(s, 50);
  53. fclose(f);
  54. return 0;
  55. }
  56.  
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60. #define MAX 5000
  61.  
  62. typedef struct oameni{
  63. char nume[35];
  64. float nota;
  65. }student;
  66.  
  67. /*void heapify(int *s, student *temp, int *d) {
  68. int i, j;
  69. int ret = 0;
  70. i = *s;
  71. j = 2 * i;
  72. *temp = s[i];
  73. ret = 0;
  74. while((j <= *d) && (!ret)) {
  75. if(j < *d)
  76. if(strcmp(st[j].nume, st[j+1].nume) < 0) j += 1;
  77. if(strcmp((*temp).nume,st[j].nume) < 0) {
  78. st[i] = st[j];
  79. i = j;
  80. j = 2 * i;
  81. } else {
  82. ret = 1;
  83. }
  84. st[i] = temp;
  85. }
  86. }*/
  87. /*void heapsort(student st[], int n) {
  88. int s, d;
  89. student temp;
  90. s = (n / 2) + 1; d = n - 1;
  91. while(s > 0) {
  92. s = s - 1;
  93. heapify(&s, &temp, &d);
  94. }
  95. while(d > 0) {
  96. temp = st[0];
  97. st[0] = st[d];
  98. st[d] = temp;
  99. }
  100. }*/
  101. void heapSort(student st[]) {
  102. buildMaxHeap(st);
  103. for(int i = n; i >= 1; i--) {
  104. student temp = st[1];
  105. st[1] = st[i];
  106. st[i] = st[1];
  107. n -= 1;
  108. heapify(st, i);
  109. }
  110. }
  111. void heapify(student st[], int i) {
  112. int left = 2*i;
  113. int right = 2*i+1;
  114.  
  115. }
  116. void printStructure(student st[], int n) {
  117. for(int i = 0; i < n; i++) {
  118. printf("%s\t%f", st[i].nume, st[i].nota);
  119. printf("\n");
  120. }
  121. }
  122. int main() {
  123.  
  124. student s[MAX];
  125. FILE *f = fopen("Studenti2.txt", "r");
  126. if(f == NULL) {
  127. printf("Eroare la deschidere. ");
  128. exit(1);
  129. }
  130. for(int i = 0; i < MAX; i++) {
  131. fgets(s[i].nume,35,f);
  132. s[i].nume[strlen(s[i].nume) - 1] = '\0';
  133. fscanf(f, "%f\n", &s[i].nota);
  134. }
  135. heapsort(s, MAX);
  136. printStructure(s, MAX);
  137. fclose(f);
  138. return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement