Guest User

Untitled

a guest
Mar 25th, 2019
72
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define MAX 30
  5. #define MAXKIDS 128
  6.  
  7. typedef struct{
  8. char i[MAX];
  9. char p[MAX];
  10. int g;
  11. } Student;
  12.  
  13. void swap(Student niz[], int i, int j)
  14. {
  15. char tmpi[MAX], tmpp[MAX];
  16. int tmpg = niz[i].g;
  17. niz[i].g = niz[j].g;
  18. niz[j].g = tmpg;
  19. strcpy(tmpi, niz[i].i);
  20. strcpy(niz[i].i, niz[j].i);
  21. strcpy(niz[j].i, tmpi);
  22. strcpy(tmpp, niz[i].p);
  23. strcpy(niz[i].p, niz[j].p);
  24. strcpy(niz[j].p, tmpp);
  25. }
  26.  
  27. void check(Student niz[], int n)
  28. {
  29. int i;
  30. for(i=1; i<n+1; i++)
  31. printf("%s %s %d \n",niz[i].i, niz[i].p, niz[i].g );
  32. }
  33.  
  34. void sort(Student niz[], int n)
  35. {
  36. int state, i;
  37. do {
  38. state = 0;
  39. for(i=0; i<n; i++)
  40. if(niz[i].g>niz[i+1].g)
  41. {
  42. swap(niz, i, i+1);
  43. state=1;
  44. }
  45. } while (state);
  46. printf("\n");
  47. check(niz, n);
  48. }
  49.  
  50.  
  51.  
  52. int printout(char *in)
  53. {
  54. FILE *in_file = fopen(in, "r");
  55. if(!in_file)
  56. {
  57. printf("Datoteka nije uspeno otvorena!\n", in);
  58. exit(-1);
  59. }
  60. int c, counter=1;
  61. while ((c = getc(in_file)) != EOF)
  62. {
  63. putchar(c);
  64. if(c=='\n')
  65. counter++;
  66. }
  67. putchar('\n');
  68. close(in_file);
  69. return counter;
  70. }
  71.  
  72. void sortkids(char *in, char *out, int n)
  73. {
  74. FILE *in_file = fopen(in, "r");
  75. FILE *out_file = fopen(out, "w");
  76. Student niz[MAXKIDS];
  77. int i, tmp[MAXKIDS];
  78. if(in_file==NULL || out_file==NULL)
  79. {
  80. printf("Datoteka nije uspeno otvorena!\n", in);
  81. exit(-1);
  82. }
  83. for(i=0; i<n; i++)
  84. fscanf(in_file, "%s %s %d\n",&niz[i].i, &niz[i].p, &niz[i].g);
  85. sort(niz, n);
  86. for(i=1; i<n+1; i++)
  87. fprintf(out_file, "%s %s %d\n",niz[i].i, niz[i].p, niz[i].g);
  88. close(in_file);
  89. close(out_file);
  90. }
  91. int main(int argc, char *argv[])
  92. {
  93. if(argc!=3)
  94. {
  95. printf("Greska u pozivu!\n");
  96. system("pause");
  97. return -1;
  98. }
  99. char *in=argv[1], *out=argv[2];
  100. int counter=printout(in);
  101. sortkids(in, out, counter);
  102. system("pause");
  103. }
RAW Paste Data