Advertisement
Guest User

Untitled

a guest
Jan 17th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h> /* Zbog funkcije exit() */
  3.  
  4. struct Student {
  5. char prezime[20];
  6. char ime[15];
  7. int broj_bodova;
  8. };
  9.  
  10. int main() {
  11. struct Student studenti[300], tmp;
  12. FILE *ulaz, *izlaz;
  13. int i, j, vel, max;
  14.  
  15. /* Otvaranje datoteka */
  16. if ((ulaz = fopen("ispit.txt","r")) == NULL) {
  17. printf("Greska pri otvaranju datoteke ispit.txt");
  18. return 1;
  19. }
  20.  
  21. if ((izlaz = fopen("ispit_sortiran.txt","w")) == NULL) {
  22. fclose(ulaz);
  23. printf("Greska pri otvaranju datoteke ispit_sortiran.txt");
  24. return 1;
  25. }
  26.  
  27. /* Ucitavanje datoteke u niz studenti */
  28. i=0;
  29. while (fscanf(ulaz,"%20s%15s%2d%\n", studenti[i].prezime, studenti[i].ime, &studenti[i].broj_bodova) == 3 && i<300)
  30. i++;
  31. vel = i;
  32.  
  33. /* Sortiranje niza po broju bodova */
  34. for (i=0; i<vel; i++) {
  35. max = i;
  36. for (j=i+1; j<vel; j++){
  37. if (studenti[max].broj_bodova < studenti[j].broj_bodova) max = j;
  38.  
  39. }
  40.  
  41. tmp = studenti[i];
  42. studenti[i] = studenti[max];
  43. studenti[max] = tmp;
  44. }
  45.  
  46. // Upis niza u datoteku
  47. for (i=0; i<vel; i++)
  48. fprintf(izlaz,"%-20s%-15s%2d\n", studenti[i].prezime, studenti[i].ime, studenti[i].broj_bodova);
  49.  
  50. printf ("Izlazna datoteka ispit_sortiran.txt je kreirana\n");
  51. fclose(ulaz);
  52. fclose(izlaz);
  53. return 0;
  54. }
  55. #include <stdio.h>
  56. #include <stdlib.h> /* Zbog funkcije exit() */
  57.  
  58. struct Student {
  59. char prezime[20];
  60. char ime[15];
  61. int broj_bodova;
  62. };
  63.  
  64. int main() {
  65. struct Student studenti[300], tmp;
  66. FILE *ulaz, *izlaz;
  67. int i, j, vel, max;
  68.  
  69. /* Otvaranje datoteka */
  70. if ((ulaz = fopen("ispit.txt","r")) == NULL) {
  71. printf("Greska pri otvaranju datoteke ispit.txt");
  72. return 1;
  73. }
  74.  
  75. if ((izlaz = fopen("ispit_sortiran.txt","w")) == NULL) {
  76. fclose(ulaz);
  77. printf("Greska pri otvaranju datoteke ispit_sortiran.txt");
  78. return 1;
  79. }
  80.  
  81. /* Ucitavanje datoteke u niz studenti */
  82. i=0;
  83. while (fscanf(ulaz,"%20s%15s%2d%\n", studenti[i].prezime, studenti[i].ime, &studenti[i].broj_bodova) == 3 && i<300)
  84. i++;
  85. vel = i;
  86.  
  87. /* Sortiranje niza po broju bodova */
  88. for (i=0; i<vel; i++) {
  89. max = i;
  90. for (j=i+1; j<vel; j++){
  91. if (studenti[max].broj_bodova < studenti[j].broj_bodova) max = j;
  92.  
  93. }
  94.  
  95. tmp = studenti[i];
  96. studenti[i] = studenti[max];
  97. studenti[max] = tmp;
  98. }
  99.  
  100. // Upis niza u datoteku
  101. for (i=0; i<vel; i++)
  102. fprintf(izlaz,"%-20s%-15s%2d\n", studenti[i].prezime, studenti[i].ime, studenti[i].broj_bodova);
  103.  
  104. printf ("Izlazna datoteka ispit_sortiran.txt je kreirana\n");
  105. fclose(ulaz);
  106. fclose(izlaz);
  107. return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement