Advertisement
Vladislav8653

laba_3_3_c++

Dec 7th, 2022
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. ifstream fin;
  6. ofstream fout;
  7.  
  8. int inputData() {
  9.     int n = 0;
  10.     bool isIncorrect;;
  11.     do {
  12.         cin >> n;
  13.         isIncorrect = false;
  14.         if (cin.fail()) {
  15.             cout << "Please, enter an integer number:" << endl;
  16.             isIncorrect = true;
  17.             cin.clear();
  18.             while (cin.get() != '\n');
  19.         }
  20.     } while (isIncorrect);
  21.     return n;
  22. }
  23.  
  24. bool choose() {
  25.     int inputNumber;
  26.     bool isIncorrect;
  27.     const int MIN_NUM = 0;
  28.     const int MAX_NUM = 1;
  29.     do {
  30.         inputNumber = inputData();
  31.         isIncorrect = false;
  32.         if (cin.fail()) {
  33.             isIncorrect = true;
  34.             cout << "Please, enter a number." << endl;
  35.             cin.clear();
  36.             while (cin.get() != '\n');
  37.         }
  38.         if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
  39.             cout << "You are out of input range!" << endl;
  40.             isIncorrect = true;
  41.         }
  42.     } while (isIncorrect);
  43.     if (inputNumber == 1)
  44.         return true;
  45.     else
  46.         return false;
  47. }
  48.  
  49. int inputArraySize() {
  50.     bool isIncorrect;
  51.     int num = 0;
  52.     const int MIN_SIZE = 2;
  53.     do {
  54.         num = inputData();
  55.         isIncorrect = false;
  56.         if (num < MIN_SIZE) {
  57.             cout << "Please, enter a number > 2:" << endl;
  58.             isIncorrect = true;
  59.             cin.clear();
  60.             while (cin.get() != '\n');
  61.         }
  62.     } while (isIncorrect);
  63.     return num;
  64. }
  65.  
  66. int* inputArray(int num) {
  67.     int* arr = new int[num];
  68.     for (int i = 0; i < num; i++)
  69.         arr[i] = inputData();
  70.     return arr;
  71. }
  72.  
  73. int* sortArrayBySelectionSort(int* arr, int num) {
  74.     cout << "Sort stages:" << endl;
  75.     int min;
  76.     for (int i = 0; i < num - 1; i++) {
  77.         for (int j = i + 1; j < num; j++)
  78.             if (arr[j] < arr[i]) {
  79.                 min = arr[j];
  80.                 arr[j] = arr[i];
  81.                 arr[i] = min;
  82.             }
  83.         cout << "Stage " << (i + 1) << ": ";
  84.         for (int j = 0; j < num; j++)
  85.             cout << arr[j] << " ";
  86.         cout << endl;
  87.     }
  88.     return arr;
  89. }
  90.  
  91. void consoleOutput(int* arr, int num) {
  92.     for (int i = 0; i < num; i++)
  93.         cout << arr[i] << ' ';
  94.     cout << endl;
  95. }
  96.  
  97.  
  98. string inputFilePath() {
  99.     const int EXTENSION_SIZE = 4;
  100.     string path;
  101.     string ext;
  102.     bool isIncorrect;
  103.     do {
  104.         isIncorrect = false;
  105.         cout << "Input path to file: " << endl;
  106.         cin >> path;
  107.         fin.open(path);
  108.         if (path.size() > EXTENSION_SIZE) {
  109.             ext = path.substr(path.size() - EXTENSION_SIZE, EXTENSION_SIZE);
  110.         }
  111.         else {
  112.             cout << "Incorrect file name." << endl;
  113.             isIncorrect = true;
  114.         } if (!isIncorrect && ext != ".txt") {
  115.             cout << "Must have .txt!" << endl;
  116.             isIncorrect = true;
  117.         }
  118.         else if (!isIncorrect && !fin.is_open()) {
  119.             cout << "Wrong way to file." << endl;
  120.             isIncorrect = true;
  121.         }
  122.     } while (isIncorrect);
  123.     fin.close();
  124.     return path;
  125. }
  126.  
  127. int inputSizeOfArrayFromFile(string path) {
  128.     int num;
  129.     bool isIncorrect;
  130.     const int MIN = 2;
  131.     do{
  132.         isIncorrect = false;
  133.         fin.open(path);
  134.         fin >> num;
  135.         if (num < MIN) {
  136.             isIncorrect = true;
  137.             cout << "Matrix size should be at least 2" << endl;
  138.             path = inputFilePath();
  139.         }
  140.     } while (isIncorrect);
  141.     return num;
  142. }
  143.  
  144. int* inputArrayFile (string path, int num){
  145.     ifstream fileIn(path);
  146.     bool isIncorrect = false;
  147.     int* arr = new int [num];
  148.     for (int i = 0; i < num; i++) {
  149.         do {
  150.             isIncorrect = false;
  151.             fin >> arr[i];
  152.             if (fin.fail()) {
  153.                 cout << "Reading of array elements failed." << endl;
  154.                 isIncorrect = true;
  155.                 path = inputFilePath();
  156.             }
  157.         } while (isIncorrect);
  158.     }
  159.     return arr;
  160. }
  161.  
  162. void fileOutput(int* arr, string path, int num) {
  163.     fout.open(path);
  164.     fout << "Sorted array: " << endl;
  165.     for (int i = 0; i < num; i++)
  166.         fout << arr[i] << " ";
  167.     cout << "Successful output in file." << endl;
  168.     fout.close();
  169. }
  170. /* в c++ main отличается от остальных языков, так как нет функций input и output(в отличие от delphi и java).
  171. Это из-за отсутствия функции, возвращающей длину массива(High(массив) или массив.length в других языках). */
  172. int main() {
  173.     cout << "Selection sort. Demonstration." << endl;;
  174.     cout << "Enter type of input: " << '\n' << "1 is console input, 0 is file input." << endl;
  175.     int num;
  176.     int* arr;
  177.     string path;
  178.     bool chose = choose();
  179.     if (chose) {
  180.         cout << "Input size of array:" << endl;
  181.         num = inputArraySize();
  182.         cout << "Input array elements:" << endl;
  183.         arr = inputArray(num);
  184.         cout << "Unsorted array:" << endl;
  185.         consoleOutput(arr, num);
  186.     }
  187.     else {
  188.         path = inputFilePath();
  189.         num = inputSizeOfArrayFromFile(path);
  190.         arr = inputArrayFile(path, num);
  191.     }
  192.     arr = sortArrayBySelectionSort(arr, num);
  193.     cout << "Enter type of output: " << '\n' << "1 is console output, 0 is file output." << endl;
  194.     chose = choose();
  195.     if (chose) {
  196.         cout << "Sorted array:" << endl;
  197.         consoleOutput(arr, num);
  198.     }
  199.     else {
  200.         path = inputFilePath();
  201.         fileOutput(arr, path, num);
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement