Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. #include <stdio.h>
  2. int x;
  3. int nrcitiri;
  4.  
  5.  
  6. void merge(char* source_file1, char* source_file2, char* destination_file1, char* destination_file2, int size, int* repeat)
  7. {
  8. int i, j;
  9. int x; // aici trebuie folosit un element de tipul elementelor din fisier
  10. int y; // aici trebuie folosit un element de tipul elementelor din fisier
  11. int eof1, eof2;
  12. FILE* source1;
  13. FILE* source2;
  14. FILE* destination1;
  15. FILE* destination2;
  16. FILE* destination;
  17. source1 = fopen(source_file1, "rb");
  18. if (!source1)
  19. {
  20. printf("ERROR! File not available\n");
  21. return;
  22. }
  23. source2 = fopen(source_file2, "rb");
  24. if (!source2)
  25. {
  26. printf("ERROR! File not available\n");
  27. return;
  28. }
  29. destination1 = fopen(destination_file1, "wb");
  30. destination2 = fopen(destination_file2, "wb");
  31. eof1 = feof(source1);
  32. eof2 = feof(source2);
  33. if (!eof1)
  34. if (fread(&x, sizeof(int), 1, source1) < 1)
  35. eof1 = 1;
  36. if (!eof2)
  37. if (fread(&y, sizeof(int), 1, source2) < 1)
  38. eof2 = 1;
  39. destination = destination1;
  40. do
  41. {
  42. i = 0;
  43. j = 0;
  44. while (i < size && j < size && !eof1 && !eof2)
  45. {
  46. if (x > y)
  47. {
  48. fwrite(&x, sizeof(int), 1, destination);
  49. i++;
  50. if (feof(source1))
  51. eof1 = 1;
  52. else if (fread(&x, sizeof(int), 1, source1) < 1)
  53. eof1 = 1;
  54. }
  55. else
  56. {
  57. fwrite(&y, sizeof(int), 1, destination);
  58. j++;
  59. if (feof(source2))
  60. eof2 = 1;
  61. else if (fread(&y, sizeof(int), 1, source2) < 1)
  62. eof2 = 1;
  63. }
  64. }
  65. while (i < size && !eof1)
  66. {
  67. fwrite(&x, sizeof(int), 1, destination);
  68. i++;
  69. if (feof(source1))
  70. eof1 = 1;
  71. else if (fread(&x, sizeof(int), 1, source1) < 1)
  72. eof1 = 1;
  73. }
  74. while (j < size && !eof2)
  75. {
  76. fwrite(&y, sizeof(int), 1, destination);
  77. j++;
  78. if (feof(source2))
  79. eof2 = 1;
  80. else if (fread(&y, sizeof(int), 1, source2) < 1)
  81. eof2 = 1;
  82. }
  83. if (destination == destination1)
  84. destination = destination2;
  85. else
  86. destination = destination1;
  87. if (!eof1 || !eof2)
  88. *repeat = 1;
  89. } while (!eof1 || !eof2);
  90. fclose(source1);
  91. fclose(source2);
  92. fclose(destination1);
  93. fclose(destination2);
  94. }
  95. char* four_way_sequence_merging(char* file_name, char* auxiliary1, char* auxiliary2, char* auxiliary3)
  96. {
  97. FILE* source;
  98. FILE* destination1;
  99. FILE* destination2;
  100. int i, direction;
  101. int x; // aici trebuie folosit un element de tipul elementelor din fisier
  102. int size;
  103. int repeat;
  104. source = fopen(file_name, "rb");
  105. if (!source)
  106. {
  107. printf("ERROR! File not available\n");
  108. return NULL;
  109. }
  110. destination1 = fopen(auxiliary1, "wb");
  111. destination2 = fopen(auxiliary2, "wb");
  112. i = 0;
  113. while (!feof(source))
  114. {
  115. if (fread(&x, sizeof(int), 1, source) < 1)
  116. break;
  117. if (i % 2 == 0)
  118. fwrite(&x, sizeof(int), 1, destination1);
  119. else
  120. fwrite(&x, sizeof(int), 1, destination2);
  121. i++;
  122. nrcitiri++;
  123. }
  124. fclose(source);
  125. fclose(destination1);
  126. fclose(destination2);
  127. direction = 1;
  128. size = 1;
  129. do
  130. {
  131. repeat = 0;
  132. if (direction == 1)
  133. merge(auxiliary1, auxiliary2, file_name, auxiliary3, size, &repeat);
  134. else
  135. merge(file_name, auxiliary3, auxiliary1, auxiliary2, size, &repeat);
  136. size = size * 2;
  137. direction = -direction;
  138. } while (repeat != 0);
  139. if (direction == -1)
  140. return file_name;
  141. else
  142. return auxiliary1;
  143. }
  144. int main(void){
  145. FILE *fis = fopen("fis2.bin","wb");
  146. FILE *o = fopen("data2.bin", "rb");
  147. while (!feof(o)){
  148.  
  149. fread(&x, sizeof(int), 1, o);
  150. fwrite(&x, sizeof(int), 1, fis);
  151. }
  152. four_way_sequence_merging("fis2.bin", "a1.bin", "a2.bin", "a3.bin");
  153. printf("nr operatii %d\n", nrcitiri);
  154. fclose(o);
  155. fclose(fis);
  156. FILE *fis2 = fopen("fis2.bin", "rb");
  157. FILE *txt = fopen("out.txt", "w");
  158. char c;
  159. while (!feof(fis2)){
  160. fread(&c, sizeof(char), 1, o);
  161. fprintf(txt,"%c",c);
  162. }
  163.  
  164. fclose(fis2);
  165. fclose(txt);
  166. return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement