Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdbool.h>
- #include <stdio.h>
- static int takeCorrectInt(const int min, const int max) {
- int mem = 0;
- bool isNotCorrect = true;
- do {
- scanf("%d", &mem);
- if ((mem < min) || (mem > max)) {
- printf("Not correct variable range. It must be integer from %d to %d. Try again. ", min, max);
- } else {
- isNotCorrect = false;
- }
- } while (isNotCorrect);
- return mem;
- }
- void doubleInsertionSorting(int *array, int size) {
- int *sortArr = malloc(sizeof(int) * (size * 2 - 1));
- int left = size - 1;
- int right = size - 1;
- int test = 0;
- int j = 0;
- sortArr[size - 1] = array[0];
- for (int i = 1; i < size; i++) {
- test = array[i];//todo
- if (test > (array[0])) {
- right++;
- j = right;
- while (test < sortArr[j - 1]) {
- sortArr[j] = sortArr[j - 1];
- j--;
- }
- sortArr[j] = test;
- } else {
- left--;
- j = left;
- while (test > sortArr[j + 1]) {
- sortArr[j] = sortArr[j + 1];
- j++;
- }
- sortArr[j] = test;
- }
- }
- printf("\nDouble insertion sorting result:\n ");
- for (j = 0; j < size; j++) {
- array[j] = sortArr[j + left];
- printf("%d ", array[j]);
- }
- }
- bool isNotCorrectRange(int mem, int min, int max) {
- return ((mem < min) || (mem > max));
- }
- //int *getArrayFromFile(char *fileName, int *arrSize, const int arrMin, const int arrMax, const int min, const int max) {
- // FILE *file;
- // int *fileArr = NULL;
- // if ((file = fopen(fileName, "rt")) == NULL) {
- // printf("Cannot use this file %s. Possible reasons: the file does not exist, the contents of the file do not match.\n",
- // fileName);
- // } else {
- // fscanf(file, "%d", arrSize);
- // if (isNotCorrectRange(*arrSize, arrMin, arrMax)) {
- // printf("");
- // } else {
- // int *fileArr = (int *) malloc(*arrSize * sizeof(int));
- // for (int i = 0; i < *arrSize; i++) {
- // fscanf(file, "%d", &fileArr[i]);
- // }
- // bool isNotCorrectFileContent = false;
- // for (int j = 0; j < *arrSize; j++) {
- // if (isNotCorrectRange(fileArr[j], min, max)) {
- // isNotCorrectFileContent = true;
- // }
- // }
- // if (isNotCorrectFileContent) {
- // fileArr = NULL;
- // }
- // fclose(file);
- // }
- // return fileArr;
- // }
- //}
- char *chooseFileName( char *fileName1, char *fileName2, char *fileName3) {
- int answer;
- char* fileName = "";
- printf("Select the file you want to use:\n1) %s\n2) %s\n3) %s", fileName1, fileName2, fileName3);
- answer = 1;//takeCorrectInt(1, 3);
- if (answer == 1) {
- fileName = fileName1;
- } else if (answer == 2) {
- fileName = fileName2;
- } else if (answer == 3) {
- fileName = fileName3;
- }
- return fileName;
- }
- int *takeArrayFromFile(char *fileName, int arrMin, int arrMax, const int min, const int max, int *arrLength) {
- FILE *file;
- int *fileArr = NULL;
- if ((file = fopen(fileName, "rt")) == NULL) {
- printf("Cannot use this file %s. Possible reasons: the file does not exist, the contents of the file do not match.\n", fileName);
- } else {
- fscanf(file, "%d", arrLength);
- if(isNotCorrectRange(*arrLength, arrMin, arrMax)) {
- printf("Not correct file content. Array size is not un the range from %d to %d.\n", arrMin, arrMax);
- } else {
- printf("File content:\nArray size: %d \n", arrLength);
- int *fileArr = (int *) malloc(*arrLength * sizeof(int));
- printf("\nArray members:\n");
- bool isNotCorrectFileContent = false;
- for (int i = 0; i < *arrLength; i++) {
- fscanf(file, "%d", &fileArr[i]);
- if(isNotCorrectRange(&fileArr[i], min, max)) {
- isNotCorrectFileContent = true;
- }
- }
- fclose(file);
- if(isNotCorrectFileContent) {
- fileArr = NULL;
- }
- }
- return fileArr;
- }
- }
- int *takeArray(int *arrLength) {
- int answer;
- int *originalArr = NULL;
- int arrSize = 0;
- printf("\nIf you want to use default file, enter '0'.\nIf you want to use the console input file, enter '1'.\n");
- answer =1;
- ///takeCorrectInt(0, 1);
- if (answer == 0) {
- char *fileName1 = "lab3_3input1.txt";
- char *fileName2 = "lab3_3input2.txt";
- char *fileName3 = "lab3_3input3.txt";
- char *fileName = "";
- fileName = chooseFileName(fileName1, fileName2, fileName3);
- FILE *file;
- if ((file = fopen(fileName, "rt")) == NULL) {
- printf("File does not exists.", fileName);
- originalArr = 0;
- } else {
- originalArr = takeArrayFromFile(fileName, 0, 20, 0, 100, arrLength);
- }
- }
- if (answer == 1) {
- printf("Enter array size less than 20.");
- *arrLength = 3;//takeCorrectInt(0, 20);
- originalArr = (int *) malloc(*arrLength * sizeof(int));
- printf("Enter %d members of array.\n", *arrLength);
- int n=95;
- for (int i = 0; i < *arrLength; i++) {
- n -= 13;
- printf("The %d element of array: ", i);
- originalArr[i] = n;//takeCorrectInt(0, 100);
- }
- }
- return originalArr;
- }
- void writeArrayToFile(char *fileName, int *originalArr, int *array, int arrayLength) {
- FILE *file;
- if ((file = fopen(fileName, "w")) == NULL) {
- printf("Sorry. Unable to found file %s\n.", fileName);
- } else {
- printf("Here you can see original array members: \n");
- for (int i = 0; i < arrayLength; i++) {
- fprintf(file, "%d", originalArr[i]);
- }
- for (int i = 0; i < arrayLength; i++) {
- fprintf(file, "%d ", array[i]);
- }
- printf("Success!!!");
- }
- fclose(file);
- }
- int* copyArr(int* primaryArr, int arrLength) {
- int *copyArray = malloc(arrLength * sizeof(int));
- for (int i = 0; i < arrLength; i++) {
- copyArray[i] = primaryArr[i];
- }
- return copyArray;
- }
- int main() {
- printf("This program performs two-way insertion sorting. Let's start.");
- int arrLength ;
- int *originalArray = takeArray(&arrLength);
- if (originalArray == NULL) {
- printf("Input mismatch.");
- } else {
- int *testArray = copyArr(originalArray, arrLength);
- doubleInsertionSorting(originalArray, arrLength);
- printf("Now it's time to capture the result in a file.");
- char *outputFileName = "";
- outputFileName = chooseFileName("output1.txt", "output2.txt", "output3.txt");
- // writeArrayToFile(outputFileName, originalArray, arrLength);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement