Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4.  
  5. static int takeCorrectInt(const int min, const int max) {
  6. int mem = 0;
  7. bool isNotCorrect = true;
  8. do {
  9. scanf("%d", &mem);
  10. if ((mem < min) || (mem > max)) {
  11. printf("Not correct variable range. It must be integer from %d to %d. Try again. ", min, max);
  12. } else {
  13. isNotCorrect = false;
  14. }
  15. } while (isNotCorrect);
  16. return mem;
  17. }
  18.  
  19. void doubleInsertionSorting(int *array, int size) {
  20. int *sortArr = malloc(sizeof(int) * (size * 2 - 1));
  21. int left = size - 1;
  22. int right = size - 1;
  23. int test = 0;
  24. int j = 0;
  25. sortArr[size - 1] = array[0];
  26. for (int i = 1; i < size; i++) {
  27. test = array[i];//todo
  28. if (test > (array[0])) {
  29. right++;
  30. j = right;
  31. while (test < sortArr[j - 1]) {
  32. sortArr[j] = sortArr[j - 1];
  33. j--;
  34. }
  35. sortArr[j] = test;
  36. } else {
  37. left--;
  38. j = left;
  39. while (test > sortArr[j + 1]) {
  40. sortArr[j] = sortArr[j + 1];
  41. j++;
  42. }
  43. sortArr[j] = test;
  44. }
  45. }
  46. printf("\nDouble insertion sorting result:\n ");
  47. for (j = 0; j < size; j++) {
  48. array[j] = sortArr[j + left];
  49. printf("%d ", array[j]);
  50. }
  51. }
  52.  
  53.  
  54. bool isNotCorrectRange(int mem, int min, int max) {
  55. return ((mem < min) || (mem > max));
  56. }
  57.  
  58. //int *getArrayFromFile(char *fileName, int *arrSize, const int arrMin, const int arrMax, const int min, const int max) {
  59. // FILE *file;
  60. // int *fileArr = NULL;
  61. // if ((file = fopen(fileName, "rt")) == NULL) {
  62. // printf("Cannot use this file %s. Possible reasons: the file does not exist, the contents of the file do not match.\n",
  63. // fileName);
  64. // } else {
  65. // fscanf(file, "%d", arrSize);
  66. // if (isNotCorrectRange(*arrSize, arrMin, arrMax)) {
  67. // printf("");
  68. // } else {
  69. // int *fileArr = (int *) malloc(*arrSize * sizeof(int));
  70. // for (int i = 0; i < *arrSize; i++) {
  71. // fscanf(file, "%d", &fileArr[i]);
  72. // }
  73. // bool isNotCorrectFileContent = false;
  74. // for (int j = 0; j < *arrSize; j++) {
  75. // if (isNotCorrectRange(fileArr[j], min, max)) {
  76. // isNotCorrectFileContent = true;
  77. // }
  78. // }
  79. // if (isNotCorrectFileContent) {
  80. // fileArr = NULL;
  81. // }
  82. // fclose(file);
  83. // }
  84. // return fileArr;
  85. // }
  86. //}
  87.  
  88.  
  89. char *chooseFileName( char *fileName1, char *fileName2, char *fileName3) {
  90. int answer;
  91. char* fileName = "";
  92. printf("Select the file you want to use:\n1) %s\n2) %s\n3) %s", fileName1, fileName2, fileName3);
  93. answer = 1;//takeCorrectInt(1, 3);
  94. if (answer == 1) {
  95. fileName = fileName1;
  96. } else if (answer == 2) {
  97. fileName = fileName2;
  98. } else if (answer == 3) {
  99. fileName = fileName3;
  100. }
  101. return fileName;
  102. }
  103. int *takeArrayFromFile(char *fileName, int arrMin, int arrMax, const int min, const int max, int *arrLength) {
  104. FILE *file;
  105. int *fileArr = NULL;
  106. if ((file = fopen(fileName, "rt")) == NULL) {
  107. printf("Cannot use this file %s. Possible reasons: the file does not exist, the contents of the file do not match.\n", fileName);
  108. } else {
  109.  
  110. fscanf(file, "%d", arrLength);
  111. if(isNotCorrectRange(*arrLength, arrMin, arrMax)) {
  112. printf("Not correct file content. Array size is not un the range from %d to %d.\n", arrMin, arrMax);
  113. } else {
  114. printf("File content:\nArray size: %d \n", arrLength);
  115. int *fileArr = (int *) malloc(*arrLength * sizeof(int));
  116. printf("\nArray members:\n");
  117. bool isNotCorrectFileContent = false;
  118. for (int i = 0; i < *arrLength; i++) {
  119. fscanf(file, "%d", &fileArr[i]);
  120. if(isNotCorrectRange(&fileArr[i], min, max)) {
  121. isNotCorrectFileContent = true;
  122. }
  123. }
  124. fclose(file);
  125. if(isNotCorrectFileContent) {
  126. fileArr = NULL;
  127. }
  128. }
  129. return fileArr;
  130. }
  131. }
  132.  
  133. int *takeArray(int *arrLength) {
  134. int answer;
  135. int *originalArr = NULL;
  136. int arrSize = 0;
  137.  
  138. printf("\nIf you want to use default file, enter '0'.\nIf you want to use the console input file, enter '1'.\n");
  139. answer =1;
  140. ///takeCorrectInt(0, 1);
  141. if (answer == 0) {
  142. char *fileName1 = "lab3_3input1.txt";
  143. char *fileName2 = "lab3_3input2.txt";
  144. char *fileName3 = "lab3_3input3.txt";
  145. char *fileName = "";
  146. fileName = chooseFileName(fileName1, fileName2, fileName3);
  147. FILE *file;
  148. if ((file = fopen(fileName, "rt")) == NULL) {
  149. printf("File does not exists.", fileName);
  150. originalArr = 0;
  151. } else {
  152. originalArr = takeArrayFromFile(fileName, 0, 20, 0, 100, arrLength);
  153. }
  154. }
  155. if (answer == 1) {
  156. printf("Enter array size less than 20.");
  157. *arrLength = 3;//takeCorrectInt(0, 20);
  158. originalArr = (int *) malloc(*arrLength * sizeof(int));
  159. printf("Enter %d members of array.\n", *arrLength);
  160. int n=95;
  161. for (int i = 0; i < *arrLength; i++) {
  162. n -= 13;
  163. printf("The %d element of array: ", i);
  164. originalArr[i] = n;//takeCorrectInt(0, 100);
  165. }
  166. }
  167. return originalArr;
  168. }
  169.  
  170. void writeArrayToFile(char *fileName, int *originalArr, int *array, int arrayLength) {
  171. FILE *file;
  172. if ((file = fopen(fileName, "w")) == NULL) {
  173. printf("Sorry. Unable to found file %s\n.", fileName);
  174. } else {
  175. printf("Here you can see original array members: \n");
  176. for (int i = 0; i < arrayLength; i++) {
  177. fprintf(file, "%d", originalArr[i]);
  178. }
  179. for (int i = 0; i < arrayLength; i++) {
  180. fprintf(file, "%d ", array[i]);
  181. }
  182. printf("Success!!!");
  183.  
  184. }
  185. fclose(file);
  186. }
  187.  
  188. int* copyArr(int* primaryArr, int arrLength) {
  189. int *copyArray = malloc(arrLength * sizeof(int));
  190. for (int i = 0; i < arrLength; i++) {
  191. copyArray[i] = primaryArr[i];
  192. }
  193. return copyArray;
  194. }
  195.  
  196. int main() {
  197. printf("This program performs two-way insertion sorting. Let's start.");
  198. int arrLength ;
  199. int *originalArray = takeArray(&arrLength);
  200. if (originalArray == NULL) {
  201. printf("Input mismatch.");
  202. } else {
  203.  
  204. int *testArray = copyArr(originalArray, arrLength);
  205. doubleInsertionSorting(originalArray, arrLength);
  206. printf("Now it's time to capture the result in a file.");
  207. char *outputFileName = "";
  208. outputFileName = chooseFileName("output1.txt", "output2.txt", "output3.txt");
  209. // writeArrayToFile(outputFileName, originalArray, arrLength);
  210. }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement