Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #define DIM 20
  6. int main(int argc, char **argv)
  7. {
  8. if(argc!=2)
  9. {
  10. fprintf(stderr,"wrong number of arguments\n");
  11. return EXIT_FAILURE;
  12. }
  13. int n,i,temp;
  14. time_t t;
  15. n=atoi(argv[1]);
  16. char *str1={"F1.txt"};
  17. char *str2={"F2.txt"};
  18. FILE *fp1,*fp2;
  19. if(((fp1=fopen(str1,"w"))==NULL)||((fp2=fopen(str2,"w"))==NULL))
  20. {
  21. fprintf(stderr,"unable to open files\n");
  22. return EXIT_FAILURE;
  23. }
  24. srand((unsigned) time(&t));
  25. for(i=0;i<n;i++)
  26. {
  27. temp=rand()%100000;
  28. fprintf(fp1,"%d\n",temp);
  29. }
  30.  
  31. for(i=0;i<n;i++)
  32. {
  33. temp=rand()%100000;
  34. fprintf(fp2,"%d\n",temp);
  35. }
  36. fclose(fp1);
  37. fclose(fp2);
  38. char str3[DIM];
  39. sprintf(str3,"sort -n -o ./%s ./%s",str1,str1);
  40. printf("%s\n",str3);
  41. system(str3);
  42. sprintf(str3,"sort -n -o ./%s ./%s ",str2,str2);
  43. printf("%s\n",str3);
  44. system(str3);
  45. if(((fp1=fopen(str1,"r"))==NULL)||((fp2=fopen(str2,"r"))==NULL))
  46. {
  47. fprintf(stderr,"unable to open files\n");
  48. return EXIT_FAILURE;
  49. }
  50. strcpy(str1,"F1.bin");
  51. strcpy(str2,"F2.bin");
  52. FILE *fp3,*fp4;
  53. if(((fp3=fopen(str1,"wb"))==NULL)||((fp4=fopen(str2,"wb"))==NULL))
  54. {
  55. fprintf(stderr,"unable to open files\n");
  56. return EXIT_FAILURE;
  57. }
  58.  
  59. while(fscanf(fp1,"%d",&i)!=EOF)
  60. {
  61. fwrite(&i,sizeof(int),1,fp3);
  62. }
  63.  
  64. while(fscanf(fp2,"%d",&i)!=EOF)
  65. {
  66. fwrite(&i,sizeof(int),1,fp4);
  67. }
  68. fclose(fp1);
  69. fclose(fp2);
  70. fclose(fp3);
  71. fclose(fp4);
  72. if((fp1=fopen("F12.sorted","w+b"))==NULL)
  73. {
  74. fprintf(stderr,"unable to open files\n");
  75. return EXIT_FAILURE;
  76. }
  77. if(((fp3=fopen(str1,"r+b"))==NULL)||((fp4=fopen(str2,"r+b"))==NULL))
  78. {
  79. fprintf(stderr,"unable to open files\n");
  80. return EXIT_FAILURE;
  81. }
  82.  
  83. int j=0,h1,h2;
  84. i=0;
  85. fread(&h1,sizeof(int),1,fp3);
  86. fread(&h2,sizeof(int),1,fp4);
  87. while((i<n)&&(j<n))
  88. {
  89. if(h1>h2)
  90. {
  91. fwrite(&h2,sizeof(int),1,fp1);
  92. i++;
  93. if(i<n)
  94. fread(&h2,sizeof(int),1,fp4);
  95. }
  96. else
  97. {
  98. fwrite(&h1,sizeof(int),1,fp1);
  99. j++;
  100. if(j<n)
  101. fread(&h1,sizeof(int),1,fp3);
  102.  
  103. }
  104. }
  105. while(i<n)
  106. {
  107. fwrite(&h2,sizeof(int),1,fp1);
  108. i++;
  109. if(i<n)
  110. fread(&h2,sizeof(int),1,fp4);
  111. }
  112. while(j<n)
  113. {
  114. fwrite(&h1,sizeof(int),1,fp1);
  115. j++;
  116. if(j<n)
  117. fread(&h1,sizeof(int),1,fp3);
  118. }
  119. fclose(fp1);
  120. fclose(fp3);
  121. fclose(fp4);
  122. return EXIT_SUCCESS;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement