Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #define DIM 20
- int main(int argc, char **argv)
- {
- if(argc!=2)
- {
- fprintf(stderr,"wrong number of arguments\n");
- return EXIT_FAILURE;
- }
- int n,i,temp;
- time_t t;
- n=atoi(argv[1]);
- char *str1={"F1.txt"};
- char *str2={"F2.txt"};
- FILE *fp1,*fp2;
- if(((fp1=fopen(str1,"w"))==NULL)||((fp2=fopen(str2,"w"))==NULL))
- {
- fprintf(stderr,"unable to open files\n");
- return EXIT_FAILURE;
- }
- srand((unsigned) time(&t));
- for(i=0;i<n;i++)
- {
- temp=rand()%100000;
- fprintf(fp1,"%d\n",temp);
- }
- for(i=0;i<n;i++)
- {
- temp=rand()%100000;
- fprintf(fp2,"%d\n",temp);
- }
- fclose(fp1);
- fclose(fp2);
- char str3[DIM];
- sprintf(str3,"sort -n -o ./%s ./%s",str1,str1);
- printf("%s\n",str3);
- system(str3);
- sprintf(str3,"sort -n -o ./%s ./%s ",str2,str2);
- printf("%s\n",str3);
- system(str3);
- if(((fp1=fopen(str1,"r"))==NULL)||((fp2=fopen(str2,"r"))==NULL))
- {
- fprintf(stderr,"unable to open files\n");
- return EXIT_FAILURE;
- }
- strcpy(str1,"F1.bin");
- strcpy(str2,"F2.bin");
- FILE *fp3,*fp4;
- if(((fp3=fopen(str1,"wb"))==NULL)||((fp4=fopen(str2,"wb"))==NULL))
- {
- fprintf(stderr,"unable to open files\n");
- return EXIT_FAILURE;
- }
- while(fscanf(fp1,"%d",&i)!=EOF)
- {
- fwrite(&i,sizeof(int),1,fp3);
- }
- while(fscanf(fp2,"%d",&i)!=EOF)
- {
- fwrite(&i,sizeof(int),1,fp4);
- }
- fclose(fp1);
- fclose(fp2);
- fclose(fp3);
- fclose(fp4);
- if((fp1=fopen("F12.sorted","w+b"))==NULL)
- {
- fprintf(stderr,"unable to open files\n");
- return EXIT_FAILURE;
- }
- if(((fp3=fopen(str1,"r+b"))==NULL)||((fp4=fopen(str2,"r+b"))==NULL))
- {
- fprintf(stderr,"unable to open files\n");
- return EXIT_FAILURE;
- }
- int j=0,h1,h2;
- i=0;
- fread(&h1,sizeof(int),1,fp3);
- fread(&h2,sizeof(int),1,fp4);
- while((i<n)&&(j<n))
- {
- if(h1>h2)
- {
- fwrite(&h2,sizeof(int),1,fp1);
- i++;
- if(i<n)
- fread(&h2,sizeof(int),1,fp4);
- }
- else
- {
- fwrite(&h1,sizeof(int),1,fp1);
- j++;
- if(j<n)
- fread(&h1,sizeof(int),1,fp3);
- }
- }
- while(i<n)
- {
- fwrite(&h2,sizeof(int),1,fp1);
- i++;
- if(i<n)
- fread(&h2,sizeof(int),1,fp4);
- }
- while(j<n)
- {
- fwrite(&h1,sizeof(int),1,fp1);
- j++;
- if(j<n)
- fread(&h1,sizeof(int),1,fp3);
- }
- fclose(fp1);
- fclose(fp3);
- fclose(fp4);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement