Advertisement
Egor_Vakar

lab3_3(C++)

Nov 2nd, 2021 (edited)
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int takeInt(int min, int max) {
  8.     int number = 0;
  9.     bool isIncorrect;
  10.     do {
  11.         isIncorrect = false;
  12.         cin >> number;
  13.         if (!cin.good()){
  14.             isIncorrect = true;
  15.             cout << "Incorrect input!!!\n";
  16.             while (cin.get() != '\n') {
  17.                 cin.clear();
  18.             }
  19.         }
  20.         if ((!isIncorrect) && (number < min || number > max)){
  21.             isIncorrect = true;
  22.             cout << "Incorrect input!!!\n";
  23.         }
  24.     } while (isIncorrect);
  25.     return number;
  26. }
  27.  
  28. char takeSource() {
  29.     char choice;
  30.     cin >> choice;
  31.     while ((choice != '1') && (choice != '2')) {
  32.         cout << "Incorrect input!!!\nSelect the source:\n1:Console\n2:File\nEnter 1 or 2: ";
  33.         cin >> choice;
  34.     }
  35.     return choice;
  36. }
  37.  
  38. string takeInPath() {
  39.     string path;
  40.     bool isIncorrect;
  41.     cout << "Enter file path: ";
  42.     do {
  43.         isIncorrect = true;
  44.         cin >> path;
  45.         ifstream fin(path);
  46.         if (fin.is_open()) {
  47.             isIncorrect = false;
  48.             fin.close();
  49.         }
  50.         else {
  51.             cout << "File is not found\nEnter file path: ";
  52.         }
  53.         if (!isIncorrect && ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt")))) {
  54.             cout << "File is found, but it is not \".txt\" type file\nEnter file path : ";
  55.             isIncorrect = true;
  56.         }
  57.     } while (isIncorrect);
  58.     return path;
  59. }
  60.  
  61. string takeOutPath() {
  62.     string path;
  63.     bool isIncorrect;
  64.     cout << "Enter file path: ";
  65.     do{
  66.         isIncorrect = false;
  67.         cin >> path;
  68.         if ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt"))) {
  69.             cout << "It should be a \".txt\" file\nEnter file path: ";
  70.             isIncorrect = true;
  71.         }
  72.     } while (isIncorrect);
  73.     return path;
  74. }
  75.  
  76. vector<int> takeArrayFromConsole() {
  77.     const int MIN_ELEMENT = -1000;
  78.     const int MAX_ELEMENT = 1000;
  79.     const int MIN_SIZE = 2;
  80.     const int MAX_SIZE = 100;
  81.     cout << "Enter array size (in range " << MIN_SIZE << ", " << MAX_SIZE << "): ";
  82.     int size = takeInt(MIN_SIZE, MAX_SIZE);
  83.     vector <int> array(size);
  84.     for (int i = 0; i < size; i++) {
  85.         cout << "Enter element " << (i + 1) << ": ";
  86.         array[i] = takeInt(MIN_ELEMENT, MAX_ELEMENT);
  87.     }
  88.     return array;
  89. }
  90.  
  91. vector <int> takeArrayFromFile(const string path) {
  92.     ifstream fin(path);
  93.     int size;
  94.     fin >> size;
  95.     vector <int> array(size);
  96.     for (int i = 0; i < size; i++) {
  97.         fin >> array[i];
  98.     }
  99.     fin.close();
  100.     return array;
  101. }
  102.  
  103. vector <int> takeArray(const char source) {
  104.     string inPath;
  105.     vector <int> array;
  106.     if (source == '1') {
  107.         array = takeArrayFromConsole();
  108.     }
  109.     else {
  110.         inPath = takeInPath();
  111.         array = takeArrayFromFile(inPath);
  112.     }
  113.     return array;
  114. }
  115.  
  116. vector<vector<int>> findStagesOfCocktailSort(vector<int> array){
  117.     int size = array.size();
  118.     vector<vector<int>>answer(size);
  119.     for (int i = 0; i < size; i++)
  120.         answer[i].resize(size);
  121.     for (int k = 0; k < size; k++) {
  122.         answer[0][k] = array[k];
  123.     }
  124.     int counter = 0;
  125.     int leftBorder = 0, rightBorder = size - 1;
  126.     bool wasThereSwap = true;
  127.     while ((leftBorder < rightBorder) && wasThereSwap) {
  128.         wasThereSwap = false;
  129.         for (int i = leftBorder; i < rightBorder; i++) {
  130.             if (array[i] > array[i + 1]) {
  131.                 swap(array[i], array[i + 1]);
  132.                 wasThereSwap = true;
  133.             }
  134.         }
  135.         if (wasThereSwap) {
  136.             for (int k = 0; k < size; k++) {
  137.                 answer[counter + 1][k] = array[k];
  138.             }
  139.         }
  140.         counter++;
  141.         wasThereSwap = false;
  142.         rightBorder--;
  143.         for (int i = rightBorder; i > leftBorder; i--) {
  144.             if (array[i - 1] > array[i]) {
  145.                 swap(array[i - 1], array[i]);
  146.                 wasThereSwap = true;
  147.             }
  148.         }
  149.         leftBorder++;
  150.         if (wasThereSwap) {
  151.             for (int k = 0; k < size; k++) {
  152.                 answer[counter + 1][k] = array[k];
  153.             }
  154.  
  155.         }
  156.         counter++;
  157.     }
  158.     vector<vector<int>> allStages(counter + 1);
  159.     for (int i = 0; i < counter + 1; i++) {
  160.         allStages[i] = answer[i];
  161.     }
  162.     return allStages;
  163. }
  164.  
  165. static void outputToConsole(vector<vector<int>> answer) {
  166.     int numberOfAnswers = answer.size();
  167.     int size = answer[0].size();
  168.     for (int i = 0; i < numberOfAnswers; i++) {
  169.         for (int j = 0; j < size; j++)
  170.             cout << answer[i][j] << " ";
  171.         cout << "\n";
  172.     }
  173. }
  174.  
  175. void outputToFile(string path, vector<vector<int>> answer) {
  176.     ofstream fout;
  177.     fout.open(path);
  178.     int size = answer.size();
  179.     for (int i = 0; i < size; i++) {
  180.         for (int j = 0; j < size; j++)
  181.             fout << answer[i][j] << " ";
  182.         fout << "\n";
  183.     }
  184.     fout.close();
  185.     cout << "Done";
  186. }
  187.  
  188. void output(const char source, vector<vector<int>> answer) {
  189.     string outPath;
  190.     if (source == '1') {
  191.         outputToConsole(answer);
  192.     }
  193.     else {
  194.         outPath = takeOutPath();
  195.         outputToFile(outPath, answer);
  196.     }
  197. }
  198.  
  199. int main(){
  200.     cout << "Welcome to the program that will sort an array using cocktail sort\nSelect the source for entering the array:\n1:Console\n2:File\nEnter 1 or 2: ";
  201.     char inputSource = takeSource();
  202.     vector<int> array = takeArray(inputSource);
  203.     vector<vector<int>> answer = findStagesOfCocktailSort(array);
  204.     cout << "Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ";
  205.     char outputSource = takeSource();
  206.     output(outputSource, answer);
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement