Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int x;
- int nrcitiri;
- void merge(char* source_file1, char* source_file2, char* destination_file1, char* destination_file2, int size, int* repeat)
- {
- int i, j;
- int x; // aici trebuie folosit un element de tipul elementelor din fisier
- int y; // aici trebuie folosit un element de tipul elementelor din fisier
- int eof1, eof2;
- FILE* source1;
- FILE* source2;
- FILE* destination1;
- FILE* destination2;
- FILE* destination;
- source1 = fopen(source_file1, "rb");
- if (!source1)
- {
- printf("ERROR! File not available\n");
- return;
- }
- source2 = fopen(source_file2, "rb");
- if (!source2)
- {
- printf("ERROR! File not available\n");
- return;
- }
- destination1 = fopen(destination_file1, "wb");
- destination2 = fopen(destination_file2, "wb");
- eof1 = feof(source1);
- eof2 = feof(source2);
- if (!eof1)
- if (fread(&x, sizeof(int), 1, source1) < 1)
- eof1 = 1;
- if (!eof2)
- if (fread(&y, sizeof(int), 1, source2) < 1)
- eof2 = 1;
- destination = destination1;
- do
- {
- i = 0;
- j = 0;
- while (i < size && j < size && !eof1 && !eof2)
- {
- if (x > y)
- {
- fwrite(&x, sizeof(int), 1, destination);
- i++;
- if (feof(source1))
- eof1 = 1;
- else if (fread(&x, sizeof(int), 1, source1) < 1)
- eof1 = 1;
- }
- else
- {
- fwrite(&y, sizeof(int), 1, destination);
- j++;
- if (feof(source2))
- eof2 = 1;
- else if (fread(&y, sizeof(int), 1, source2) < 1)
- eof2 = 1;
- }
- }
- while (i < size && !eof1)
- {
- fwrite(&x, sizeof(int), 1, destination);
- i++;
- if (feof(source1))
- eof1 = 1;
- else if (fread(&x, sizeof(int), 1, source1) < 1)
- eof1 = 1;
- }
- while (j < size && !eof2)
- {
- fwrite(&y, sizeof(int), 1, destination);
- j++;
- if (feof(source2))
- eof2 = 1;
- else if (fread(&y, sizeof(int), 1, source2) < 1)
- eof2 = 1;
- }
- if (destination == destination1)
- destination = destination2;
- else
- destination = destination1;
- if (!eof1 || !eof2)
- *repeat = 1;
- } while (!eof1 || !eof2);
- fclose(source1);
- fclose(source2);
- fclose(destination1);
- fclose(destination2);
- }
- char* four_way_sequence_merging(char* file_name, char* auxiliary1, char* auxiliary2, char* auxiliary3)
- {
- FILE* source;
- FILE* destination1;
- FILE* destination2;
- int i, direction;
- int x; // aici trebuie folosit un element de tipul elementelor din fisier
- int size;
- int repeat;
- source = fopen(file_name, "rb");
- if (!source)
- {
- printf("ERROR! File not available\n");
- return NULL;
- }
- destination1 = fopen(auxiliary1, "wb");
- destination2 = fopen(auxiliary2, "wb");
- i = 0;
- while (!feof(source))
- {
- if (fread(&x, sizeof(int), 1, source) < 1)
- break;
- if (i % 2 == 0)
- fwrite(&x, sizeof(int), 1, destination1);
- else
- fwrite(&x, sizeof(int), 1, destination2);
- i++;
- nrcitiri++;
- }
- fclose(source);
- fclose(destination1);
- fclose(destination2);
- direction = 1;
- size = 1;
- do
- {
- repeat = 0;
- if (direction == 1)
- merge(auxiliary1, auxiliary2, file_name, auxiliary3, size, &repeat);
- else
- merge(file_name, auxiliary3, auxiliary1, auxiliary2, size, &repeat);
- size = size * 2;
- direction = -direction;
- } while (repeat != 0);
- if (direction == -1)
- return file_name;
- else
- return auxiliary1;
- }
- int main(void){
- FILE *fis = fopen("fis2.bin","wb");
- FILE *o = fopen("data2.bin", "rb");
- while (!feof(o)){
- fread(&x, sizeof(int), 1, o);
- fwrite(&x, sizeof(int), 1, fis);
- }
- four_way_sequence_merging("fis2.bin", "a1.bin", "a2.bin", "a3.bin");
- printf("nr operatii %d\n", nrcitiri);
- fclose(o);
- fclose(fis);
- FILE *fis2 = fopen("fis2.bin", "rb");
- FILE *txt = fopen("out.txt", "w");
- char c;
- while (!feof(fis2)){
- fread(&c, sizeof(char), 1, o);
- fprintf(txt,"%c",c);
- }
- fclose(fis2);
- fclose(txt);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement